
    9iT                         S SK JrJr  S SKJr  S SKJrJrJrJ	r	  S SK
r
SrSr " S S\5      r " S	 S
\5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)    )ABCabstractmethod)UserDict)IterableListOptionalTupleNag  
    Args:
        input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size * num_beams, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using any class inheriting from :class:`~transformers.PretrainedTokenizer`. See
            :meth:`transformers.PreTrainedTokenizer.encode` and :meth:`transformers.PreTrainedTokenizer.__call__` for
            details.

            `What are input IDs? <../glossary.html#input-ids>`__
        next_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, 2 * num_beams)`):
            Current scores of the top :obj:`2 * num_beams` non-finished beam hypotheses.
        next_tokens (:obj:`torch.LongTensor` of shape :obj:`(batch_size, 2 * num_beams)`):
            :obj:`input_ids` of the tokens corresponding to the top :obj:`2 * num_beams` non-finished beam hypotheses.
        next_indices (:obj:`torch.LongTensor` of shape :obj:`(batch_size, 2 * num_beams)`):
            Beam indices indicating to which beam hypothesis the :obj:`next_tokens` correspond.
        pad_token_id (:obj:`int`, `optional`):
            The id of the `padding` token.
        eos_token_id (:obj:`int`, `optional`):
            The id of the `end-of-sequence` token.

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

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

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

            Indices can be obtained using any class inheriting from :class:`~transformers.PretrainedTokenizer`. See
            :meth:`transformers.PreTrainedTokenizer.encode` and :meth:`transformers.PreTrainedTokenizer.__call__` for
            details.

            `What are input IDs? <../glossary.html#input-ids>`__
        final_beam_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size * num_beams)`):
            The final scores of all non-finished beams.
        final_beam_tokens (:obj:`torch.FloatTensor` of shape :obj:`(batch_size * num_beams)`):
            The last tokens to be added to the non-finished beam_hypotheses.
        final_beam_indices (:obj:`torch.FloatTensor` of shape :obj:`(batch_size * num_beams)`):
            The beam indices indicating to which beam the :obj:`final_beam_tokens` shall be added.
        pad_token_id (:obj:`int`, `optional`):
            The id of the `padding` token.
        eos_token_id (:obj:`int`, `optional`):
            The id of the `end-of-sequence` token.

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

c                   6   \ rS rSrSr\S\R                  S\R                  S\R                  S\R                  S\	\R                     4
S j5       r\S\R                  S\R                  S\R                  S\R                  S\R                  4
S	 j5       rS
rg)
BeamScorerS   z
Abstract base class for all beam scorers that are used for :meth:`~transformers.PretrainedModel.beam_search` and
:meth:`~transformers.PretrainedModel.beam_sample`.
	input_idsnext_scoresnext_tokensnext_indicesreturnc                     [        S5      eNzThis is an abstract method.NotImplementedErrorselfr   r   r   r   kwargss         k/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/nlp/mglm/generation_utils.pyprocessBeamScorer.processY   s    
 ""?@@    c                     [        S5      er   r   r   s         r   finalizeBeamScorer.finalize`   s     ""?@@r    N)__name__
__module____qualname____firstlineno____doc__r   torch
LongTensorFloatTensorr	   Tensorr   r   __static_attributes__r    r   r   r   r   S   s    
 A!1!1 A"..A=B=M=MA#..A #5<<0A A A%"2"2 A#//A>C>N>NA$//A>C>N>NA Ar   r   c                      \ rS rSrSr   SS\S\S\S\R                  S\\	   S\\
   S	\\   4S
 jjr\S\
4S j5       r   SS\R                  S\R                  S\R                  S\R                  S\\   S\\   S\\R"                     4S jjr   SS\R                  S\R                  S\R                  S\R                  S\\   S\\   S\\R                  \\R"                     4   4S jjrSrg)BeamSearchScorerg   a  
:class:`transformers.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>`__.

