o
    hp                     @   s   d dl mZmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlZd dlZddlmZ ddlmZmZ d	Zd
ZG dd deZG dd deZG dd deZG dd dZdS )    )ABCabstractmethod)UserDict)DictListOptionalTupleUnionN   )add_start_docstrings   )
ConstraintConstraintListStatea  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size * num_beams, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using any class inheriting from [`PreTrainedTokenizer`]. See
            [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        next_scores (`torch.FloatTensor` of shape `(batch_size, 2 * num_beams)`):
            Current scores of the top `2 * num_beams` non-finished beam hypotheses.
        next_tokens (`torch.LongTensor` of shape `(batch_size, 2 * num_beams)`):
            `input_ids` of the tokens corresponding to the top `2 * num_beams` non-finished beam hypotheses.
        next_indices (`torch.LongTensor` of shape `(batch_size, 2 * num_beams)`):
            Beam indices indicating to which beam hypothesis the `next_tokens` correspond.
        pad_token_id (`int`, *optional*):
            The id of the *padding* token.
        eos_token_id (`Union[int, List[int]]`, *optional*):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
        beam_indices (`torch.LongTensor`, *optional*):
            Beam indices indicating to which beam hypothesis each token correspond.
        group_index (`int`, *optional*):
            The index of the group of beams. Used with [`~PreTrainedModel.group_beam_search`].

    Return:
        `UserDict`: A dictionary composed of the fields as defined above:

            - **next_beam_scores** (`torch.FloatTensor` of shape `(batch_size * num_beams)`) -- Updated scores of all
              non-finished beams.
            - **next_beam_tokens** (`torch.FloatTensor` of shape `(batch_size * num_beams)`) -- Next tokens to be added
              to the non-finished beam_hypotheses.
            - **next_beam_indices** (`torch.FloatTensor` of shape `(batch_size * num_beams)`) -- Beam indices
              indicating to which beam the next tokens shall be added.

a  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size * num_beams, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using any class inheriting from [`PreTrainedTokenizer`]. See
            [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        final_beam_scores (`torch.FloatTensor` of shape `(batch_size * num_beams)`):
            The final scores of all non-finished beams.
        final_beam_tokens (`torch.FloatTensor` of shape `(batch_size * num_beams)`):
            The last tokens to be added to the non-finished beam_hypotheses.
        final_beam_indices (`torch.FloatTensor` of shape `(batch_size * num_beams)`):
            The beam indices indicating to which beam the `final_beam_tokens` shall be added.
        pad_token_id (`int`, *optional*):
            The id of the *padding* token.
        eos_token_id (`Union[int, List[int]]`, *optional*):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.

    Return:
        `torch.LongTensor` of shape `(batch_size * num_return_sequences, sequence_length)`: The generated sequences.
        The second dimension (sequence_length) is either equal to `max_length` or shorter if all batches finished early
        due to the `eos_token_id`.

c                   @   s   e Zd ZdZeeedejdej	dejdejde
ej f
ddZeeedejdej	dejdejd	edejfd
dZdS )
BeamScorerz
    Abstract base class for all beam scorers that are used for [`~PreTrainedModel.beam_search`] and
    [`~PreTrainedModel.beam_sample`].
    	input_idsnext_scoresnext_tokensnext_indicesreturnc                 K      t dNzThis is an abstract method.NotImplementedError)selfr   r   r   r   kwargs r   w/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/generation/beam_search.pyprocessa   s   
