o
    ߥi                     @   s   d dl Z d dlmZmZ d dlZd dlmZ d dlmZ ddlm	Z	m
Z
mZ G dd dejZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N)ListOptional)Tensor   )ConstraintStateOrderedConstraintStateUnorderedConstraintStatec                       st   e Zd Z fddZ		dddZejjdd Zejjde	e
 d	efd
dZde
fddZde
fddZ  ZS )Searchc                    s~   t    |j| _|j| _|j| _dd | 	 D }dd |
 	 D }|| t|| _td| _d| _d| _d S )Nc                 S      i | ]\}}||qS  r   .0keyvaluer   r   e/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/multi_modal/ofa/generate/search.py
<dictcomp>       z#Search.__init__.<locals>.<dictcomp>c                 S   r
   r   r   r   r   r   r   r          F)super__init__pad_token_idpadunk_token_idunkeos_token_ideos	get_vocabitemsget_added_vocabupdatelen
vocab_sizetorchtensorsrc_lengthssupports_constraintsstop_on_max_len)self	tokenizertgt_dictadded	__class__r   r   r      s   




zSearch.__init__Nc                 C   s   t )a  Take a single search step.

        Args:
            step: the current search step, starting at 0
            lprobs: (bsz x input_beam_size x vocab_size)
                the model's log-probabilities over the vocabulary at the current step
            scores: (bsz x input_beam_size x step)
                the historical model scores of each hypothesis up to this point
            prev_output_tokens: (bsz x step)
                the previously generated oputput tokens
            original_batch_idxs: (bsz)
                the tensor with the batch indices, in the range [0, bsz)
                this is useful in case there has been applied a re-ordering
                and we need to know the original indices

        Return: A tuple of (scores, indices, beams) where:
            scores: (bsz x output_beam_size)
                the scores of the chosen elements; output_beam_size can be
                larger than input_beam_size, e.g., we may return
                2*input_beam_size to account for EOS
            indices: (bsz x output_beam_size)
                the indices of the chosen elements
            beams: (bsz x output_beam_size)
                the hypothesis ids of the chosen elements, in the range [0, input_beam_size)
        )NotImplementedError)r(   steplprobsscoresprev_output_tokensoriginal_batch_idxsr   r   r   r/   $   s   zSearch.stepc                 C   s
   || _ d S N)r%   )r(   r%   r   r   r   set_src_lengthsE   s   
zSearch.set_src_lengthsbatch_constraints	beam_sizec                 C      dS )aW  Initialize constraint states for constrained decoding (if supported).

        Args:
            batch_constraints: (torch.Tensor, optional)
                the list of constraints, in packed form
            beam_size: (int)
                the beam size
        Returns:
            *encoder_out* rearranged according to *new_order*
        Nr   )r(   r6   r7   r   r   r   init_constraintsI   s   zSearch.init_constraints
batch_idxsc                 C   r8   )a(  
        Removes constraint states for completed sentences (if supported).
        This is called from sequence_generator._generate() when sentences are
        deleted from the batch.

        Args:
            batch_idxs: Indices of *sentences* whose constraint state should be *kept*.
        Nr   r(   r:   r   r   r   prune_sentencesX   s   	zSearch.prune_sentencesactive_hyposc                 C   r8   )a  
        Updates the constraint states by selecting the beam items that are retained.
        This is called at each time step of sequence_generator._generate() when
        the set of 2 * {beam_size} candidate hypotheses are reduced to the beam size.

        Args:
            active_hypos: (batch size, beam size)
              list of integers denoting, for each sentence, which beam candidate items
              should be kept.
        Nr   )r(   r=   r   r   r   update_constraintsc   s   zSearch.update_constraintsNN)__name__
__module____qualname__r   r/   r#   jitexportr5   r   r   intr9   r<   r>   __classcell__r   r   r,   r   r	      s    
!

r	   c                       sT   e Zd ZdZ fddZejj		ddede	e
 de	e
 de	e
 fd	d