Args:
    batch_size (:obj:`int`):
        Batch Size of :obj:`input_ids` for which beam search decoding is run in parallel.
    max_length (:obj:`int`):
        The maximum length of the sequence to be generated.
    num_beams (:obj:`int`):
        Number of beams for beam search.
    device (:obj:`torch.device`):
        Defines the device type (*e.g.*, :obj:`"cpu"` or :obj:`"cuda"`) on which this instance of
        :obj:`BeamSearchScorer` will be allocated.
    length_penalty (:obj:`float`, `optional`, defaults to 1.0):
        Exponential penalty to the length. 1.0 means no penalty. Set to values < 1.0 in order to encourage the
        model to generate shorter sequences, to a value > 1.0 in order to encourage the model to produce longer
        sequences.
    do_early_stopping (:obj:`bool`, `optional`, defaults to :obj:`False`):
        Whether to stop the beam search when at least ``num_beams`` sentences are finished per batch or not.
    num_beam_hyps_to_keep (:obj:`int`, `optional`, defaults to 1):
        The number of beam hypotheses that shall be returned upon calling
        :meth:`~transformer.BeamSearchScorer.finalize`.

batch_size
max_length	num_beamsdevicelength_penaltydo_early_stoppingnum_beam_hyps_to_keepc           
         X l         X0l        X@l        XPl        X`l        Xpl        SU l        [        U5       Vs/ s H7  n[        U R                  U R                   U R                  U R                  S9PM9     snU l	        [        R                  " [        U5       Vs/ s H  nSPM     sn[        R                  U R                  S9U l        g s  snf s  snf )NF)r0   r/   r2   early_stoppingdtyper1   )r/   r0   r1   r2   r3   r4   _is_initrangeBeamHypotheses
_beam_hypsr&   tensorbool_done)	r   r.   r/   r0   r1   r2   r3   r4   _s	            r   __init__BeamSearchScorer.__init__   s     %",!2%:" Z(
 )! ..??#22#55	
 )
 \\%
2C"D2CQ52C"D(-

)-6

 #Es   >CCr   c                 6    U R                   R                  5       $ N)r?   allr   s    r   is_doneBeamSearchScorer.is_done   s    zz~~r   Nr   r   r   r   pad_token_ideos_token_idc                 v   UR                   S   n[        U R                  5      n	XR                   S   U R                  -  :X  d   e[	        U[
        5      (       a  U/nUR                  n
[        R                  " XR                  4UR                  U
S9n[        R                  " XR                  4UR                  U
S9n[        R                  " XR                  4UR                  U
S9n[        U R                  5       GH  u  pU R                  U   (       a`  [        U5      U R                  :  d    SR                  U R                  5      5       eUb  Uc   S5       eSXS S 24'   X\US S 24'   SXS S 24'   Mz  Sn[        [        X>   X.   XN   5      5       H  u  nu  nnnXR                  -  U-   nUb  UR                  5       U;   au  UU R                  :  nU(       a  MJ  UR                  UU   R!                  5       UR                  5       U(       a%  U Vs/ s H  nUUR                  5       /   PM     snOS S9  OUXU4'   UXU4'   UXU4'   US-  nUU R                  :X  d  M    O   UU R                  :  a&  [#        SU R                   S	X>    S
U SX>    S3	5      eU R                  U   =(       d0    UR%                  X.   R'                  5       R                  5       U5      U R                  U'   GM     [)        UR+                  S5      UR+                  S5      UR+                  S5      S.5      $ s  snf )Nr   r7   z?Batch can only be done if at least {} beams have been generatedzMgenerated beams >= num_beams -> eos_token_id and pad_token have to be definedmems   zAt most z tokens in z  can be equal to `eos_token_id: z`. Make sure z are corrected.)next_beam_scoresnext_beam_tokensnext_beam_indices)shapelenr<   r0   
isinstanceintr1   r&   zerosr8   	enumerater?   formatzipitemaddclone
ValueErrorrG   maxr   view)r   r   r   r   r   rI   rJ   rN   cur_lenr.   r1   rP   rQ   rR   	batch_idxbeam_hypbeam_idxbeam_token_rank
next_token
next_score
next_indexbatch_beam_idx&is_beam_token_worse_than_top_num_beamsmems                           r   r   BeamSearchScorer.process   s    //"%)
ooa0DNNBCCClC(((>L## ;;
NN'C-8->->.46 !;;
NN'C-8->->.46 "KK^^(D.:.@.@/57 $-T__#=Izz)$MT^^3$T[[NN$$3 !,1IcbcI 23 A.1=A.23!Q,/ H1:&)+*@*5*@*6*A'C2D. ".*j", "+^^!;j!H ,:??3D6B4C >MPTP^P^=^:= LL!.1779")26/)-# ":??#4"56)-/<@	 ! B =G$%89<F$%89=K%&9:MH t~~-52D8 $..( t~~.k+:P9QQqr~q  @M  NY  Nd  Me  et  u 
 %)JJy$9 %>X=M=M&**,113W>>DJJy!i $>n  0 5 5b 9 0 5 5b 9!2!7!7!;
  	-/s   L6final_beam_scoresfinal_beam_tokensfinal_beam_indicesc                    [        U R                  5      n[        U R                  5       H  u  pU R                  U	   (       a  M  [	        U R
                  5       HX  nXR
                  -  U-   nX,   R                  5       nX   nU
R                  UUU(       a  U Vs/ s H  nX/   PM
     snOS S9  MZ     M     UR                  XR                  -  5      n/ n[        U R                  5       H~  u  nn
[        U
R                  S S9n[	        U R                  5       HJ  nUR                  5       SS  u  nn[        U5      UU R                  U-  U-   '   UR                  UU45        ML     M     [        UR                  5       R                  5       U R                   5      nUR                  XR                  -  U5      nUR                  5       R                  5       UR                  5       R                  5       :w  a  Uc   S5       eUR#                  U5        / n[        U5       H9  u  nu  nnUUUS UU   24'   UU   U:  a
  UUUUU   4'   UR                  U5        M;     U(       aZ  US   (       aP  [	        [        US   5      5       VVs/ s H+  n[$        R&                  " U Vs/ s H  oU   PM	     snSS9PM-     snnOS nUU4$ s  snf s  snf s  snnf )NrM   c                     U S   $ )Nr   r    )xs    r   <lambda>+BeamSearchScorer.finalize.<locals>.<lambda>  s    qtr   )keyrO   z `pad_token_id` has to be definedr   )dim)rT   r<   rX   r?   r:   r0   r[   r\   newr4   sortedbeamspopappendminr_   r/   fill_r&   cat)r   r   rm   rn   ro   rI   rJ   rN   r.   rb   rc   beam_idri   final_scorefinal_tokensrk   sent_lengthsbestisorted_hypsjbest_hypsent_max_lendecodedhypos                            r   r   BeamSearchScorer.finalize   s    )
 $-T__#=Izz)$ !0!*^^!;g!E/?DDF(8 .2T+%)c ./%)+8<	  >	 1 $>  !}}Z2L2L%LM %T__5KAx ^DK4556!,!212!6$$'M T77!; ! "Xt,-	 7 6 <++-224dooF$-MM333\%C ""$(8(8(:(?(?(AA+O-OO+MM,' 'oNA{c+/GA'Q''(A-.:<?*+KK	 . d1g 3tAw<(
( II.#1v.A6(
 $( 	 }I+B /
s   KK)K7
KK)	r<   r?   r9   r1   r3   r2   r/   r4   r0   )g      ?FrO   )NNN)r!   r"   r#   r$   r%   rV   r&   r1   r   floatr>   rA   propertyrG   r'   r(   r	   r)   r   r   r   r*   r    r   r   r,   r,   g   s   B +.,1/066 6 	6
 6 !6 $D>6  (}6D       /3.2S ++S"..S #--S $..	S
 'smS 'smS $ELL1St 04/3;!,,;$)$5$5; %*$4$4; &+%5%5	;
  (};  (};  %U%5%5tELL7I%IJ; ;r   r,   c                   t    \ rS rSrS\S\S\S\4S jrS rSS	\	R                  S
\4S jjrS\S\S\4S jrSrg)r;   i<  r0   r/   r2   r6   c                 X    US-
  U l         X0l        X@l        Xl        / U l        SU l        g)z'
Initialize n-best list of hypotheses.
rO   g    eAN)r/   r2   r6   r0   ry   worst_score)r   r0   r/   r2   r6   s        r   rA   BeamHypotheses.__init__>  s/    
 %q.,,"