zBeamScorer.process
max_lengthc                 K   r   r   r   )r   r   r   r   r   r   r   r   r   r   finalizem   s   zBeamScorer.finalizeN)__name__
__module____qualname____doc__r   r   PROCESS_INPUTS_DOCSTRINGtorch
LongTensorFloatTensorr   Tensorr   FINALIZE_INPUTS_DOCSTRINGintr   r   r   r   r   r   [   s<    
r   c                   @   sl  e Zd ZdZ					d$dededejd	ee d
ee	e
ef  dee dee dee fddZede
fddZ					d%dejdejdejdejdee	eejf  dee	eee ejf  deej dee dee deeejf fddZ				d&dejdejd ejd!ejdedee	eejf  dee	eee ejf  deej dee deej fd"d#ZdS )'BeamSearchScorera	  
    [`BeamScorer`] implementing standard beam search decoding.

    Adapted in part from [Facebook's XLM beam search
    code](https://github.com/facebookresearch/XLM/blob/9e6f6814d17be4fe5b15f2e6c43eb2b2d76daeb4/src/model/transformer.py#L529).

    Reference for the diverse beam search algorithm and implementation [Ashwin Kalyan's DBS
    implementation](https://github.com/ashwinkalyan/dbs/blob/master/dbs/beam_utils.lua)

    Args:
        batch_size (`int`):
            Batch Size of `input_ids` for which standard beam search decoding is run in parallel.
        num_beams (`int`):
            Number of beams for beam search.
        device (`torch.device`):
            Defines the device type (*e.g.*, `"cpu"` or `"cuda"`) on which this instance of `BeamSearchScorer` will be
            allocated.
        length_penalty (`float`, *optional*, defaults to 1.0):
            Exponential penalty to the length that is used with beam-based generation. It is applied as an exponent to
            the sequence length, which in turn is used to divide the score of the sequence. Since the score is the log
            likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while
            `length_penalty` < 0.0 encourages shorter sequences.
        do_early_stopping (`bool` or `str`, *optional*, defaults to `False`):
            Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values:
            `True`, where the generation stops as soon as there are `num_beams` complete candidates; `False`, where an
            heuristic is applied and the generation stops when is it very unlikely to find better candidates;
            `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical
            beam search algorithm).
        num_beam_hyps_to_keep (`int`, *optional*, defaults to 1):
            The number of beam hypotheses that shall be returned upon calling
            [`~transformers.BeamSearchScorer.finalize`].
        num_beam_groups (`int`, *optional*, defaults to 1):
            Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams.
            See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details.
        max_length (`int`, *optional*):
            The maximum length of the sequence to be generated.
          ?Fr   N
batch_size	num_beamsdevicelength_penaltydo_early_stoppingnum_beam_hyps_to_keepnum_beam_groupsr   c	           	         s   |_ |_|_|_|_|_j j _d_ fddt|j D _	t
jdd t|j D t
jjd_t|trJ|dkrRtd| dt|tra||ksa|| d	krltd
| d| dd S )NFc                    "   g | ]}t jjj d qS )r.   r0   early_stoppingr   )BeamHypotheses
group_sizer0   r1   .0_r   r   r   r   
<listcomp>       z-BeamSearchScorer.__init__.<locals>.<listcomp>c                 S      g | ]}d qS Fr   r9   r   r   r   r=          dtyper/   r   A`num_beams` has to be an integer strictly greater than 1, but is G. For `num_beams` == 1, one should make use of `greedy_search` instead.r   `num_beam_groups` has to be an integer smaller or equal than `num_beams` and `num_beams` has to be divisible by `num_beam_groups`, but is  with `num_beams` being .)r.   r/   r0   r1   r2   r3   r8   _is_initrange
_beam_hypsr%   tensorbool_done
isinstancer*   
ValueError)	r   r-   r.   r/   r0   r1   r2   r3   r   r   r<   r   __init__   s6   
zBeamSearchScorer.__init__r   c                 C   
   | j  S NrN   allr   r   r   r   is_done      
zBeamSearchScorer.is_doner   r   r   r   r   pad_token_ideos_token_idbeam_indicesgroup_indexdecoder_prompt_lenc
                 C   s  |j d d }
t| j| j }||j d | j ks<| jdkr-td|j d  d| j dtd|j d  d| j d|j}tj|| jf|j	|d	}tj|| jf|j	|d	}tj|| jf|j	|d	}|d urzt
|tjszt
|tru|g}t|}t|D ]}|| j | }| j| r| jt| j| k rtd
| j d|d u s|d u rtdd||d d f< |||d d f< d||d d f< q~d}tt|| || || D ]f\}\}}}|| j | }|d ur| |v r|| jk}|rq|d ur|| }||f }nd }| j| j||  | ||
|	 d n||||f< ||||f< ||||f< |d7 }|| jkr: nq|| jk rWtd| j d||  d| d||  d	| j| pl| j| ||   |
|	| j|< q~t|d|d|ddS )Nr   r   A group beam size of 0 is used as the input, but a group beam size of   is expected by the beam scorer.A beam size of * is used as the input, but a beam size of rB   #Batch can only be done if at least  beams have been generatedMGenerated beams >= num_beams -> eos_token_id and pad_token have to be definedr[   generated_lenAt most  tokens in   can be equal to `eos_token_id: `. Make sure  are corrected.next_beam_scoresnext_beam_tokensnext_beam_indices)shapelenrK   r3   r8   rP   r/   r%   zerosrC   rO   r(   r*   rL   rJ   rN   r.   	enumeratezipitemaddclonerW   maxr   view)r   r   r   r   r   rY   rZ   r[   r\   r]   cur_lenr-   r/   ro   rp   rq   	batch_idxbatch_group_idxbeam_idxbeam_token_rank
next_token
next_score
next_indexbatch_beam_idx&is_beam_token_worse_than_top_num_beams
beam_indexr   r   r   r      s   







zBeamSearchScorer.processfinal_beam_scoresfinal_beam_tokensfinal_beam_indicesc
           &      C   s  t | j| j }
|d urt|tjst|tr|g}t|}t| jD ]=\}}| j	| r.q$t
| jD ]-}|| j | }||  }|| }|d urN|| nd }|jd |	 }|j||||d q3q$||
| j }g }g }tj|
| j | jtjd}t
|
D ]V}| j|| j |d | j  }dd |D }t|dd d	}t
| jD ]0}| }|d
 }|d }|d }t ||| j| | < || || |||| j | < qq|  d } |d urt| |n| }!||
| j |!}"t |d
kr
|d
 d ur
||
| j |!}#nd }#|  |  kr'|d u r"td|"| |#d ur1|#d tt||D ]4\}\}$}%|$|"|d || f< |#d urZt|%|#|d t |%f< || |!k rk|d
 |"||| f< q8t|"||#dS )Nr^   rg   r/   rC   r   c                 S   s   g | ]
}|j D ]}|qqS r   )beams)r:   beam_hypbeamr   r   r   r=   k  s    z-BeamSearchScorer.finalize.<locals>.<listcomp>c                 S      | d S Nr   r   xr   r   r   <lambda>l      z+BeamSearchScorer.finalize.<locals>.<lambda>keyr   r
    `pad_token_id` has to be defined	sequencessequence_scoresr[   )rs   rK   r3   rO   r%   r(   r*   rL   ru   rN   rJ   r8   rw   rr   rx   newr2   rt   r/   float32sortedpopappendrz   minrP   fill_rv   r   )&r   r   r   r   r   r   rY   rZ   r[   r]   r-   r~   r   index_per_groupr   final_scorefinal_tokensr   rh   sent_lengthsbestbest_indicesbest_scoresibeam_hyps_in_batchcandidate_beamssorted_hypsjbest_hyp_tuple
best_scorebest_hyp
best_indexsent_lengths_maxsent_max_lendecodedindiceshypobest_idxr   r   r   r   @  sv   


	






zBeamSearchScorer.finalizer,   Fr   r   N)NNNr   r   NNNr   )r    r!   r"   r#   r*   r%   r/   r   floatr	   rM   strrQ   propertyrW   r&   r'   r(   r   r   r   r   r   r   r   r   r   r+   {   s    +	
1		

p	
r+   c                   @   s  e Zd ZdZ					d1dededee d	ejd
e	e
 de	eeef  de	e de	e de	e fddZedefddZdd Zdd Z				d2dejdejdejdejdejde	eeejf  de	eeee ejf  d e	ej d!e	e deej fd"d#Z	d3d$edejd%ejd&ejd'ejd(ejd)efd*d+Z				d2dejd,ejd-ejd.ejdede	eeejf  de	eeee ejf  d e	ej d!e	e deej fd/d0ZdS )4ConstrainedBeamSearchScorera 	  
    [`BeamScorer`] implementing constrained beam search decoding.


    Args:
        batch_size (`int`):
            Batch Size of `input_ids` for which standard beam search decoding is run in parallel.
        num_beams (`int`):
            Number of beams for beam search.
        constraints (`List[Constraint]`):
            A list of positive constraints represented as `Constraint` objects that must be fulfilled in the generation
            output. For more information, the documentation of [`Constraint`] should be read.
        device (`torch.device`):
            Defines the device type (*e.g.*, `"cpu"` or `"cuda"`) on which this instance of `BeamSearchScorer` will be
            allocated.
        length_penalty (`float`, *optional*, defaults to 1.0):
            Exponential penalty to the length that is used with beam-based generation. It is applied as an exponent to
            the sequence length, which in turn is used to divide the score of the sequence. Since the score is the log
            likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while
            `length_penalty` < 0.0 encourages shorter sequences.
        do_early_stopping (`bool` or `str`, *optional*, defaults to `False`):
            Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values:
            `True`, where the generation stops as soon as there are `num_beams` complete candidates; `False`, where an
            heuristic is applied and the generation stops when is it very unlikely to find better candidates;
            `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical
            beam search algorithm).
        num_beam_hyps_to_keep (`int`, *optional*, defaults to 1):
            The number of beam hypotheses that shall be returned upon calling
            [`~transformers.BeamSearchScorer.finalize`].
        num_beam_groups (`int`, *optional*, defaults to 1):
            Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams.
            See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details.
        max_length (`int`, *optional*):
            The maximum length of the sequence to be generated.
    r,   Fr   Nr-   r.   constraintsr/   r0   r1   r2   r3   r   c
           
         s   |_ |_|_|_|_|_j j _|_d_ fddt	|D _
tjdd t	|D tjjd_t|trG|dkrOtd| dt|tr^||ks^|| d	kritd
| d| dd S )NFc                    r4   r5   )r7   r.   r0   r1   r9   r<   r   r   r=     r>   z8ConstrainedBeamSearchScorer.__init__.<locals>.<listcomp>c                 S   r?   r@   r   r9   r   r   r   r=     rA   rB   r   rD   rE   r   rF   rG   rH   )r.   r/   r0   r1   r2   r3   r8   r   rI   rJ   rK   r%   rL   rM   rN   rO   r*   rP   )
r   r-   r.   r   r/   r0   r1   r2   r3   r   r   r<   r   rQ     s4   $	
z$ConstrainedBeamSearchScorer.__init__r   c                 C   rR   rS   rT   rV   r   r   r   rW     rX   z#ConstrainedBeamSearchScorer.is_donec                    s    fddt |D S )Nc                    s    g | ]}t d d  jD qS )c                 S      g | ]}|  qS r   )copy)r:   
constraintr   r   r   r=         zQConstrainedBeamSearchScorer.make_constraint_states.<locals>.<listcomp>.<listcomp>)r   r   r9   rV   r   r   r=     s     zFConstrainedBeamSearchScorer.make_constraint_states.<locals>.<listcomp>)rJ   )r   nr   rV   r   make_constraint_states  s   z2ConstrainedBeamSearchScorer.make_constraint_statesc                 C   s   |  dd }|| |jS )Nr   r   )r   reset	completed)r   sequence	new_stater   r   r   check_completes_constraints  s   
z7ConstrainedBeamSearchScorer.check_completes_constraintsr   r   r   r   r   scores_for_all_vocabrY   rZ   r[   r]   c
                 C   s<  |j d d }