Z  ZS )
BeamSearcha'  
    Beam search strategy.

    step 1. Calculate top k candidates in model's log-probability under descending order. While k is the minor of
        `beam_size * 2` and `beam_size * vocabulary_size`.
    step 2. Modify hypothesis score, relative indices, beam indices for the final result.
    c                    s   t  | d | _d S r4   )r   r   constraint_states)r(   r*   r,   r   r   r   z   s   
zBeamSearch.__init__Nr/   r1   r2   r3   c                 C   s   |  \}}}|dkr|dddd|ddf  }n|dus"J ||dddd|d f d }tj||dt|d ||d dd d}	|	d }
|	d }tj||dd}||}|
||fS )	  
        Take a single search step.

        Args:
            step (`int`):  Current step, start with 0.
            lprobs (`Tensor` with size `(bsz, input_beam_size, vocab_size)`):
                the model's log-probabilities over the vocabulary at the current step.
            scores (`Tensor` with size `(bsz, input_beam_size, step - 1)`):
                Previous sampling scores for each beam.
            prev_output_tokens (`Tensor`, **optional**. default to `None`):
                Previous output tokens, no usage in this function, will be deprecated in next version.
            original_batch_idxs (`Tensor`, **optional**, default to `None`):
                the tensor with the batch indices, in the range [0, bsz)
                this is useful in case there has been applied a re-ordering
                and we need to know the original indices

        Returns: A tuple of (scores_buf, indices_buf, beams_buf), where:
            scores_buf (`Tensor` with size `(bsz, output_beam_size)`):
                The model's log-probabilities over the elements selected to sample from.
                `output_beam_size` is the minor of `2 * input_beam_size` and `vocab_size - 1`.
                which cumulates the score before.
            indices_buf (`Tensor` with size `(bsz, output_beam_size)`):
                The indices of chosen elements.
            beams_buf (`Tensor` with size `(bsz, output_beam_size)`):
                The indices of each beam.
        r   Nr   r      kfloor)rounding_mode)	size
contiguous	unsqueezer#   topkviewmindivfmodr(   r/   r0   r1   r2   r3   bszr7   r"   top_prediction
scores_bufindices_buf	beams_bufr   r   r   r/   ~   s"   #"$
	

zBeamSearch.stepr?   r@   rA   rB   __doc__r   r#   rC   rD   rE   r   r   r/   rF   r   r   r,   r   rG   q   s    rG   c                       sV   e Zd ZdZ fddZejjdd Zejjde	de
de
d	e
d
e
f
ddZ  ZS )PrefixConstrainedBeamSearcha  
    Prefix constrained beam search.

    step 1. Calculate a mask according to a `prefix_allowed_tokens_fn`
        function with input of previous hypothesis tokens and indices.
    step 2. Calculate a candidate set of `lprobs` with `lprobs` and mask produced in step 1.
    step 3. Just like beam search strategy to generate the hypothesis token.
        And the difference is the k in top k function is the minor of `beam_size` and `vocab_size -1`
    c                    s   t  | || _d| _d S NT)r   r   prefix_allowed_tokens_fnr'   )r(   r*   ra   r,   r   r   r         
