
    -`i                     Z   d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	 e G d d                      Z
eee
f         Ze G d dee                               Zeeedz           z  Zeee         z  Zd	ed
efdZd	ed
efdZdeez  dee         dee         deedz           deded
dfdZdS )    N)IterableIteratorMutableSequence)	dataclassfield)overloadc                   F    e Zd ZU dZeed<   dZedz  ed<   dZe	dz  ed<   dS )LogprobzInfos for supporting OpenAI compatible logprobs and token ranks.

    Attributes:
        logprob: The logprob of chosen token
        rank: The vocab rank of chosen token (>=1)
        decoded_token: The decoded chosen token index
    logprobNrankdecoded_token)
__name__
__module____qualname____doc__float__annotations__r   intr   str     a/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/logprobs.pyr
   r
      sO           NNND#* $M3:$$$$$r   r
   c            
       <   e Zd ZU dZ ee          Zee         ed<    ee          Z	ee         ed<    ee          Z
ee         ed<    ee          Zee         ed<    ee          Zeedz           ed<    ee          Zeedz           ed	<   d
edz  ddfdZdee         dee         dej        e         d	eedz           ddf
dZddZdefdZededefd            Zededd fd            Zdeez  fdZddZddZddZdee         fdZdS )FlatLogprobsa6  
    Flat logprobs of a request into multiple primitive type lists.

    Compared to list[dict[int, Logprob]], this data structure reduced GC
    overhead significantly. As it flattened logprob information for
    all positions and ranks in to multiple primitive type lists (i.e.
    logprobs, token_ids, ranks per token_ids, decoded_tokens).
    So regardless of the sequence length and top_logprobs setup,
    FlatLogprobs would only introduce a constant amount of objects.

    As each position might contains different amount of ranks,
    start_indices_per_position would be used to access the logprob ranges
    for different positions.

    NOTE: To reduce the migration overhead and improve backward compatibility,
    we support the key Sequence APIs of list, so it could act as
    list[LogprobsOnePosition]
    )default_factorystart_indicesend_indices	token_idslogprobsNranksdecoded_tokenslogprobs_one_positionreturnc                    | j                             t          | j                             |r|                                D ]|\  }}| j                            |           | j                            |j                   | j                            |j                   | j	                            |j
                   }| j                            t          | j                             dS )z9Appends the container with logprobs for the next positionN)r   appendlenr   itemsr   r   r    r   r!   r   r   )selfr"   token_idr   s       r   r%   zFlatLogprobs.append?   s    !!#dm"4"4555  	B%:%@%@%B%B B B!'%%h///$$W_555
!!',///#**7+@AAAADM 2 233333r   c                    | j                             t          | j                             t	          ||||          D ]o\  }}}}| j                            |           | j                            |           | j                            |           | j                            |           p| j                            t          | j                             dS )zv
        Appends logprobs for the next position without creating
        the intermediate logprob dictionary.
        N)	r   r%   r&   r   zipr   r    r!   r   )	r(   r   r   r    r!   r)   r   r   r   s	            r   append_fastzFlatLogprobs.append_fastJ   s     	!!#dm"4"455569x7
 7
 	6 	62Hgt] N!!(+++M  )))Jd###&&}5555DM 2 233333r   c                 :    |D ]}|                      |           dS )zCExtends the container with logprobs for the next multiple positionsN)r%   )r(   logprobs_multi_positionsr"   s      r   extendzFlatLogprobs.extend_   s2    %= 	/ 	/!KK-....	/ 	/r   c                 *    t          | j                  S )z0Gets number of positions stored in the container)r&   r   )r(   s    r   __len__zFlatLogprobs.__len__d   s    4%&&&r   positionc                     d S Nr   )r(   r2   s     r   __getitem__zFlatLogprobs.__getitem__h   s    ADr   sc                    d S r4   r   )r(   r6   s     r   r5   zFlatLogprobs.__getitem__k   s    :=#r   indexc           
      6    t          |t                    r2 fdt           j        |          j        |                   D             S t          |t
                    r j        |         d          j        |         d         }t          fd j        |         D             fd j        |         D              j        |          j        |          j	        |          j
        |                   S t          dt          |                     )z.Extracts logprobs of a given position or slicec           	          i | ]A}j         |         t          j        |         j        |         j        |                    BS )r   r   r   )r   r
   r   r    r!   ).0ir(   s     r   
<dictcomp>z,FlatLogprobs.__getitem__.<locals>.<dictcomp>q   sa         q!7 M!,A"&"5a"8$ $ $  r   r   c                     g | ]}|z
  S r   r   r<   r=   	min_indexs     r   
<listcomp>z,FlatLogprobs.__getitem__.<locals>.<listcomp>   s    PPPq9}PPPr   c                     g | ]}|z
  S r   r   rA   s     r   rC   z,FlatLogprobs.__getitem__.<locals>.<listcomp>   s    LLLqQ]LLLr   )r   r   r   r   r    r!   zInvalid index type: )
isinstancer   ranger   r   slicer   r   r   r    r!   	TypeErrortype)r(   r8   	max_indexrB   s   `  @r   r5   zFlatLogprobs.__getitem__n   sK   eS!! 	B    t1%8$:J5:QRR    u%% 	B*51!4I(/3I QPPPd6H6OPPPLLLLD4DU4KLLL.9)<=y':;j9!45#29Y3FG	 	 	 	 @4;;@@AAAr   c                      t          d          )Nz#Cannot set logprobs in FlatLogprobsrH   )r(   itemvalues      r   __setitem__zFlatLogprobs.__setitem__   s    =>>>r   c                      t          d          )Nz(Cannot delete logprobs from FlatLogprobsrL   r(   rM   s     r   __delitem__zFlatLogprobs.__delitem__   s    BCCCr   c                      t          d          )Nz&Cannot insert logprobs to FlatLogprobsrL   rQ   s     r   insertzFlatLogprobs.insert   s    @AAAr   c              #      K   t          dt          | j                            D ]}|                     |          V  dS )zb
        Iterates the container and yields LogprobsOnePosition for
        each position.
        r   N)rF   r&   r   r5   )r(   r=   s     r   __iter__zFlatLogprobs.__iter__   sS      
 q#d01122 	& 	&A""1%%%%%%	& 	&r   )r#   N) r   r   r   r   r   listr   r   r   r   r   r   r   r    r!   r   LogprobsOnePositionr%   	itertoolschainr   r,   r/   r1   r   r5   rG   rO   rR   rT   r   rV   r   r   r   r   r      ss         (  %uT:::M49:::"U4888Kc888
 !5666ItCy666!E$777Hd5k777#eD999E4d
999',uT'B'B'BNDt$BBB	4,?$,F 	44 	4 	4 	4 	4494 u+4 s#	4
 !t,4 
4 4 4 4*/ / / /
' ' ' ' ' DCD,?DDD XD=U=.=== X=Bu B B B B6? ? ? ?D D D DB B B B&(#67 & & & & & &r   r   flat_logprobsr#   c                 T    | rt                      ng }|                    d           |S )z:Creates a container to store prompt logprobs for a requestN)r   r%   )r[   r   s     r   create_prompt_logprobsr]      s+    !.6|~~~BHOODOr   c                 &    | rt                      ng S )z:Creates a container to store decode logprobs for a request)r   )r[   s    r   create_sample_logprobsr_      s    *2<>>>2r   request_logprobsr   r   r!   r   num_logprobsc           	      >   |dk    rt          |          }t          d|dz             }t          j        |f|          }t	          | t
                    r|                     ||||           dS |                     d t          ||||          D                        dS )z&Appends logprobs for the next positionr?      c                 >    i | ]\  }}}}|t          |||           S r;   )r
   )r<   r)   r   r   tokens        r   r>   z5append_logprobs_for_next_position.<locals>.<dictcomp>   sL     	 	 	 3HgtU '#"'  	 	 	r   N)	r&   rF   rY   rZ   rE   r   r,   r%   r+   )r`   r   r   r!   r   ra   
topk_ranksr    s           r   !append_logprobs_for_next_positionrg      s     r8}} q,*++JOTGZ00E"L11 
$$Y%PPPPP	 	 7:x7 7	 	 		
 	
 	
 	
 	
r   )rY   collections.abcr   r   r   dataclassesr   r   typingr   r
   dictr   rX   r   rW   PromptLogprobsSampleLogprobsboolr]   r_   r   r   rg   r   r   r   <module>ro      s       ? ? ? ? ? ? ? ? ? ? ( ( ( ( ( ( ( (       % % % % % % % % 3<(  y& y& y& y& y&?#67 y& y& y&| %84%? @@%8 99$ >    3$ 3> 3 3 3 3

$~5
Cy
 5k
 S4Z(	

 
 
 

 
 
 
 
 
r   