t| j}||j d | j ks9| jdkr*td|j d  d| j dtd|j d  d| j d|j}tj|| jf|j	|d	}tj|| jf|j	|d	}tj|| jf|j	|d	}|d
urwt
|tjswt
|trr|g}t|}t| jD ]\}}| j| r| jt|k rtd| j d|d
u s|d
u rtdd||d
d
f< |||d
d
f< d||d
d
f< q|d}tt|| || || D ]o\}\}}}|| j | }|d
ur| |v r|| jk}|rq| ||  }|r|d
ur|| }||f }nd
}|j||  | ||
|	 d n||||f< ||||f< ||||f< |d7 }|| jkr: nq| ||||| || || \}}}|||< |||< |||< || jk rvtd| j d||  d| d||  d	| j| p|||   |
|	| j|< q|t|d|d|ddS )al	  
        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size * num_beams, sequence_length)`):
                Indices of input sequence tokens in the vocabulary.

                Indices can be obtained using any class inheriting from [`PreTrainedTokenizer`]. See
                [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.

                [What are input IDs?](../glossary#input-ids)
            next_scores (`torch.FloatTensor` of shape `(batch_size, 2 * num_beams)`):
                Current scores of the top `2 * num_beams` non-finished beam hypotheses.
            next_tokens (`torch.LongTensor` of shape `(batch_size, 2 * num_beams)`):
                `input_ids` of the tokens corresponding to the top `2 * num_beams` non-finished beam hypotheses.
            next_indices (`torch.LongTensor` of shape `(batch_size, 2 * num_beams)`):
                Beam indices indicating to which beam hypothesis the `next_tokens` correspond.
            scores_for_all_vocab (`torch.FloatTensor` of shape `(batch_size * num_beams, sequence_length)`):
                The scores of all tokens in the vocabulary for each of the beam hypotheses.
            pad_token_id (`int`, *optional*):
                The id of the *padding* token.
            eos_token_id (`Union[int, List[int]]`, *optional*):
                The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
            beam_indices (`torch.LongTensor`, *optional*):
                Beam indices indicating to which beam hypothesis each token correspond.
            decoder_prompt_len (`int`, *optional*):
                The length of prompt that is included in the input to decoder.
        Return:
            `UserDict`: A dictionary composed of the fields as defined above:

                - **next_beam_scores** (`torch.FloatTensor` of shape `(batch_size * num_beams)`) -- Updated scores of
                  all
                non-finished beams.

                - **next_beam_tokens** (`torch.FloatTensor` of shape `(batch_size * num_beams)`) -- Next tokens to be
                  added
                to the non-finished beam_hypotheses.
                - **next_beam_indices** (`torch.FloatTensor` of shape `(batch_size * num_beams)`) -- Beam indices
                indicating to which beam the next tokens shall be added.
        r^   r   r   r_   r`   ra   rb   rc   rB   Nrd   re   rf   rg   ri   rj   rk   rl   rm   rn   )rr   rs   rK   r8   r3   rP   r/   r%   rt   rC   rO   r(   r*   rL   ru   rN   r.   rv   rw   r   tolistrx   ry   step_sentence_constraintrW   rz   r   r{   )r   r   r   r   r   r   rY   rZ   r[   r]   r|   r-   r/   ro   rp   rq   r}   r   r   r   r   r   r   r   r   completes_constraintr   