z$PrefixConstrainedBeamSearch.__init__c           	   	   C   sz   |j d |j d  }|dd|f  }t|tj }t	t
||D ]\}\}}d||d d | ||f< q'|S )Nr   r   r   )shaperQ   repeatflattentolistr#   	full_likemathinf	enumeratezipra   )	r(   xr2   r3   r7   masksent_isentbatch_ir   r   r   
apply_mask   s   
z&PrefixConstrainedBeamSearch.apply_maskr/   r0   r1   r2   r3   c                 C   s   |  \}}}|| ||| d||||||7 }|dkr1|dddd|ddf  }n|dus7J ||dddd|d f d }tj||dt|||d dd d}	|	d }
|	d }|| }||}|
||fS )j  
        Take a single search step.

        Args:
            step (`int`):  Current step, start with 0.
            lprobs (`Tensor` with size `(bsz, input_beam_size, vocab_size)`):
                the model's log-probabilities over the vocabulary at the current step.
            scores (`Tensor` with size `(bsz, input_beam_size, step - 1)`):
                Previous sampling scores for each beam.
            prev_output_tokens (`Tensor`, **optional**. default to `None`):
                Previous output tokens, no usage in this function, will be deprecated in next version.
            original_batch_idxs (`Tensor`, **optional**, default to `None`):
                the tensor with the batch indices, in the range [0, bsz)
                this is useful in case there has been applied a re-ordering
                and we need to know the original indices

        Returns: A tuple of (scores_buf, indices_buf, beams_buf), where:
            scores_buf (`Tensor` with size `(bsz, input_beam_size)`):
                The model's log-probabilities over the elements selected to sample from.
                which cumulates the score before.
            indices_buf (`Tensor` with size `(bsz, input_beam_size)`):
                The indices of chosen elements.
            beams_buf (`Tensor` with size `(bsz, input_beam_size)`):
                The indices of each beam.
        r   r   Nr   rK   )	rO   rq   rS   rP   rQ   r#   rR   rT   rV   rW   r   r   r   r/      s0   "
"$
	

z PrefixConstrainedBeamSearch.step)r@   rA   rB   r^   r   r#   rC   rD   rq   rE   r   r/   rF   r   r   r,   r   r_      s"    

r_   c                       s   e Zd ZdZ fddZejjdee	 de
fddZejjde	fd	d
Zejjde	fddZejj		dde
de	dee	 dee	 dee	 f
ddZejjde
de
de	deee  de	de	de	fddZ  ZS )LexicallyConstrainedBeamSearcha  Implements lexically constrained beam search as described in

        Fast Lexically Constrained Decoding with Dynamic Beam
        Allocation for Neural Machine Translation.  Post & Vilar,
        NAACL 2018.  https://www.aclweb.org/anthology/N18-1119/

    and

        Improved Lexically Constrained Decoding for Translation and
        Monolingual Rewriting. Hu et al, NAACL
        2019. https://www.aclweb.org/anthology/N19-1090/

    This is accomplished by maintaining, for each beam hypothesis, a
    ConstraintState object (see constraints.py) that tracks which
    constraints have been generated and using this information to
    shape the beam for each input sentence.
    c                    sb   t  | || _dd |  D }dd |  D }|| t|| _d| _	d| _
d S )Nc                 S   r
   r   r   r   r   r   r   r   6  r   z;LexicallyConstrainedBeamSearch.__init__.<locals>.<dictcomp>c                 S   r
   r   r   r   r   r   r   r   7  r   r   T)r   r   representationr   r   r   r    r!   r"   	num_candsr&   )r(   r)   rt   r*   r+   r,   r   r   r   3  s   



