o
    -i                     @   s   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 dZ
eee
f ZeG dd dee ZeeedB  B Zeee B Zd	ed
efddZd	ed
efddZdeeB dee dee deedB  deded
dfddZdS )    N)IterableIteratorMutableSequence)	dataclassfield)overloadc                   @   s:   e Zd ZU dZeed< dZedB ed< dZe	dB 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 r   r   J/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/logprobs.pyr      s
   
 r   c                
   @   sn  e Zd ZU dZeedZee ed< eedZ	ee ed< eedZ
ee ed< eedZee ed< eedZeedB  ed< eedZeedB  ed	< d
edB ddfddZdee dee deje d	eedB  ddf
ddZd#ddZdefddZededefddZededd fddZdeeB fddZd#ddZd#ddZd#dd Zdee fd!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                 C   st   | j t| j |r/| D ]\}}| j| | j|j | j|j | j	|j
 q| 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	   r   r   r   r    ?   s   zFlatLogprobs.appendc           	      C   st   | j t| j t||||D ]\}}}}| j| | j| | j| | j| q| 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   r   r   r   append_fastJ   s   zFlatLogprobs.append_fastc                 C   s   |D ]}|  | qdS )zCExtends the container with logprobs for the next multiple positionsN)r    )r#   logprobs_multi_positionsr   r   r   r   extend_   s   zFlatLogprobs.extendc                 C   s
   t | jS )z0Gets number of positions stored in the container)r!   r   r#   r   r   r   __len__d   s   
zFlatLogprobs.__len__positionc                 C      d S Nr   )r#   r+   r   r   r   __getitem__h      zFlatLogprobs.__getitem__sc                C   r,   r-   r   )r#   r0   r   r   r   r.   k   r/   indexc              	      s   t |trfddtj| j| D S t |tr\j| d  j| d }t fddj| D  fddj| D j | j | j	 | j
 | dS td	t| )
z.Extracts logprobs of a given position or slicec                    s4   i | ]} j | t j|  j|  j| d qS )r	   r
   r   )r   r   r   r   r   .0ir)   r   r   
<dictcomp>q   s    
z,FlatLogprobs.__getitem__.<locals>.<dictcomp>r   c                       g | ]}|  qS r   r   r3   	min_indexr   r   
<listcomp>       z,FlatLogprobs.__getitem__.<locals>.<listcomp>c                    r8   r   r   r3   r9   r   r   r;      r<   )r   r   r   r   r   r   zInvalid index type: )
isinstancer   ranger   r   slicer   r   r   r   r   	TypeErrortype)r#   r1   	max_indexr   )r:   r#   r   r.   n   s    


c                 C      t d)Nz#Cannot set logprobs in FlatLogprobsr@   )r#   itemvaluer   r   r   __setitem__      zFlatLogprobs.__setitem__c                 C   rC   )Nz(Cannot delete logprobs from FlatLogprobsrD   r#   rE   r   r   r   __delitem__   rH   zFlatLogprobs.__delitem__c                 C   rC   )Nz&Cannot insert logprobs to FlatLogprobsrD   rI   r   r   r   insert   rH   zFlatLogprobs.insertc                 c   s(    t dt| jD ]}| |V  q	dS )zb
        Iterates the container and yields LogprobsOnePosition for
        each position.
        r   N)r>   r!   r   r.   )r#   r5   r   r   r   __iter__   s   zFlatLogprobs.__iter__)r   N) r   r   r   r   r   listr   r   r   r   r   r   r   r   r   r   LogprobsOnePositionr    	itertoolschainr   r&   r(   r*   r   r.   r?   rG   rJ   rK   r   rL   r   r   r   r   r      s>   
 





r   flat_logprobsr   c                 C   s   | rt  ng }|d |S )z:Creates a container to store prompt logprobs for a requestN)r   r    )rQ   r   r   r   r   create_prompt_logprobs   s   
rR   c                 C   s   | rt  S g S )z:Creates a container to store decode logprobs for a request)r   )rQ   r   r   r   create_sample_logprobs   s   rS   request_logprobsr   r   r   r
   num_logprobsc                 C   sl   |dkrt |}td|d }t|f|}t| tr%| |||| dS | dd t||||D  dS )z&Appends logprobs for the next positionr7      c                 S   s$   i | ]\}}}}|t |||d qS r2   )r   )r4   r$   r	   r
   tokenr   r   r   r6      s    
z5append_logprobs_for_next_position.<locals>.<dictcomp>N)	r!   r>   rO   rP   r=   r   r&   r    r%   )rT   r   r   r   r
   rU   
topk_ranksr   r   r   r   !append_logprobs_for_next_position   s   	
rY   )rO   collections.abcr   r   r   dataclassesr   r   typingr   r   dictr   rN   r   rM   PromptLogprobsSampleLogprobsboolrR   rS   r   r   rY   r   r   r   r   <module>   s8   ~