new_scores
new_tokensnew_indicesr   r   r   r     s   4








	z#ConstrainedBeamSearchScorer.processr}   vocab_scoressent_beam_scoressent_beam_tokenssent_beam_indicespush_progressc           +      C   sb  | d}|j}	| |}
| |}|| |d | }}||| }||| }tj|| |dfdd}| g g g g d}t|D ]\}}|
| }|||   || }||  |j	stj
| tj|	d}|D ]L}|jdd}||  t||dfd }||d	 vr|d	 | |d
 ||  |d | |d || | |d | qqqE|rt|| d\}}t||dfd}|| }| }|| ||d	 vr|d	 | |d
 | |d | |d | |d | qEt|d
 dkrtj
|d
 |	d}t|d |	}t|d |	}|
|d  }t||fd} t||fd}!tj
dd |D |	d}"|"d |! }#|#jddj}$|"|$ }%d}&|%d }'g }(|%D ]})|)|'kr~|&d7 }&nd}&|)}'|(|& qrt
tj|(dd}*|$|* d | }$|!|$ }| |$ }t||f|$ }|||fS )Nr   r   r^   )dim)new_seqs
new_statesr   r   r   rB   T)statefulr   r   r   r   r   )r/   c                 S   r   r   )get_bank)r:   oner   r   r   r=     r   zHConstrainedBeamSearchScorer.step_sentence_constraint.<locals>.<listcomp>d   )
descending	mergesort)kind)sizer/   r   r%   cat	unsqueezer   ru   r   r   rL   advancelongr   rx   r   takerz   rs   stacktosortr   npargsort)+r   r}   r   r   r   r   r   r   orig_lenr/   topk_contraint_statesadvance_constraint_statessidxeidxthis_batch_input_idsthis_batch_token_scoresfull_hypotheses	track_newseq_idxpre_seq
topk_stateadvance_stateadvance_tokensadvance_tokenr   advance_seq	new_score	new_tokenr   r   r   
all_states
all_tokens
all_scores	all_bankszippedr   sorted_bankscountercur_bank
incrementsbankrearrangersr   r   r   r     s   