z'LexicallyConstrainedBeamSearch.__init__r6   r7   c                    s\   g | _ |D ]&}| jdkrt| n
| jdkrt| | j  fddt|D  qd S )Nordered	unorderedc                    s   g | ]} qS r   r   r   iconstraint_stater   r   
<listcomp>M  s    zCLexicallyConstrainedBeamSearch.init_constraints.<locals>.<listcomp>)rH   rt   r   creater   appendrange)r(   r6   r7   constraint_tensorr   rz   r   r9   @  s   

z/LexicallyConstrainedBeamSearch.init_constraintsr:   c                    s    fdd|  D  _d S )Nc                    s   g | ]} j | qS r   rH   rx   r(   r   r   r|   Q  s    
zBLexicallyConstrainedBeamSearch.prune_sentences.<locals>.<listcomp>)rf   rH   r;   r   r   r   r<   O  s   
z.LexicallyConstrainedBeamSearch.prune_sentencesr=   c                    sD    j r|d}t|D ] fdd| D  j < qd S d S )Nr   c                    s   g | ]	} j  | qS r   r   rx   r(   sentidr   r   r|   Z  s    zELexicallyConstrainedBeamSearch.update_constraints.<locals>.<listcomp>)rH   rO   r   )r(   r=   
batch_sizer   r   r   r>   U  s   
z1LexicallyConstrainedBeamSearch.update_constraintsNr/   r0   r1   r2   r3   c                  C   s  d}|j }| \}}	}
t|	d ||ddd | _| j}|rb|dkrbg }t|D ]\}}t|D ]\}}||	 | }|jsF|| q4q,t	
|}| dkrbtj |||	 d|| jf< |dkrw|dddd|	ddf  }n|dus}J ||dddd|d f d }t	||d| j}|\}}||
 }||
}|s|||fS |dkrt	j|||	 d|dd\}}||d}||d}t	j||fdd}t	j||fdd}t	jd|	|d|d}t	j||fdd}t	j|d|	 f|d}t	j|d|	 f|d }t	j|d|	 f|d }t|D ]5\}}| |||| || ||  ||  ||  \}}}}|||< |||< |||< || j|< q|||fS )	a  
        A constrained step builds a large candidates list from the following:
        - the top 2 * {beam_size} items over the whole beam
        - for each item in the beam
          - the top {each_k} (default 1)
          - all next constraints
        We then compute the constrained state of each beam item, and assign
        stripe codes: 0 to the best in each bank, 1 to the 2nd-best, and so
        on. We then sort by (stripe, score), and truncate the list at
        2 * beam size.

        Args:
            step: the decoder step
            lprobs: (batch size, beam size, target vocab)
                the target-vocab distributions for each item in the beam.
        Retrun: A tuple of (scores, indices, beams, constraints) where:
            scores: (batch, output beam size)
                the scores of the chosen elements
            indices: (batch, output beam size)
                the target vocab indices of the chosen elements
            beams: (batch, output beam size)
                the 0-indexed hypothesis ids of the chosen elements
            constraints: (batch, output beam size)
                the new constraint states
        r   rJ   r   r   N)rL   dimr   device)r   rO   rT   rS   ru   rH   rj   finishedr~   r#   r$   numelrh   ri   r   rP   rQ   rR   rV   catarangerd   zeroslongstep_sentenceclone) r(   r/   r0   r1   r2   r3   each_kr   r   r7   r"   rH   not_finished_indicessentnosent_constraintsbeamnostateindexrY   rZ   r[   r\   
top_scorestop_indices	new_beamsnew_scores_bufnew_indices_bufnew_beams_bufstatesindicesbeams
new_statesr   r   r   r/   _  s   "	

"$






	
z#LexicallyConstrainedBeamSearch.stepr   rH   r\   r[   rZ   c                    s  |j }tD ]K\}	}
tjt|
 |d }| dkrLt|ftj|	|d	|
d }t|f||	 |d}t||f}|dkrR nq
d}fddt|D tjdd D |d t|
j}d}|  | | }|jddd	\}}|| }| |  |  fd
d|D dd }| jd   }|||k}t||}t|t|t | d}|dd D ]}|sވ| ||7 }qՇ fddtt d D }t }d} d }t D ]\}}||krd}|}n|d7 }|| ||  ||< q|jdd\}}|| }| | fdd|D |d| j }d| j d| j |fS )a  Does per-sentence processing. Adds all constraints for each
        hypothesis to the list of candidates; then removes duplicates,
        sorts, and dynamically stripes across the banks. All tensor inputs
        are collapsed to those pertaining to a single input sentence.
        r   r   r   c                    s"   g | ]} |   | qS r   )advancerx   )r\   rH   r[   r   r   r|     s    z@LexicallyConstrainedBeamSearch.step_sentence.<locals>.<listcomp>c                 S   s   g | ]}|j qS r   )bank)r   r   r   r   r   r|     s    iT)r   