r   c                 ,    [        U R                  5      $ )z#
Number of hypotheses in the list.
)rT   ry   rF   s    r   __len__BeamHypotheses.__len__J  s     4::r   Nhypsum_logprobsc           
         U[        UR                  S   S5      U R                  -  -  n[        U 5      U R                  :  d  X@R
                  :  a  U R                  R                  XAU45        [        U 5      U R                  :  a\  [        [        U R                  5       VVVs/ s H  u  nu  n  ovU4PM     snnn5      nU R                  US   S   	 US   S   U l        g[        X@R
                  5      U l        ggs  snnnf )z#
Add a new hypothesis to the list.
rL   rO   r   N)r_   rS   r2   rT   r0   r   ry   r{   rx   rX   r|   )	r   r   r   rN   scoreidxsr@   sorted_next_scoress	            r   r\   BeamHypotheses.addP  s     CIIbM1 5t7J7J JKt9t~~%1A1A)AJJu4014y4>>)%+3<TZZ3H-3Hiq!QH3H- &" JJ1!4Q78#5a#8#; #&u.>.>#?  *B-s   &D best_sum_logprobsra   r   c                     [        U 5      U R                  :  a  gU R                  (       a  gXU R                  -  -  nU R                  U:  nU$ )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.
FT)rT   r0   r6   r2   r   )r   r   ra   	cur_scorerets        r   rG   BeamHypotheses.is_done`  sK     t9t~~%  )T5H5H,HHI""i/CJr   )ry   r6   r2   r/   r0   r   rD   )r!   r"   r#   r$   rV   r   r>   rA   r   r&   r'   r\   rG   r*   r    r   r   r;   r;   <  s_    
# 
3 
 
!%
@u'' @u @    r   r;   c                   j    \ rS rSrSrS\R                  S\R                  S\R                  4S jrSr	g)	LogitsProcessorip  zSAbstract base class for all logit processors that can be applied during generation.r   scoresr   c                 2    [        U R                   S35      e)z#Torch method for processing logits.zH is an abstract class. Only classes inheriting this class can be called.)r   	__class__)r   r   r   s      r   __call__LogitsProcessor.__call__s  s!     "~~fg
 	
r   r    N
r!   r"   r#   r$   r%   r&   r'   r(   r   r*   r    r   r   r   r   p  s3    ]
%"2"2 
**
/4/@/@
r   r   c                   j    \ rS rSrSrS\R                  S\R                  S\R                  4S jrSr	g)	LogitsProcessorListi{  ab  
This class can be used to create a list of :class:`~transformers.LogitsProcessor` or
:class:`~transformers.LogitsWarper` to subsequently process a :obj:`scores` input tensor. This class inherits from
list and adds a specific `__call__` method to apply each :class:`~transformers.LogitsProcessor` or
:class:`~transformers.LogitsProcessor` to the inputs.
r   r   r   c                 (    U  H  nU" X5      nM     U$ rD   r    )r   r   r   	processors       r   r   LogitsProcessorList.__call__  s    Iy1F r   r    Nr   r    r   r   r   r   {  s5    %"2"2 **/4/@/@r   r   c                   |    \ rS rSrSrS\S\4S jrS\R                  S\R                  S\R                  4S	 jr
S
rg)MinLengthLogitsProcessori  a9  
:class:`transformers.LogitsProcessor` enforcing a min-length by setting EOS probability to 0.

Args:
    min_length (:obj:`int`):
        The minimum length below which the score of :obj:`eos_token_id` is set to :obj:`-float("Inf")`.
    eos_token_id (:obj:`int`):
        The id of the `end-of-sequence` token.

min_lengthrJ   c                     [        U[        5      (       a  US:  a  [        SU 35      e[        U[        5      (       a  US:  a  [        SU 35      eXl        X l        g )Nr   z2`min_length` has to be a positive integer, but is z4`eos_token_id` has to be a positive integer, but is )rU   rV   r^   r   rJ   )r   r   rJ   s      r   rA   !MinLengthLogitsProcessor.__init__  sf    *c**j1nDZLQ  ,,,q0@F|nU  %(r   r   r   r   c                 |    UR                   S   nX0R                  :  a  [        S5      * US S 2U R                  4'   U$ )NrL   inf)rS   r   r   rJ   )r   r   r   ra   s       r   r   !MinLengthLogitsProcessor.__call__  s:    //"%__$,1%L=F1d'''(r   )rJ   r   N)r!   r"   r#   r$   r%   rV   rA   r&   r'   r(   r   r*   r    r   r   r   r     sH    )3 )c )%"2"2 **/4/@/@r   r   c            
           \ rS rSrSrS\4S jrS\R                  S\R                  S\R                  4S jr
S	\R                  S
\S\S\\\      4S jrSrg)NoRepeatNGramLogitsProcessori  a<  
:class:`transformers.LogitsProcessor` that enforces no repetition of n-grams. See `Fairseq
<https://github.com/pytorch/fairseq/blob/a07cb6f40480928c9e0548b737aadd36ee66ac76/fairseq/sequence_generator.py#L345>`__.