z4ConstrainedBeamSearchScorer.step_sentence_constraintr   r   r   c
           &      C   sZ  t | j}
|d urt|tjst|tr|g}t|}t| jD ]\}}| j| r+q!g }t	| j
D ];}|| j
 | }||  }|| }| | }|rm|d urV|| nd }|jd |	 }|j||||d || q2t || jk rt	| j
D ]/}||vr|| j
 | }||  }|| }|jd |	 }|j|||d t || jkr nqzq!||
| j }g }g }tj|
| j | jtjd}t| jD ]C\}}t|jdd d}t	| jD ]0}| }|d }|d	 }|d
 }t ||| j| | < || || |||| j | < qq|  d	 } |d urt| |n| }!||
| j |!}"t |dkrB|d d urB||
| j |!}#nd }#|  |  kr_|d u rZtd|"| |#d uri|#d tt||D ]4\}\}$}%|$|"|d || f< |#d urt|%|#|d t |%f< || |!k r|d |"||| f< qpt|"||#dS )Nr^   rg   )rh   r   c                 S   r   r   r   r   r   r   r   r   j  r   z6ConstrainedBeamSearchScorer.finalize.<locals>.<lambda>r   r   r   r
   r   r   )rs   rK   rO   r%   r(   r*   rL   ru   rN   rJ   r.   rw   r   r   rr   rx   r   r2   r   rt   r/   r   r   r   r   rz   r   rP   r   rv   r   )&r   r   r   r   r   r   rY   rZ   r[   r]   r-   r}   r   ids_collectbeam_idr   r   r   r   r   rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   -  s   