descendingc                       g | ]} | qS r   r   rx   r   r   r   r|         c                 S   s$   t j| d d| dd fddS )z^Rolls a 1d tensor left by 1.

            [0, 1, 2, 3, 4] becomes [4, 0, 1, 2, 3]
            r   r   r   )r#   r   rQ   )tr   r   r   roll$  s   $z:LexicallyConstrainedBeamSearch.step_sentence.<locals>.rollr   Nc                    s   g | ]
}|t  d   qS )r   )r!   )r   offset)banksr   r   r|   P  s    r   c                    r   r   r   rx   r   r   r   r|   d  r   )r   rj   r#   r$   listnext_tokensr   r   r   rd   rO   takerS   r   r!   tokenssortr"   masked_selectpop
zeros_likeru   )r(   r/   r   r0   rH   r\   r[   rZ   r   r   r   r   
next_beamsnext_values
cands_sizenum_constraint_tokens	MAX_SCOREsort_keysort_valuessort_indicesr   uniques_maskry   rm   stripe_offsetsstripescur_bank_countcur_bankr   r   )r   r\   rH   r[   r   r     s   






z,LexicallyConstrainedBeamSearch.step_sentencer?   )r@   rA   rB   r^   r   r#   rC   rD   r   r   rE   r9   r<   r>   r/   r   r   r   rF   r   r   r,   r   rs      sR    
	y
rs   c                       D   e Zd ZdZ fddZ		d
dedee dee fdd	Z  Z	S )LengthConstrainedBeamSearcha  
    Length constrained beam search for generation.

    step 1. Build length constraints in model's log-probability. If `min_lens` > `step`,
        set eos token's score to `-math.inf`, so the generation will not be easily stopped.
        Otherwise, `max_lens` <= `step`, set eos token's score to `0`, so the generation will
        be easily stopped.
    step 2. Using beam search to generate the hypothesis tokens with scores.
    c                    s8   t  | || _|| _|| _|| _t|| _d| _d S r`   )	r   r   	min_len_a	min_len_b	max_len_a	max_len_brG   beamneeds_src_lengths)r(   r*   r   r   r   r   r,   r   r   r   y  s   

z$LengthConstrainedBeamSearch.__init__Nr/   r2   r3   c                 C   sh   | j | j | j }| j| j | j }tj |||k dd| jf< d|||kdd| jf< | j	|||S )rI   Nr   )
r   r%   r   r   r   rh   ri   r   r   r/   )r(   r/   r0   r1   r2   r3   min_lensmax_lensr   r   r   r/     s
   "z LengthConstrainedBeamSearch.stepr?   
r@   rA   rB   r^   r   rE   r   r   r/   rF   r   r   r,   r   r   n  s    
r   c                	       sL   e Zd ZdZ fddZejj		d
dede	e
 de	e
 fdd	Z  ZS )DiverseBeamSearchzDiverse Beam Search.

    See "Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence
    Models" for details.

    We only implement the Hamming Diversity penalty here, which performed best
    in the original paper.
    c                    s(   t  | || _| | _t|| _d S r4   )r   r   
num_groupsdiversity_strengthrG   r   )r(   r*   r   r   r,   r   r   r     s   zDiverseBeamSearch.__init__Nr/   r2   r3   c              	   C   s  |  \}}}|| j dkrtdt|dddddf   |}	g g g }
}}t| jD ]s}|dd|d| jddf }|dkrV|dd|d| jddf nd}|dkritj||	d| j	d}n|
 }| j|||\}}}|| j| |
|  ||  ||  |	d|t|  |	 q2tj|
dd|d}tj|dd|d}tj|dd|d}|||fS )	aj  
        Take a single search step.

        Args:
            step (`int`):  Current step, start with 0.
            lprobs (`Tensor` with size `(bsz, input_beam_size, vocab_size)`):
                the model's log-probabilities over the vocabulary at the current step.
            scores (`Tensor` with size `(bsz, input_beam_size, step - 1)`):
                Previous sampling scores for each beam.
            prev_output_tokens (`Tensor`, **optional**. default to `None`):
                Previous output tokens, no usage in this function, will be deprecated in next version.
            original_batch_idxs (`Tensor`, **optional**, default to `None`):
                the tensor with the batch indices, in the range [0, bsz)
                this is useful in case there has been applied a re-ordering
                and we need to know the original indices

        Returns: A tuple of (scores_buf, indices_buf, beams_buf), where:
            scores_buf (`Tensor` with size `(bsz, input_beam_size)`):
                The model's log-probabilities over the elements selected to sample from,
                which cumulates the score before.
            indices_buf (`Tensor` with size `(bsz, input_beam_size)`):
                The indices of chosen elements.
            beams_buf (`Tensor` with size `(bsz, input_beam_size)`):
                The indices of each beam.
        r   zIDiverseBeamSearch requires --beam to be divisible by the number of groupsNr   )otheralpharJ   r   r   )rO   r   
ValueErrorr#   r   tor   addrQ   r   rP   r   r/   mul_add_r~   r   scatter_add_onesstackrS   )r(   r/   r0   r1   r2   r3   rX   r7   r"   diversity_bufscores_G	indices_Gbeams_Gglprobs_gscores_grZ   r[   r\   r   r   r   r/     s@   "&*

zDiverseBeamSearch.stepr?   r]   r   r   r,   r   r     s    	r   c                	       sh   e Zd ZU dZeed< eed< d fdd	Zdd	 Ze	j
j	
	
ddedee dee fddZ  ZS )Samplingaz  
    Sampling search for generation.

    1. Calculate the sample set.
        1.1 If `sampling_topk` is not None, chose the candidates which cumulative sum of model's
            log-probability under descending order is less than `sampling_topk`.
        1.2 If `sampling_topp` is not None, chose the top k candidates by model's log-probability under
            the descending order.
        1.3 Chose the whole input set as sampling set.
    2. Using multinomial sample strategy to sample candidates from sample set as hypothesis.
    3. Modify hypothesis score, relative indices, beam indices for the final result.

    Attributes:
        sampling_topk (`int`, **optional**, default to `-1`):
            The value of k in the sampling strategy of top k.
        sampling_topp (`float`, **optional**, default to '-1.0'):
            The value of p The sampling strategy of top p.
    sampling_topksampling_toppr         c                    s   t  | || _|| _d S r4   )r   r   r   r   )r(   r*   r   r   r,   r   r   r   "  rb   zSampling.__init__c                 C   s   |  }|jdd\}}|jdd}|| j}|jdd}|ddddddf }|d| d d  |d|d}| }	|ddddd|	d f }
|ddddd|	d f }|ddddd|	d f }|
 }|	|d}||fS )	a  Sample among the smallest set of elements whose cumulative probability mass exceeds p.

        See `"The Curious Case of Neural Text Degeneration"
        (Holtzman et al., 2019) <https://arxiv.org/abs/1904.09751>`_.

        Args:
            lprobs: (bsz x input_beam_size x vocab_size)
                the model's log-probabilities over the vocabulary at the current step

        Return: A tuple of (trimed_probs, truncated_indices) where:
            trimed_probs: (bsz x input_beam_size x ?)
                the model's probabilities over the elements selected to sample from. The
                width of the third dimension is determined by top-P.
            truncated_indices: (bsz x input_beam_size x ?)
                the indices of the chosen elements.
        T)r   rJ   r   Nr   r   r   )
