L i%S :ddlZddlZddlmZddlmZmZmZmZddl Z ddl Z ddl m Z ddlmZddlmZerddlmZeeZd ZGd d ZGd d eZGddeZGddeZGddeZGddeZGddeZGddeZGddeZ GddeZ!GddeZ"Gd d!eZ#Gd"d#eZ$d$e%d%e jLd&e%fd'Z'd(Z(d$e%d%e jLd&e%d)e%d*eee%f d+Z)Gd,d-eZ*Gd.d/eZ+Gd0d1eZ,Gd2d3e,Z-Gd4d5eZ.Gd6d7eZ/Gd8d9eZ0Gd:d;eZ1Gd<d=eZ2Gd>d?eZ3Gd@dAeZ4GdBdCeZ5GdDdEeZ6GdFdGeZ7GdHdIeZ8GdJdKeZ9GdLdMeZ:GdNdOeZ;GdPdQeZ<GdRdSeZ=GdTdUZ>GdVdWeZ?GdXdYeZ@GdZd[eZAGd\d]eZBy)^N)Iterable) TYPE_CHECKINGCallableOptionalUnion)isin_mps_friendly)add_start_docstrings) get_logger)GenerationConfiga[ Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids) scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search Return: `torch.FloatTensor` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. cveZdZdZeedejdejdejfdZ y)LogitsProcessorzSAbstract base class for all logit processors that can be applied during generation. input_idsscoresreturnc2t|jd)NzH is an abstract class. Only classes inheriting this class can be called.)NotImplementedError __class__)selfrrs l/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/generation/logits_process.py__call__zLogitsProcessor.__call__5s!~~f g  N __name__ __module__ __qualname____doc__r !LOGITS_PROCESSOR_INPUTS_DOCSTRINGtorch LongTensor FloatTensorrrrrr2sE];< %"2"2 Ez/LogitsProcessorList.__call__..VsSS3&=SsNz,Make sure that all the required parameters: z for z$ are passed to the logits processor.) inspect signaturer parameterslenalllistkeys ValueErrorr)rrrr* processor function_argss ` rrzLogitsProcessorList.__call__Cs  6I#--i.@.@ALLM=!A%SD9K9K9M4Nqr4RSS$FtML^L^L`GaFbbg$..//SU#9f??"9f5 6 rN)rrrrrr r!rr"rrr$r$<s4 %"2"2E>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m") >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m") >>> inputs = tokenizer("A number:", return_tensors="pt") >>> gen_out = model.generate(**inputs) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) A number: one >>> # setting `min_length` to a value smaller than the uncontrolled output length has no impact >>> gen_out = model.generate(**inputs, min_length=3) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) A number: one >>> # setting a larger `min_length` will force the model to generate beyond its natural ending point, which is not >>> # necessarily incorrect >>> gen_out = model.generate(**inputs, min_length=10) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) A number: one thousand, nine hundred and ninety-four ``` min_length eos_token_iddevicect|tr|dkrtd|t|tjs*t|tr|g}tj ||}||_||_y)Nrz6`min_length` has to be a non-negative integer, but is r:) isinstanceintr3rTensortensorr8r9)rr8r9r:s r__init__z!MinLengthLogitsProcessor.__init__se*c*j1nUV`Uabc c, 5,, ,~  << VDL$(rrrrc2tj|jd|j}t ||j }|j }|jd|jkr&tj|tj |}|SNr<) rarangeshaper:r r9cloner8wheremathinf)rrr vocab_tensoreos_token_maskscores_processeds rrz!MinLengthLogitsProcessor.__call__sq||FLL$4V]]K *<9J9JK!<<> ??2  0${{>DHH9fM rNcpurrrrr>rr1rr?strrAr rr r!rr"rrr7r7bsy$L )3 )eCcELL>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m") >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m") >>> inputs = tokenizer(["A number:"], return_tensors="pt") >>> gen_out = model.generate(**inputs) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) A number: one >>> # setting `min_new_tokens` will force the model to generate beyond its natural ending point, which is not >>> # necessarily incorrect >>> gen_out = model.generate(**inputs, min_new_tokens=2) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) A number: one thousand ``` prompt_length_to_skipmin_new_tokensr9r:c"d|fd|ffD]+\}}t|tr|dkstd|d|t|tjs*t|tr|g}tj ||}||_||_||_y)NrTrUr`z'` has to be a positive integer, but is r<) r=r>r3rr?r@rTrUr9)rrTrUr9r:arg_name arg_values rrAz*MinNewTokensLengthLogitsProcessor.__init__s%&; < ~ .$  c Hii-Q 1XJ.UV_U`!abb  c, 5,, ,~  << VDL%:",(rrrrcP|jd|jz }|j}tj|jd|j }t ||j}||jkr&tj|tj |}|SrC) rFrTrGrrEr:r r9rUrHrIrJ)rrrnew_tokens_lengthrMrKrLs rrz*MinNewTokensLengthLogitsProcessor.__call__s%OOB/$2L2LL!<<>||FLL$4V]]K *<9J9JK t22 2${{>DHH9fM rNrNrPr"rrrSrSs"R )"))CcELL89 )  ).;< %"2"2 E Make sure that `do_sample=True` is included in the `generate` arguments otherwise the temperature value won't have any effect. Args: temperature (`float`): Strictly positive float value used to modulate the logits distribution. A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting all probability mass to the most likely token. Examples: ```python >>> import torch >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> set_seed(0) # for reproducibility >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> model.config.pad_token_id = model.config.eos_token_id >>> inputs = tokenizer(["Hugging Face Company is"], return_tensors="pt") >>> # With temperature=1.0, the default, we consistently get random outputs due to random sampling. >>> generate_kwargs = {"max_new_tokens": 10, "do_sample": True, "temperature": 1.0, "num_return_sequences": 2} >>> outputs = model.generate(**inputs, **generate_kwargs) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) ['Hugging Face Company is one of these companies that is going to take a', "Hugging Face Company is a brand created by Brian A. O'Neil"] >>> # However, with temperature close to 0, it approximates greedy decoding strategies (invariant) >>> generate_kwargs["temperature"] = 0.0001 >>> outputs = model.generate(**inputs, **generate_kwargs) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) ['Hugging Face Company is a company that has been around for over 20 years', 'Hugging Face Company is a company that has been around for over 20 years'] ``` temperaturect|tr|dkDs+d|d}t|tr |dk(r|dz }t|||_y)Nrz`temperature` (=zX) has to be a strictly positive float, otherwise your next token scores will be invalid.zI If you're looking for greedy decoding strategies, set `do_sample=False`.)r=floatr3r^)rr^ except_msgs rrAz TemperatureLogitsWarper.__init__sY+u-kAo";-0** +u-+2Dii Z( (&rrrrc$||jz }|Sr')r^rrrrMs rrz TemperatureLogitsWarper.__call__#s!D$4$44rN) rrrrrarAr rrr r!rr"rrr]r]sV-^ 'E ';< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM, RepetitionPenaltyLogitsProcessor >>> # Initializing the model and tokenizer for it >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer(["I'm not going to"], return_tensors="pt") >>> # This shows a normal generate without any specific parameters >>> summary_ids = model.generate(**inputs) >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0]) I'm not going to be able to do that. I'm going to be able to do that >>> # This generates a penalty for repeated tokens >>> penalized_ids = model.generate(**inputs, repetition_penalty=1.1) >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0]) I'm not going to be able to do that. I'll just have to go out and play >>> # We can also exclude the input prompt by creating an instance of this class >>> # with a `prompt_ignore_length` and passing it as a custom logit processor >>> rep_pen_processor = RepetitionPenaltyLogitsProcessor( ... penalty=1.1, ... prompt_ignore_length=inputs["input_ids"].shape[-1] ... ) >>> penalized_ids = model.generate(**inputs, logits_processor=[rep_pen_processor]) >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0]) I'm not going to be able to do that. I'm going to have to go through a lot of things, and ``` Npenaltyprompt_ignore_lengthct|tr|dkDstd||#t|tr|dkrtd|||_||_d|_d|_y)Nr6`penalty` has to be a strictly positive float, but is z<`prompt_ignore_length` has to be a positive integer, but is )r=rar3r>rgrhlogits_indices cu_seq_lens_q)rrgrhs rrAz)RepetitionPenaltyLogitsProcessor.__init__[su'5)'A+UV]U^_` `  +/59MPQ9Q[\p[qrs s $8!"!rrkrlc ||_||_yr')rkrl)rrkrls rset_continuous_batching_contextz@RepetitionPenaltyLogitsProcessor.set_continuous_batching_contextis,*rrrrch|jr|dd|jdf}|jdk(r|j|j|j}|d|ddf}t j |tj }|j}|dd|ddz }t jt jt||j|}d|||f<t j|dk||jz||jz } t j|| ||d|ddf<|S|j\} } } |dddddf}t j |tj }|jdk(r8t j|} |jd| j!ddn|jd|dt j|dk||jz||jz } t j|| ||dddddf<|S|jdk(r|j!d}t j"|d|}t j|dk||jz||jz }|j%d||}|S)NrdtyperDr<T)rhdimrkrlr zeros_likeboolrepeat_interleaverEr/r:rHrgrFuniquescatter_ unsqueezegatherscatter)rrrlast_positions last_scores token_mask cu_seq_lenslengths seq_indicespenalty_scores batch_sizeseq_len vocab_size unique_tokensscorerMs rrz)RepetitionPenaltyLogitsProcessor.__call__msl  $ $!!T%>%>%@"@AI ::<1 "".43E3E3Q!%!4!4$Q%9: #--kL "00 %ab/K,<<#55ell3wXZehlhthtZt!u/4{{:~Wb/cq.!+,M39,,/ GZ$QAX. "--kL ==?a'$)LL$;M''=+B+B1+EtL''9d;!&[1_kDLL>XZehlhthtZt!u#(;;z>;#Wq"ax M ==?a !++A.I VQ 2 EAIut||';UT\\=QR!>>!Y>rr')rrrrrarr>rArr?rnr rr r!rr"rrrfrf)s/b " "Xc] "+ell+[`[g[g+;<( %"2"2( E>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m") >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m") >>> inputs = tokenizer(["Alice and Bob. The third member's name was"], return_tensors="pt") >>> gen_out = model.generate(**inputs) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) Alice and Bob. The third member's name was not mentioned. >>> # With the `encoder_repetition_penalty` argument we can trigger this logits processor in `generate`, which can >>> # promote the use of prompt tokens ("Bob" in this example) >>> gen_out = model.generate(**inputs, encoder_repetition_penalty=1.2) >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0]) Alice and Bob. The third member's name was Bob. The third member's name was Bob. ``` rgencoder_input_idsclt|tr|dkDstd|d|z |_||_y)Nrrjrs)r=rar3rgr)rrgrs rrAz0EncoderRepetitionPenaltyLogitsProcessor.__init__s:'5)'A+UV]U^_` `7{ !2rrrrctj|d|j}tj|dk||jz||jz }|j d|j|}|S)Nrsr)rr{rrHrgr|)rrrrrMs rrz0EncoderRepetitionPenaltyLogitsProcessor.__call__sc VQ(>(>? EAIut||';UT\\=QR!>>!T-C-CUKrN) rrrrrarr rAr rr!rr"rrrrsc"H33%:J:J3;< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> set_seed(1) >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt") >>> # With sampling, the output is unexpected -- sometimes too unexpected. >>> outputs = model.generate(**inputs, do_sample=True) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3 | < 4 (left-hand pointer) ; >>> # With `top_p` sampling, the output gets restricted to high-probability tokens. >>> # Pro tip: In practice, LLMs use `top_p` in the 0.9-0.95 range. >>> outputs = model.generate(**inputs, do_sample=True, top_p=0.1) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 ``` Infrstop_p filter_valuemin_tokens_to_keepct|}|dks|dkDrtd|t|tr|dkrtd|||_||_||_y)Nr?z.`top_p` has to be a float > 0 and < 1, but is rs:`min_tokens_to_keep` has to be a positive integer, but is )rar3r=r>rrr)rrrrs rrAzTopPLogitsWarper.__init__sje  19 MeWUV V,c27IA7MYZlYmno o ("4rrrrc(tj|d\}}|jdjd}|d|jz k}d|d|j df<|j d||}|j||j}|S)NF descendingrDrtrsr.) rsortsoftmaxcumsumrrr| masked_fillr) rrr sorted_logitssorted_indicescumulative_probssorted_indices_to_removeindices_to_removerMs rrzTopPLogitsWarper.__call__s(- 6e(L% ~(00R08??B?G$4DJJ#G DE t'>'>&>&@!@A5<rAr rrr r!rr"rrrrst%N=B%L=de 5e 55 5^a 5;<  %"2"2  E>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> set_seed(1) >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: A, B, C, D", return_tensors="pt") >>> # With sampling, the output is unexpected -- sometimes too unexpected. >>> outputs = model.generate(**inputs, do_sample=True) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: A, B, C, D, E — S — O, P — R >>> # With `top_k` sampling, the output gets restricted the k most likely tokens. >>> # Pro tip: In practice, LLMs use `top_k` in the 5-50 range. >>> outputs = model.generate(**inputs, do_sample=True, top_k=2) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: A, B, C, D, E, F, G, H, I ``` rrstop_krrczt|tr|dkrtd|t|||_||_y)Nrz6`top_k` has to be a strictly positive integer, but is )r=r>r3maxrr)rrrrs rrAzTopKLogitsWarper.__init__8s>%%!UV[U\]^ ^ 23 (rrrrct|j|jd}|tj||ddk}|j ||j }|S)NrDr.rDN)minrsizertopkrr)rrrrrrMs rrzTopKLogitsWarper.__call__?sZDJJ B0"UZZ%>q%A-%PP!--.?ARARSrNrr"rrrrst"H;@,bc)c))\_);< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> set_seed(1) >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt") >>> # With sampling, the output is unexpected -- sometimes too unexpected. >>> outputs = model.generate(**inputs, do_sample=True) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3 | < 4 (left-hand pointer) ; >>> # With `min_p` sampling, the output gets restricted to high-probability tokens. >>> # Pro tip: In practice, LLMs use `min_p` in the 0.01-0.2 range. >>> outputs = model.generate(**inputs, do_sample=True, min_p=0.1) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 ``` rrsmin_prrcd|cxkrdksntd|t|tr|dkrtd|||_||_||_y)Nrrz9`min_p` has to be a float in the [0, 1] interval, but is rsr)r3r=r>rrr)rrrrs rrAzMinPLogitsWarper.__init__wscU!c!XY^X_`a a,c27IA7MYZlYmno o ("4rrrrcltj|d}|jdd\}}|j|z}||k}tj|dd}tj |d|} d| dd|j f<| jd || } |j| |j} | S) NrDrTrtkeepdim)rrt)rtindexF.rs) rrrrargsortr{rr|rr) rrrprobs top_probs_ scaled_min_ptokens_to_removerrrrMs rrzMinPLogitsWarper.__call__s f"-yyRy6 1zzI-  </v$BG#(<<0@bP^#_ CH &?(?(?&?!?@4<rArr r!rr"rrrrHs_,\=B%L=de5e555^a5 %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m") >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m") >>> inputs = tokenizer("1, 2, 3", return_tensors="pt") >>> # We can see that greedy decoding produces a sequence of numbers >>> outputs = model.generate(**inputs) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, >>> # For this particular seed, we can see that sampling produces nearly the same low-information (= low entropy) >>> # sequence >>> set_seed(18) >>> outputs = model.generate(**inputs, do_sample=True) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 >>> # With `typical_p` set, the most obvious sequence is no longer produced, which may be good for your problem >>> set_seed(18) >>> outputs = model.generate( ... **inputs, do_sample=True, typical_p=0.1, return_dict_in_generate=True, output_scores=True ... ) >>> print(tokenizer.batch_decode(outputs.sequences, skip_special_tokens=True)[0]) 1, 2, 3 and 5 >>> # We can see that the token corresponding to "4" (token 934) in the second position, the most likely token >>> # as seen with greedy decoding, was entirely blocked out >>> print(outputs.scores[1][0, 934]) tensor(-inf) ``` g?rrsmassrrct|}|dkDr|dkstd|t|tr|dkrtd|||_||_||_y)Nrrsz2`typical_p` has to be a float > 0 and < 1, but is r)rar3r=r>rrr)rrrrs rrAzTypicalLogitsWarper.__init__slT{qTAXQRVQWXY Y,c27IA7MYZlYmno o( "4rrrrctjjj|d}tj|}||zj dd }tj | |z }tj|d\}}|jd|} | jdjd} | |jkjd} | j|jddz ||jd| jddkD} d | d d|j f<| j#d|| } |j%| |j&}|S) NrDrT)rFrrs)rr.)rnn functional log_softmaxexpnansumabsrr{rrrsumclamp_rFviewrr|rr)rrr normalizedpentshifted_scores sorted_scoresrrrlast_indrrrMs rrzTypicalLogitsWarper.__call__s^XX((44V4D IIj !Q&&r4&88ZK3#67(- >e(T% ~ b.9 (00R08??B?G%tyy055!5<M//3a78#0=3G3G8==Y[]^K_3`#` CD &?(?(?&?!?@4<= epsilon`. Takes the largest min_tokens_to_keep tokens if no tokens satisfy this constraint. See [Truncation Sampling as Language Model Desmoothing](https://huggingface.co/papers/2210.15191) for more information. Args: epsilon (`float`): If set to > 0, only the most tokens with probabilities `epsilon` or higher are kept for generation. filter_value (`float`, *optional*, defaults to -inf): All filtered values will be set to this float value. min_tokens_to_keep (`int`, *optional*, defaults to 1): Minimum number of tokens that cannot be filtered. Examples: ```python >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> set_seed(1) >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt") >>> # With sampling, the output is unexpected -- sometimes too unexpected. >>> outputs = model.generate(**inputs, do_sample=True) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3 | < 4 (left-hand pointer) ; >>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to >>> # Top P sampling, which restricts tokens based on their cumulative probability. >>> # Pro tip: The paper recommends using `epsilon_cutoff` values between 3e-4 and 9e-4 >>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=0.1) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 ``` rrsepsilonrrct|}|dks|dk\rtd|t|}|dkrtd|||_||_||_y)Nrrsz7`epsilon_cutoff` has to be a float > 0 and < 1, but is C`min_tokens_to_keep` has to be a strictly positive integer, but is )rar3r>rrr)rrrrs rrAzEpsilonLogitsWarper.__init__ss. a<7a<VW^V_`a a !34  !UVhUij  ("4rrrrc|jd}||jk}t|j|j d}||t j ||ddkz}|j||j}|S)NrDrrr) rrrrrrrrr)rrr probabilitiesrrrMs rrzEpsilonLogitsWarper.__call__$s2. )DLL8D++V[[_=-%**VU:STU:VWd:e1ef!--.?ARARSrNrr"rrrrst%N?DEl]fg 5 5U 5`c 5;<  %"2"2  E>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> set_seed(1) >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt") >>> # With sampling, the output is unexpected -- sometimes too unexpected. >>> outputs = model.generate(**inputs, do_sample=True) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3 | < 4 (left-hand pointer) ; >>> # With eta sampling, the output gets restricted to high-probability tokens. You can see it as a dynamic form of >>> # epsilon sampling that adapts its cutoff probability based on the entropy (high entropy = lower cutoff). >>> # Pro tip: The paper recommends using `eta_cutoff` values between 3e-4 to 4e-3 >>> outputs = model.generate(**inputs, do_sample=True, eta_cutoff=0.1) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 ``` rrsrOrrrr:ct|}|dks|dk\rtd|t|}|dkrtd|tj|||_||_||_y)Nrrsz3`eta_cutoff` has to be a float > 0 and < 1, but is rr<)rar3r>rr@rrr)rrrrr:s rrAzEtaLogitsWarper.__init__fs. a<7a<RSZR[\] ] !34  !UVhUij ||GF; ("4rrrrc|jd}tjj|j }tj |j tj|j tj| zd}||k}t |j|jd}||tj||ddkz}|j||j}|S)NrDr)logits).Nrr)rr distributions Categoricalentropyrrsqrtrrrrrr) rrrrretarrrMs rrzEtaLogitsWarper.__call__ws2. %%111@HHJii ejj&>G8AT&TUV_`)C/D++V[[_=-%**VU:STU:VWd:e1ef!--.?ARARSrN)rrrrrar>rQrAr rrr r!rr"rrrr2s1h6;5\M]^ns55,15WZ5hk5";<  %"2"2  EEF hE$U3BZ0 0?0C0CDTVX0Y]bce]f\g0gO, - h h 6 Fs B B cn|dz|z }t|||j}|j|gS)a Determines the banned tokens for the current hypothesis based on previously generated n-grams. Args: banned_ngrams (`dict`): A dictionary containing previously generated n-grams for each hypothesis. prev_input_ids (`torch.Tensor`): Generated token ids for the current hypothesis. ngram_size (`int`): The number sequential tokens taken as a group which may only occur once before being banned. cur_len (`int`): The current length of the token sequences for which the n-grams are being checked. Returns: List of tokens that are banned. rs)rrr) banned_ngramsrrcur_len start_idx ngram_idxs r_get_generated_ngramsrs?$! j(InYw7>>@AI   Y ++rrrc |dz|krt|Dcgc]}gc}St|||}t|Dcgc]}t||||||}}|Scc}wcc}w)z6Copied from fairseq for no_repeat_ngram in beam_searchrs)rrr)rrrrrrhypo_idx banned_tokenss r_calc_banned_ngram_tokensrs{Z!),-q--":~yIi(  .x8.:RT^`ghM  .s AA ceZdZdZdefdZeedejdejdejfdZ y) NoRepeatNGramLogitsProcessora N-grams are groups of "n" consecutive words, characters, or tokens taken from a sequence of text. Given the sentence: "She runs fast", the bi-grams (n=2) would be ("she", "runs") and ("runs", "fast"). In text generation, avoiding repetitions of word sequences provides a more diverse output. This [`LogitsProcessor`] enforces no repetition of n-grams by setting the scores of banned tokens to negative infinity which eliminates those tokens from consideration when further processing the scores. Note that, for decoder-only models like most LLMs, the prompt is also considered to obtain the n-grams. [Fairseq](https://github.com/pytorch/fairseq/blob/a07cb6f40480928c9e0548b737aadd36ee66ac76/fairseq/sequence_generator.py#L345). Use n-gram penalties with care. For instance, penalizing 2-grams (bigrams) in an article about the city of New York might lead to undesirable outcomes where the city's name appears only once in the entire text. [Reference](https://huggingface.co/blog/how-to-generate) Args: ngram_size (`int`): All ngrams of size `ngram_size` can only occur once. Examples: ```py >>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer(["Today I"], return_tensors="pt") >>> output = model.generate(**inputs) >>> print(tokenizer.decode(output[0], skip_special_tokens=True)) Today I'm not sure if I'm going to be able to do it. >>> # Now let's add ngram size using `no_repeat_ngram_size`. This stops the repetitions ("I'm") in the output. >>> output = model.generate(**inputs, no_repeat_ngram_size=2) >>> print(tokenizer.decode(output[0], skip_special_tokens=True)) Today I'm not sure if I can get a better understanding of the nature of this issue ``` rcXt|tr|dkrtd|||_y)Nrz;`ngram_size` has to be a strictly positive integer, but is )r=r>r3r)rrs rrAz%NoRepeatNGramLogitsProcessor.__init__s.*c*jAoZ[eZfgh h$rrrrc|jd}|jd}|j}t|j|||}t |D]\}}t d |||f<|SNrrDrJ)rFrGrr enumeratera) rrrnum_batch_hypothesesrrMbanned_batch_tokensrrs rrz%NoRepeatNGramLogitsProcessor.__call__sy%||A//"%!<<>7Thjqr )*= > ? A}27, Q - . ? rN rrrrr>rAr rrr r!rr"rrrrsV'R%3% ;< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m") >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m") >>> inputs = tokenizer("Alice: I love cats. What do you love?\nBob:", return_tensors="pt") >>> # With greedy decoding, we see Bob repeating Alice's opinion. If Bob was a chatbot, it would be a poor one. >>> outputs = model.generate(**inputs) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) Alice: I love cats. What do you love? Bob: I love cats. What do you >>> # With this logits processor, we can prevent Bob from repeating Alice's opinion. >>> outputs = model.generate(**inputs, encoder_no_repeat_ngram_size=2) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) Alice: I love cats. What do you love? Bob: My cats are very cute. ``` encoder_ngram_sizerc t|tr|dkrtd|||_t |j dk(r|j d}|j d|_t|||j|_ y)NrzC`encoder_ngram_size` has to be a strictly positive integer, but is rs) r=r>r3rr/rFrzrrr)rr rs rrAz,EncoderNoRepeatNGramLogitsProcessor.__init__*s,c26HA6MUVhUij -  && '1 , 1 ; ;A > +11!4 +,>@QSWSbSb crrrrc T|jd}||jz}|jd}|j}t|Dcgc]-}t |j ||z|||j |/}}t|D]\} } td || | f<|Scc}wr) rFrrGrrrrrra) rrrr num_beamsrrMrrrrs rrz,EncoderNoRepeatNGramLogitsProcessor.__call__5sLLO 0 //"%!<<> "),   "%%h)&;QSWSbSbdk   !**= > ? A}27, Q - . ?  s 2B%N) rrrrr>rr rAr rr!rr"rrrrsf#J d3 d5CSCS d;< %"2"2 E At a token-level, biasing a word is different from biasing a word with a space before it. If you want to bias "foo" mid-sentence, you'll likely want to add a prefix space and bias " foo" instead. Check the tokenizer section of our NLP course to find out why: https://huggingface.co/learn/nlp-course/chapter2/4?fw=pt Args: sequence_bias (`list[list[Union[list[int], float]]]`): List of lists that maps a sequence of tokens to its bias term (e.g. `[[[10, 45], -2.0], [[64], -7.5]]`). Positive biases increase the odds of the sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias will always be applied. Otherwise, the bias will only be applied if the sequence in question is about to be completed (in the token selection step after this processor is applied). Examples: ```python >>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct") >>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct") >>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt") >>> summary_ids = model.generate(inputs["input_ids"], max_new_tokens=4, do_sample=False) >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0]) The full name of Donald is Donald John Trump Sr. >>> def get_tokens(word): ... return tokenizer([word], add_special_tokens=False).input_ids[0] >>> # IMPORTANT: Remember our tip about adding spaces before words to bias them correctly. >>> sequence_bias = [[get_tokens("Trump"), -10.0],] # will fail to apply bias >>> biased_ids = model.generate( ... inputs["input_ids"], max_new_tokens=4, do_sample=False, sequence_bias=sequence_bias ... ) >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0]) The full name of Donald is Donald John Trump Sr. >>> sequence_bias = [[get_tokens(" Trump"), -10.0],] # will work >>> biased_ids = model.generate( ... inputs["input_ids"], max_new_tokens=4, do_sample=False, sequence_bias=sequence_bias ... ) >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0]) The full name of Donald is Donald John Harper. He >>> # We can also add a positive bias to nudge the model towards specific tokens or continuations. This technique >>> # is also more effective when paired up with beam search. >>> sequence_bias = [[get_tokens(" Donald Duck"), 10.0],] >>> biased_ids = model.generate( ... inputs["input_ids"], max_new_tokens=4, num_beams=4, do_sample=False, sequence_bias=sequence_bias ... ) >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0]) The full name of Donald is Donald Duck. He is ``` sequence_biascn||_|j|jd|_d|_y)NF)r_validate_arguments!_convert_list_arguments_into_dict length_1_biasprepared_bias_variables)rrs rrAz$SequenceBiasLogitsProcessor.__init__s4*   " ..0"',$rrrrc |js|j|tj|}||jz }|j j D]\}}t|dk(rt||jdkDr2t|dz }|d}tj|dd| dftj|dd|j|jjd}|dd|fxxtj|jtj||jtjd|jz cc<||z} | S)NrsrDrrr:rr<r`)r_prepare_bias_variablesrrurritemsr/rFeqr@rrr:prodrHrv) rrrbias sequence_idsr prefix_length last_token matching_rowsrMs rrz$SequenceBiasLogitsProcessor.__call__s]++  ( ( 0' """,0+=+=+C+C+E  'L-< A%< 9??1#55 -1M%b)J!HH!m^_,- \#2.iooiN^N^_dqdk  J 5;;""$ ]93C3CD S)9)9:$   $"D=rcP|jd}g}|jD] }|D]}||k\s |j|"t|dkDrt d|d|t j |ft j|j|_ g}g}|jjD]9\}}t|dk(s|j|d|j|;|r.t j||j|j|<d|_ y) NrDrzThe model vocabulary size is z., but the following tokens were being biased: rrsr<T) rFrappendr/r3rzerosrar:rrr@r) rrvocabulary_sizeinvalid_biasesrtoken_idsingle_token_idssingle_token_biasesrs rrz3SequenceBiasLogitsProcessor._prepare_bias_variabless5 ,,r* .. 4L( 4."))(3 4 4 ~  "//@@n!"$ #[[/);5;;W]WdWde "&"4"4":":"< 1 L$< A% '' Q8#**40 1 38<<@S\b\i\i3jD  / 0'+$rcH|j}t|tst|trt |dk(rt d|dt|tr!t d|Drt d|dt|tr!t d|Drt d|ddt|tr#t fd |Drt d |dt|tr0t d |jDrt d |dyy) NrzT`sequence_bias` has to be a non-empty dictionary, or non-empty list of lists but is .c3>K|]}t|t ywr')r=rr(rs rr+zBSequenceBiasLogitsProcessor._validate_arguments..s3 4@ </ /3 z=`sequence_bias` has to be a dict with tuples as keys, but is c3`K|]&}td|Dxst|dk((yw)c3lK|],}t|ttjf xs|dk.ywrNr=r>npintegerr(r%s rr+zLSequenceBiasLogitsProcessor._validate_arguments...0kRZZ3 *;<<L1 Lk24rN)anyr/r+s rr+zBSequenceBiasLogitsProcessor._validate_arguments..s=3  k^jk k &< A% &3 s,.zUEach key in `sequence_bias` has to be a non-empty tuple of positive integers, but is ct|dtxr*td|dDxrt|dtS)Nrc3jK|]+}t|ttjfxr|dkD-ywr/r0r3s rr+zjSequenceBiasLogitsProcessor._validate_arguments..all_token_bias_pairs_are_valid..s-mU] 8c2::->?PHqLPms13rs)r=r1r0ra)sequences rall_token_bias_pairs_are_validzWSequenceBiasLogitsProcessor._validate_arguments..all_token_bias_pairs_are_validsB8A;-3maijkalmm3x{E2 rc3PK|]}| xst|dk(ywr/)r/)r(r9r:s rr+zBSequenceBiasLogitsProcessor._validate_arguments..s03 U]/9 9 Pc(mq>P P3 s#&zkEach element in `sequence_bias` has to be a non-empty list of lists of positive integers and float, but is c3>K|]}t|t ywr')r=ra)r(rs rr+zBSequenceBiasLogitsProcessor._validate_arguments..s2rSWz$7N3N2rr,z?`sequence_bias` has to be a dict with floats as values, but is )rr=dictr1r/r3r6values)rrr:s @rrz/SequenceBiasLogitsProcessor._validate_argumentssL** -.z-QU7VZ]^kZlpqZqfgtfuuvw  mT *s3 DQ3 0 \]j\kklmn n mT *s3 !.3 0 g /$    mT *s3 an3 0 } /$  mT *s2r[h[o[o[q2r/r^_l^mmnop p0s *rct|jtr2|j}|Dcic]}t|d|dc}|_yycc}w)zSBC: we used to accept `dict{tuple of tokens: float}` directly, now we expect a listrrsN)r=rr1r)r temp_sequencesublists rrz=SequenceBiasLogitsProcessor._convert_list_arguments_into_dictsJ d(($ / ..MO\!]G% "3WQZ"?!]D  0!]sAN)rrrrr1rr>rarAr rrr r!rrrrr"rrrrIs>@-d4d3i6F0G+H&I-;< %"2"2 E In order to get the token ids of the words that should not appear in the generated text, make sure to set `add_prefix_space=True` when initializing the tokenizer, and use `tokenizer(bad_words, add_special_tokens=False).input_ids`. The `add_prefix_space` argument is only supported for some slow tokenizers, as fast tokenizers' prefixing behaviours come from `pre tokenizers`. Read more [here](https://huggingface.co/docs/tokenizers/api/pre-tokenizers). Args: bad_words_ids (`list[list[int]]`): List of list of token ids that are not allowed to be generated. eos_token_id (`Union[int, list[int], torch.Tensor]`, *optional*): The id(s) of the *end-of-sequence* token. Examples: ```python >>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> inputs = tokenizer(["In a word, the cake is a"], return_tensors="pt") >>> output_ids = model.generate(inputs["input_ids"], max_new_tokens=5, pad_token_id=tokenizer.eos_token_id) >>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]) In a word, the cake is a bit of a mess. >>> # Now let's take the bad words out. Please note that the tokenizer is initialized differently >>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("openai-community/gpt2", add_prefix_space=True) >>> def get_tokens_as_list(word_list): ... "Converts a sequence of words into a list of tokens" ... tokens_list = [] ... for word in word_list: ... tokenized_word = tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0] ... tokens_list.append(tokenized_word) ... return tokens_list >>> bad_words_ids = get_tokens_as_list(word_list=["mess"]) >>> output_ids = model.generate( ... inputs["input_ids"], max_new_tokens=5, bad_words_ids=bad_words_ids, pad_token_id=tokenizer.eos_token_id ... ) >>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]) In a word, the cake is a bit of a surprise. ``` bad_words_idsr9c|||_|j|jt|tjs(t|t r|g}tj |}|jttfd|}|Dcic]}t|td}}t|5|ycc}w)Nc.tfdDS)Nc3*K|] }|gk7 ywr'r")r(r bad_token_seqs rr+zGNoBadWordsLogitsProcessor.__init__....Fs0a!1#1E0as)r0)rHeos_token_id_lists`rz4NoBadWordsLogitsProcessor.__init__..FsS0aO`0a-arz-inf)r) bad_word_idsrr=rr?r>r@rr1filterrrasuperrA)rrDr9r9rrIrs @rrAz"NoBadWordsLogitsProcessor.__init__7s*   "  #lELL9lC0$0>L$||L9 , 3 3 5  acpqMIVVHx%-7V V }5Ws B9c|j}t|trt|dk(rt d|dt d|Drt d|dt d|Drt d|dy)Nrz3`bad_words_ids` has to be a non-empty list, but is r)c3>K|]}t|t ywr')r=r1r(rKs rr+z@NoBadWordsLogitsProcessor._validate_arguments..PsTl:lD11Tr,z2`bad_words_ids` has to be a list of lists, but is c3@K|]}td|Dyw)c3lK|],}t|ttjf xs|dk.ywr/r0r3s rr+zJNoBadWordsLogitsProcessor._validate_arguments...Sr4r5N)r6rPs rr+z@NoBadWordsLogitsProcessor._validate_arguments..Rs$  k^jk k szKEach list in `bad_words_ids` has to be a list of positive integers, but is )rKr=r1r/r3r6)rrDs rrz-NoBadWordsLogitsProcessor._validate_argumentsLs)) -.#m2D2IRS`Raabcd d TmT TQR_Q``abc c  -  ]^k]llmn   rr') rrrrr1r>rrrr?rAr __classcell__)rs@rrCrCsQ4nmq6!$s)_6>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m") >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m") >>> inputs = tokenizer("Alice and Bob", return_tensors="pt") >>> # By default, it continues generating according to the model's logits >>> outputs = model.generate(**inputs, max_new_tokens=5) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) Alice and Bob are friends >>> # We can constrain it with `prefix_allowed_tokens_fn` to force a certain behavior based on a prefix. >>> # For instance, we can force an entire entity to be generated when its beginning is detected. >>> entity = tokenizer(" Bob Marley", return_tensors="pt").input_ids[0] # 3 tokens >>> def prefix_allowed_tokens_fn(batch_id, input_ids): ... ''' ... Attempts to generate 'Bob Marley' when 'Bob' is detected. ... In this case, `batch_id` is not used, but you can set rules for each batch member. ... ''' ... if input_ids[-1] == entity[0]: ... return [entity[1].item()] ... elif input_ids[-2] == entity[0] and input_ids[-1] == entity[1]: ... return [entity[2].item()] ... return list(range(tokenizer.vocab_size)) # If no match, allow all tokens >>> outputs = model.generate(**inputs, max_new_tokens=5, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]) Alice and Bob Marley ``` prefix_allowed_tokens_fnr c ||_||_yr')_prefix_allowed_tokens_fn _num_beams)rrVr s rrAz)PrefixConstrainedLogitsProcessor.__init__s)A&#rrrrctj|tj }|jd|j z}t |D]w}t |j D]]}|||j z|z}|j||}t|dk(rtd|dd|||j z|z|f<_y||z} | S)Nrz?`prefix_allowed_tokens_fn` returned an empty list for batch ID zp.This means that the constraint is unsatisfiable. Please check your implementationof `prefix_allowed_tokens_fn` ) r full_likerIrJrFrYrrXr/r3) rrrmaskrbatch_idbeam_idsentprefix_allowed_tokensrMs rrz)PrefixConstrainedLogitsProcessor.__call__svy1__Q'4??: j) VH 1 V DOO!;g!EF(,(F(FxQU(V%,-2$YZbYcd9: UVX/'9;PPQ V V"D=rN)rrrrrr>rr?r1rAr rr r!rr"rrrUrU[sv,\$3 :MtTWy:X1Y$fi$;< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> import torch >>> # Initialize the model and tokenizer >>> tokenizer = AutoTokenizer.from_pretrained("google-t5/t5-base") >>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base") >>> # A long text about the solar system >>> text = ( ... "The Solar System is a gravitationally bound system comprising the Sun and the objects that orbit it, " ... "either directly or indirectly. Of the objects that orbit the Sun directly, the largest are the eight " ... "planets, with the remainder being smaller objects, such as the five dwarf planets and small Solar System " ... "bodies. The Solar System formed 4.6 billion years ago from the gravitational collapse of a giant " ... "interstellar molecular cloud." ... ) >>> inputs = tokenizer("summarize: " + text, return_tensors="pt") >>> # Generate diverse summary >>> outputs_diverse = model.generate( ... **inputs, ... num_beam_groups=2, ... diversity_penalty=10.0, ... max_length=100, ... num_beams=4, ... num_return_sequences=2, ... ) >>> summaries_diverse = tokenizer.batch_decode(outputs_diverse, skip_special_tokens=True) >>> # Generate non-diverse summary >>> outputs_non_diverse = model.generate( ... **inputs, ... max_length=100, ... num_beams=4, ... num_return_sequences=2, ... ) >>> summary_non_diverse = tokenizer.batch_decode(outputs_non_diverse, skip_special_tokens=True) >>> # With `diversity_penalty`, the resulting beams are much more diverse >>> print(summary_non_diverse) ['the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.', 'the Solar System formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.'] >>> print(summaries_diverse) ['the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.', 'the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets. the rest of the objects are smaller objects, such as the five dwarf planets and small solar system bodies.'] ``` diversity_penaltyr num_beam_groupsc>tjdt|tr|dkDs t d||_t|t r|dkr t d||_t|t r|dkr t d||kDr t d||z|_y)Nz`HammingDiversityLogitsProcessor` 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.r`z=`diversity_penalty` should be a float strictly larger than 0.rz8`num_beams` should be an integer strictly larger than 1.z>`num_beam_groups` should be an integer strictly larger than 1.z8`beam_groups` has to be smaller or equal to `num_beams`.) logger warning_oncer=rar3_diversity_penaltyr>rY_num_sub_beams)rrcr rds rrAz(HammingDiversityLogitsProcessor.__init__s R +U3+>>Od.A.AA4??S "_4 \\"% a M!<<>z* I$2DOO+i$//.IO.[% !$nn-BjY\\]c]j]jkO Y3y1} 6R S''/9  S   rNrr"rrrbrbsp<|;%;C;RU; - ##- !!- (( -  -    - rrbceZdZdZdefdZeedejdejdejfdZ y) ForcedBOSTokenLogitsProcessora [`LogitsProcessor`] that enforces the specified token as the first generated token. Used with encoder-decoder models. Args: bos_token_id (`int`): The id of the token to force as the first generated token. Examples: ```python >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small") >>> tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small") >>> inputs = tokenizer("Translate from English to German: I love cats.", return_tensors="pt") >>> # By default, it continues generating according to the model's logits >>> outputs = model.generate(**inputs, max_new_tokens=10) >>> print(tokenizer.batch_decode(outputs)[0]) Ich liebe Kitty. >>> # We can use `forced_bos_token_id` to force the start of generation with an encoder-decoder model >>> # (including forcing it to end straight away with an EOS token) >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_bos_token_id=tokenizer.eos_token_id) >>> print(tokenizer.batch_decode(outputs)[0]) ``` bos_token_idc||_yr')rx)rrxs rrAz&ForcedBOSTokenLogitsProcessor.__init__Bs (rrrrc|jd}|}|dk(r8tj|tj }d|dd|j f<|SNrDrsr)rFrr[rIrJrxrrrrrMs rrz&ForcedBOSTokenLogitsProcessor.__call__EsP//"%! a<$vyA 56 Q 1 11 2rNrr"rrrwrw"sU>)S);< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: 1, 2, 3", return_tensors="pt") >>> # By default, it continues generating according to the model's logits >>> outputs = model.generate(**inputs, max_new_tokens=10) >>> print(tokenizer.batch_decode(outputs)[0]) A sequence: 1, 2, 3, 4, 5, 6, 7, 8 >>> # `forced_eos_token_id` ensures the generation ends with a EOS token >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_eos_token_id=tokenizer.eos_token_id) >>> print(tokenizer.batch_decode(outputs)[0]) A sequence: 1, 2, 3, 4, 5, 6, 7,<|endoftext|> ``` max_lengthr9r:c||_t|tjs*t|tr|g}tj ||}||_tj|s|dkjrtd|y)Nr<r=`eos_token_id` has to be a list of positive integers, but is ) rr=rr?r>r@r9is_floating_pointr6r3)rrr9r:s rrAz&ForcedEOSTokenLogitsProcessor.__init__qsy$, 5,, ,~  << VDL(  " "< 0\A5E4J4J4L\]i\jkl l5Mrrrrc|jd}|}||jdz k(r8tj|tj }d|dd|j f<|Sr{)rFrrr[rIrJr9r|s rrz&ForcedEOSTokenLogitsProcessor.__call__}sZ//"%! doo) )$vyA 56 Q 1 11 2rNrNrPr"rrr~r~Os}B m3 meCcELL FLL@Y@]@]_oprNrr"rrrrsI;< %"2"2 E>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> text = "Just wanted to let you know, I" >>> inputs = tokenizer(text, return_tensors="pt") >>> # Let's consider that we want short sentences, so we limit `max_length=30`. However, we observe that the answer >>> # tends to end abruptly. >>> set_seed(1) >>> outputs = model.generate(**inputs, do_sample=True, temperature=0.9, max_length=30, pad_token_id=50256) >>> print(tokenizer.batch_decode(outputs)[0]) Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network which was published in 2010. Although >>> # To promote the appearance of the EOS token at the right time, we add the `exponential_decay_length_penalty = >>> # (start_index, decay_factor)`. Instead of cutting at max_tokens, the output comes to an end before and usually >>> # with more meaning. What happens is that starting from `start_index` the EOS token score will be increased >>> # by `decay_factor` exponentially. However, if you set a high decay factor, you may also end up with abruptly >>> # ending sequences. >>> set_seed(1) >>> outputs = model.generate( ... **inputs, ... do_sample=True, ... temperature=0.9, ... max_length=30, ... pad_token_id=50256, ... exponential_decay_length_penalty=(15, 1.6), ... ) >>> print(tokenizer.batch_decode(outputs)[0]) Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network which<|endoftext|> >>> # With a small decay factor, you will have a higher chance of getting a meaningful sequence. >>> set_seed(1) >>> outputs = model.generate( ... **inputs, ... do_sample=True, ... temperature=0.9, ... max_length=30, ... pad_token_id=50256, ... exponential_decay_length_penalty=(15, 1.01), ... ) >>> print(tokenizer.batch_decode(outputs)[0]) Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network which was published in 2010.<|endoftext|> ``` exponential_decay_length_penaltyr9input_ids_seq_lengthc0|d|z|_|d|_t|tjs(t|t r|g}tj |}||_tj|s|dkjrtd|y)Nrrsr) regulation_startregulation_factorr=rr?r>r@r9rr6r3)rrr9rs rrAz&ExponentialDecayLengthPenalty.__init__s !A CFZ Z!A!!D, 5,, ,~  << 5L(  " "< 0\A5E4J4J4L\]i\jkl l5Mrrrrc|jd}|jj|j|_t j |}|}||j kDrg||j z }t j|dd|jft|j|dz z}||dd|jf<||z}|S)NrDrs) rFr9ror:rrurrpowr)rrrr penaltiesrM penalty_idxrgs rrz&ExponentialDecayLengthPenalty.__call__s//"% --00?$$V, ! T** *!D$9$99Kiiq$*;*;'; <=TE[E[]hAilmAmnG.5Ia*** +% 1 rN)rrrrrr>rarr1rr?rAr rr r!rr"rrrrsAFm*/U *;mCcELL89m" m$;<  %"2"2  E>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> import torch >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") >>> inputs = tokenizer("A sequence: 1, 2, 3", return_tensors="pt") >>> # By default, the scores are not normalized -- the sum of their exponentials is NOT a normalized probability >>> # distribution, summing to 1 >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True) >>> print(torch.allclose(torch.sum(torch.exp(outputs.scores[-1])), torch.Tensor((1.000,)), rtol=1e-4)) False >>> # Normalizing them may have a positive impact on beam methods, or when using the scores on your application >>> outputs = model.generate(**inputs, renormalize_logits=True, return_dict_in_generate=True, output_scores=True) >>> print(torch.allclose(torch.sum(torch.exp(outputs.scores[-1])), torch.Tensor((1.000,)), rtol=1e-4)) True ``` rrrc*|jd}|S)NrDr)rrds rrzLogitNormalization.__call__ s!--"-5rNrr"rrrrsI<;< %"2"2 E>> from transformers import AutoProcessor, WhisperForConditionalGeneration >>> from datasets import load_dataset >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt") >>> # Whisper has `begin_suppress_tokens` set by default (= `[220, 50256]`). 50256 is the EOS token, so this means >>> # it can't generate and EOS token in the first iteration, but it can in the others. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True) >>> print(outputs.scores[0][0, 50256]) tensor(-inf) >>> print(outputs.scores[-1][0, 50256]) # in other places we can see some probability mass for EOS tensor(29.9010) >>> # If we disable `begin_suppress_tokens`, we can generate EOS in the first iteration. >>> outputs = model.generate( ... **inputs, return_dict_in_generate=True, output_scores=True, begin_suppress_tokens=None ... ) >>> print(outputs.scores[0][0, 50256]) tensor(11.2027) ``` r:c\tjt|||_||_yNr<)rr@r1begin_suppress_tokens begin_index)rrrr:s rrAz-SuppressTokensAtBeginLogitsProcessor.__init__Is$%*\\$7L2MV\%]"&rc||_yr'rrrs rset_begin_indexz4SuppressTokensAtBeginLogitsProcessor.set_begin_indexM &rrrrc tj|jd|j}t ||j }|}|jd|j k(r!tj|td |}|SNrDr<rJ) rrErFr:r rrrHra)rrrrKsuppress_token_maskrMs rrz-SuppressTokensAtBeginLogitsProcessor.__call__Pso||FLL$4V]]K / d>X>XY! ??2 $"2"2 2${{+>u vV rNrN) rrrrrQrArr rrr r!rr"rrrr&s[ D'3'';< %"2"2 E>> from transformers import AutoProcessor, WhisperForConditionalGeneration >>> from datasets import load_dataset >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt") >>> # Whisper has a long list of suppressed tokens. For instance, in this case, the token 1 is suppressed by default. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True) >>> print(outputs.scores[1][0, 1]) # 1 (and not 0) is the first freely generated token tensor(-inf) >>> # If we disable `suppress_tokens`, we can generate it. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True, suppress_tokens=None) >>> print(outputs.scores[1][0, 1]) tensor(6.0678) ``` r:cNtjt|||_yr)rr@r1suppress_tokens)rrr:s rrAz&SuppressTokensLogitsProcessor.__init__xs$||D,A&Qrrrrctj|jd|j}t ||j }tj |td |}|Sr)rrErFr:r rrHra)rrrrKrs rrz&SuppressTokensLogitsProcessor.__call__{sP||FLL$4V]]K / d>R>RS05<-H rNrN) rrrrrQrAr rrr r!rr"rrrr[sW8RR;<%"2"2E"` token. max_initial_timestamp_index (`int`, *optional*, defaults to 1): Used to set the maximum value of the initial timestamp. This is used to prevent the model from predicting timestamps that are too far in the future. begin_index (`int`): Token index of the first token that is generated by the model. _detect_timestamp_from_logprob (`bool`, *optional*): Whether timestamps can be predicted from logprobs over all timestamps. Examples: ``` python >>> import torch >>> from transformers import AutoProcessor, WhisperForConditionalGeneration, GenerationConfig >>> from datasets import load_dataset >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = processor(ds[3]["audio"]["array"], return_tensors="pt") >>> input_features = inputs.input_features >>> #Displaying timestamps >>> generated_ids = model.generate(inputs=input_features, return_timestamps=True) >>> transcription = processor.batch_decode(generated_ids, decode_with_timestamps=True)[0] >>> print("Transcription:", transcription) Transcription: <|startoftranscript|><|0.00|> He has grave doubts whether Sir Frederick Layton's work is really Greek after all, and can<|6.44|><|6.44|> discover in it but little of rocky Ithaca.<|9.44|><|endoftext|> >>> #No timestamps & change EOS: >>> #This allows the user to select a specific token to terminate the sequence on, in this case it's the word "can"(460) >>> model.generation_config.eos_token_id = 460 >>> generated_ids = model.generate(inputs=input_features,return_timestamps=False) >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] >>> print("Transcription:", transcription) Transcription: He has grave doubts whether Sir Frederick Layton's work is really Greek after all and can ``` Ngenerate_configr r_detect_timestamp_from_logprobc|j|_|jdz|_|jxs |j|_||n t |dd|_||_| tdt |dd|_y)NrsrTz`forced_decoder_ids` is deprecated in favor of `task` and `language` and, as such, `begin_index` must be provided to `WhisperTimeStampLogitsProcessor`. The previous default value of `begin_index` was `len(generate_config.forced_decoder_ids)`max_initial_timestamp_index) no_timestamps_token_idtimestamp_beginr9rxgetattrrrr3r)rrrrs rrAz(WhisperTimeStampLogitsProcessor.__init__s '6&L&L#.EEI+88XOR>R3R (+C1 (WB4CWCW8W %!,CH<-$Q(<(<(>%>?@Ee }$Q(;$*;*;(;%;<'(9(9$:N:N(OPJ!A%&.G%/^N&0^a%7NNSTYl] D$8$8>$I!IJ/ [4 ??1 !1!1 1;@<- Q 6$"6"6 66 7//;#33d6V6VV ;@<- L1$4$6!6788&&223C3I3I3KQS2Tyq)* LA (D,@,@,B)B C M MRT M U %-a1G43G3G1G.G%H%L%L%N " #99d>a>a?DU|m $:d&:&:$:!:;  L  rr')rrrrr>rrvrArr rrr r!rr"rrrrs}7z:> i+ii)1 i8';</ %"2"2/ E'TZZ6$++6==F>#'"2"2T5O5O"O#)!_*<#= %*"#) !(,,.(..088R8@#(D,@,@)@#AD  !>>s 'C//C8N)F)rrrrr>rvrArrpropertyrrr rrr r!rr"rrrrs]  # SW /$$';<%"2"2E This logits processor is exclusively compatible with [MusicGen](https://huggingface.co/docs/transformers/main/en/model_doc/musicgen) Args: guidance_scale (float): The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages the model to generate samples that are more closely linked to the input prompt, usually at the expense of poorer quality. Examples: ```python >>> from transformers import AutoProcessor, MusicgenForConditionalGeneration >>> processor = AutoProcessor.from_pretrained("facebook/musicgen-small") >>> model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small") >>> inputs = processor( ... text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"], ... padding=True, ... return_tensors="pt", ... ) >>> audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256) ``` c:|dkDr||_ytd|d)Nrs\Require guidance scale >1 to use the classifier free guidance processor, got guidance scale r))guidance_scaler3)rrs rrAz.ClassifierFreeGuidanceLogitsProcessor.__init__ss/ A "0D n!"!% rrrrc|jdd|jdzk7r,td|jdd|jdd|jddz}|j|d\}}|||z |jzz}|S)NrrLogits should have twice the batch size of the input ids, the first half of batches corresponding to the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got batch size  for the logits and  for the input ids.r)rFr3splitr)rrr unguided_bsz cond_logits uncond_logitsrMs rrz.ClassifierFreeGuidanceLogitsProcessor.__call__|s <<?a)//!"44 4$ll1o..B9??STCUBVVik  ||A!+ %+\\,A\%F" ](K-,G4K^K^+^^rN) rrrrrAr rrr r!rr"rrrrLsO$L;<  %"2"2  E This logits processor is exclusively compatible with [Bark](https://huggingface.co/docs/transformers/en/model_doc/bark)'s fine submodel. See the model documentation for examples. Args: input_start_len (`int`): The length of the initial input sequence. semantic_vocab_size (`int`): Vocabulary size of the semantic part, i.e number of tokens associated to the semantic vocabulary. codebook_size (`int`): Number of tokens associated to the codebook. input_start_lensemantic_vocab_size codebook_sizectt|tr|dkrtd|||_||_||_y)NrzA`input_starting_length` has to be a non-negative integer, but is )r=r>r3rrr)rrrrs rrAz,AlternatingCodebooksLogitsProcessor.__init__s@/3/?Q3F`ap`qrs s.#6 *rrrrcd|jd}||jz dzdk(}|j}|rMtd |ddd|jf<td |dd|j|j zdf<|Std |ddd|j|j zf<|S)NrDrrrJ)rFrrGrarr)rrrcurr_lenis_first_codebookrMs rrz,AlternatingCodebooksLogitsProcessor.__call__s??2&')=)==BqH!<<> ?DU|m Q :$":": :: ;TYZ_T`S` Q 8 84;M;M M OO P UZZ_T`S` Q O$":":T=O=O"O OO PrN) rrrrr>rArr r!rr"rrrrsO(++#+VY+  %"2"2  E>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> inputs = tokenizer(["Today, a dragon flew over Paris, France,"], return_tensors="pt") >>> out = model.generate(inputs["input_ids"], guidance_scale=1.5) >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0] 'Today, a dragon flew over Paris, France, killing at least 50 people and injuring more than 100' >>> # with a negative prompt >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt") >>> out = model.generate(inputs["input_ids"], guidance_scale=2, negative_prompt_ids=neg_inputs["input_ids"]) >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0] 'Today, a dragon flew over Paris, France, killing at least 130 people. French media reported that' >>> # with a positive prompt >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt") >>> out = model.generate(inputs["input_ids"], guidance_scale=0, negative_prompt_ids=neg_inputs["input_ids"]) >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0] "Today, a dragon flew over Paris, France, and I'm very happy to be here. I" ``` Nrunconditional_idsunconditional_attention_mask use_cachec:||_||_|||ddd|_y)NT)rattention_maskrpast_key_values first_pass)rrunconditional_context)rrrrrrs rrAz7UnbatchedClassifierFreeGuidanceLogitsProcessor.__init__s,- *:"# & "rc |jdr|jd|ddddf|jd<|jd?tj|jdtj|jd<|jd}|jd}d|jd<ntj|jdtj|ddddftjgd}|jd s0tj|jd|ddddfgd}n |ddddf}||jd<||jd<|j |||jd |jd  }|j d d|jd <|jS) NrrrDrrqFrsrrr)rrr)rr ones_likelongcatrrr)rrrouts rget_unconditional_logitszGUnbatchedClassifierFreeGuidanceLogitsProcessor.get_unconditional_logitss  % %l 3))+6>:CArsF:K**;7))*:;C?D..{;5::@**+;<22;?I!778HIN7>vae}RT>U..&;O2OPSggr)NNT) rrrrrarrr rvrArrr"rrrrs_1n9=CG   $E$4$45  '/u/?/?&@    $"H  rrceZdZdZd deeeeejfde de fdZ e e dejdejdejfd Zy ) !BarkEosPrioritizerLogitsProcessoraThis processor ensures that the EOS token is selected if its probability is greater than the `min_eos_p`. This logits processor is exclusively compatible with [Bark](https://huggingface.co/docs/transformers/en/model_doc/bark). See the model documentation for examples. Args: eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. min_eos_p (`float`, *optional*): Minimum end of speech threshold. r9 min_eos_pr:c>t|tjs*t|tr|g}tj||}||_tj |s|dkjrtd|||dkrtd|||_ y)Nr<rrz/`min_eos_p` has to be a positive float, but is ) r=rr?r>r@r9rr6r3r)rr9rr:s rrAz*BarkEosPrioritizerLogitsProcessor.__init__@ s, 5,, ,~  << VDL(  " "< 0\A5E4J4J4L\]i\jkl l  Y!^NykZ[ ["rrrrc|}|jrtjjj |j d}tj |t d z}|dd|jf|dd|jf<|dd|jf|jkD}tj|dd}tj|||}|S)NrDrrJrsTr) rrrrrrarr9r6rH)rrrrMrearly_stop_scores do_early_stops rrz*BarkEosPrioritizerLogitsProcessor.__call__N s! >>HH''// B/GE % 75<- G 6r1rr?rarQrAr rr r!rr"rrrr/ sy #U3S 5<<+G%H #UZ #dg #;<  %"2"2  E>> from transformers import AutoTokenizer, AutoModelForCausalLM, WatermarkingConfig >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> inputs = tokenizer(["Alice and Bob are"], return_tensors="pt") >>> # normal generation >>> out = model.generate(inputs["input_ids"], max_length=20, do_sample=False) >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0] 'Alice and Bob are both in the same room.\n\n"I\'m not sure if you\'re' >>> # watermarked generation >>> watermarking_config = WatermarkingConfig(bias=2.5, context_width=2, seeding_scheme="selfhash") >>> out = model.generate(inputs["input_ids"], watermarking_config=watermarking_config, max_length=20, do_sample=False) >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0] 'Alice and Bob are both still alive and well and the story is pretty much a one-hour adventure' >>> # to detect watermarked text use the WatermarkDetector class >>> from transformers import WatermarkDetector >>> detector = WatermarkDetector(model_config=model.config, device="cpu", watermarking_config= watermarking_config) >>> detection_preds = detector(out) >>> detection_preds array([ True]) ``` greenlist_ratior hashing_keyseeding_scheme context_widthc|dvrtd||dk\s|dkrtd|||_t|j|z|_||_||_t j||_||_ ||_ |jj|d|_ t j|j|j||_y) N)selfhashlefthashzDseeding_scheme has to be one of [`selfhash`, `lefthash`], but found rr`zLgreenlist_ratio has be in range between 0.0 and 1.0, exclusively. but found r<iCB) generatorr:)r3rr>greenlist_sizerrr Generatorrnghash_keyr manual_seed table_sizerandperm fixed_table)rrr:rrrrrs rrAz!WatermarkLogitsProcessor.__init__ s !9 9cdrcstu u c !_%;^_n^op %!$//O"CD ,??&1# * [)# >>$//TXXV\]r input_seqc||j d}|jdk(rp|j||jzdz}|j|d|jzdz}|j|z|zj j }n |j|dj z}|jj|dzy)NrrsrDl) rrrrrritemrr)rrabseeds rset_seedz!WatermarkLogitsProcessor.set_seed st11134   * ,  T__!<=AA  2!@AAEAMMA%)..0557D==9R=#5#5#77D TY/0rrc|j|tj|j|j|j }|d|j }|S)N)r:r)r$rrrr:rr)rrvocab_permutation greenlist_idss r_get_greenlist_idsz+WatermarkLogitsProcessor._get_greenlist_ids sK i !NN4??9CSCS_c_g_gh)*?D,?,?@ rrc &g}|jdd\}}tdD]K}|jtj|||dfgd}|||vs8|j ||Mtj ||jS)z Generate greenlist based on current candidate next token. Reject and move on if necessary. Runs for a fixed number of steps only for efficiency, since the methods is not batched. rDT)rtr(Nrr<)rrr(rrr!r@r:)rrrfinal_greenlistrgreedy_predictionsrr's r_score_rejection_samplingz2WatermarkLogitsProcessor._score_rejection_sampling s  & t D r >A 33EIIyJ\]^`d]dJe>fln4opM!!$ 5&&'9!'<= >||OI4D4DEErrc|jd|jkr5tjd|jd|jdd|S|j }t |D]T\}}|j dk(r|j|||}n|j|}|||f|jz|||f<V|S)NrDz"`input_ids` should have at least `z` tokens but has z7. The seeding will be skipped for this generation step!r) rFrrfwarningrGrrr-r(r)rrrrMb_idxrr's rrz!WatermarkLogitsProcessor.__call__ s ??2 !3!3 3 NN4T5G5G4HHYZcZiZijlZmYnoHH M!<<> )) 4 h E9""j0 $ > >y&QV- X $ 7 7 B 5Ee]FZ5[^b^g^g5g UM1 2  h rN)g?g@iKrrs)rrrrrar>rQrArr r$r(r!r-r rrr"rrr r ^ s;B"&#(^ ^  ^  ^^^:1%"2"21E,<,<AQAQ F53C3C FUM^M^ Fchcscs F;< %"2"2 Err:rAr"rrr2r2 s4%"   rr2c8eZdZdZ d(dedeededededejded efd Z d efd Z d ejdejdejfdZ e edejd ejdejfdZ d)dejdejdededejf dZdejdejfdZdejdejdeejejffdZdejdejfdZdejfdZdejdejfd Zdejdejfd!Zdejd"edejfd#Zd*d$ed%edefd&Zy')+#SynthIDTextWatermarkLogitsProcessora Logits processor that implements watermarking techniques for text generation models. This class facilitates the application of SynthID text watermarking, a method for embedding imperceptible signals into generated text to aid in detecting synthetic content. It operates by subtly manipulating the probabilities of token selection during text generation in a manner that can be reliably recovered later for verification. Key Features: * **State Management:** Maintains internal state to track token sequences and generate watermarking keys dynamically. * **Key Generation:** Computes hashes based on token sequences and watermarking parameters to create unique keys for each position. * **G-Value Sampling:** Employs a pre-computed sampling table to sample watermarking values (g-values) based on the generated keys. * **Score Adjustment:** Applies calculated g-values to modify token probabilities during generation, embedding the watermark. * **Context Repetition Handling:** Incorporates logic to avoid watermarking tokens in repeated contexts, preserving naturalness. * **EOS Token Masking:** Supports masking end-of-sentence tokens to prevent their inclusion in watermarking calculations. * **Utility Functions:** Provides functions to compute g-values directly, check for context repetition, create EOS token masks, and estimate expected mean g-values. Refer to paper url: https://www.nature.com/articles/s41586-024-08025-4 for more details around this. Args: ngram_len (`int`): Ngram length. keys (`list[int]`): A sequence of watermarking keys, one for each depth. sampling_table_size (`int`): Size of the sampling table. sampling_table_seed (`int`): Random seed to generate the sampling table. context_history_size (`int`): Size of the tensor to keep track of seen contexts. device (`torch.device`): Device to use. skip_first_ngram_calls (`bool`, *optional*, defaults to `False`): Whether to skip first ngram calls. debug_mode (`bool`, optional, *optional*, defaults to `False`): Logits are modified to uniform one got before watermarking modification is applied. This is to test the implementation. Examples: ```python >>> from transformers import AutoModelForCausalLM, AutoTokenizer, SynthIDTextWatermarkingConfig >>> tokenizer = AutoTokenizer.from_pretrained('google/gemma-2-2b', padding_side="left") >>> model = AutoModelForCausalLM.from_pretrained('google/gemma-2-2b') >>> # SynthID Text configuration >>> watermarking_config = SynthIDTextWatermarkingConfig( ... keys=[654, 400, 836, 123, 340, 443, 597, 160, 57], ... ngram_len=5, ... ) >>> # Generation with watermarking >>> tokenized_prompts = tokenizer(["Once upon a time, "], return_tensors="pt", padding=True) >>> output_sequences = model.generate( ... **tokenized_prompts, watermarking_config=watermarking_config, do_sample=True, max_new_tokens=10 ... ) >>> watermarked_text = tokenizer.batch_decode(output_sequences, skip_special_tokens=True) ``` r3r2sampling_table_sizesampling_table_seedr4r:skip_first_ngram_calls debug_modec ||_tj|||_tj|j |} tj dd|f| ||_||_||_ d|_ ||_ ||_ y)Nr<rr)lowhighrrr:) r3rr@r2rrrandintsampling_tabler4r:stater>r?) rr3r2r<r=r4r:r>r?rs rrAz,SynthIDTextWatermarkLogitsProcessor.__init__S s#LLf5 OO62>>?RS $mm%'  %9!  &<#$rrcht||j|j|j|_y)zInitializes the state.rr3r4r:N)r2r3r4r:rE)rrs r _init_statez/SynthIDTextWatermarkLogitsProcessor._init_statet s(.!nn!%!:!:;;   rrg_valuesrc|j\}}}tj|d}t|D]/}|dddd|f}||zj dd}|d|z|z z}1tj |} tj tj| | tj| jj} | S)aZUpdates scores using the g values. We assume that the scores are in the log space. Args: scores (`torch.FloatTensor`): Scores (batch_size, vocab_size). g_values (`torch.FloatTensor`): G values (batch_size, vocab_size, depth). Returns: Updated scores (batch_size, vocab_size). rsrNT)axiskeepdims) rFrrrrlogrHisfiniterrrr) rrrIrdepthrrg_values_at_depthg_mass_at_depth log_probss r update_scoresz1SynthIDTextWatermarkLogitsProcessor.update_scores} snn 1e f!,u FA (Aq 1 058==1t=TOQ!22_DEE F IIe$ KKy 99ekkR[RaRaFbFfFfg rrc |j||j\}}|jrtj|}tj t |Dcgc]#}tj||j%c}}|j|j|nstj|jj|ddddffd|j_ |jjddddf|j_ |j td|jxjdz c_|jr%|jj|j kr|S|j#|jj|\}}|j%|} |j'|| } |dddf}|jj(|k(j+dd} tj||jj(fdddddf|j_tj,| || } | Scc}w) Nr<rDrsrzJself.state can't be None! Call `self._init_state` to initialize the state.Tr)inputother)_check_input_ids_shaperFr?rrstackrrEr:rErHconcatr7r3r9r>r3 _compute_keyssample_g_valuesrSr8r6rH) rrrrrr all_indices ngram_keyshash_result_with_just_contextrIupdated_scoresis_repeated_contextupdated_watermarked_scoress rrz,SynthIDTextWatermarkLogitsProcessor.__call__ s ##I.!' J ??__V,FkkY^_iYj"kTU5<< 4;;#O"kl ::    Z ( "'##Yq"#v%67"DJJ "&!3!3AqrE!:DJJ  :: ij j !  & &4::+?+?$..+PM594F4FtzzGYGY[f4g1 1'' 3++FH=)Fag(N%#zz99=ZZ__` &+\\ *DJJ,F,F G&  SbS&& " &+[[  & " *)i#ls(I# current_hashdata multiplier incrementct|jdD]I}tj||d|f}tj||}tj||}K|S)aG Accumulate hash of data on current hash. Method uses adapted linear congruential generator with newlib/musl parameters. This function has following property - f(x, data[T]) = f(f(x, data[:T - 1]), data[T]) This function expects current_hash.shape and data.shape[:-1] to match/broadcastable. Args: current_hash (`torch.LongTensor`): (shape,) data (`torch.LongTensor`): (shape, tensor_len) multiplier (`int`, optional, *optional*, defaults to 6364136223846793005): multiplier of linear congruential generator increment (`int`, optional, *optional*, defaults to 1): increment of linear congruential generator Returns: updated hash (shape,) rD.)rrFraddmul)rrbrcrdrers raccumulate_hashz3SynthIDTextWatermarkLogitsProcessor.accumulate_hash s`>tzz"~& >A 99\4Q<@L 99\:>L 99\9=L >rngramsc6t|jdk7rtd|j|jd|jk7r%td|jd|j|j\}}}t j ||j tj}t j|jdd ||}|jd d d d d f}t j|jd d ||}|S) zComputes random keys for each ngram and depth. Args: ngrams (`torch.LongTensor`): Ngrams (batch_size, num_ngrams, ngram_len). Returns: ngram keys (batch_size, num_ngrams, depth). rpzFNgrams should be of shape (batch_size, num_ngrams, ngram_len), but is rzRNgrams should be of shape (batch_size, num_ngrams, ngram_len), where ngram_len is z , but is r:rrNrsrsin_dimsout_dimsNNr) r/rFr3r3ronesr:rvmaprir2)rrjrr hash_resultr2s rcompute_ngram_keysz6SynthIDTextWatermarkLogitsProcessor.compute_ngram_keys s  v||  !eflfrfrestu u <<?dnn ,''+~~&6i ~O "<< AqjjDKKuzzR Vejj!5!5ySTUVacij yytQ,-Vejj!5!5ySTUVacgh rn_minus_1_gramsindicesc|j\}}tj||jtj}|j ||}tj |j dd||dddddf}|jdddddf}tj |j dd||}||fS)ahComputes random keys for each ngram and depth. Args: n_minus_1_grams (`torch.LongTensor`): Ngrams (batch_size, ngram_len - 1). indices (`torch.LongTensor`): indices of the continuations (batch_size, num_indices) Returns: Ngram keys (batch_size, num_indices, depth). rlrmrsrnNrqr)rFrrrr:rrirsr2)rrvrwrrrtr^r2s rrZz1SynthIDTextWatermarkLogitsProcessor._compute_keys s(-- AjjDKKuzzR )-(<(<[/(Z%Vejj!5!5ySTU )71a:+> yytQ,-Uejj!5!5ySTUVacgh 999rr]c|jj\}|jjdd|f}||z}tj||dS)a Samples g values from Bernoulli distribution. It is not possible to pass random keys in a vectorized way in torch. Instead we pre-compute a random sampling table, and use apply modulo table size to map from ngram keys (int64) to g values. Args: ngram_keys (`torch.LongTensor`): Random keys (batch_size, num_ngrams, depth). Returns: G values (batch_size, num_ngrams, depth). rsr)rwrt)rDrFreshapertake_along_dim)rr]r<rDs rr[z3SynthIDTextWatermarkLogitsProcessor.sample_g_valuesB sW"&!4!4!:!: ,,44aFZ[[[rN)FF)l-*PBrs)g?)rrrrr>r1rr:rvrArHr!rSr rr rrirurrZr[rWrrrrarr"rrr;r; sUE^(- %%3i%! % ! % " % %!%%%B c E$5$5ARARW\WhWh0;<<*%"2"2<*EN>NB&:$//&::?:J:J&: u!1!11 2&:PO%*:*:Ou?O?OO(p0@0@p 0%*:*:0u?O?O0 ,89I9I,8eN^N^,8\00@0@0PS0X]XhXh04\\E\TY\rr;ceZdZdZd dedeefdZee de jde jde jfd Z y) (DiaClassifierFreeGuidanceLogitsProcessora [`LogitsProcessor`] for classifier free guidance (CFG). Similar to the original `ClassifierFreeGuidanceLogitsProcessor` with some modifications on the overall calculation, e.g. conditioned logits centered, and an additional top k selection option. This logits processor is exclusively compatible with [Dia](https://huggingface.co/docs/transformers/main/en/model_doc/dia) Args: guidance_scale (float): The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages the model to generate samples that are more closely linked to the input prompt, usually at the expense of poorer quality. guidance_top_k (int, *optional*): The number of highest probability vocabulary tokens to keep for top-k-filtering. However, we do not keep the logits of the combined CFG output, but the conditioned output only. Nrguidance_top_kc|dkDr||_ntd|d||_|j(|jdkrtd|jyy)Nrsrr)zH`guidance_top_k` has to be a strictly positive integer if given, but is )rr3r)rrrs rrAz1DiaClassifierFreeGuidanceLogitsProcessor.__init__ sz A "0D n!"!%  -    *t/B/BQ/FZ[_[n[nZop 0G *rrrrc(|jdd|jdzk7r,td|jdd|jdd|jddz}|j|d\}}|||z |jzz}|jzt j ||jd\}}t j|t j } | jd|d } |j| td  }|S) NrrrrrrrD)rrtrqF)rtrvaluerJ) rFr3rrrrrrrvr|rra) rrrrrrrMr top_k_indices top_k_masks rrz1DiaClassifierFreeGuidanceLogitsProcessor.__call__ s <<?a)//!"44 4$ll1o..B9??STCUBVVik  ||A!+ %+\\,A\%F" ]&+ *EI\I\)\\    *$zz*:d>Q>QWYZ A})9LJ#++-u+UJ*66zE%L=Q rr')rrrrrarr>rAr rrr r!rr"rrrr s`. u hsm ;< %"2"2 E This logits processor is exclusively compatible with [Dia](https://huggingface.co/docs/transformers/en/model_doc/dia). Args: num_channels (`int`): Number of audio codebooks. Simplifies access to the first channel on the logits. eos_token_id (`int`): The id of *end-of-sequence* token. num_channelsr9cp|dkrtd|d|dkrtd|d||_||_y)Nrsz5Audio codebooks need at least one channel, but found z channels.z8Expected `eos_token_id` to be a positive integer, found z instead.)r3reos_id)rrr9s rrAz+DiaEOSChannelFilterLogitsProcessor.__init__ sQ ! TUaTbblmn n ! WXdWeenop p(" rrrrc|jd|j|jd}tj|dddd|j dft d |dddd|j df<tj|ddd|j dzdft d |ddd|j dzdf<|jd|jd}tj|d}||j k(}tj|tj}d||d|j f<|j|t d }||j k7}tj|tj}d|||j f<|j|t d }|S) NrDrsrJ) fill_valuerrrqT) rzrrFrr[rrarargmaxrurvr)rrrtop_logit_indiceseos_highest_maskmask_eos_highesteos_not_highest_maskmask_eos_unless_highests rrz+DiaEOSChannelFilterLogitsProcessor.__call__' sD$5$5v||B7GH (- 1ab$++-' (e }( q!"dkkm#$+0// 1aq** +e }+ q!T[[1_&&'Rb!12"LLR8- ; ++F%**E<@)=T[[=89##$4uU|mD 1DKK?"'"2"26"LEI 4dkk AB##$;eEl]K rNrr"rrrr s\,#S##;<$%"2"2$E This logits processor is exclusively compatible with [Dia](https://huggingface.co/docs/transformers/en/model_doc/dia). Args: delay_pattern (`List[int]`): The delays per channel in the audio codebooks. eos_token_id (`int`): The id of *end-of-sequence* token. max_generation_len (`int`): The max sequence length that can be generated. device (`str`, *optional*, defaults to `"cpu"`): The device to allocate the tensors on. delay_patternr9max_generation_lenr:ct||_d|_tj||tj dddf|_||_|t|z dz |_ ||_ y)Nrlrs) r/ractive_batchesrr@r>rr9rrr:)rrr9rr:s rrAz*DiaEOSDelayPatternLogitsProcessor.__init__t sb .""\\-eiiXY]_`Y`a("4s=7I"IA"M rrrrc|jd|j|jd}|jq|jj |jdd|_t j|jdf|jt j|_t j|ddddf|jk(}|jd|jk(}|xj|zc_|xj|zc_|jdddf|jdk(z}|jd\}}td |||ddf<d ||||jf<|jd|jd}|xj|jdddfjzc_|S) NrDrrs)rr:rrrT)as_tuplerJr`)rzrrFrrrepeatrr"r:rvrr9rrrar>)rrrchannel_generated_eosreached_max_lenforced_eos_channelsidx_bsz idx_channels rrz*DiaEOSDelayPatternLogitsProcessor.__call__} sD$5$5v||B7GH    &!%!3!3!:!:6<<?A!ND "'++FLLO3Edkkafakak"lD !& V P>PTU>UV2::D:I,1<-w Q&':=w T%6%667FLL$45 d11!T':>>@@ rNrN)rrrrr1r>rQrAr rrr r!rr"rrrrO sq"Hd3isX[eh;<!%"2"2!Er?rrrrrrrCrUrbrwr~rrrrrrrrrrrr r2r;rrrr"rrrs  $;; -(&A H  %!  #$#L: : zE E P? o? Dm m `4 o4 n@ @ F2 2 jJ J ZU /U pB /B JQ oQ hC#:,.  %*\\ >A LO  (3- 8 ?8 vB /B Jt^/t^nX ;XvE E P| o| ~* O* Z5 O5 p / *b Ob J" " J2 ?2 j%O%PI oI X::z= O= @* /* Zs _s l, , ^K K \>w\/w\t ? ? DEEPPPr