Args:
    ngram_size (:obj:`int`):
        All ngrams of size :obj:`ngram_size` can only occur once.

ngram_sizec                 b    [        U[        5      (       a  US::  a  [        SU 35      eXl        g )Nr   z;`ngram_size` has to be a strictly positive integer, but is )rU   rV   r^   r   )r   r   s     r   rA   %NoRepeatNGramLogitsProcessor.__init__  s3    *c**jAoMj\Z  %r   r   r   r   c                     UR                   S   nUR                   S   nU R                  XU5      n[        U5       H  u  pg[        S5      * X&U4'   M     U$ )Nr   rL   r   )rS   _calc_banned_ngram_tokensrX   r   )r   r   r   num_batch_hypothesesra   banned_batch_tokensr   banned_tokenss           r   r   %NoRepeatNGramLogitsProcessor.__call__  sc    %||A//"%"<<W6 !**= >A(-e}Fm#$ !? r   prev_input_ids	num_hyposra   c           	      .  ^ ^^^ TS-   T R                   :  a  [        U5       Vs/ s H  n/ PM     sn$ [        U5       Vs/ s H  n0 PM     snm[        U5       Hy  nTU   R                  5       nTU   n[        [        T R                   5       Vs/ s H  nXhS PM	     sn6  H,  n	[	        U	SS 5      n
UR                  U
/ 5      U	S   /-   Xz'   M.     M{     UUUU 4S jn[        U5       Vs/ s H
  o" U5      PM     nnU$ s  snf s  snf s  snf s  snf )z6Copied from fairseq for no_repeat_ngram in beam_searchrO   NrL   c                    > TS-   TR                   -
  n[        TU UT24   R                  5       5      nTU    R                  U/ 5      $ )NrO   )r   tupletolistget)hypo_idx	start_idx	ngram_idxra   generated_ngramsr   r   s      r   _get_generated_ngramsUNoRepeatNGramLogitsProcessor._calc_banned_ngram_tokens.<locals>._get_generated_ngrams  sV    !doo5InX-6w->.? @@FJI#H-11)R@@r   )r   r:   r   rZ   r   r   )r   r   r   ra   r@   r   
gen_tokensgenerated_ngramr   ngramprev_ngram_tupler   r   r   r   s   `` `          @r   r   6NoRepeatNGramLogitsProcessor._calc_banned_ngram_tokens  s=    Q;( %i 01 01B 011(-i(89(81B(89#C',335J.s3O(-doo(>@(>1  *"~(>@ A#(s#4 4C4G4G$b5*-22YK581A $	A 	A =B)<L
<L!(+<L 	 
 + 29@
s   DDD
.D)r   N)r!   r"   r#   r$   r%   rV   rA   r&   r'   r(   r   r)   r   r   r   r*   r    r   r   r   r     sr    %3 %
%"2"2 
**
/4/@/@
 -0+.373Fr   r   )abcr   r   collectionsr   typingr   r   r   r	   r&   PROCESS_INPUTS_DOCSTRINGFINALIZE_INPUTS_DOCSTRINGr   r,   r;   r   listr   r   r   r    r   r   <module>r      s    $   2 2  B :A A(Rz Rj1 1h
c 
$  B8? 8r   