exp_r   cumsumltr   clamp_rO   scatter_maxmasked_fill_)r(   r0   probssorted_probssorted_indicescumsum_probsrm   cumsum_masklast_includedmax_dimtruncated_masktruncated_probstruncated_indices	trim_masktrimed_probsr   r   r   _sample_topp'  s   zSampling._sample_toppNr/   r2   r3   c                 C   s  |  \}}}|dkr|dddd|ddf  }| jdkr(| |\}	}
n| jdkr:|| j\}}
| }	n| }	td	|	}
|dkr[tj
|	|d|dd||}ntj
|	|| dddd||}|dkrx|	||d}	tj|	d|dd}| |d}| jdks| jdkrtj|
||dd|ddd}|dkr|||}n#td|	||d}|tj|dddd|d f d|d |||fS )	rr   r   Nr   T)replacementr   rJ   )r   r   )rO   rP   r   r   r   rR   r   r#   emptyr   multinomialrS   expandgatherrQ   log_squeeze	new_zerosr   rd   r   )r(   r/   r0   r1   r2   r3   rX   r7   r"   r   r   r[   rZ   r\   r   r   r   r/   T  sd   " 



$
zSampling.step)r   r   r?   )r@   rA   rB   r^   rE   __annotations__floatr   r   r#   rC   rD   r   r   r/   rF   r   r   r,   r   r     s    
 -r   c                       r   )DiverseSiblingsSearcha_  
    Beam search with diverse siblings.

    See "A Simple, Fast Diverse Decoding Algorithm for Neural Generation" for details.
    https://arxiv.org/abs/1611.08562

    1/ Calculate hypotheses for each beam
    2/ Intra-sibling ordering
    3/ Rewrite scores
    4/ Choose top K hypotheses

    if diversity_rate == 0 is equivalent to BeamSearch
    c                    s    t  | || _t|| _d S r4   )r   r   diversity_raterG   r   )r(   r*   r  r,   r   r   r     s   zDiverseSiblingsSearch.__init__Nr/   r2   r3   c                    s     \}}}t|d  |d dd }	 fddt|D }
 fddt|D }td|	d  | j }|dkrG| j	| |S  
