L iddlmZmZddlmZddlmZmZddlZ ddl Z ddl m Z m Z ddlmZmZe j"eZd Zd ZGd d eZGd deZGddeZGddZy))ABCabstractmethod)UserDict)OptionalUnionN)add_start_docstringslogging) ConstraintConstraintListStatea 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. 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`. cZeZdZdZeeedejdejdejdejde ejf dZ eee dejdejdejdejdedejf d Zy ) 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_indicesreturnc tdNzThis is an abstract method.NotImplementedError)selfrrrrkwargss i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/generation/beam_search.pyprocesszBeamScorer.processbs""?@@ max_lengthc tdrr)rrrrrrrs rfinalizezBeamScorer.finalizens""?@@rN)__name__ __module__ __qualname____doc__rr PROCESS_INPUTS_DOCSTRINGtorch LongTensor FloatTensortupleTensorrFINALIZE_INPUTS_DOCSTRINGintr rrrr\s 23A##A&&A%% A && A u|| A4A34 A## A&& A%% A && A  A    A5 ArrceZdZdZ ddededej dedee e fded ed e efd Z e d e fd Z ddejdej dejdejde eeej"fde eeeeej"fde ejdeded ee ej"ffdZ ddejdej dejdejd ede eeej"fde eeeeej"fde ejded eejfdZy)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://huggingface.co/papers/1610.02424) for more details. max_length (`int`, *optional*): The maximum length of the sequence to be generated. N batch_size num_beamsdevicelength_penaltydo_early_stoppingnum_beam_hyps_to_keepnum_beam_groupsrc tjd||_||_||_||_||_||_|j|jz|_d|_ t||jzD cgc]/} t|j|j|j |1c} |_ tjt||jzD cgc]} dc} tj|j|_t#|t$r|dkrt'd|dt#|t$r ||kDs||zdk7rt'd |d |d ycc} wcc} w) Nz`BeamSearchScorer` is deprecated and will be removed in v4.62.0, as constrained beam search has been moved to the Hub: https://hf.co/transformers-community/constrained-beam-search.Fr1r3early_stoppingrdtyper2r 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.rz`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 z with `num_beams` being .)logger warning_oncer1r2r3r4r5r6 group_size_is_initrangeBeamHypotheses _beam_hypsr&tensorbool_done isinstancer, ValueError) rr0r1r2r3r4r5r6r_s r__init__zBeamSearchScorer.__init__s  C # ,!2%:"...D,@,@@ :(<(<<=  //#22#55%   \\!*t/C/C"CD EqU EUZZ`d`k`k  )S)Y!^ST]S^_CC  /3/Oi4OU^apUptuUu;;J:KKcdmcnnoq Vv+  Fs ?4E($ E-rcR|jjjSNrHallitemrs ris_donezBeamSearchScorer.is_donezz~~$$&&rrrrr pad_token_id eos_token_id beam_indices group_indexdecoder_prompt_lenc |jddz} t|j|jz} | |jd|jzk7ra|jdkDr)t d|jdd|jdt d|jdd|jd|j } tj| |jf|j| } tj| |jf|j| }tj| |jf|j| }|Bt|tjs(t|tr|g}tj|}t| D]}||jz|z}|j|ri|j t|j|krt d |j d || t d d| |ddf<|||ddf<d||ddf<d}t#t%||||||D]\}\}}}||jz|z}|y|j'|vrg||jk\}|rB| ||}||fz}nd}|j|j)||j+|j'|| | z n|| ||f<||||f<||||f<|dz }||jk(sn||jkr(t d|jd||d|d||d |j|xs?|j|j-||j/j'| | |j|< t1| j3d|j3d|j3ddS)Nr rzA group beam size of z0 is used as the input, but a group beam size of z is expected by the beam scorer.zA beam size of z* is used as the input, but a beam size of r:#Batch can only be done if at least  beams have been generatedMGenerated beams >= num_beams -> eos_token_id and pad_token have to be definedrW generated_lenAt most tokens in can be equal to `eos_token_id: `. Make sure  are corrected.next_beam_scoresnext_beam_tokensnext_beam_indices)shapelenrEr6rArJr2r&zerosr;rIr*r,rFrCrHr1 enumerateziprQaddclonerSmaxrview)rrrrrrUrVrWrXrYcur_lenr0r2rgrhri batch_idxbatch_group_idxbeam_idxbeam_token_rank next_token next_score next_indexbatch_beam_idx&is_beam_token_worse_than_top_num_beams beam_indexs rrzBeamSearchScorer.processs=//"%))T-A-AA )//!,? @##a' +IOOA,>+?@#//OQ !%iooa&8%99c''GI !! ;; DOO'DKL]L]flm ;; DOO'DKL]L]flm!KKT__(E\M_M_hno  #J|U\\,R,, ,~  << 5Lz*9 I'$*>*>>LOzz/*>>C(H$II$'J4>>JZZt%uvv'<+?$%tuu12 A.1= A.23!)Q,/HIRK *K ,BLQZD[\J E!E*j*"+T__!2C%C %) OOO488!.1779")%/&-0B&B 9=G$Y%89PJDJJ 'o9 v$4$9$9"$=$4$9$9"$=%6%;%;B%?   rfinal_beam_scoresfinal_beam_tokensfinal_beam_indicesc t|j|jz} |Bt|tj s(t|t r|g}t j|}t|jD]\} } |j| rt|jD]\} | |jz| z}||j}||}|||nd}|jd| z }| j||||^|j| |j z}g}g}t j"| |j z|j$tj&}t| D]}|j||jz|dz|jz}|D cgc]} | j(D]}|}} }t+|d}t|j D]v}|j-}|d}|d}|d} t|||j |z|z<|j/||j/| ||||j z|z<x|j1jdz}!| t3|!|n|!}"|j| |j z|"}#t|dkDr%|d |j| |j z|"}$nd}$|j3j|j1jk7r| t5d |#j7||$|$j7dtt9||D]Q\}\}%}&|%|#|d||f<|$%t j|&|$|dt|&f<|||"ksE|d|#|||f<St;|#||$d Scc}} w) Nr[r_r2r;r c |dSNrr-xs rz+BeamSearchScorer.finalize..ps !rkeyrr `pad_token_id` has to be defined sequencessequence_scoresrW)rkrEr6rIr&r*r,rFrmrHrCrArQrjronewr5rlr2float32beamssortedpopappendrqminrJfill_rnr)'rrr~rrrrUrVrWrYr0rubeam_hypindex_per_groupr{ final_score final_tokensr}r` sent_lengthsbest best_indices best_scoresibeam_hyps_in_batchbeamcandidate_beams sorted_hypsjbest_hyp_tuple best_scorebest_hyp best_indexsent_lengths_max sent_max_lendecodedindiceshypobest_idxs' rr zBeamSearchScorer.finalizeDs)T-A-AA  #J|U\\,R,, ,~  << 5L*34??)C n %OXzz/*$)#9 n!04??!B_!T/?DDF (8 =I=U\.9[_ , 2 22 69K K  \;Z_l m  n n!}}Z$2L2L%LM  kk*t/I/I"IRVR]R]ejerers z" MA!%T5I5I1IQQRUVZVjVjLj!k 4FbS[SaSab4tbtbOb nEK4556 M!,!2+A. )!,+A. CFx= T77!;a?@ H%##J/BL A : ::Q>? M  M((++-224q8 Ah,0GA(a(( )".3ll8.D?S]?*+A-.:1o<?*+ >$#. '   _cs&O )?Fr r N)NNNrrNNNr)r!r"r#r$r,r&r2floatrrGstrrrLpropertyrSr'r(r*listdictrr)r r-rrr/r/|sV$V!$.3%& $(222 2  2 !s+ 2 #22SM2h'''<@FJ37"#g ##g &&g %% g && g uS%,,%678 g uS$s)U\\%ABCg u//0g g  g  c5<< g `<@FJ37"#` ##` !,,` !++ ` ",, `  ` uS%,,%678` uS$s)U\\%ABC` u//0`  `  u ` rr/cJeZdZdZ d%dededeedejde de e e fd ed e efd Zed e fd ZdZdZ d&dej&dej(dej&dej&dej(de e eej*fde e eeeej*fde ej&ded eej*fdZ d'dedej&dej(dej(dej&dej&de fd Z d&dej&d!ej(d"ej&d#ej&d ede e eej*fde e eeeej*fde ej&ded eej&fd$Zy)(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`]. max_length (`int`, *optional*): The maximum length of the sequence to be generated. Nr0r1 constraintsr2r3r4r5rc tjd||_||_||_||_||_||_d|_t|D cgc]/} t|j|j|j |1c} |_ tjt|D cgc]} dc} tj|j|_t!|t"r|dkrt%d|dycc} wcc} w)Nz`ConstrainedBeamSearchScorer` is deprecated and will be removed in v4.62.0, as constrained beam search has been moved to the Hub: https://hf.co/transformers-community/constrained-beam-search.Fr8r:r r<r=)r?r@r1r2r3r4r5rrBrCrDrEr&rFrGrHrIr,rJ) rr0r1rr2r3r4r5rrKs rrLz$ConstrainedBeamSearchScorer.__init__s  N # ,!2%:"& :&  ..#22#55%   \\% 2C"DQ5"DEJJ_c_j_jk )S)Y!^ST]S^_CC .< #Es 4D, DrcR|jjjSrNrOrRs rrSz#ConstrainedBeamSearchScorer.is_donerTrc t|Dcgc]3}t|jDcgc]}|jc}5c}}Scc}wcc}}wrN)rCr rcopy)rnrK constraints rmake_constraint_statesz2ConstrainedBeamSearchScorer.make_constraint_statess>ejklemn`a#IYIY$Z:Z__%6$Z[nn$ZnsAA  A Acd|jdd}|j||jS)Nr r)rreset completed)rsequence new_states rcheck_completes_constraintsz7ConstrainedBeamSearchScorer.check_completes_constraintss///215 !"""rrrrrscores_for_all_vocabrUrVrWrYc |jddz} t|j} |j} t j | |j f|j| } t j | |j f|j| }t j | |j f|j| }|Bt|tjs(t|tr|g}t j|}t|jD]&\}}|j|r\|j t|krtd|j d|| tdd| |ddf<|||ddf<d||ddf<rd}tt||||||D]\}\}}}||j z|z}||j!|vr~||j k\}|rB|j#||j%}|rb| ||}||fz}nd}|j'||j)|j!|| | z n|| ||f<||||f<||||f<|dz }||j k(sn|j+|||| |||||\}}}|| |<|||<|||<||j kr(td |j d ||d |d ||d |j|xs2|j-||j/j!| | |j|<)t1| j3d|j3d|j3ddS)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:Nr\r]r^rr_rarbrcrdrerf)rjrkrEr2r&rlr1r;rIr*r,rFrmrHrJrnrQrtolistrorpstep_sentence_constraintrSrqrrr)rrrrrrrUrVrWrYrsr0r2rgrhrirtrrvrwrxryrzr{r|completes_constraintr} new_scores new_tokens new_indicess rrz#ConstrainedBeamSearchScorer.processsh//"%)) !! ;; DNN'C;K\K\ekl ;; DNN'C;K\K\ekl!KKT^^(DLL^L^gmn  #J|U\\,R,, ,~  << 5L#,T__#=H  Ixzz)$>>CM1$'J4>>JZZt%uvv'<+?$%tuu12 A.1= A.23!)Q,/HIRK *K ,BLQZD[\J" E!E*j*"+T^^!;j!H ,:??3D 3T=LPTP^P^=^:= +/+K+KIVdLeLlLlLn+o(+'3)5n)EJ)3~6G)GJ)-J %n5;;=&OO-)3*14F*F %=G$Y%89P>DJJy !MH T$4$9$9"$=$4$9$9"$=%6%;%;B%?   rrt vocab_scoressent_beam_scoressent_beam_tokenssent_beam_indices push_progresscB |jd}|j} |j|} |j|} ||z|dz|z} } || | }|| | }tj|||j dfd}|j ggggd}t|D]\\}}| |}|j||j | |}|j|j |js$tj|jtj| }|D]}|jd}|j|j tj||j dfdj }||d vsq|d j||d j| |z|d j||d j||j!||d j||stj"||d\}}tj||j dfd}| |}|j }|j|||d vs|d j||d j||d j||d j||d j|_t%|d dkDrtj|d | }tj&|d j)| }tj&|d j)| }| |d z}tj||fd} tj||fd}!tj|D"cgc]}"|"j+c}"| }#|#dz|!z}$|$j-dj.}%|#|%}&d}'|&d}(g})|&D]"}*|*|(k(r|'dz }'nd}'|*}(|)j|'$tjt1j2|)d}+|%|+d|}%|!|%}| |%}tj||f|%}|||fScc}"w)Nrr r[)dim)new_seqs new_statesrrrr:T)statefulrrrrr)r2d) descending mergesort)kind)sizer2rr&cat unsqueezerrmrrrFadvancelongrrortakerqrkstacktoget_banksortrnpargsort),rrtrrrrrrorig_lenr2topk_constraint_statesadvance_constraint_statessidxeidxthis_batch_input_idsthis_batch_token_scoresfull_hypotheses track_newseq_idxpre_seq topk_state advance_stateadvance_tokens advance_tokenr advance_seq new_score new_tokenrrr all_states all_tokens all_scoresone all_bankszippedr sorted_bankscountercur_bank incrementsbank rearrangerss, rrz4ConstrainedBeamSearchScorer.step_sentence_constraints(%))!,"))"&! ?? B GW08J   _W5<<> ?5g>M    0 1 **!&m.C.C.EUZZ`f!g%3 BM - 2 2D 2 AIMM-"6"6"89"'))Wm6M6Ma6P,QSU"V"]"]"_K")J*??!*-44[A!-077wG!,/66}E!,/667Nw7W7\7\]j7kl!,/66yA B((-yy1H1QST'U$ 9#ii)2E2Ea2H(I2N 9' B )002 ##K0i &;;j)00=m,33G<l+229=l+229=l+22=A? BB y' (1 ,,,y'?OKY|%<=@@HJY|%<=@@HJ/)L2IIJ$4j#A2FJ$4j#A2FJ %Kclln%KTZ[I_z1FkkTk2::G$W-LG#AHJ$ +8#qLGG#H!!'*  + ,,rzz*;'OPKk*9H5G)'2 )'2  % +.Zs qtrrrr rrr)rkrErIr&r*r,rFrmrHrCr1rQrrrjrorr5rrlr2rrrrrqrrJrrnr)&rrr~rrrrUrVrWrYr0rtr ids_collectbeam_idr{rrrr}r`rrrrrrrrrrrrrrrrrs& rr z$ConstrainedBeamSearchScorer.finalizest)  #J|U\\,R,, ,~  << 5L$-T__#=  Ixzz)$ K 0 0!*T^^!;g!E/?DDF (8 '+'G'G H[H[H]'^$'AMAYn!=_cJ$0$6$6r$:=O$OMLL{cpLq&&w/ 0;$"<"<<$T^^4Gk1)2T^^)Cg)M&7&G&L&L&N '0'@ (4(:(:2(>AS(S  \;m \;'4+E+EE/ D!}}Z$2L2L%LM  kk*t/I/I"IRVR]R]ejerers %T__5 MKAx ^DK4556 M!,!2+A. )!,+A. CFx= T77!;a?@ H%##J/BL A : ::Q>? M M$(++-224q8 Ah,0GA(a(( )".3ll8.D?S]?*+A-.:1o<?*+ >$#. '   r)rFr Nr)F)r!r"r#r$r,rr r&r2rrrGrrrLrrSrrr'r(r*r)rrr r-rrrrsN!$.3%&$(%%%*% %  %  %!s+% #%SM%N'''o#<@FJ37"#R ##R &&R %% R && R $// R uS%,,%678R uS$s)U\\%ABCR u//0R  R  u|| R x$KEKE##KE'' KE  ++ KE  ** KE!++KEKEh<@FJ37"#r ##r !,,r !++ r ",, r  r uS%,,%678r uS$s)U\\%ABCr u//0r  r  u r rrc eZdZ ddededeeefdeefdZ dZ dde jd ed ee jd eefd Z dd edededefdZy)rDNr1r3r9rctjd||_||_||_||_g|_d|_t|jts|j tdyy)z7 Initialize n-best list of hypotheses. z`BeamHypotheses` is deprecated and will be removed in v4.62.0, as constrained beam search has been moved to the Hub: https://hf.co/transformers-community/constrained-beam-search.geANzWhen `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.) r?r@r3r9rr1r worst_scorerIrGrJ)rr1r3r9rs rrLzBeamHypotheses.__init__s|  A -,$" $--t49PE :Q4rc,t|jS)z3 Number of hypotheses in the list. )rkrrRs r__len__zBeamHypotheses.__len__s4::rhyp sum_logprobsrWr`c  ||||jzz }n||jd|jzz }t||jks||jkDr|j j |||ft||jkDrZtt|j Dcgc] \}\}}}||fc}}}} |j | dd=| dd|_yt||j|_yycc}}}w)z3 Add a new hypothesis to the list. Nr[rr ) r3rjrkr1r rrrrmr) rrrrWr`scoreidxsrKsorted_next_scoress rrozBeamHypotheses.adds  $ M43F3F$FGE!CIIbMT5H5H$HIE t9t~~ %1A1A)A JJ  uc<8 94y4>>)%+IVZV`V`La,b,b.#y1aaX,b%c"JJ1!4Q78#5a#8#; #&ud.>.>#? *B-cs0D best_sum_logprobsrsrYrct||jkry|jdury|jdur&|||z |jzz }|j|k\}|S|jdkDr:|j |kr t d||j |z |jzz }n|||z |jzz }|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. FTgz3max_length is not larger than decoder prompt length)rkr1r9r3r rrJ)rrrsrYhighest_attainable_scorerets rrSzBeamHypotheses.is_dones t9t~~ %   $ & E )'8GFX>CJ ""S(??&88$%Z[[%;M)MRVReRe(ee) ,=J\@\aeatat?t+t(""&>>CJrrN)NN)r)r!r"r#r,rrrGrrrLr r&r'rorSr-rrrDrDssw.3EJ4QT9EUcklocp,48'+ @   @@u//0 @  } @2"""RU"^b"rrD)abcrr collectionsrtypingrrnumpyrr&utilsr r beam_constraintsr r get_loggerr!r?r%r+rr/rrDr-rrr sz $ " 1=   H % D8AA@h zh V h *h VXXr