z$ConstrainedBeamSearchScorer.finalizer   r   r@   )r    r!   r"   r#   r*   r   r   r%   r/   r   r   r	   rM   r   rQ   r   rW   r   r   r&   r'   r(   r   r   r   r   r   r   r   r   r     s    *	

-	

 (
 	
r   c                
   @   s   e Zd Zddedededee fddZdd	 Z		dd
e	j
dedee	j
 dee fddZddededee defddZdS )r7   Nr.   r0   r6   r   c                 C   sJ   || _ || _|| _|| _g | _d| _t| jts!| jdu r#tddS dS )z7
        Initialize n-best list of hypotheses.
        g    eANzWhen `do_early_stopping` is set to a string, `max_length` must be defined. Ensure it is passed to the BeamScorer class instance at initialization time.)	r0   r6   r   r.   r   worst_scorerO   rM   rP   )r   r.   r0   r6   r   r   r   r   rQ     s   zBeamHypotheses.__init__c                 C   s
   t | jS )z3
        Number of hypotheses in the list.
        )rs   r   rV   r   r   r   __len__  s   
zBeamHypotheses.__len__hypsum_logprobsr[   rh   c                 C   s   |dur||| j   }n
||jd | j   }t| | jk s"|| jkrX| j|||f t| | jkrOtdd t| jD }| j|d d = |d d | _dS t	|| j| _dS dS )z3
        Add a new hypothesis to the list.
        Nr^   c                 S   s   g | ]\}\}}}||fqS r   r   )r:   idxsr;   r   r   r   r=     s    z&BeamHypotheses.add.<locals>.<listcomp>r   r   )
r0   rr   rs   r.   r	  r   r   r   ru   r   )r   r  r  r[   rh   scoresorted_next_scoresr   r   r   rx     s   
zBeamHypotheses.addr   best_sum_logprobsr|   r]   r   c                 C   s   t | | jk r	dS | jdu rdS | jdu r%||| | j  }| j|k}|S | jdkr>| j|kr3td|| j| | j  }n	||| | j  }| j|k}|S )z
        If there are enough hypotheses and that none of the hypotheses being generated can become better than the worst
        one in the heap, then we are done with this sentence.
        FTg        z3max_length is not larger than decoder prompt length)rs   r.   r6   r0   r	  r   rP   )r   r  r|   r]   highest_attainable_scoreretr   r   r   rW     s    





zBeamHypotheses.is_donerS   )NN)r   )r    r!   r"   r*   r   rM   r   rQ   r
  r%   r&   rx   rW   r   r   r   r   r7     s     

$r7   )abcr   r   collectionsr   typingr   r   r   r   r	   numpyr   r%   utilsr   beam_constraintsr   r   r$   r)   r   r+   r   r7   r   r   r   r   <module>   s&   $   *    