|dddd|d f d t|D ]+}tj dd|ddf |d|	|
| || fd	 || | |
| | q^tj|dd
|d}td }t j jd}t j jd}ttj|
dd
|d|	\}}||	 }t|D ]}|| ||  ||< q|||fS )a  
        Take a single search step.

        Args:
            step (`int`):  Current step, start with 0.
            lprobs (`Tensor` with size `(bsz, input_beam_size, vocab_size)`):
                the model's log-probabilities over the vocabulary at the current step.
            scores (`Tensor` with size `(bsz, input_beam_size, step - 1)`):
                Previous sampling scores for each beam.
            prev_output_tokens (`Tensor`, **optional**. default to `None`):
                Previous output tokens, no usage in this function, will be deprecated in next version.
            original_batch_idxs (`Tensor`, **optional**, default to `None`):
                the tensor with the batch indices, in the range [0, bsz)
                this is useful in case there has been applied a re-ordering
                and we need to know the original indices

        Returns: A tuple of (scores_buf, indices_buf, beams_buf), where:
            final_scores (`Tensor` with size `(bsz, output_beam_size)`):
                The model's log-probabilities over the elements selected to sample from,
                which cumulates the score before. `output_beam_size` is the minor of
                `2 * input_beam_size` and `vocab_size - 1`.
            final_indices (`Tensor` with size `(bsz, output_beam_size)`):
                The indices of chosen elements.
            final_beams (`Tensor` with size `(bsz, ourput_beam_size)`):
                The indices of each beam.
        rJ   r   r   c                    s   g | ]
}t d  qS )r   )r#   r   r   rx   r0   r   r   r|     s    z.DiverseSiblingsSearch.step.<locals>.<listcomp>c                    s   g | ]}t  j jd qS )r   )r#   
LongTensorr   r   rx   r  r   r   r|     s    r   N)outr   r   )rO   rT   rS   r   r#   r   r   r  r   r/   r   rQ   rR   fmod_sub_r   r   r  r   )r(   r/   r0   r1   r2   r3   rX   r7   r"   rL   s_listi_listsibling_scorery   r   final_scoresfinal_indicesfinal_beamsr   r  r   r/     s>   "
&,
zDiverseSiblingsSearch.stepr?   r   r   r   r,   r   r    s    
r  )rh   typingr   r   r#   torch.nnnnr   token_generation_constraintsr   r   r   Moduler	   rG   r_   rs   r   r   r   r  r   r   r   r   <module>   s"   _Na  P@^ )