o
    :/i!                     @   s   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ eG dd dZeG d	d
 d
ZG dd dZ	ddee dedededef
ddZdedefddZdS )    )	dataclass)EncoderDecoderInputsTokenInputstoken_inputs)DecoderInputs)Logprob)LoRARequest)MultiModalInputs	mm_inputsc                   @   s   e Zd ZU dZeeB eB ed< ee	 ed< ee
e	ef  ed< dZedB ed< dZeed< dZedB ed	< dZedB ed
< dZe	eB dB ed< dd ZdedefddZdS )BeamSearchSequencezA sequence for beam search.
    It keeps track of the tokens and the log probability of the sequence.
    The text field is optional and will only be filled when the sequence is
    about to be returned to the user.
    orig_prompttokenslogprobsNlora_requestg        cum_logprobtextfinish_reasonstop_reasonc                 C   sn   | j }|d dkr| |S |d}|d}|d dkr&t| j||dS t| j|d |d |d	 ||d
S )Ntypeenc_decprompt
cache_salttokenr   r   	mm_kwargs	mm_hashesmm_placeholders)prompt_token_idsr   r   r   r   r   )r   _build_encoder_decoder_inputsgetr   r   r
   )selfr   prompt_textr    r"   ]/lsinfo/ai/hellotax_ai/llm_service/venv_vllm/lib/python3.10/site-packages/vllm/beam_search.py
get_prompt    s&   


zBeamSearchSequence.get_promptr   returnc              	   C   sr   |d }|d dkr"t | j|d |d |d |d|dd	}nt| j|d|dd
}td|d |dS )a  Rebuild the encoder-decoder inputs with the current beam search
        sequence's tokens.

        FIXME (alex) - the encoder multimodal cache is not properly wired up
        yet, which means that currently we are running the encoder on every
        new beam because num_computed_tokens is 0 on each new request. This
        will be fixed once the cache is correctly implemented.
        decoder_promptr   
multimodalr   r   r   r   r   )r   r   r   r   r   r   r   encoder_prompt)r   r(   r&   )r
   r   r   r   r   )r    r   
dec_promptnew_dec_promptr"   r"   r#   r   :   s(   	z0BeamSearchSequence._build_encoder_decoder_inputs)__name__
__module____qualname____doc__r   r	   r   __annotations__listintdictr   r   r   r   floatr   strr   r   r$   r   r"   r"   r"   r#   r      s    
 r   c                   @   s   e Zd ZU dZee ed< dS )BeamSearchOutputzThe output of beam search.
    It contains the list of the best beam search sequences.
    The length of the list is equal to the beam width.
    	sequencesN)r+   r,   r-   r.   r0   r   r/   r"   r"   r"   r#   r5   a   s   
 r5   c                
   @   sD   e Zd Z		ddeeB eB dedB deee	e
f  dB fddZdS )BeamSearchInstanceNr   r   r   c                 K   sV   |d dkr|n|d }|d }t d|||d u rg nt||d|g| _g | _d S )Nr   r   r&   r   )r   r   r   r   r"   )r   r0   beams	completed)r    r   r   r   kwargsr&   initial_tokensr"   r"   r#   __init__l   s   
	zBeamSearchInstance.__init__)NN)r+   r,   r-   r   r	   r   r   r0   r2   r1   r   r<   r"   r"   r"   r#   r7   k   s    
r7         ?r   cumulative_logprobeos_token_idlength_penaltyr%   c                 C   s(   t | }| d |kr|d8 }|||  S )zCalculate the beam search score with length penalty.

    Adapted from

    https://github.com/huggingface/transformers/blob/ccb92be23def445f2afdea94c31286f84b89eb5b/src/transformers/generation/beam_search.py#L938
       )len)r   r>   r?   r@   seq_lenr"   r"   r#   get_beam_search_score   s   rE   c                    s   dt dtf fdd}|S )Nxr%   c                    s   t | j| j S )N)rE   r   r   )rF   r?   r@   r"   r#   sort_beams_key   s   z6create_sort_beams_key_function.<locals>.sort_beams_key)r   r3   )r?   r@   rH   r"   rG   r#   create_sort_beams_key_function   s   rI   N)r=   )dataclassesr   vllm.inputsr   r   r   vllm.inputs.datar   vllm.logprobsr   vllm.lora.requestr   vllm.multimodal.inputsr	   r
   r   r5   r7   r0   r1   r3   rE   rI   r"   r"   r"   r#   <module>   s0   S	
