o
    پiZ                     @   s   d dl mZmZ d dlmZ d dlmZmZmZm	Z	 d dl
ZeG dd dZG dd deZG d	d
 d
eZe ZG dd deZe ZG dd deZe ZdS )    )ABCabstractmethod)	dataclass)AnyDictListOptionalNc                   @   s.   e Zd ZU eed< dZeeeef  ed< dS )ChoicesDecisiondecisionN	meta_info)	__name__
__module____qualname__str__annotations__r   r   r   r    r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/sglang/lang/choices.pyr	      s   
 r	   c                   @   sn   e Zd ZedefddZedddee dee	 deee
  d	eee
  d
eeee
   defddZdS )ChoicesSamplingMethodreturnc                 C      dS )NFr   selfr   r   r   requires_unconditional_logprobs      z5ChoicesSamplingMethod.requires_unconditional_logprobsNunconditional_token_logprobschoicesnormalized_prompt_logprobsinput_token_logprobsoutput_token_logprobsr   c                C   s   d S Nr   )r   r   r   r   r   r   r   r   r   __call__   s   	zChoicesSamplingMethod.__call__)r   r   r   propertyboolr   r   r   r   floatr   r   r	   r!   r   r   r   r   r      s$    

r   c                   @   sX   e Zd Zdddee dee deee  deee  deeee   defd	d
Z	dS )TokenLengthNormalizedNr   r   r   r   r   r   r   c                C   s&   |t | }|||d}t||dS )zJSelect the option with the highest token length normalized prompt logprob.)r   r   r   r
   r   )npargmaxr	   )r   r   r   r   r   r   best_choicer   r   r   r   r!   "   s   
zTokenLengthNormalized.__call__)
r   r   r   r   r   r$   r   r   r	   r!   r   r   r   r   r%       s    	

r%   c                   @   sh   e Zd Zdddee dee deee  deee  deeee   defd	d
Z	dd Z
dd ZdS )GreedyTokenSelectionNr   r   r   r   r   r   r   c                C   s`   t |}tdd |D }| |||}| |||}	||	d  }
|||| d}t|
|dS )zSelect the option based on greedy logprob selection. For overlapping options
        where one option is a subset of a longer option, extend the shorter option using
        its average logprob for comparison against the longer option.c                 s   s    | ]}t |V  qd S r    )len).0optionr   r   r   	<genexpr>H   s    z0GreedyTokenSelection.__call__.<locals>.<genexpr>r   )r   r   r   greedy_logprob_matrixr&   )r+   max_build_logprob_matrix_greedy_selectiontolistr	   )r   r   r   r   r   r   num_options
max_tokenslogprob_matrix	remainingr)   r   r   r   r   r!   :   s   zGreedyTokenSelection.__call__c           	      C   sp   t ||f}t|D ]*\}}dd |D }t |}|||d t|f< t||k r5|||t|d f< q|S )Nc                 S      g | ]}|d  qS r   r   r,   tokenr   r   r   
<listcomp>Z       z>GreedyTokenSelection._build_logprob_matrix.<locals>.<listcomp>)r'   zeros	enumeratemeanr+   )	r   r   r5   r4   r6   ir-   actual_logprobsavg_logprobr   r   r   r1   W   s   
z*GreedyTokenSelection._build_logprob_matrixc                 C   sT   t |}t|D ]}t |||f }||||f |k }t|dkr' |S q	|S )N   )r'   arangeranger0   r+   )r   r6   r4   r5   r7   jmax_logprobr   r   r   r2   a   s   
z&GreedyTokenSelection._greedy_selection)r   r   r   r   r   r$   r   r   r	   r!   r1   r2   r   r   r   r   r*   8   s"    	



r*   c                   @   sr   e Zd ZedefddZdddee dee deee	  d	eee	  d
e
eee	   defddZdd ZdS )!UnconditionalLikelihoodNormalizedr   c                 C   r   )NTr   r   r   r   r   r   p   r   zAUnconditionalLikelihoodNormalized.requires_unconditional_logprobsNr   r   r   r   r   r   c          	      C   sF   |du rt d| ||}|t| }|||||d}t||dS )a  Select the option with the highest average token logprob once normalized by
        the unconditional token logprobs.

        The first unconditional token logprob is assumed to be None. If so, it is
        replaced with 0 for the purposes of normalization.Nz:Unconditional token logprobs are required for this method.)r   r   r   r   (normalized_unconditional_prompt_logprobsr&   )
ValueError_normalize_logprobsr'   r(   r	   )	r   r   r   r   r   r   rJ   r)   r   r   r   r   r!   t   s   z*UnconditionalLikelihoodNormalized.__call__c                 C   sl   g }t ||D ],\}}tdd |D }tdd |D }|d p$d|d< |tt||  q|S )Nc                 S   r8   r9   r   r:   r   r   r   r<      r=   zIUnconditionalLikelihoodNormalized._normalize_logprobs.<locals>.<listcomp>c                 S   r8   r9   r   r:   r   r   r   r<      r=   r   )zipr'   arrayappendr$   r@   )r   r   r   rJ   inputsunconditionalsinputs_logprobsunconditionals_logprobsr   r   r   rL      s   z5UnconditionalLikelihoodNormalized._normalize_logprobs)r   r   r   r"   r#   r   r   r   r$   r   r   r	   r!   rL   r   r   r   r   rI   n   s$    



"rI   )abcr   r   dataclassesr   typingr   r   r   r   numpyr'   r	   r   r%   token_length_normalizedr*   greedy_token_selectionrI   #unconditional_likelihood_normalizedr   r   r   r   <module>   s    3
6