logits_process.py 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231
  1. # coding=utf-8
  2. # Copyright 2024 The HuggingFace Inc. team and Google DeepMind.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import inspect
  16. import math
  17. from collections.abc import Iterable
  18. from typing import TYPE_CHECKING, Callable, Optional, Union
  19. import numpy as np
  20. import torch
  21. from ..pytorch_utils import isin_mps_friendly
  22. from ..utils import add_start_docstrings
  23. from ..utils.logging import get_logger
  24. # TODO (joao): We shouldn't need this, but there would be a circular import
  25. if TYPE_CHECKING:
  26. from ..generation.configuration_utils import GenerationConfig
  27. logger = get_logger(__name__)
  28. LOGITS_PROCESSOR_INPUTS_DOCSTRING = r"""
  29. Args:
  30. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  31. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  32. scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):
  33. Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam
  34. search or log softmax for each vocabulary token when using beam search
  35. Return:
  36. `torch.FloatTensor` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.
  37. """
  38. class LogitsProcessor:
  39. """Abstract base class for all logit processors that can be applied during generation."""
  40. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  41. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  42. raise NotImplementedError(
  43. f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."
  44. )
  45. class LogitsProcessorList(list):
  46. """
  47. This class can be used to create a list of [`LogitsProcessor`] to subsequently process a `scores` input tensor.
  48. This class inherits from list and adds a specific *__call__* method to apply each [`LogitsProcessor`] to the
  49. inputs.
  50. """
  51. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> torch.FloatTensor:
  52. r"""
  53. Args:
  54. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  55. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  56. scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):
  57. Prediction scores of a language modeling head. These can be logits for each vocabulary when not using
  58. beam search or log softmax for each vocabulary token when using beam search
  59. kwargs (`dict[str, Any]`, *optional*):
  60. Additional kwargs that are specific to a logits processor.
  61. Return:
  62. `torch.FloatTensor` of shape `(batch_size, config.vocab_size)`:
  63. The processed prediction scores.
  64. """
  65. for processor in self:
  66. function_args = inspect.signature(processor.__call__).parameters
  67. if len(function_args) > 2:
  68. if not all(arg in kwargs for arg in list(function_args.keys())[2:]):
  69. raise ValueError(
  70. f"Make sure that all the required parameters: {list(function_args.keys())} for "
  71. f"{processor.__class__} are passed to the logits processor."
  72. )
  73. scores = processor(input_ids, scores, **kwargs)
  74. else:
  75. scores = processor(input_ids, scores)
  76. return scores
  77. class MinLengthLogitsProcessor(LogitsProcessor):
  78. r"""
  79. [`LogitsProcessor`] enforcing a min-length by setting EOS probability to 0. Note that, for decoder-only models
  80. like most LLMs, the length includes the prompt.
  81. Args:
  82. min_length (`int`):
  83. The minimum length below which the score of `eos_token_id` is set to `-float("Inf")`.
  84. eos_token_id (`Union[int, list[int], torch.Tensor]`):
  85. The id(s) of the *end-of-sequence* token.
  86. device (`str`, *optional*, defaults to `"cpu"`):
  87. The device to allocate the tensors.
  88. Examples:
  89. ```python
  90. >>> from transformers import AutoModelForCausalLM, AutoTokenizer
  91. >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
  92. >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
  93. >>> inputs = tokenizer("A number:", return_tensors="pt")
  94. >>> gen_out = model.generate(**inputs)
  95. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  96. A number: one
  97. >>> # setting `min_length` to a value smaller than the uncontrolled output length has no impact
  98. >>> gen_out = model.generate(**inputs, min_length=3)
  99. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  100. A number: one
  101. >>> # setting a larger `min_length` will force the model to generate beyond its natural ending point, which is not
  102. >>> # necessarily incorrect
  103. >>> gen_out = model.generate(**inputs, min_length=10)
  104. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  105. A number: one thousand, nine hundred and ninety-four
  106. ```
  107. """
  108. def __init__(self, min_length: int, eos_token_id: Union[int, list[int], torch.Tensor], device: str = "cpu"):
  109. if not isinstance(min_length, int) or min_length < 0:
  110. raise ValueError(f"`min_length` has to be a non-negative integer, but is {min_length}")
  111. if not isinstance(eos_token_id, torch.Tensor):
  112. if isinstance(eos_token_id, int):
  113. eos_token_id = [eos_token_id]
  114. eos_token_id = torch.tensor(eos_token_id, device=device)
  115. self.min_length = min_length
  116. self.eos_token_id = eos_token_id
  117. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  118. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  119. vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)
  120. eos_token_mask = isin_mps_friendly(vocab_tensor, self.eos_token_id)
  121. scores_processed = scores.clone()
  122. if input_ids.shape[-1] < self.min_length:
  123. scores_processed = torch.where(eos_token_mask, -math.inf, scores)
  124. return scores_processed
  125. class MinNewTokensLengthLogitsProcessor(LogitsProcessor):
  126. r"""
  127. [`LogitsProcessor`] enforcing a min-length of new tokens by setting EOS (End-Of-Sequence) token probability to 0.
  128. Contrarily to [`MinLengthLogitsProcessor`], this processor ignores the prompt.
  129. Args:
  130. prompt_length_to_skip (`int`):
  131. The input tokens length. Not a valid argument when used with `generate` as it will automatically assign the
  132. input length.
  133. min_new_tokens (`int`):
  134. The minimum *new* tokens length below which the score of `eos_token_id` is set to `-float("Inf")`.
  135. eos_token_id (`Union[int, list[int], torch.Tensor]`):
  136. The id(s) of the *end-of-sequence* token.
  137. device (`str`, *optional*, defaults to `"cpu"`):
  138. The device to allocate the tensors.
  139. Examples:
  140. ```python
  141. >>> from transformers import AutoModelForCausalLM, AutoTokenizer
  142. >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
  143. >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
  144. >>> inputs = tokenizer(["A number:"], return_tensors="pt")
  145. >>> gen_out = model.generate(**inputs)
  146. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  147. A number: one
  148. >>> # setting `min_new_tokens` will force the model to generate beyond its natural ending point, which is not
  149. >>> # necessarily incorrect
  150. >>> gen_out = model.generate(**inputs, min_new_tokens=2)
  151. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  152. A number: one thousand
  153. ```
  154. """
  155. def __init__(
  156. self,
  157. prompt_length_to_skip: int,
  158. min_new_tokens: int,
  159. eos_token_id: Union[int, list[int], torch.Tensor],
  160. device: str = "cpu",
  161. ):
  162. for arg_name, arg_value in [
  163. ("prompt_length_to_skip", prompt_length_to_skip),
  164. ("min_new_tokens", min_new_tokens),
  165. ]:
  166. if not isinstance(arg_value, int) or arg_value < 0:
  167. raise ValueError(f"`{arg_name}` has to be a positive integer, but is {arg_value}")
  168. if not isinstance(eos_token_id, torch.Tensor):
  169. if isinstance(eos_token_id, int):
  170. eos_token_id = [eos_token_id]
  171. eos_token_id = torch.tensor(eos_token_id, device=device)
  172. self.prompt_length_to_skip = prompt_length_to_skip
  173. self.min_new_tokens = min_new_tokens
  174. self.eos_token_id = eos_token_id
  175. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  176. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  177. new_tokens_length = input_ids.shape[-1] - self.prompt_length_to_skip
  178. scores_processed = scores.clone()
  179. vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)
  180. eos_token_mask = isin_mps_friendly(vocab_tensor, self.eos_token_id)
  181. if new_tokens_length < self.min_new_tokens:
  182. scores_processed = torch.where(eos_token_mask, -math.inf, scores)
  183. return scores_processed
  184. class TemperatureLogitsWarper(LogitsProcessor):
  185. r"""
  186. [`LogitsProcessor`] for temperature (exponential scaling output probability distribution), which effectively means
  187. that it can control the randomness of the predicted tokens. Often used together with [`TopPLogitsWarper`] and
  188. [`TopKLogitsWarper`].
  189. <Tip>
  190. Make sure that `do_sample=True` is included in the `generate` arguments otherwise the temperature value won't have
  191. any effect.
  192. </Tip>
  193. Args:
  194. temperature (`float`):
  195. Strictly positive float value used to modulate the logits distribution. A value smaller than `1` decreases
  196. randomness (and vice versa), with `0` being equivalent to shifting all probability mass to the most likely
  197. token.
  198. Examples:
  199. ```python
  200. >>> import torch
  201. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  202. >>> set_seed(0) # for reproducibility
  203. >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
  204. >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
  205. >>> model.config.pad_token_id = model.config.eos_token_id
  206. >>> inputs = tokenizer(["Hugging Face Company is"], return_tensors="pt")
  207. >>> # With temperature=1.0, the default, we consistently get random outputs due to random sampling.
  208. >>> generate_kwargs = {"max_new_tokens": 10, "do_sample": True, "temperature": 1.0, "num_return_sequences": 2}
  209. >>> outputs = model.generate(**inputs, **generate_kwargs)
  210. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
  211. ['Hugging Face Company is one of these companies that is going to take a',
  212. "Hugging Face Company is a brand created by Brian A. O'Neil"]
  213. >>> # However, with temperature close to 0, it approximates greedy decoding strategies (invariant)
  214. >>> generate_kwargs["temperature"] = 0.0001
  215. >>> outputs = model.generate(**inputs, **generate_kwargs)
  216. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
  217. ['Hugging Face Company is a company that has been around for over 20 years',
  218. 'Hugging Face Company is a company that has been around for over 20 years']
  219. ```
  220. """
  221. def __init__(self, temperature: float):
  222. if not isinstance(temperature, float) or not (temperature > 0):
  223. except_msg = (
  224. f"`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token "
  225. "scores will be invalid."
  226. )
  227. if isinstance(temperature, float) and temperature == 0.0:
  228. except_msg += " If you're looking for greedy decoding strategies, set `do_sample=False`."
  229. raise ValueError(except_msg)
  230. self.temperature = temperature
  231. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  232. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  233. scores_processed = scores / self.temperature
  234. return scores_processed
  235. class RepetitionPenaltyLogitsProcessor(LogitsProcessor):
  236. r"""
  237. [`LogitsProcessor`] that prevents the repetition of previous tokens through a penalty. This penalty is applied at
  238. most once per token. Note that, for decoder-only models like most LLMs, the considered tokens include the prompt
  239. by default.
  240. In the original [paper](https://huggingface.co/papers/1909.05858), the authors suggest the use of a penalty of around
  241. 1.2 to achieve a good balance between truthful generation and lack of repetition. To penalize and reduce
  242. repetition, use `penalty` values above 1.0, where a higher value penalizes more strongly. To reward and encourage
  243. repetition, use `penalty` values between 0.0 and 1.0, where a lower value rewards more strongly.
  244. Args:
  245. penalty (`float`):
  246. The parameter for repetition penalty. 1.0 means no penalty. Above 1.0 penalizes previously generated
  247. tokens. Between 0.0 and 1.0 rewards previously generated tokens.
  248. prompt_ignore_length (`int`, *optional*):
  249. The original input ids sequence length, which if provided, will not be used in the penalty calculation.
  250. Examples:
  251. ```py
  252. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, RepetitionPenaltyLogitsProcessor
  253. >>> # Initializing the model and tokenizer for it
  254. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  255. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  256. >>> inputs = tokenizer(["I'm not going to"], return_tensors="pt")
  257. >>> # This shows a normal generate without any specific parameters
  258. >>> summary_ids = model.generate(**inputs)
  259. >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
  260. I'm not going to be able to do that. I'm going to be able to do that
  261. >>> # This generates a penalty for repeated tokens
  262. >>> penalized_ids = model.generate(**inputs, repetition_penalty=1.1)
  263. >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])
  264. I'm not going to be able to do that. I'll just have to go out and play
  265. >>> # We can also exclude the input prompt by creating an instance of this class
  266. >>> # with a `prompt_ignore_length` and passing it as a custom logit processor
  267. >>> rep_pen_processor = RepetitionPenaltyLogitsProcessor(
  268. ... penalty=1.1,
  269. ... prompt_ignore_length=inputs["input_ids"].shape[-1]
  270. ... )
  271. >>> penalized_ids = model.generate(**inputs, logits_processor=[rep_pen_processor])
  272. >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])
  273. I'm not going to be able to do that. I'm going to have to go through a lot of things, and
  274. ```
  275. """
  276. def __init__(self, penalty: float, prompt_ignore_length: Optional[int] = None):
  277. if not isinstance(penalty, float) or not (penalty > 0):
  278. raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")
  279. if prompt_ignore_length is not None and (
  280. not isinstance(prompt_ignore_length, int) or prompt_ignore_length < 0
  281. ):
  282. raise ValueError(f"`prompt_ignore_length` has to be a positive integer, but is {prompt_ignore_length}")
  283. self.penalty = penalty
  284. self.prompt_ignore_length = prompt_ignore_length
  285. self.logits_indices = None
  286. self.cu_seq_lens_q = None
  287. def set_continuous_batching_context(self, logits_indices: torch.Tensor, cu_seq_lens_q: torch.Tensor):
  288. self.logits_indices = logits_indices
  289. self.cu_seq_lens_q = cu_seq_lens_q
  290. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  291. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  292. if self.prompt_ignore_length:
  293. input_ids = input_ids[:, self.prompt_ignore_length :]
  294. if scores.dim() == 3:
  295. if self.logits_indices is not None and self.cu_seq_lens_q is not None:
  296. last_positions = self.logits_indices
  297. last_scores = scores[0, last_positions, :]
  298. # Prepare token mask
  299. token_mask = torch.zeros_like(last_scores, dtype=torch.bool)
  300. cu_seq_lens = self.cu_seq_lens_q
  301. lengths = cu_seq_lens[1:] - cu_seq_lens[:-1]
  302. seq_indices = torch.repeat_interleave(torch.arange(len(lengths), device=input_ids.device), lengths)
  303. token_mask[seq_indices, input_ids] = True
  304. # Apply penalty
  305. penalty_scores = torch.where(last_scores < 0, last_scores * self.penalty, last_scores / self.penalty)
  306. scores[0, last_positions, :] = torch.where(token_mask, penalty_scores, last_scores)
  307. else:
  308. batch_size, seq_len, vocab_size = scores.shape
  309. last_scores = scores[:, -1, :]
  310. token_mask = torch.zeros_like(last_scores, dtype=torch.bool)
  311. if input_ids.dim() == 1:
  312. unique_tokens = torch.unique(input_ids)
  313. token_mask.scatter_(1, unique_tokens.unsqueeze(0), True)
  314. else:
  315. token_mask.scatter_(1, input_ids, True)
  316. # if last_scores < 0 then repetition penalty has to be multiplied to reduce the token probabilities
  317. penalty_scores = torch.where(last_scores < 0, last_scores * self.penalty, last_scores / self.penalty)
  318. scores[:, -1, :] = torch.where(token_mask, penalty_scores, last_scores)
  319. return scores
  320. if input_ids.dim() == 1:
  321. input_ids = input_ids.unsqueeze(1)
  322. score = torch.gather(scores, 1, input_ids)
  323. # if score < 0 then repetition penalty has to be multiplied to reduce the token probabilities
  324. score = torch.where(score < 0, score * self.penalty, score / self.penalty)
  325. scores_processed = scores.scatter(1, input_ids, score)
  326. return scores_processed
  327. class EncoderRepetitionPenaltyLogitsProcessor(LogitsProcessor):
  328. r"""
  329. [`LogitsProcessor`] that works similarly to [`RepetitionPenaltyLogitsProcessor`], but with an *inverse* penalty
  330. that is applied to the tokens present in the prompt. In other words, a penalty above 1.0 increases the odds of
  331. selecting tokens that were present in the prompt.
  332. It was designed to avoid hallucination in input-grounded tasks, like summarization. Although originally intended
  333. for encoder-decoder models, it can also be used with decoder-only models like LLMs.
  334. Args:
  335. penalty (`float`):
  336. The parameter for repetition penalty. 1.0 means no penalty. Above 1.0 rewards prompt tokens. Between 0.0
  337. and 1.0 penalizes prompt tokens.
  338. encoder_input_ids (`torch.LongTensor`):
  339. The encoder_input_ids that should be repeated within the decoder ids.
  340. Examples:
  341. ```python
  342. >>> from transformers import AutoModelForCausalLM, AutoTokenizer
  343. >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
  344. >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
  345. >>> inputs = tokenizer(["Alice and Bob. The third member's name was"], return_tensors="pt")
  346. >>> gen_out = model.generate(**inputs)
  347. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  348. Alice and Bob. The third member's name was not mentioned.
  349. >>> # With the `encoder_repetition_penalty` argument we can trigger this logits processor in `generate`, which can
  350. >>> # promote the use of prompt tokens ("Bob" in this example)
  351. >>> gen_out = model.generate(**inputs, encoder_repetition_penalty=1.2)
  352. >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
  353. Alice and Bob. The third member's name was Bob. The third member's name was Bob.
  354. ```
  355. """
  356. def __init__(self, penalty: float, encoder_input_ids: torch.LongTensor):
  357. if not isinstance(penalty, float) or not (penalty > 0):
  358. raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")
  359. self.penalty = 1 / penalty
  360. self.encoder_input_ids = encoder_input_ids
  361. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  362. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  363. score = torch.gather(scores, 1, self.encoder_input_ids)
  364. # if score < 0 then hallucination penalty has to be multiplied to increase the token probabilities
  365. score = torch.where(score < 0, score * self.penalty, score / self.penalty)
  366. scores_processed = scores.scatter(1, self.encoder_input_ids, score)
  367. return scores_processed
  368. class TopPLogitsWarper(LogitsProcessor):
  369. """
  370. [`LogitsProcessor`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.
  371. Often used together with [`TemperatureLogitsWarper`] and [`TopKLogitsWarper`].
  372. Args:
  373. top_p (`float`):
  374. If set to < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or
  375. higher are kept for generation.
  376. filter_value (`float`, *optional*, defaults to -inf):
  377. All filtered values will be set to this float value.
  378. min_tokens_to_keep (`int`, *optional*, defaults to 1):
  379. Minimum number of tokens that cannot be filtered.
  380. Examples:
  381. ```python
  382. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  383. >>> set_seed(1)
  384. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  385. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  386. >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
  387. >>> # With sampling, the output is unexpected -- sometimes too unexpected.
  388. >>> outputs = model.generate(**inputs, do_sample=True)
  389. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  390. A sequence: 1, 2, 3 | < 4 (left-hand pointer) ;
  391. <BLANKLINE>
  392. <BLANKLINE>
  393. >>> # With `top_p` sampling, the output gets restricted to high-probability tokens.
  394. >>> # Pro tip: In practice, LLMs use `top_p` in the 0.9-0.95 range.
  395. >>> outputs = model.generate(**inputs, do_sample=True, top_p=0.1)
  396. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  397. A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
  398. ```
  399. """
  400. def __init__(self, top_p: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
  401. top_p = float(top_p)
  402. if top_p < 0 or top_p > 1.0:
  403. raise ValueError(f"`top_p` has to be a float > 0 and < 1, but is {top_p}")
  404. if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):
  405. raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")
  406. self.top_p = top_p
  407. self.filter_value = filter_value
  408. self.min_tokens_to_keep = min_tokens_to_keep
  409. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  410. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  411. sorted_logits, sorted_indices = torch.sort(scores, descending=False)
  412. cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1)
  413. # Remove tokens with cumulative top_p above the threshold (token with 0 are kept)
  414. sorted_indices_to_remove = cumulative_probs <= (1 - self.top_p)
  415. # Keep at least min_tokens_to_keep
  416. sorted_indices_to_remove[..., -self.min_tokens_to_keep :] = 0
  417. # scatter sorted tensors to original indexing
  418. indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
  419. scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)
  420. return scores_processed
  421. class TopKLogitsWarper(LogitsProcessor):
  422. r"""
  423. [`LogitsProcessor`] that performs top-k, i.e. restricting to the k highest probability elements. Often used
  424. together with [`TemperatureLogitsWarper`] and [`TopPLogitsWarper`].
  425. Args:
  426. top_k (`int`):
  427. The number of highest probability vocabulary tokens to keep for top-k-filtering.
  428. filter_value (`float`, *optional*, defaults to -inf):
  429. All filtered values will be set to this float value.
  430. min_tokens_to_keep (`int`, *optional*, defaults to 1):
  431. Minimum number of tokens that cannot be filtered.
  432. Examples:
  433. ```python
  434. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  435. >>> set_seed(1)
  436. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  437. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  438. >>> inputs = tokenizer("A sequence: A, B, C, D", return_tensors="pt")
  439. >>> # With sampling, the output is unexpected -- sometimes too unexpected.
  440. >>> outputs = model.generate(**inputs, do_sample=True)
  441. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  442. A sequence: A, B, C, D, E — S — O, P — R
  443. >>> # With `top_k` sampling, the output gets restricted the k most likely tokens.
  444. >>> # Pro tip: In practice, LLMs use `top_k` in the 5-50 range.
  445. >>> outputs = model.generate(**inputs, do_sample=True, top_k=2)
  446. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  447. A sequence: A, B, C, D, E, F, G, H, I
  448. ```
  449. """
  450. def __init__(self, top_k: int, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
  451. if not isinstance(top_k, int) or top_k <= 0:
  452. raise ValueError(f"`top_k` has to be a strictly positive integer, but is {top_k}")
  453. self.top_k = max(top_k, min_tokens_to_keep)
  454. self.filter_value = filter_value
  455. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  456. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  457. top_k = min(self.top_k, scores.size(-1)) # Safety check
  458. # Remove all tokens with a probability less than the last token of the top-k
  459. indices_to_remove = scores < torch.topk(scores, top_k)[0][..., -1, None]
  460. scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)
  461. return scores_processed
  462. class MinPLogitsWarper(LogitsProcessor):
  463. """
  464. [`LogitsProcessor`] that performs min-p, i.e. keeps all tokens that are above a minimum probability, scaled by the
  465. probability of the most likely token. As a result, the filter becomes more aggressive in the presence of
  466. high-probability tokens, which is a sign of a confident output that we shouldn't deviate from.
  467. Often used together with [`TemperatureLogitsWarper`]. Used as an alternative to [`TopPLogitsWarper`] and
  468. [`TopKLogitsWarper`].
  469. Created by @menhguin and @kalomaze (github handles). Code adapted from [this external PR](https://github.com/oobabooga/text-generation-webui/pull/4449/files)
  470. Args:
  471. min_p (`float`):
  472. Minimum token probability, which will be scaled by the probability of the most likely token. It must be a
  473. value between 0 and 1. Typical values are in the 0.01-0.2 range, comparably selective as setting `top_p` in
  474. the 0.99-0.8 range (use the opposite of normal `top_p` values).
  475. filter_value (`float`, *optional*, defaults to -inf):
  476. All filtered values will be set to this float value.
  477. min_tokens_to_keep (`int`, *optional*, defaults to 1):
  478. Minimum number of tokens that cannot be filtered.
  479. Examples:
  480. ```python
  481. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  482. >>> set_seed(1)
  483. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  484. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  485. >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
  486. >>> # With sampling, the output is unexpected -- sometimes too unexpected.
  487. >>> outputs = model.generate(**inputs, do_sample=True)
  488. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  489. A sequence: 1, 2, 3 | < 4 (left-hand pointer) ;
  490. <BLANKLINE>
  491. <BLANKLINE>
  492. >>> # With `min_p` sampling, the output gets restricted to high-probability tokens.
  493. >>> # Pro tip: In practice, LLMs use `min_p` in the 0.01-0.2 range.
  494. >>> outputs = model.generate(**inputs, do_sample=True, min_p=0.1)
  495. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  496. A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
  497. ```
  498. """
  499. def __init__(self, min_p: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
  500. if not (0 <= min_p <= 1.0):
  501. raise ValueError(f"`min_p` has to be a float in the [0, 1] interval, but is {min_p}")
  502. if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):
  503. raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")
  504. self.min_p = min_p
  505. self.filter_value = filter_value
  506. self.min_tokens_to_keep = min_tokens_to_keep
  507. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  508. # Convert logits to probabilities
  509. probs = torch.softmax(scores, dim=-1)
  510. # Get the probability of the top token for each sequence in the batch
  511. top_probs, _ = probs.max(dim=-1, keepdim=True)
  512. # Calculate the actual min_p threshold by scaling min_p with the top token's probability
  513. scaled_min_p = self.min_p * top_probs
  514. # Create a mask for tokens that have a probability less than the scaled min_p
  515. tokens_to_remove = probs < scaled_min_p
  516. sorted_indices = torch.argsort(scores, descending=True, dim=-1)
  517. sorted_indices_to_remove = torch.gather(tokens_to_remove, dim=-1, index=sorted_indices)
  518. # Keep at least min_tokens_to_keep
  519. sorted_indices_to_remove[..., : self.min_tokens_to_keep] = False
  520. indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
  521. scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)
  522. return scores_processed
  523. class TypicalLogitsWarper(LogitsProcessor):
  524. r"""
  525. [`LogitsProcessor`] that performs typical decoding. Inspired on how humans use language, it prioritizes tokens
  526. whose log probability is close to the entropy of the token probability distribution. This means that the most
  527. likely tokens may be discarded in the process.
  528. See [Typical Decoding for Natural Language Generation](https://huggingface.co/papers/2202.00666) for more information.
  529. Args:
  530. mass (`float`, *optional*, defaults to 0.9):
  531. Value of typical_p between 0 and 1 inclusive, defaults to 0.9.
  532. filter_value (`float`, *optional*, defaults to -inf):
  533. All filtered values will be set to this float value.
  534. min_tokens_to_keep (`int`, *optional*, defaults to 1):
  535. Minimum number of tokens that cannot be filtered.
  536. Examples:
  537. ```python
  538. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  539. >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
  540. >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
  541. >>> inputs = tokenizer("1, 2, 3", return_tensors="pt")
  542. >>> # We can see that greedy decoding produces a sequence of numbers
  543. >>> outputs = model.generate(**inputs)
  544. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  545. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  546. >>> # For this particular seed, we can see that sampling produces nearly the same low-information (= low entropy)
  547. >>> # sequence
  548. >>> set_seed(18)
  549. >>> outputs = model.generate(**inputs, do_sample=True)
  550. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  551. 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10
  552. >>> # With `typical_p` set, the most obvious sequence is no longer produced, which may be good for your problem
  553. >>> set_seed(18)
  554. >>> outputs = model.generate(
  555. ... **inputs, do_sample=True, typical_p=0.1, return_dict_in_generate=True, output_scores=True
  556. ... )
  557. >>> print(tokenizer.batch_decode(outputs.sequences, skip_special_tokens=True)[0])
  558. 1, 2, 3 and 5
  559. >>> # We can see that the token corresponding to "4" (token 934) in the second position, the most likely token
  560. >>> # as seen with greedy decoding, was entirely blocked out
  561. >>> print(outputs.scores[1][0, 934])
  562. tensor(-inf)
  563. ```
  564. """
  565. def __init__(self, mass: float = 0.9, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
  566. mass = float(mass)
  567. if not (mass > 0 and mass < 1):
  568. raise ValueError(f"`typical_p` has to be a float > 0 and < 1, but is {mass}")
  569. if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):
  570. raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")
  571. self.filter_value = filter_value
  572. self.mass = mass
  573. self.min_tokens_to_keep = min_tokens_to_keep
  574. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  575. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  576. # calculate entropy
  577. normalized = torch.nn.functional.log_softmax(scores, dim=-1)
  578. p = torch.exp(normalized)
  579. ent = -(normalized * p).nansum(-1, keepdim=True)
  580. # shift and sort
  581. shifted_scores = torch.abs((-normalized) - ent)
  582. sorted_scores, sorted_indices = torch.sort(shifted_scores, descending=False)
  583. sorted_logits = scores.gather(-1, sorted_indices)
  584. cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1)
  585. # Remove tokens with cumulative mass above the threshold
  586. last_ind = (cumulative_probs < self.mass).sum(dim=1)
  587. last_ind.clamp_(max=sorted_scores.shape[-1] - 1)
  588. sorted_indices_to_remove = sorted_scores > sorted_scores.gather(1, last_ind.view(-1, 1))
  589. sorted_indices_to_remove[..., : self.min_tokens_to_keep] = 0
  590. indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
  591. scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)
  592. return scores_processed
  593. class EpsilonLogitsWarper(LogitsProcessor):
  594. r"""
  595. [`LogitsProcessor`] that performs epsilon-sampling, i.e. restricting to tokens with `prob >= epsilon`. Takes the
  596. largest min_tokens_to_keep tokens if no tokens satisfy this constraint. See [Truncation Sampling as Language Model
  597. Desmoothing](https://huggingface.co/papers/2210.15191) for more information.
  598. Args:
  599. epsilon (`float`):
  600. If set to > 0, only the most tokens with probabilities `epsilon` or higher are kept for generation.
  601. filter_value (`float`, *optional*, defaults to -inf):
  602. All filtered values will be set to this float value.
  603. min_tokens_to_keep (`int`, *optional*, defaults to 1):
  604. Minimum number of tokens that cannot be filtered.
  605. Examples:
  606. ```python
  607. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  608. >>> set_seed(1)
  609. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  610. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  611. >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
  612. >>> # With sampling, the output is unexpected -- sometimes too unexpected.
  613. >>> outputs = model.generate(**inputs, do_sample=True)
  614. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  615. A sequence: 1, 2, 3 | < 4 (left-hand pointer) ;
  616. <BLANKLINE>
  617. <BLANKLINE>
  618. >>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to
  619. >>> # Top P sampling, which restricts tokens based on their cumulative probability.
  620. >>> # Pro tip: The paper recommends using `epsilon_cutoff` values between 3e-4 and 9e-4
  621. >>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=0.1)
  622. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  623. A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
  624. ```
  625. """
  626. def __init__(self, epsilon: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
  627. epsilon = float(epsilon)
  628. if epsilon <= 0 or epsilon >= 1:
  629. raise ValueError(f"`epsilon_cutoff` has to be a float > 0 and < 1, but is {epsilon}")
  630. min_tokens_to_keep = int(min_tokens_to_keep)
  631. if min_tokens_to_keep < 1:
  632. raise ValueError(
  633. f"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}"
  634. )
  635. self.epsilon = epsilon
  636. self.filter_value = filter_value
  637. self.min_tokens_to_keep = min_tokens_to_keep
  638. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  639. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  640. # Determine which indices to remove
  641. probabilities = scores.softmax(dim=-1)
  642. indices_to_remove = probabilities < self.epsilon
  643. # Keep the words with the 'min_tokens_to_keep'-highest probabilities
  644. top_k = min(self.min_tokens_to_keep, scores.size(-1)) # Safety check
  645. indices_to_remove = indices_to_remove & (scores < torch.topk(scores, top_k)[0][..., -1, None])
  646. scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)
  647. return scores_processed
  648. class EtaLogitsWarper(LogitsProcessor):
  649. r"""
  650. [`LogitsProcessor`] that performs eta-sampling, a technique to filter out tokens with probabilities below a dynamic
  651. cutoff value, `eta`, which is calculated based on a combination of the hyperparameter `epsilon` and the entropy of
  652. the token probabilities, i.e. `eta := min(epsilon, sqrt(epsilon * e^-entropy(probabilities)))`. Takes the largest
  653. min_tokens_to_keep tokens if no tokens satisfy this constraint. It addresses the issue of poor quality in long
  654. samples of text generated by neural language models leading to more coherent and fluent text. See [Truncation
  655. Sampling as Language Model Desmoothing](https://huggingface.co/papers/2210.15191) for more information. Note: `do_sample`
  656. must be set to `True` for this `LogitsProcessor` to work.
  657. Args:
  658. epsilon (`float`):
  659. A float value in the range (0, 1). Hyperparameter used to calculate the dynamic cutoff value, `eta`. The
  660. suggested values from the paper ranges from 3e-4 to 4e-3 depending on the size of the model.
  661. filter_value (`float`, *optional*, defaults to -inf):
  662. All values that are found to be below the dynamic cutoff value, `eta`, are set to this float value. This
  663. parameter is useful when logits need to be modified for very low probability tokens that should be excluded
  664. from generation entirely.
  665. min_tokens_to_keep (`int`, *optional*, defaults to 1):
  666. Specifies the minimum number of tokens that must be kept for generation, regardless of their probabilities.
  667. For example, if `min_tokens_to_keep` is set to 1, at least one token will always be kept for generation,
  668. even if all tokens have probabilities below the cutoff `eta`.
  669. device (`str`, *optional*, defaults to `"cpu"`):
  670. The device to allocate the tensors.
  671. Examples:
  672. ```python
  673. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  674. >>> set_seed(1)
  675. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  676. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  677. >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
  678. >>> # With sampling, the output is unexpected -- sometimes too unexpected.
  679. >>> outputs = model.generate(**inputs, do_sample=True)
  680. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  681. A sequence: 1, 2, 3 | < 4 (left-hand pointer) ;
  682. <BLANKLINE>
  683. <BLANKLINE>
  684. >>> # With eta sampling, the output gets restricted to high-probability tokens. You can see it as a dynamic form of
  685. >>> # epsilon sampling that adapts its cutoff probability based on the entropy (high entropy = lower cutoff).
  686. >>> # Pro tip: The paper recommends using `eta_cutoff` values between 3e-4 to 4e-3
  687. >>> outputs = model.generate(**inputs, do_sample=True, eta_cutoff=0.1)
  688. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  689. A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
  690. ```
  691. """
  692. def __init__(
  693. self, epsilon: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1, device: str = "cpu"
  694. ):
  695. epsilon = float(epsilon)
  696. if epsilon <= 0 or epsilon >= 1:
  697. raise ValueError(f"`eta_cutoff` has to be a float > 0 and < 1, but is {epsilon}")
  698. min_tokens_to_keep = int(min_tokens_to_keep)
  699. if min_tokens_to_keep < 1:
  700. raise ValueError(
  701. f"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}"
  702. )
  703. self.epsilon = torch.tensor(epsilon, device=device)
  704. self.filter_value = filter_value
  705. self.min_tokens_to_keep = min_tokens_to_keep
  706. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  707. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  708. probabilities = scores.softmax(dim=-1)
  709. entropy = torch.distributions.Categorical(logits=scores).entropy()
  710. eta = torch.min(self.epsilon, torch.sqrt(self.epsilon) * torch.exp(-entropy))[..., None]
  711. indices_to_remove = probabilities < eta
  712. # Keep the words with the 'min_tokens_to_keep'-highest probabilities
  713. top_k = min(self.min_tokens_to_keep, scores.size(-1)) # Safety check
  714. indices_to_remove = indices_to_remove & (scores < torch.topk(scores, top_k)[0][..., -1, None])
  715. scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)
  716. return scores_processed
  717. def _get_ngrams(ngram_size: int, prev_input_ids: torch.Tensor, num_hypos: int):
  718. """
  719. Assume ngram_size=2 and prev_input_ids=tensor([[40, 2883, 2712, 4346]]). The output of generated ngrams look like
  720. this {(40,): [2883], (2883,): [2712], (2712,): [4346]}.
  721. Args:
  722. ngram_size (`int`):
  723. The number sequential tokens taken as a group which may only occur once before being banned.
  724. prev_input_ids (`torch.Tensor`):
  725. Generated token ids for the current hypothesis.
  726. num_hypos (`int`):
  727. The number of hypotheses for which n-grams need to be generated.
  728. Returns:
  729. generated_ngrams (`dict`):
  730. Dictionary of generated ngrams.
  731. """
  732. # Initialize an empty list of dictionaries, one for each hypothesis (index) in the range of num_hypos
  733. generated_ngrams = [{} for _ in range(num_hypos)]
  734. for idx in range(num_hypos):
  735. gen_tokens = prev_input_ids[idx].tolist()
  736. generated_ngram = generated_ngrams[idx]
  737. # Loop through each n-gram of size ngram_size in the list of tokens (gen_tokens)
  738. for ngram in zip(*[gen_tokens[i:] for i in range(ngram_size)]):
  739. prev_ngram_tuple = tuple(ngram[:-1])
  740. generated_ngram[prev_ngram_tuple] = generated_ngram.get(prev_ngram_tuple, []) + [ngram[-1]]
  741. return generated_ngrams
  742. def _get_generated_ngrams(banned_ngrams, prev_input_ids, ngram_size, cur_len):
  743. """
  744. Determines the banned tokens for the current hypothesis based on previously generated n-grams.
  745. Args:
  746. banned_ngrams (`dict`):
  747. A dictionary containing previously generated n-grams for each hypothesis.
  748. prev_input_ids (`torch.Tensor`):
  749. Generated token ids for the current hypothesis.
  750. ngram_size (`int`):
  751. The number sequential tokens taken as a group which may only occur once before being banned.
  752. cur_len (`int`):
  753. The current length of the token sequences for which the n-grams are being checked.
  754. Returns:
  755. List of tokens that are banned.
  756. """
  757. # Before decoding the next token, prevent decoding of ngrams that have already appeared
  758. start_idx = cur_len + 1 - ngram_size
  759. ngram_idx = tuple(prev_input_ids[start_idx:cur_len].tolist())
  760. return banned_ngrams.get(ngram_idx, [])
  761. def _calc_banned_ngram_tokens(
  762. ngram_size: int, prev_input_ids: torch.Tensor, num_hypos: int, cur_len: int
  763. ) -> list[Iterable[int]]:
  764. """Copied from fairseq for no_repeat_ngram in beam_search"""
  765. if cur_len + 1 < ngram_size:
  766. # return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet
  767. return [[] for _ in range(num_hypos)]
  768. generated_ngrams = _get_ngrams(ngram_size, prev_input_ids, num_hypos)
  769. banned_tokens = [
  770. _get_generated_ngrams(generated_ngrams[hypo_idx], prev_input_ids[hypo_idx], ngram_size, cur_len)
  771. for hypo_idx in range(num_hypos)
  772. ]
  773. return banned_tokens
  774. class NoRepeatNGramLogitsProcessor(LogitsProcessor):
  775. r"""
  776. N-grams are groups of "n" consecutive words, characters, or tokens taken from a sequence of text. Given the
  777. sentence: "She runs fast", the bi-grams (n=2) would be ("she", "runs") and ("runs", "fast"). In text generation,
  778. avoiding repetitions of word sequences provides a more diverse output. This [`LogitsProcessor`] enforces no
  779. repetition of n-grams by setting the scores of banned tokens to negative infinity which eliminates those tokens
  780. from consideration when further processing the scores. Note that, for decoder-only models like most LLMs, the
  781. prompt is also considered to obtain the n-grams.
  782. [Fairseq](https://github.com/pytorch/fairseq/blob/a07cb6f40480928c9e0548b737aadd36ee66ac76/fairseq/sequence_generator.py#L345).
  783. <Tip>
  784. Use n-gram penalties with care. For instance, penalizing 2-grams (bigrams) in an article about the city of New York
  785. might lead to undesirable outcomes where the city's name appears only once in the entire text.
  786. [Reference](https://huggingface.co/blog/how-to-generate)
  787. </Tip>
  788. Args:
  789. ngram_size (`int`):
  790. All ngrams of size `ngram_size` can only occur once.
  791. Examples:
  792. ```py
  793. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  794. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  795. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  796. >>> inputs = tokenizer(["Today I"], return_tensors="pt")
  797. >>> output = model.generate(**inputs)
  798. >>> print(tokenizer.decode(output[0], skip_special_tokens=True))
  799. Today I'm not sure if I'm going to be able to do it.
  800. >>> # Now let's add ngram size using `no_repeat_ngram_size`. This stops the repetitions ("I'm") in the output.
  801. >>> output = model.generate(**inputs, no_repeat_ngram_size=2)
  802. >>> print(tokenizer.decode(output[0], skip_special_tokens=True))
  803. Today I'm not sure if I can get a better understanding of the nature of this issue
  804. ```
  805. """
  806. def __init__(self, ngram_size: int):
  807. if not isinstance(ngram_size, int) or ngram_size <= 0:
  808. raise ValueError(f"`ngram_size` has to be a strictly positive integer, but is {ngram_size}")
  809. self.ngram_size = ngram_size
  810. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  811. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  812. num_batch_hypotheses = scores.shape[0]
  813. cur_len = input_ids.shape[-1]
  814. scores_processed = scores.clone()
  815. banned_batch_tokens = _calc_banned_ngram_tokens(self.ngram_size, input_ids, num_batch_hypotheses, cur_len)
  816. for i, banned_tokens in enumerate(banned_batch_tokens):
  817. scores_processed[i, banned_tokens] = -float("inf")
  818. return scores_processed
  819. class EncoderNoRepeatNGramLogitsProcessor(LogitsProcessor):
  820. r"""
  821. [`LogitsProcessor`] that works similarly to [`NoRepeatNGramLogitsProcessor`], but applied exclusively to prevent
  822. the repetition of n-grams present in the prompt.
  823. It was designed to promote chattiness in a language model, by preventing the generation of n-grams present in
  824. previous conversation rounds.
  825. Args:
  826. encoder_ngram_size (`int`):
  827. All ngrams of size `ngram_size` can only occur within the encoder input ids.
  828. encoder_input_ids (`int`):
  829. The encoder_input_ids that should not be repeated within the decoder ids.
  830. Examples:
  831. ```py
  832. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  833. >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
  834. >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
  835. >>> inputs = tokenizer("Alice: I love cats. What do you love?\nBob:", return_tensors="pt")
  836. >>> # With greedy decoding, we see Bob repeating Alice's opinion. If Bob was a chatbot, it would be a poor one.
  837. >>> outputs = model.generate(**inputs)
  838. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  839. Alice: I love cats. What do you love?
  840. Bob: I love cats. What do you
  841. >>> # With this logits processor, we can prevent Bob from repeating Alice's opinion.
  842. >>> outputs = model.generate(**inputs, encoder_no_repeat_ngram_size=2)
  843. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  844. Alice: I love cats. What do you love?
  845. Bob: My cats are very cute.
  846. ```
  847. """
  848. def __init__(self, encoder_ngram_size: int, encoder_input_ids: torch.LongTensor):
  849. if not isinstance(encoder_ngram_size, int) or encoder_ngram_size <= 0:
  850. raise ValueError(
  851. f"`encoder_ngram_size` has to be a strictly positive integer, but is {encoder_ngram_size}"
  852. )
  853. self.ngram_size = encoder_ngram_size
  854. if len(encoder_input_ids.shape) == 1:
  855. encoder_input_ids = encoder_input_ids.unsqueeze(0)
  856. self.batch_size = encoder_input_ids.shape[0]
  857. self.generated_ngrams = _get_ngrams(encoder_ngram_size, encoder_input_ids, self.batch_size)
  858. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  859. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  860. # B x num_beams
  861. num_hypos = scores.shape[0]
  862. num_beams = num_hypos // self.batch_size
  863. cur_len = input_ids.shape[-1]
  864. scores_processed = scores.clone()
  865. banned_batch_tokens = [
  866. _get_generated_ngrams(
  867. self.generated_ngrams[hypo_idx // num_beams], input_ids[hypo_idx], self.ngram_size, cur_len
  868. )
  869. for hypo_idx in range(num_hypos)
  870. ]
  871. for i, banned_tokens in enumerate(banned_batch_tokens):
  872. scores_processed[i, banned_tokens] = -float("inf")
  873. return scores_processed
  874. class SequenceBiasLogitsProcessor(LogitsProcessor):
  875. """
  876. [`LogitsProcessor`] that applies an additive bias on sequences. The bias is applied to the last token of a sequence
  877. when the next generated token can complete it. Consequently, to take the most of biasing sequences with more than
  878. one token, consider using beam methods (to gracefully work around partially completed sequences that have a
  879. negative bias) and applying the bias to their prefixes (to ensure the bias is applied earlier).
  880. <Tip>
  881. At a token-level, biasing a word is different from biasing a word with a space before it. If you want to bias
  882. "foo" mid-sentence, you'll likely want to add a prefix space and bias " foo" instead. Check the tokenizer section
  883. of our NLP course to find out why: https://huggingface.co/learn/nlp-course/chapter2/4?fw=pt
  884. </Tip>
  885. Args:
  886. sequence_bias (`list[list[Union[list[int], float]]]`):
  887. List of lists that maps a sequence of tokens to its bias term (e.g. `[[[10, 45], -2.0],
  888. [[64], -7.5]]`). Positive biases increase the odds of the
  889. sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias
  890. will always be applied. Otherwise, the bias will only be applied if the sequence in question is about to be
  891. completed (in the token selection step after this processor is applied).
  892. Examples:
  893. ```python
  894. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  895. >>> model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
  896. >>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
  897. >>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt")
  898. >>> summary_ids = model.generate(inputs["input_ids"], max_new_tokens=4, do_sample=False)
  899. >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
  900. The full name of Donald is Donald John Trump Sr.
  901. >>> def get_tokens(word):
  902. ... return tokenizer([word], add_special_tokens=False).input_ids[0]
  903. >>> # IMPORTANT: Remember our tip about adding spaces before words to bias them correctly.
  904. >>> sequence_bias = [[get_tokens("Trump"), -10.0],] # will fail to apply bias
  905. >>> biased_ids = model.generate(
  906. ... inputs["input_ids"], max_new_tokens=4, do_sample=False, sequence_bias=sequence_bias
  907. ... )
  908. >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
  909. The full name of Donald is Donald John Trump Sr.
  910. >>> sequence_bias = [[get_tokens(" Trump"), -10.0],] # will work
  911. >>> biased_ids = model.generate(
  912. ... inputs["input_ids"], max_new_tokens=4, do_sample=False, sequence_bias=sequence_bias
  913. ... )
  914. >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
  915. The full name of Donald is Donald John Harper. He
  916. >>> # We can also add a positive bias to nudge the model towards specific tokens or continuations. This technique
  917. >>> # is also more effective when paired up with beam search.
  918. >>> sequence_bias = [[get_tokens(" Donald Duck"), 10.0],]
  919. >>> biased_ids = model.generate(
  920. ... inputs["input_ids"], max_new_tokens=4, num_beams=4, do_sample=False, sequence_bias=sequence_bias
  921. ... )
  922. >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
  923. The full name of Donald is Donald Duck. He is
  924. ```
  925. """
  926. def __init__(self, sequence_bias: list[list[Union[list[int], float]]]):
  927. self.sequence_bias = sequence_bias
  928. self._validate_arguments()
  929. self._convert_list_arguments_into_dict()
  930. # Bias variables that will be populated on the first call (for retrocompatibility purposes, the vocabulary size
  931. # is inferred in the first usage, which inhibits initializing here)
  932. self.length_1_bias = None
  933. self.prepared_bias_variables = False
  934. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  935. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  936. # 1 - Prepares the bias tensors. This is only needed the first time the logit processor is called.
  937. if not self.prepared_bias_variables:
  938. self._prepare_bias_variables(scores)
  939. # 2 - prepares an empty bias to add
  940. bias = torch.zeros_like(scores)
  941. # 3 - include the bias from length = 1
  942. bias += self.length_1_bias
  943. # 4 - include the bias from length > 1, after determining which biased sequences may be completed.
  944. for sequence_ids, sequence_bias in self.sequence_bias.items():
  945. if len(sequence_ids) == 1: # the sequence is of length 1, already applied
  946. continue
  947. if len(sequence_ids) > input_ids.shape[1]: # the sequence is longer than the context, ignore
  948. continue
  949. prefix_length = len(sequence_ids) - 1
  950. last_token = sequence_ids[-1]
  951. matching_rows = torch.eq(
  952. input_ids[:, -prefix_length:],
  953. torch.tensor(sequence_ids[:-1], dtype=input_ids.dtype, device=input_ids.device),
  954. ).prod(dim=1)
  955. bias[:, last_token] += torch.where(
  956. matching_rows.bool(),
  957. torch.tensor(sequence_bias, device=input_ids.device),
  958. torch.tensor(0.0, device=input_ids.device),
  959. )
  960. # 5 - apply the bias to the scores
  961. scores_processed = scores + bias
  962. return scores_processed
  963. def _prepare_bias_variables(self, scores: torch.FloatTensor):
  964. vocabulary_size = scores.shape[-1]
  965. # Check biased tokens out of bounds
  966. invalid_biases = []
  967. for sequence_ids in self.sequence_bias:
  968. for token_id in sequence_ids:
  969. if token_id >= vocabulary_size:
  970. invalid_biases.append(token_id)
  971. if len(invalid_biases) > 0:
  972. raise ValueError(
  973. f"The model vocabulary size is {vocabulary_size}, but the following tokens were being biased: "
  974. f"{invalid_biases}"
  975. )
  976. # Precompute the bias tensors to be applied. Sequences of length 1 are kept separately, as they can be applied
  977. # with simpler logic.
  978. self.length_1_bias = torch.zeros((vocabulary_size,), dtype=torch.float, device=scores.device)
  979. # Extract single-token sequences and their biases
  980. single_token_ids = []
  981. single_token_biases = []
  982. for sequence_ids, bias in self.sequence_bias.items():
  983. if len(sequence_ids) == 1:
  984. single_token_ids.append(sequence_ids[0])
  985. single_token_biases.append(bias)
  986. if single_token_ids: # Only if we have any single-token sequences
  987. self.length_1_bias[single_token_ids] = torch.tensor(single_token_biases, device=scores.device)
  988. self.prepared_bias_variables = True
  989. def _validate_arguments(self):
  990. sequence_bias = self.sequence_bias
  991. if not isinstance(sequence_bias, dict) and not isinstance(sequence_bias, list) or len(sequence_bias) == 0:
  992. raise ValueError(
  993. f"`sequence_bias` has to be a non-empty dictionary, or non-empty list of lists but is {sequence_bias}."
  994. )
  995. if isinstance(sequence_bias, dict) and any(
  996. not isinstance(sequence_ids, tuple) for sequence_ids in sequence_bias
  997. ):
  998. raise ValueError(f"`sequence_bias` has to be a dict with tuples as keys, but is {sequence_bias}.")
  999. if isinstance(sequence_bias, dict) and any(
  1000. any((not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in sequence_ids)
  1001. or len(sequence_ids) == 0
  1002. for sequence_ids in sequence_bias
  1003. ):
  1004. raise ValueError(
  1005. f"Each key in `sequence_bias` has to be a non-empty tuple of positive integers, but is "
  1006. f"{sequence_bias}."
  1007. )
  1008. def all_token_bias_pairs_are_valid(sequence):
  1009. return (
  1010. isinstance(sequence[0], list)
  1011. and all(isinstance(token_id, (int, np.integer)) and token_id > 0 for token_id in sequence[0])
  1012. and isinstance(sequence[1], float)
  1013. )
  1014. if isinstance(sequence_bias, list) and any(
  1015. (not all_token_bias_pairs_are_valid(sequence)) or len(sequence) == 0 for sequence in sequence_bias
  1016. ):
  1017. raise ValueError(
  1018. f"Each element in `sequence_bias` has to be a non-empty list of lists of positive integers and float, but is "
  1019. f"{sequence_bias}."
  1020. )
  1021. if isinstance(sequence_bias, dict) and any(not isinstance(bias, float) for bias in sequence_bias.values()):
  1022. raise ValueError(f"`sequence_bias` has to be a dict with floats as values, but is {sequence_bias}.")
  1023. def _convert_list_arguments_into_dict(self):
  1024. """BC: we used to accept `dict{tuple of tokens: float}` directly, now we expect a list"""
  1025. if isinstance(self.sequence_bias, list):
  1026. temp_sequence = self.sequence_bias
  1027. self.sequence_bias = {tuple(sublist[0]): sublist[1] for sublist in temp_sequence}
  1028. class NoBadWordsLogitsProcessor(SequenceBiasLogitsProcessor):
  1029. """
  1030. [`LogitsProcessor`] that enforces that specified sequences will never be selected.
  1031. <Tip>
  1032. In order to get the token ids of the words that should not appear in the generated text, make sure to set
  1033. `add_prefix_space=True` when initializing the tokenizer, and use `tokenizer(bad_words,
  1034. add_special_tokens=False).input_ids`. The `add_prefix_space` argument is only supported for some slow tokenizers,
  1035. as fast tokenizers' prefixing behaviours come from `pre tokenizers`. Read more
  1036. [here](https://huggingface.co/docs/tokenizers/api/pre-tokenizers).
  1037. </Tip>
  1038. Args:
  1039. bad_words_ids (`list[list[int]]`):
  1040. List of list of token ids that are not allowed to be generated.
  1041. eos_token_id (`Union[int, list[int], torch.Tensor]`, *optional*):
  1042. The id(s) of the *end-of-sequence* token.
  1043. Examples:
  1044. ```python
  1045. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  1046. >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
  1047. >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
  1048. >>> inputs = tokenizer(["In a word, the cake is a"], return_tensors="pt")
  1049. >>> output_ids = model.generate(inputs["input_ids"], max_new_tokens=5, pad_token_id=tokenizer.eos_token_id)
  1050. >>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0])
  1051. In a word, the cake is a bit of a mess.
  1052. >>> # Now let's take the bad words out. Please note that the tokenizer is initialized differently
  1053. >>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("openai-community/gpt2", add_prefix_space=True)
  1054. >>> def get_tokens_as_list(word_list):
  1055. ... "Converts a sequence of words into a list of tokens"
  1056. ... tokens_list = []
  1057. ... for word in word_list:
  1058. ... tokenized_word = tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0]
  1059. ... tokens_list.append(tokenized_word)
  1060. ... return tokens_list
  1061. >>> bad_words_ids = get_tokens_as_list(word_list=["mess"])
  1062. >>> output_ids = model.generate(
  1063. ... inputs["input_ids"], max_new_tokens=5, bad_words_ids=bad_words_ids, pad_token_id=tokenizer.eos_token_id
  1064. ... )
  1065. >>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0])
  1066. In a word, the cake is a bit of a surprise.
  1067. ```
  1068. """
  1069. def __init__(
  1070. self, bad_words_ids: list[list[int]], eos_token_id: Optional[Union[int, list[int], torch.Tensor]] = None
  1071. ):
  1072. self.bad_word_ids = bad_words_ids
  1073. self._validate_arguments()
  1074. # Filter EOS token from bad_words_ids
  1075. if eos_token_id is not None:
  1076. if not isinstance(eos_token_id, torch.Tensor):
  1077. if isinstance(eos_token_id, int):
  1078. eos_token_id = [eos_token_id]
  1079. eos_token_id = torch.tensor(eos_token_id)
  1080. eos_token_id_list = eos_token_id.tolist() # convert to python list before
  1081. bad_words_ids = list(
  1082. filter(lambda bad_token_seq: all(bad_token_seq != [i] for i in eos_token_id_list), bad_words_ids)
  1083. )
  1084. # Forbidding a sequence is equivalent to setting its bias to -inf
  1085. sequence_bias = {tuple(sequence): float("-inf") for sequence in bad_words_ids}
  1086. super().__init__(sequence_bias=sequence_bias)
  1087. def _validate_arguments(self):
  1088. bad_words_ids = self.bad_word_ids
  1089. if not isinstance(bad_words_ids, list) or len(bad_words_ids) == 0:
  1090. raise ValueError(f"`bad_words_ids` has to be a non-empty list, but is {bad_words_ids}.")
  1091. if any(not isinstance(bad_word_ids, list) for bad_word_ids in bad_words_ids):
  1092. raise ValueError(f"`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.")
  1093. if any(
  1094. any((not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in bad_word_ids)
  1095. for bad_word_ids in bad_words_ids
  1096. ):
  1097. raise ValueError(
  1098. f"Each list in `bad_words_ids` has to be a list of positive integers, but is {bad_words_ids}."
  1099. )
  1100. class PrefixConstrainedLogitsProcessor(LogitsProcessor):
  1101. r"""
  1102. [`LogitsProcessor`] that enforces constrained generation and is useful for prefix-conditioned constrained
  1103. generation. See [Autoregressive Entity Retrieval](https://huggingface.co/papers/2010.00904) for more information.
  1104. Args:
  1105. prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`):
  1106. This function constraints the beam search to allowed tokens only at each step. This function takes 2
  1107. arguments `inputs_ids` and the batch ID `batch_id`. It has to return a list with the allowed tokens for the
  1108. next generation step conditioned on the previously generated tokens `inputs_ids` and the batch ID
  1109. `batch_id`.
  1110. Examples:
  1111. ```py
  1112. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  1113. >>> model = AutoModelForCausalLM.from_pretrained("bigscience/bloomz-560m")
  1114. >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/bloomz-560m")
  1115. >>> inputs = tokenizer("Alice and Bob", return_tensors="pt")
  1116. >>> # By default, it continues generating according to the model's logits
  1117. >>> outputs = model.generate(**inputs, max_new_tokens=5)
  1118. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  1119. Alice and Bob are friends
  1120. >>> # We can constrain it with `prefix_allowed_tokens_fn` to force a certain behavior based on a prefix.
  1121. >>> # For instance, we can force an entire entity to be generated when its beginning is detected.
  1122. >>> entity = tokenizer(" Bob Marley", return_tensors="pt").input_ids[0] # 3 tokens
  1123. >>> def prefix_allowed_tokens_fn(batch_id, input_ids):
  1124. ... '''
  1125. ... Attempts to generate 'Bob Marley' when 'Bob' is detected.
  1126. ... In this case, `batch_id` is not used, but you can set rules for each batch member.
  1127. ... '''
  1128. ... if input_ids[-1] == entity[0]:
  1129. ... return [entity[1].item()]
  1130. ... elif input_ids[-2] == entity[0] and input_ids[-1] == entity[1]:
  1131. ... return [entity[2].item()]
  1132. ... return list(range(tokenizer.vocab_size)) # If no match, allow all tokens
  1133. >>> outputs = model.generate(**inputs, max_new_tokens=5, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn)
  1134. >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
  1135. Alice and Bob Marley
  1136. ```
  1137. """
  1138. def __init__(self, prefix_allowed_tokens_fn: Callable[[int, torch.Tensor], list[int]], num_beams: int):
  1139. self._prefix_allowed_tokens_fn = prefix_allowed_tokens_fn
  1140. self._num_beams = num_beams
  1141. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1142. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1143. mask = torch.full_like(scores, -math.inf)
  1144. batch_size = input_ids.shape[0] // self._num_beams
  1145. for batch_id in range(batch_size):
  1146. for beam_id in range(self._num_beams):
  1147. sent = input_ids[batch_id * self._num_beams + beam_id]
  1148. prefix_allowed_tokens = self._prefix_allowed_tokens_fn(batch_id, sent)
  1149. if len(prefix_allowed_tokens) == 0:
  1150. raise ValueError(
  1151. f"`prefix_allowed_tokens_fn` returned an empty list for batch ID {batch_id}."
  1152. f"This means that the constraint is unsatisfiable. Please check your implementation"
  1153. f"of `prefix_allowed_tokens_fn` "
  1154. )
  1155. mask[batch_id * self._num_beams + beam_id, prefix_allowed_tokens] = 0
  1156. scores_processed = scores + mask
  1157. return scores_processed
  1158. class HammingDiversityLogitsProcessor(LogitsProcessor):
  1159. r"""
  1160. [`LogitsProcessor`] that enforces diverse beam search.
  1161. Note that this logits processor is only effective for [`PreTrainedModel.group_beam_search`]. See [Diverse Beam
  1162. Search: Decoding Diverse Solutions from Neural Sequence Models](https://huggingface.co/papers/1610.02424) for more
  1163. details.
  1164. Traditional beam search often generates very similar sequences across different beams.
  1165. `HammingDiversityLogitsProcessor` addresses this by penalizing beams that generate tokens already chosen by other
  1166. beams in the same time step.
  1167. Args:
  1168. diversity_penalty (`float`):
  1169. This value is subtracted from a beam's score if it generates a token same as any beam from other group at a
  1170. particular time. A higher `diversity_penalty` will enforce greater diversity among the beams. Adjusting
  1171. this value can help strike a balance between diversity and natural likelihood.
  1172. num_beams (`int`):
  1173. Number of beams for beam search. 1 means no beam search.
  1174. num_beam_groups (`int`):
  1175. Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams.
  1176. [this paper](https://huggingface.co/papers/1610.02424) for more details.
  1177. Examples:
  1178. ```python
  1179. >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
  1180. >>> import torch
  1181. >>> # Initialize the model and tokenizer
  1182. >>> tokenizer = AutoTokenizer.from_pretrained("google-t5/t5-base")
  1183. >>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base")
  1184. >>> # A long text about the solar system
  1185. >>> text = (
  1186. ... "The Solar System is a gravitationally bound system comprising the Sun and the objects that orbit it, "
  1187. ... "either directly or indirectly. Of the objects that orbit the Sun directly, the largest are the eight "
  1188. ... "planets, with the remainder being smaller objects, such as the five dwarf planets and small Solar System "
  1189. ... "bodies. The Solar System formed 4.6 billion years ago from the gravitational collapse of a giant "
  1190. ... "interstellar molecular cloud."
  1191. ... )
  1192. >>> inputs = tokenizer("summarize: " + text, return_tensors="pt")
  1193. >>> # Generate diverse summary
  1194. >>> outputs_diverse = model.generate(
  1195. ... **inputs,
  1196. ... num_beam_groups=2,
  1197. ... diversity_penalty=10.0,
  1198. ... max_length=100,
  1199. ... num_beams=4,
  1200. ... num_return_sequences=2,
  1201. ... )
  1202. >>> summaries_diverse = tokenizer.batch_decode(outputs_diverse, skip_special_tokens=True)
  1203. >>> # Generate non-diverse summary
  1204. >>> outputs_non_diverse = model.generate(
  1205. ... **inputs,
  1206. ... max_length=100,
  1207. ... num_beams=4,
  1208. ... num_return_sequences=2,
  1209. ... )
  1210. >>> summary_non_diverse = tokenizer.batch_decode(outputs_non_diverse, skip_special_tokens=True)
  1211. >>> # With `diversity_penalty`, the resulting beams are much more diverse
  1212. >>> print(summary_non_diverse)
  1213. ['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.',
  1214. '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.']
  1215. >>> print(summaries_diverse)
  1216. ['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.',
  1217. '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.']
  1218. ```
  1219. """
  1220. def __init__(self, diversity_penalty: float, num_beams: int, num_beam_groups: int):
  1221. logger.warning_once(
  1222. "`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."
  1223. )
  1224. if not isinstance(diversity_penalty, float) or (not diversity_penalty > 0.0):
  1225. raise ValueError("`diversity_penalty` should be a float strictly larger than 0.")
  1226. self._diversity_penalty = diversity_penalty
  1227. if not isinstance(num_beams, int) or num_beams < 2:
  1228. raise ValueError("`num_beams` should be an integer strictly larger than 1.")
  1229. self._num_beams = num_beams
  1230. if not isinstance(num_beam_groups, int) or num_beam_groups < 2:
  1231. raise ValueError("`num_beam_groups` should be an integer strictly larger than 1.")
  1232. if num_beam_groups > num_beams:
  1233. raise ValueError("`beam_groups` has to be smaller or equal to `num_beams`.")
  1234. self._num_sub_beams = num_beams // num_beam_groups
  1235. def __call__(
  1236. self,
  1237. input_ids: torch.LongTensor,
  1238. scores: torch.FloatTensor,
  1239. current_tokens: torch.LongTensor,
  1240. beam_group_idx: int,
  1241. ) -> torch.FloatTensor:
  1242. r"""
  1243. Args:
  1244. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  1245. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  1246. scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):
  1247. Prediction scores of a language modeling head. These can be logits for each vocabulary when not using
  1248. beam search or log softmax for each vocabulary token when using beam search
  1249. current_tokens (`torch.LongTensor` of shape `(batch_size)`):
  1250. Indices of input sequence tokens in the vocabulary, corresponding to the tokens selected by the other
  1251. beam groups in the current generation step.
  1252. beam_group_idx (`int`):
  1253. The index of the beam group currently being processed.
  1254. Return:
  1255. `torch.FloatTensor` of shape `(batch_size, config.vocab_size)`:
  1256. The processed prediction scores.
  1257. """
  1258. # hamming diversity: penalise using same token in current group which was used in previous groups at
  1259. # the same time step
  1260. batch_size = current_tokens.shape[0] // self._num_beams
  1261. group_start_idx = beam_group_idx * self._num_sub_beams
  1262. group_end_idx = min(group_start_idx + self._num_sub_beams, self._num_beams)
  1263. group_size = group_end_idx - group_start_idx
  1264. vocab_size = scores.shape[-1]
  1265. if group_start_idx == 0:
  1266. return scores
  1267. scores_processed = scores.clone()
  1268. for batch_idx in range(batch_size):
  1269. # predicted tokens of last time step of previous groups
  1270. previous_group_tokens = current_tokens[
  1271. batch_idx * self._num_beams : batch_idx * self._num_beams + group_start_idx
  1272. ]
  1273. token_frequency = torch.bincount(previous_group_tokens, minlength=vocab_size).to(scores.device)
  1274. scores_processed[batch_idx * group_size : (batch_idx + 1) * group_size] -= (
  1275. self._diversity_penalty * token_frequency
  1276. )
  1277. return scores_processed
  1278. class ForcedBOSTokenLogitsProcessor(LogitsProcessor):
  1279. r"""
  1280. [`LogitsProcessor`] that enforces the specified token as the first generated token. Used with encoder-decoder
  1281. models.
  1282. Args:
  1283. bos_token_id (`int`):
  1284. The id of the token to force as the first generated token.
  1285. Examples:
  1286. ```python
  1287. >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
  1288. >>> model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
  1289. >>> tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
  1290. >>> inputs = tokenizer("Translate from English to German: I love cats.", return_tensors="pt")
  1291. >>> # By default, it continues generating according to the model's logits
  1292. >>> outputs = model.generate(**inputs, max_new_tokens=10)
  1293. >>> print(tokenizer.batch_decode(outputs)[0])
  1294. <pad> Ich liebe Kitty.</s>
  1295. >>> # We can use `forced_bos_token_id` to force the start of generation with an encoder-decoder model
  1296. >>> # (including forcing it to end straight away with an EOS token)
  1297. >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_bos_token_id=tokenizer.eos_token_id)
  1298. >>> print(tokenizer.batch_decode(outputs)[0])
  1299. <pad></s>
  1300. ```
  1301. """
  1302. def __init__(self, bos_token_id: int):
  1303. self.bos_token_id = bos_token_id
  1304. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1305. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1306. cur_len = input_ids.shape[-1]
  1307. scores_processed = scores
  1308. if cur_len == 1:
  1309. scores_processed = torch.full_like(scores, -math.inf)
  1310. scores_processed[:, self.bos_token_id] = 0
  1311. return scores_processed
  1312. class ForcedEOSTokenLogitsProcessor(LogitsProcessor):
  1313. r"""
  1314. [`LogitsProcessor`] that enforces the specified token as the last generated token when `max_length` is reached.
  1315. Args:
  1316. max_length (`int`):
  1317. The maximum length of the sequence to be generated.
  1318. eos_token_id (`Union[int, list[int], torch.Tensor]`):
  1319. The id(s) of the *end-of-sequence* token.
  1320. device (`str`, *optional*, defaults to `"cpu"`):
  1321. The device to allocate the tensors.
  1322. Examples:
  1323. ```python
  1324. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  1325. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  1326. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  1327. >>> inputs = tokenizer("A sequence: 1, 2, 3", return_tensors="pt")
  1328. >>> # By default, it continues generating according to the model's logits
  1329. >>> outputs = model.generate(**inputs, max_new_tokens=10)
  1330. >>> print(tokenizer.batch_decode(outputs)[0])
  1331. A sequence: 1, 2, 3, 4, 5, 6, 7, 8
  1332. >>> # `forced_eos_token_id` ensures the generation ends with a EOS token
  1333. >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_eos_token_id=tokenizer.eos_token_id)
  1334. >>> print(tokenizer.batch_decode(outputs)[0])
  1335. A sequence: 1, 2, 3, 4, 5, 6, 7,<|endoftext|>
  1336. ```
  1337. """
  1338. def __init__(self, max_length: int, eos_token_id: Union[int, list[int], torch.Tensor], device: str = "cpu"):
  1339. self.max_length = max_length
  1340. if not isinstance(eos_token_id, torch.Tensor):
  1341. if isinstance(eos_token_id, int):
  1342. eos_token_id = [eos_token_id]
  1343. eos_token_id = torch.tensor(eos_token_id, device=device)
  1344. self.eos_token_id = eos_token_id
  1345. if torch.is_floating_point(eos_token_id) or (eos_token_id < 0).any():
  1346. raise ValueError(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")
  1347. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1348. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1349. cur_len = input_ids.shape[-1]
  1350. scores_processed = scores
  1351. if cur_len == self.max_length - 1:
  1352. scores_processed = torch.full_like(scores, -math.inf)
  1353. scores_processed[:, self.eos_token_id] = 0
  1354. return scores_processed
  1355. class InfNanRemoveLogitsProcessor(LogitsProcessor):
  1356. r"""
  1357. [`LogitsProcessor`] that removes all `nan` and `inf` values to avoid the generation method to fail. Note that using
  1358. the logits processor should only be used if necessary since it can slow down the generation method.
  1359. This logits processor has no `generate` example, as there shouldn't be a correct combination of flags that warrants
  1360. its use.
  1361. """
  1362. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1363. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1364. # set all nan values to 0.0
  1365. scores_processed = torch.where(scores != scores, 0.0, scores)
  1366. # set all +/-inf values to max/min possible value
  1367. scores_processed = torch.where(scores == float("inf"), torch.finfo(scores.dtype).max, scores_processed)
  1368. scores_processed = torch.where(scores == -float("inf"), torch.finfo(scores.dtype).min, scores_processed)
  1369. return scores_processed
  1370. class ExponentialDecayLengthPenalty(LogitsProcessor):
  1371. r"""
  1372. [`LogitsProcessor`] that exponentially increases the score of the `eos_token_id` after `start_index` has been
  1373. reached. This allows generating shorter sequences without having a hard cutoff, allowing the `eos_token` to be
  1374. predicted in a meaningful position.
  1375. Args:
  1376. exponential_decay_length_penalty (`tuple(int, float)`):
  1377. This tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty
  1378. starts and `decay_factor` represents the factor of exponential decay
  1379. eos_token_id (`Union[int, list[int], torch.Tensor]`):
  1380. The id(s) of the *end-of-sequence* token.
  1381. input_ids_seq_length (`int`):
  1382. The length of the input sequence.
  1383. Examples:
  1384. ```python
  1385. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
  1386. >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
  1387. >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
  1388. >>> text = "Just wanted to let you know, I"
  1389. >>> inputs = tokenizer(text, return_tensors="pt")
  1390. >>> # Let's consider that we want short sentences, so we limit `max_length=30`. However, we observe that the answer
  1391. >>> # tends to end abruptly.
  1392. >>> set_seed(1)
  1393. >>> outputs = model.generate(**inputs, do_sample=True, temperature=0.9, max_length=30, pad_token_id=50256)
  1394. >>> print(tokenizer.batch_decode(outputs)[0])
  1395. Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network which was
  1396. published in 2010. Although
  1397. >>> # To promote the appearance of the EOS token at the right time, we add the `exponential_decay_length_penalty =
  1398. >>> # (start_index, decay_factor)`. Instead of cutting at max_tokens, the output comes to an end before and usually
  1399. >>> # with more meaning. What happens is that starting from `start_index` the EOS token score will be increased
  1400. >>> # by `decay_factor` exponentially. However, if you set a high decay factor, you may also end up with abruptly
  1401. >>> # ending sequences.
  1402. >>> set_seed(1)
  1403. >>> outputs = model.generate(
  1404. ... **inputs,
  1405. ... do_sample=True,
  1406. ... temperature=0.9,
  1407. ... max_length=30,
  1408. ... pad_token_id=50256,
  1409. ... exponential_decay_length_penalty=(15, 1.6),
  1410. ... )
  1411. >>> print(tokenizer.batch_decode(outputs)[0])
  1412. Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network
  1413. which<|endoftext|>
  1414. >>> # With a small decay factor, you will have a higher chance of getting a meaningful sequence.
  1415. >>> set_seed(1)
  1416. >>> outputs = model.generate(
  1417. ... **inputs,
  1418. ... do_sample=True,
  1419. ... temperature=0.9,
  1420. ... max_length=30,
  1421. ... pad_token_id=50256,
  1422. ... exponential_decay_length_penalty=(15, 1.01),
  1423. ... )
  1424. >>> print(tokenizer.batch_decode(outputs)[0])
  1425. Just wanted to let you know, I received a link to an ebook, the book How To Start A Social Network which was
  1426. published in 2010.<|endoftext|>
  1427. ```
  1428. """
  1429. def __init__(
  1430. self,
  1431. exponential_decay_length_penalty: tuple[int, float],
  1432. eos_token_id: Union[int, list[int], torch.Tensor],
  1433. input_ids_seq_length: int,
  1434. ):
  1435. self.regulation_start = exponential_decay_length_penalty[0] + input_ids_seq_length
  1436. self.regulation_factor = exponential_decay_length_penalty[1]
  1437. if not isinstance(eos_token_id, torch.Tensor):
  1438. if isinstance(eos_token_id, int):
  1439. eos_token_id = [eos_token_id]
  1440. eos_token_id = torch.tensor(eos_token_id)
  1441. self.eos_token_id = eos_token_id
  1442. if torch.is_floating_point(eos_token_id) or (eos_token_id < 0).any():
  1443. raise ValueError(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")
  1444. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1445. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1446. cur_len = input_ids.shape[-1]
  1447. self.eos_token_id = self.eos_token_id.to(scores.device)
  1448. penalties = torch.zeros_like(scores)
  1449. scores_processed = scores
  1450. if cur_len > self.regulation_start:
  1451. penalty_idx = cur_len - self.regulation_start
  1452. # To support negative logits we compute the penalty of the absolute value and add to the original logit
  1453. penalty = torch.abs(scores[:, self.eos_token_id]) * (pow(self.regulation_factor, penalty_idx) - 1)
  1454. penalties[:, self.eos_token_id] = penalty
  1455. scores_processed = scores + penalties
  1456. return scores_processed
  1457. class LogitNormalization(LogitsProcessor):
  1458. r"""
  1459. [`LogitsProcessor`] for normalizing the scores using log-softmax. It's important to normalize
  1460. the scores during beam search, after applying the logits processors or warpers, since the search algorithm used in
  1461. this library doesn't do it (it only does it before, but they may need re-normalization) but it still supposes that
  1462. the scores are normalized when comparing the hypotheses.
  1463. Examples:
  1464. ```python
  1465. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  1466. >>> import torch
  1467. >>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
  1468. >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
  1469. >>> inputs = tokenizer("A sequence: 1, 2, 3", return_tensors="pt")
  1470. >>> # By default, the scores are not normalized -- the sum of their exponentials is NOT a normalized probability
  1471. >>> # distribution, summing to 1
  1472. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
  1473. >>> print(torch.allclose(torch.sum(torch.exp(outputs.scores[-1])), torch.Tensor((1.000,)), rtol=1e-4))
  1474. False
  1475. >>> # Normalizing them may have a positive impact on beam methods, or when using the scores on your application
  1476. >>> outputs = model.generate(**inputs, renormalize_logits=True, return_dict_in_generate=True, output_scores=True)
  1477. >>> print(torch.allclose(torch.sum(torch.exp(outputs.scores[-1])), torch.Tensor((1.000,)), rtol=1e-4))
  1478. True
  1479. ```
  1480. """
  1481. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1482. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1483. scores_processed = scores.log_softmax(dim=-1)
  1484. return scores_processed
  1485. class SuppressTokensAtBeginLogitsProcessor(LogitsProcessor):
  1486. r"""
  1487. [`SuppressTokensAtBeginLogitsProcessor`] suppresses a list of tokens as soon as the `generate` function starts
  1488. generating using `begin_index` tokens. This should ensure that the tokens defined by `begin_suppress_tokens` are
  1489. not generated at the beginning. Originally created for
  1490. [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper).
  1491. Examples:
  1492. ```python
  1493. >>> from transformers import AutoProcessor, WhisperForConditionalGeneration
  1494. >>> from datasets import load_dataset
  1495. >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en")
  1496. >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
  1497. >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
  1498. >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt")
  1499. >>> # Whisper has `begin_suppress_tokens` set by default (= `[220, 50256]`). 50256 is the EOS token, so this means
  1500. >>> # it can't generate and EOS token in the first iteration, but it can in the others.
  1501. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
  1502. >>> print(outputs.scores[0][0, 50256])
  1503. tensor(-inf)
  1504. >>> print(outputs.scores[-1][0, 50256]) # in other places we can see some probability mass for EOS
  1505. tensor(29.9010)
  1506. >>> # If we disable `begin_suppress_tokens`, we can generate EOS in the first iteration.
  1507. >>> outputs = model.generate(
  1508. ... **inputs, return_dict_in_generate=True, output_scores=True, begin_suppress_tokens=None
  1509. ... )
  1510. >>> print(outputs.scores[0][0, 50256])
  1511. tensor(11.2027)
  1512. ```
  1513. """
  1514. def __init__(self, begin_suppress_tokens, begin_index, device: str = "cpu"):
  1515. self.begin_suppress_tokens = torch.tensor(list(begin_suppress_tokens), device=device)
  1516. self.begin_index = begin_index
  1517. def set_begin_index(self, begin_index):
  1518. self.begin_index = begin_index
  1519. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1520. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1521. vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)
  1522. suppress_token_mask = isin_mps_friendly(vocab_tensor, self.begin_suppress_tokens)
  1523. scores_processed = scores
  1524. if input_ids.shape[-1] == self.begin_index:
  1525. scores_processed = torch.where(suppress_token_mask, -float("inf"), scores)
  1526. return scores_processed
  1527. class SuppressTokensLogitsProcessor(LogitsProcessor):
  1528. r"""
  1529. This processor can be used to suppress a list of tokens. The processor will set their log probs to `-inf` so
  1530. that they are not generated. Originally created for
  1531. [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper).
  1532. Examples:
  1533. ```python
  1534. >>> from transformers import AutoProcessor, WhisperForConditionalGeneration
  1535. >>> from datasets import load_dataset
  1536. >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en")
  1537. >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
  1538. >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
  1539. >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt")
  1540. >>> # Whisper has a long list of suppressed tokens. For instance, in this case, the token 1 is suppressed by default.
  1541. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
  1542. >>> print(outputs.scores[1][0, 1]) # 1 (and not 0) is the first freely generated token
  1543. tensor(-inf)
  1544. >>> # If we disable `suppress_tokens`, we can generate it.
  1545. >>> outputs = model.generate(**inputs, return_dict_in_generate=True, output_scores=True, suppress_tokens=None)
  1546. >>> print(outputs.scores[1][0, 1])
  1547. tensor(6.0678)
  1548. ```
  1549. """
  1550. def __init__(self, suppress_tokens, device: str = "cpu"):
  1551. self.suppress_tokens = torch.tensor(list(suppress_tokens), device=device)
  1552. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1553. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1554. vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)
  1555. suppress_token_mask = isin_mps_friendly(vocab_tensor, self.suppress_tokens)
  1556. scores = torch.where(suppress_token_mask, -float("inf"), scores)
  1557. return scores
  1558. class WhisperTimeStampLogitsProcessor(LogitsProcessor):
  1559. r"""
  1560. [`LogitsProcessor`] that modifies the logits for the generation of timestamps in the transcription. When the input
  1561. tokens are at a specific threshold, the processor sets the scores to negative infinity. The processor makes sure
  1562. that timestamp tokens appear in pairs, by masking out the logits that would break this pairing pattern. This is
  1563. done to maintain the consistency and structure of generated timestamps. It also ensures that when the predicted
  1564. probability of sampling any of the timestamp token is greater than any individual non-timestamp token, those
  1565. non-timestamp logits are set to negative infinity. This is done to ensure the generation of timestamps over other
  1566. potential tokens.
  1567. See [the paper](https://huggingface.co/papers/2212.04356) for more information.
  1568. Args:
  1569. generate_config (`GenerateConfig`):
  1570. The generate config used to generate the output. The following parameters are required:
  1571. eos_token_id (`int`, *optional*, defaults to 50257):
  1572. The id of the *end-of-sequence* token.
  1573. no_timestamps_token_id (`int`, *optional*, defaults to 50363):
  1574. The id of the `"<|notimestamps|>"` token.
  1575. max_initial_timestamp_index (`int`, *optional*, defaults to 1):
  1576. Used to set the maximum value of the initial timestamp. This is used to prevent the model from
  1577. predicting timestamps that are too far in the future.
  1578. begin_index (`int`):
  1579. Token index of the first token that is generated by the model.
  1580. _detect_timestamp_from_logprob (`bool`, *optional*):
  1581. Whether timestamps can be predicted from logprobs over all timestamps.
  1582. Examples:
  1583. ``` python
  1584. >>> import torch
  1585. >>> from transformers import AutoProcessor, WhisperForConditionalGeneration, GenerationConfig
  1586. >>> from datasets import load_dataset
  1587. >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en")
  1588. >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
  1589. >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
  1590. >>> inputs = processor(ds[3]["audio"]["array"], return_tensors="pt")
  1591. >>> input_features = inputs.input_features
  1592. >>> #Displaying timestamps
  1593. >>> generated_ids = model.generate(inputs=input_features, return_timestamps=True)
  1594. >>> transcription = processor.batch_decode(generated_ids, decode_with_timestamps=True)[0]
  1595. >>> print("Transcription:", transcription)
  1596. 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|>
  1597. >>> #No timestamps & change EOS:
  1598. >>> #This allows the user to select a specific token to terminate the sequence on, in this case it's the word "can"(460)
  1599. >>> model.generation_config.eos_token_id = 460
  1600. >>> generated_ids = model.generate(inputs=input_features,return_timestamps=False)
  1601. >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
  1602. >>> print("Transcription:", transcription)
  1603. Transcription: He has grave doubts whether Sir Frederick Layton's work is really Greek after all and can
  1604. ```
  1605. """
  1606. def __init__(
  1607. self,
  1608. generate_config: "GenerationConfig",
  1609. begin_index: int,
  1610. _detect_timestamp_from_logprob: Optional[bool] = None,
  1611. ): # support for the kwargs
  1612. self.no_timestamps_token_id = generate_config.no_timestamps_token_id
  1613. self.timestamp_begin = generate_config.no_timestamps_token_id + 1
  1614. self.eos_token_id = generate_config.eos_token_id or generate_config.bos_token_id
  1615. # this variable is mostly just used for testing
  1616. self._detect_timestamp_from_logprob = (
  1617. _detect_timestamp_from_logprob
  1618. if _detect_timestamp_from_logprob is not None
  1619. else getattr(generate_config, "_detect_timestamp_from_logprob", True)
  1620. )
  1621. self.begin_index = begin_index
  1622. if begin_index is None:
  1623. raise ValueError(
  1624. "`forced_decoder_ids` is deprecated in favor of `task` and `language` and, as such, `begin_index` "
  1625. "must be provided to `WhisperTimeStampLogitsProcessor`. The previous default value of `begin_index` "
  1626. "was `len(generate_config.forced_decoder_ids)`"
  1627. )
  1628. self.max_initial_timestamp_index = getattr(generate_config, "max_initial_timestamp_index", None)
  1629. # TODO(Patrick): Make sure that official models have max_initial_timestamp_index set to 50
  1630. # self.max_initial_timestamp_index = 50
  1631. def set_begin_index(self, begin_index):
  1632. self.begin_index = begin_index
  1633. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1634. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1635. # suppress <|notimestamps|> which is handled by without_timestamps
  1636. scores_processed = scores.clone()
  1637. scores_processed[:, self.no_timestamps_token_id] = -float("inf")
  1638. # timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly
  1639. for k in range(input_ids.shape[0]):
  1640. sampled_tokens = input_ids[k, self.begin_index :]
  1641. seq = list(sampled_tokens.tolist())
  1642. last_was_timestamp = len(seq) >= 1 and seq[-1] >= self.timestamp_begin
  1643. penultimate_was_timestamp = len(seq) < 2 or seq[-2] >= self.timestamp_begin
  1644. if last_was_timestamp:
  1645. if penultimate_was_timestamp: # has to be non-timestamp
  1646. scores_processed[k, self.timestamp_begin :] = -float("inf")
  1647. else: # cannot be normal text tokens
  1648. scores_processed[k, : self.eos_token_id] = -float("inf")
  1649. timestamps = sampled_tokens[sampled_tokens.ge(self.timestamp_begin)]
  1650. if timestamps.numel() > 0:
  1651. # `timestamps` shouldn't decrease; forbid timestamp tokens smaller than the last
  1652. # The following lines of code are copied from: https://github.com/openai/whisper/pull/914/files#r1137085090
  1653. if last_was_timestamp and not penultimate_was_timestamp:
  1654. timestamp_last = timestamps[-1]
  1655. else:
  1656. # Avoid to emit <|0.00|> again
  1657. timestamp_last = timestamps[-1] + 1
  1658. scores_processed[k, self.timestamp_begin : timestamp_last] = -float("inf")
  1659. # apply the `max_initial_timestamp` option
  1660. if input_ids.shape[1] == self.begin_index:
  1661. scores_processed[:, : self.timestamp_begin] = -float("inf")
  1662. if self.max_initial_timestamp_index is not None:
  1663. last_allowed = self.timestamp_begin + self.max_initial_timestamp_index
  1664. scores_processed[:, last_allowed + 1 :] = -float("inf")
  1665. # if sum of probability over timestamps is above any other token, sample timestamp
  1666. logprobs = torch.nn.functional.log_softmax(scores_processed.float(), dim=-1)
  1667. for k in range(input_ids.shape[0]):
  1668. timestamp_logprob = logprobs[k, self.timestamp_begin :].logsumexp(dim=-1)
  1669. max_text_token_logprob = logprobs[k, : self.timestamp_begin].max()
  1670. if timestamp_logprob > max_text_token_logprob and self._detect_timestamp_from_logprob:
  1671. scores_processed[k, : self.timestamp_begin] = -float("inf")
  1672. return scores_processed
  1673. class WhisperNoSpeechDetection(LogitsProcessor):
  1674. r"""This processor can be used to detect silence when using Whisper. It should take as input unprocessed logits to follow the original implementation"""
  1675. def __init__(self, no_speech_token: int, begin_index: int, scores_is_logprobs: bool = False):
  1676. self.no_speech_token = no_speech_token
  1677. # offset between <start-of-transcription> token, <SOT>, in paper and first generated token
  1678. # is equal to the position of the first generated token index
  1679. self.start_of_trans_offset = begin_index
  1680. # `self.begin_index` is a running value that is changed on the fly
  1681. self.begin_index = begin_index
  1682. self._no_speech_prob = [0.0]
  1683. self.is_scores_logprobs = scores_is_logprobs
  1684. # overwritten dynamically
  1685. self.model = None
  1686. self.inputs = None
  1687. def set_model(self, model):
  1688. self.model = model
  1689. def set_inputs(self, inputs):
  1690. self.inputs = {**self.model.prepare_inputs_for_generation(**inputs), **inputs}
  1691. self.inputs["input_features"] = self.inputs.pop("inputs")
  1692. # Whisper encoder-decoder does not accept the input_ids as input
  1693. if "input_ids" not in inspect.signature(self.model.forward).parameters:
  1694. self.inputs.pop("input_ids", None)
  1695. @property
  1696. def no_speech_prob(self):
  1697. return self._no_speech_prob
  1698. def set_begin_index(self, begin_index):
  1699. self.begin_index = begin_index
  1700. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1701. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1702. is_scores_logprobs = self.is_scores_logprobs
  1703. if input_ids.shape[1] == self.begin_index:
  1704. if self.start_of_trans_offset > 1:
  1705. with torch.no_grad():
  1706. logits = self.model(**self.inputs).logits
  1707. no_speech_index = self.begin_index - self.start_of_trans_offset
  1708. no_speech_scores = logits[:, no_speech_index]
  1709. is_scores_logprobs = False
  1710. else:
  1711. no_speech_scores = scores
  1712. if is_scores_logprobs:
  1713. probs = no_speech_scores.exp()
  1714. else:
  1715. probs = no_speech_scores.float().softmax(dim=-1)
  1716. self._no_speech_prob = probs[:, self.no_speech_token]
  1717. return scores
  1718. class ClassifierFreeGuidanceLogitsProcessor(LogitsProcessor):
  1719. r"""
  1720. [`LogitsProcessor`] for classifier free guidance (CFG). The scores are split over the batch dimension,
  1721. where the first half correspond to the conditional logits (predicted from the input prompt) and the second half
  1722. correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a
  1723. weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`.
  1724. See [the paper](https://huggingface.co/papers/2306.05284) for more information.
  1725. <Tip warning={true}>
  1726. This logits processor is exclusively compatible with
  1727. [MusicGen](https://huggingface.co/docs/transformers/main/en/model_doc/musicgen)
  1728. </Tip>
  1729. Args:
  1730. guidance_scale (float):
  1731. The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`.
  1732. Higher guidance scale encourages the model to generate samples that are more closely linked to the input
  1733. prompt, usually at the expense of poorer quality.
  1734. Examples:
  1735. ```python
  1736. >>> from transformers import AutoProcessor, MusicgenForConditionalGeneration
  1737. >>> processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
  1738. >>> model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
  1739. >>> inputs = processor(
  1740. ... text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"],
  1741. ... padding=True,
  1742. ... return_tensors="pt",
  1743. ... )
  1744. >>> audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
  1745. ```
  1746. """
  1747. def __init__(self, guidance_scale):
  1748. if guidance_scale > 1:
  1749. self.guidance_scale = guidance_scale
  1750. else:
  1751. raise ValueError(
  1752. "Require guidance scale >1 to use the classifier free guidance processor, got guidance scale "
  1753. f"{guidance_scale}."
  1754. )
  1755. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1756. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1757. # simple check to make sure we have compatible batch sizes between our
  1758. # logits scores (cond + uncond) and input ids (cond only)
  1759. if scores.shape[0] != 2 * input_ids.shape[0]:
  1760. raise ValueError(
  1761. f"Logits should have twice the batch size of the input ids, the first half of batches corresponding to "
  1762. f"the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got "
  1763. f"batch size {scores.shape[0]} for the logits and {input_ids.shape[0]} for the input ids."
  1764. )
  1765. unguided_bsz = scores.shape[0] // 2
  1766. cond_logits, uncond_logits = scores.split(unguided_bsz, dim=0)
  1767. scores_processed = uncond_logits + (cond_logits - uncond_logits) * self.guidance_scale
  1768. return scores_processed
  1769. class AlternatingCodebooksLogitsProcessor(LogitsProcessor):
  1770. r"""
  1771. [`LogitsProcessor`] enforcing alternated generation between the two codebooks of Bark.
  1772. <Tip warning={true}>
  1773. This logits processor is exclusively compatible with
  1774. [Bark](https://huggingface.co/docs/transformers/en/model_doc/bark)'s fine submodel. See the model documentation
  1775. for examples.
  1776. </Tip>
  1777. Args:
  1778. input_start_len (`int`):
  1779. The length of the initial input sequence.
  1780. semantic_vocab_size (`int`):
  1781. Vocabulary size of the semantic part, i.e number of tokens associated to the semantic vocabulary.
  1782. codebook_size (`int`):
  1783. Number of tokens associated to the codebook.
  1784. """
  1785. def __init__(self, input_start_len: int, semantic_vocab_size: int, codebook_size: int):
  1786. if not isinstance(input_start_len, int) or input_start_len < 0:
  1787. raise ValueError(f"`input_starting_length` has to be a non-negative integer, but is {input_start_len}")
  1788. self.input_start_len = input_start_len
  1789. self.semantic_vocab_size = semantic_vocab_size
  1790. self.codebook_size = codebook_size
  1791. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1792. curr_len = input_ids.shape[-1]
  1793. # even -> first codebook, odd -> second codebook
  1794. is_first_codebook = ((curr_len - self.input_start_len) % 2) == 0
  1795. scores_processed = scores.clone()
  1796. if is_first_codebook:
  1797. scores_processed[:, : self.semantic_vocab_size] = -float("inf")
  1798. scores_processed[:, self.semantic_vocab_size + self.codebook_size :] = -float("inf")
  1799. else:
  1800. scores_processed[:, : self.semantic_vocab_size + self.codebook_size] = -float("inf")
  1801. return scores_processed
  1802. class UnbatchedClassifierFreeGuidanceLogitsProcessor(LogitsProcessor):
  1803. r"""
  1804. Logits processor for Classifier-Free Guidance (CFG). The processors computes a weighted average across scores
  1805. from prompt conditional and prompt unconditional (or negative) logits, parameterized by the `guidance_scale`.
  1806. The unconditional scores are computed internally by prompting `model` with the `unconditional_ids` branch.
  1807. See [the paper](https://huggingface.co/papers/2306.17806) for more information.
  1808. Args:
  1809. guidance_scale (`float`):
  1810. The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale != 1`.
  1811. Higher guidance scale encourages the model to generate samples that are more closely linked to the input
  1812. prompt, usually at the expense of poorer quality. A value smaller than 1 has the opposite effect, while
  1813. making the negative prompt provided with negative_prompt_ids (if any) act as a positive prompt.
  1814. model (`PreTrainedModel`):
  1815. The model computing the unconditional scores. Supposedly the same as the one computing the conditional
  1816. scores. Both models must use the same tokenizer.
  1817. unconditional_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
  1818. Indices of input sequence tokens in the vocabulary for the unconditional branch. If unset, will default to
  1819. the last token of the prompt.
  1820. unconditional_attention_mask (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
  1821. Attention mask for unconditional_ids.
  1822. use_cache (`bool`, *optional*, defaults to `True`):
  1823. Whether to cache key/values during the negative prompt forward pass.
  1824. Examples:
  1825. ```python
  1826. >>> from transformers import AutoTokenizer, AutoModelForCausalLM
  1827. >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
  1828. >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
  1829. >>> inputs = tokenizer(["Today, a dragon flew over Paris, France,"], return_tensors="pt")
  1830. >>> out = model.generate(inputs["input_ids"], guidance_scale=1.5)
  1831. >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
  1832. 'Today, a dragon flew over Paris, France, killing at least 50 people and injuring more than 100'
  1833. >>> # with a negative prompt
  1834. >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
  1835. >>> out = model.generate(inputs["input_ids"], guidance_scale=2, negative_prompt_ids=neg_inputs["input_ids"])
  1836. >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
  1837. 'Today, a dragon flew over Paris, France, killing at least 130 people. French media reported that'
  1838. >>> # with a positive prompt
  1839. >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
  1840. >>> out = model.generate(inputs["input_ids"], guidance_scale=0, negative_prompt_ids=neg_inputs["input_ids"])
  1841. >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
  1842. "Today, a dragon flew over Paris, France, and I'm very happy to be here. I"
  1843. ```
  1844. """
  1845. def __init__(
  1846. self,
  1847. guidance_scale: float,
  1848. model,
  1849. unconditional_ids: Optional[torch.LongTensor] = None,
  1850. unconditional_attention_mask: Optional[torch.LongTensor] = None,
  1851. use_cache: bool = True,
  1852. ):
  1853. self.guidance_scale = guidance_scale
  1854. self.model = model
  1855. self.unconditional_context = {
  1856. "input_ids": unconditional_ids,
  1857. "attention_mask": unconditional_attention_mask,
  1858. "use_cache": use_cache,
  1859. "past_key_values": None,
  1860. "first_pass": True,
  1861. }
  1862. def get_unconditional_logits(self, input_ids):
  1863. if self.unconditional_context["first_pass"]:
  1864. if self.unconditional_context["input_ids"] is None:
  1865. self.unconditional_context["input_ids"] = input_ids[:, -1:]
  1866. if self.unconditional_context["attention_mask"] is None:
  1867. self.unconditional_context["attention_mask"] = torch.ones_like(
  1868. self.unconditional_context["input_ids"], dtype=torch.long
  1869. )
  1870. input_ids = self.unconditional_context["input_ids"]
  1871. attention_mask = self.unconditional_context["attention_mask"]
  1872. self.unconditional_context["first_pass"] = False
  1873. else:
  1874. attention_mask = torch.cat(
  1875. [
  1876. self.unconditional_context["attention_mask"],
  1877. torch.ones_like(input_ids[:, -1:], dtype=torch.long),
  1878. ],
  1879. dim=1,
  1880. )
  1881. if not self.unconditional_context["use_cache"]:
  1882. input_ids = torch.cat([self.unconditional_context["input_ids"], input_ids[:, -1:]], dim=1)
  1883. else:
  1884. input_ids = input_ids[:, -1:]
  1885. self.unconditional_context["input_ids"] = input_ids
  1886. self.unconditional_context["attention_mask"] = attention_mask
  1887. out = self.model(
  1888. input_ids,
  1889. attention_mask=attention_mask,
  1890. use_cache=self.unconditional_context["use_cache"],
  1891. past_key_values=self.unconditional_context["past_key_values"],
  1892. )
  1893. self.unconditional_context["past_key_values"] = out.get("past_key_values", None)
  1894. return out.logits
  1895. def __call__(self, input_ids, scores):
  1896. scores = torch.nn.functional.log_softmax(scores, dim=-1)
  1897. if self.guidance_scale == 1:
  1898. return scores
  1899. logits = self.get_unconditional_logits(input_ids)
  1900. unconditional_logits = torch.nn.functional.log_softmax(logits[:, -1], dim=-1)
  1901. scores_processed = self.guidance_scale * (scores - unconditional_logits) + unconditional_logits
  1902. return scores_processed
  1903. class BarkEosPrioritizerLogitsProcessor(LogitsProcessor):
  1904. r"""This processor ensures that the EOS token is selected if its probability is greater than the `min_eos_p`.
  1905. <Tip warning={true}>
  1906. This logits processor is exclusively compatible with
  1907. [Bark](https://huggingface.co/docs/transformers/en/model_doc/bark). See the model documentation for examples.
  1908. </Tip>
  1909. Args:
  1910. eos_token_id (`Union[int, list[int], torch.Tensor]`):
  1911. The id(s) of the *end-of-sequence* token.
  1912. min_eos_p (`float`, *optional*):
  1913. Minimum end of speech threshold.
  1914. """
  1915. def __init__(self, eos_token_id: Union[int, list[int], torch.Tensor], min_eos_p: float, device: str = "cpu"):
  1916. if not isinstance(eos_token_id, torch.Tensor):
  1917. if isinstance(eos_token_id, int):
  1918. eos_token_id = [eos_token_id]
  1919. eos_token_id = torch.tensor(eos_token_id, device=device)
  1920. self.eos_token_id = eos_token_id
  1921. if torch.is_floating_point(eos_token_id) or (eos_token_id < 0).any():
  1922. raise ValueError(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")
  1923. if min_eos_p is not None and min_eos_p <= 0:
  1924. raise ValueError(f"`min_eos_p` has to be a positive float, but is {min_eos_p}")
  1925. self.min_eos_p = min_eos_p
  1926. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  1927. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  1928. scores_processed = scores
  1929. if self.min_eos_p:
  1930. probs = torch.nn.functional.softmax(scores.float(), dim=-1)
  1931. # create scores full of -inf except for the eos_token_id
  1932. early_stop_scores = torch.ones_like(scores) * -float("inf")
  1933. early_stop_scores[:, self.eos_token_id] = scores[:, self.eos_token_id]
  1934. do_early_stop = probs[:, self.eos_token_id] > self.min_eos_p
  1935. do_early_stop = torch.any(do_early_stop, dim=1, keepdim=True)
  1936. scores_processed = torch.where(do_early_stop, early_stop_scores, scores)
  1937. return scores_processed
  1938. class WatermarkLogitsProcessor(LogitsProcessor):
  1939. r"""
  1940. Logits processor for watermarking generated text. The processor modifies model output scores by adding a small bias to
  1941. randomized set of "green" tokens before generating the next token. "Green" tokens selection process depends on the
  1942. `seeding_scheme` used. The code was based on the [original repo](https://github.com/jwkirchenbauer/lm-watermarking/tree/main).
  1943. The text generated by this `LogitsProcessor` can be detected using `WatermarkDetector`. See [`~WatermarkDetector.__call__`] for details,
  1944. See [the paper](https://huggingface.co/papers/2306.04634) for more information.
  1945. Args:
  1946. vocab_size (`int`):
  1947. The model tokenizer's vocab_size. Used to calculate "green" tokens ratio.
  1948. device (`str`):
  1949. The device where model is allocated.
  1950. greenlist_ratio (`float`, optional, *optional*, defaults to 0.25):
  1951. The ratio of "green" tokens used to the vocabulary size. Defaults to 0.25.
  1952. bias (`float`, optional, *optional*, defaults to 2.0):
  1953. The bias added to the selected "green" tokens' logits. Consider lowering the
  1954. `bias` if the text generation quality degrades. Recommended values are in the
  1955. range of [0.5, 2.0]. Defaults to 2.0.
  1956. hashing_key (`int`, optional, *optional*, defaults to 15485863):
  1957. Key used for hashing. If you deploy this watermark, we advise using another private key.
  1958. Defaults to 15485863 (the millionth prime).
  1959. seeding_scheme (`str`, optional, *optional*, defaults to `"lefthash"`):
  1960. The seeding scheme used for selecting "green" tokens. Accepts values:
  1961. - "lefthash" (default): "green" tokens selection depend on the last token (Algorithm 2 from paper)
  1962. - "selfhash": "green" tokens selection depends on the current token itself (Algorithm 3 from paper)
  1963. The downside of this scheme is that it considers all possible next tokens and can be slower than "lefthash".
  1964. The context length of previous tokens to use in seeding. Higher context length makes watermarking more robust.
  1965. context_width (`int`, *optional*, defaults to 1):
  1966. The number of previous tokens to use when setting the seed.
  1967. Examples:
  1968. ```python
  1969. >>> from transformers import AutoTokenizer, AutoModelForCausalLM, WatermarkingConfig
  1970. >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
  1971. >>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
  1972. >>> inputs = tokenizer(["Alice and Bob are"], return_tensors="pt")
  1973. >>> # normal generation
  1974. >>> out = model.generate(inputs["input_ids"], max_length=20, do_sample=False)
  1975. >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
  1976. 'Alice and Bob are both in the same room.\n\n"I\'m not sure if you\'re'
  1977. >>> # watermarked generation
  1978. >>> watermarking_config = WatermarkingConfig(bias=2.5, context_width=2, seeding_scheme="selfhash")
  1979. >>> out = model.generate(inputs["input_ids"], watermarking_config=watermarking_config, max_length=20, do_sample=False)
  1980. >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
  1981. 'Alice and Bob are both still alive and well and the story is pretty much a one-hour adventure'
  1982. >>> # to detect watermarked text use the WatermarkDetector class
  1983. >>> from transformers import WatermarkDetector
  1984. >>> detector = WatermarkDetector(model_config=model.config, device="cpu", watermarking_config= watermarking_config)
  1985. >>> detection_preds = detector(out)
  1986. >>> detection_preds
  1987. array([ True])
  1988. ```
  1989. """
  1990. def __init__(
  1991. self,
  1992. vocab_size,
  1993. device,
  1994. greenlist_ratio: float = 0.25,
  1995. bias: float = 2.0,
  1996. hashing_key: int = 15485863,
  1997. seeding_scheme: str = "lefthash",
  1998. context_width: int = 1,
  1999. ):
  2000. if seeding_scheme not in ["selfhash", "lefthash"]:
  2001. raise ValueError(f"seeding_scheme has to be one of [`selfhash`, `lefthash`], but found {seeding_scheme}")
  2002. if greenlist_ratio >= 1.0 or greenlist_ratio <= 0.0:
  2003. raise ValueError(
  2004. f"greenlist_ratio has be in range between 0.0 and 1.0, exclusively. but found {greenlist_ratio}"
  2005. )
  2006. self.vocab_size = vocab_size
  2007. self.greenlist_size = int(self.vocab_size * greenlist_ratio)
  2008. self.bias = bias
  2009. self.seeding_scheme = seeding_scheme
  2010. self.rng = torch.Generator(device=device)
  2011. self.hash_key = hashing_key
  2012. self.context_width = context_width
  2013. self.rng.manual_seed(hashing_key)
  2014. self.table_size = 1_000_003
  2015. self.fixed_table = torch.randperm(self.table_size, generator=self.rng, device=device)
  2016. def set_seed(self, input_seq: torch.LongTensor):
  2017. input_seq = input_seq[-self.context_width :]
  2018. if self.seeding_scheme == "selfhash":
  2019. a = self.fixed_table[input_seq % self.table_size] + 1
  2020. b = self.fixed_table[input_seq[-1] % self.table_size] + 1
  2021. seed = (self.hash_key * a * b).min().item()
  2022. else:
  2023. seed = self.hash_key * input_seq[-1].item()
  2024. self.rng.manual_seed(seed % (2**64 - 1))
  2025. def _get_greenlist_ids(self, input_seq: torch.LongTensor) -> torch.LongTensor:
  2026. self.set_seed(input_seq)
  2027. vocab_permutation = torch.randperm(self.vocab_size, device=input_seq.device, generator=self.rng)
  2028. greenlist_ids = vocab_permutation[: self.greenlist_size]
  2029. return greenlist_ids
  2030. def _score_rejection_sampling(self, input_seq: torch.LongTensor, scores: torch.FloatTensor) -> torch.LongTensor:
  2031. """
  2032. Generate greenlist based on current candidate next token. Reject and move on if necessary.
  2033. Runs for a fixed number of steps only for efficiency, since the methods is not batched.
  2034. """
  2035. final_greenlist = []
  2036. _, greedy_predictions = scores.sort(dim=-1, descending=True)
  2037. # 40 is an arbitrary number chosen to save compute and not run for long (taken from orig repo)
  2038. for i in range(40):
  2039. greenlist_ids = self._get_greenlist_ids(torch.cat([input_seq, greedy_predictions[i, None]], dim=-1))
  2040. if greedy_predictions[i] in greenlist_ids:
  2041. final_greenlist.append(greedy_predictions[i])
  2042. return torch.tensor(final_greenlist, device=input_seq.device)
  2043. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  2044. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  2045. if input_ids.shape[-1] < self.context_width:
  2046. logger.warning(
  2047. f"`input_ids` should have at least `{self.context_width}` tokens but has {input_ids.shape[-1]}. "
  2048. "The seeding will be skipped for this generation step!"
  2049. )
  2050. return scores
  2051. scores_processed = scores.clone()
  2052. for b_idx, input_seq in enumerate(input_ids):
  2053. if self.seeding_scheme == "selfhash":
  2054. greenlist_ids = self._score_rejection_sampling(input_seq, scores[b_idx])
  2055. else:
  2056. greenlist_ids = self._get_greenlist_ids(input_seq)
  2057. scores_processed[b_idx, greenlist_ids] = scores_processed[b_idx, greenlist_ids] + self.bias
  2058. return scores_processed
  2059. class SynthIDTextWatermarkState:
  2060. """SynthID watermarking state."""
  2061. def __init__(
  2062. self,
  2063. batch_size: int,
  2064. ngram_len: int,
  2065. context_history_size: int,
  2066. device: torch.device,
  2067. ):
  2068. """Initializes the state.
  2069. Args:
  2070. batch_size (`int`): Batch size.
  2071. ngram_len (`int`): Ngram length.
  2072. context_history_size (`int`): Size of the tensor to keep track of seen contexts.
  2073. device (`int`): Device to use.
  2074. """
  2075. self.context = torch.zeros(
  2076. (batch_size, ngram_len - 1),
  2077. dtype=torch.int64,
  2078. device=device,
  2079. )
  2080. self.context_history = torch.zeros(
  2081. (batch_size, context_history_size),
  2082. dtype=torch.int64,
  2083. device=device,
  2084. )
  2085. self.num_calls = 0
  2086. class SynthIDTextWatermarkLogitsProcessor(LogitsProcessor):
  2087. r"""
  2088. Logits processor that implements watermarking techniques for text generation models.
  2089. This class facilitates the application of SynthID text watermarking, a method for embedding imperceptible signals
  2090. into generated text to aid in detecting synthetic content. It operates by subtly manipulating the probabilities of
  2091. token selection during text generation in a manner that can be reliably recovered later for verification.
  2092. Key Features:
  2093. * **State Management:** Maintains internal state to track token sequences and generate watermarking keys
  2094. dynamically.
  2095. * **Key Generation:** Computes hashes based on token sequences and watermarking parameters to create unique keys
  2096. for each position.
  2097. * **G-Value Sampling:** Employs a pre-computed sampling table to sample watermarking values (g-values) based on
  2098. the generated keys.
  2099. * **Score Adjustment:** Applies calculated g-values to modify token probabilities during generation, embedding the
  2100. watermark.
  2101. * **Context Repetition Handling:** Incorporates logic to avoid watermarking tokens in repeated contexts,
  2102. preserving naturalness.
  2103. * **EOS Token Masking:** Supports masking end-of-sentence tokens to prevent their inclusion in watermarking
  2104. calculations.
  2105. * **Utility Functions:** Provides functions to compute g-values directly, check for context repetition, create
  2106. EOS token masks, and estimate expected mean g-values.
  2107. Refer to paper url: https://www.nature.com/articles/s41586-024-08025-4 for more details around this.
  2108. Args:
  2109. ngram_len (`int`):
  2110. Ngram length.
  2111. keys (`list[int]`):
  2112. A sequence of watermarking keys, one for each depth.
  2113. sampling_table_size (`int`):
  2114. Size of the sampling table.
  2115. sampling_table_seed (`int`):
  2116. Random seed to generate the sampling table.
  2117. context_history_size (`int`):
  2118. Size of the tensor to keep track of seen contexts.
  2119. device (`torch.device`):
  2120. Device to use.
  2121. skip_first_ngram_calls (`bool`, *optional*, defaults to `False`):
  2122. Whether to skip first ngram calls.
  2123. debug_mode (`bool`, optional, *optional*, defaults to `False`):
  2124. Logits are modified to uniform one got before watermarking modification is applied. This is to test the
  2125. implementation.
  2126. Examples:
  2127. ```python
  2128. >>> from transformers import AutoModelForCausalLM, AutoTokenizer, SynthIDTextWatermarkingConfig
  2129. >>> tokenizer = AutoTokenizer.from_pretrained('google/gemma-2-2b', padding_side="left")
  2130. >>> model = AutoModelForCausalLM.from_pretrained('google/gemma-2-2b')
  2131. >>> # SynthID Text configuration
  2132. >>> watermarking_config = SynthIDTextWatermarkingConfig(
  2133. ... keys=[654, 400, 836, 123, 340, 443, 597, 160, 57],
  2134. ... ngram_len=5,
  2135. ... )
  2136. >>> # Generation with watermarking
  2137. >>> tokenized_prompts = tokenizer(["Once upon a time, "], return_tensors="pt", padding=True)
  2138. >>> output_sequences = model.generate(
  2139. ... **tokenized_prompts, watermarking_config=watermarking_config, do_sample=True, max_new_tokens=10
  2140. ... )
  2141. >>> watermarked_text = tokenizer.batch_decode(output_sequences, skip_special_tokens=True)
  2142. ```
  2143. """
  2144. def __init__(
  2145. self,
  2146. ngram_len: int,
  2147. keys: list[int],
  2148. sampling_table_size: int,
  2149. sampling_table_seed: int,
  2150. context_history_size: int,
  2151. device: torch.device,
  2152. skip_first_ngram_calls: bool = False,
  2153. debug_mode: bool = False,
  2154. ):
  2155. self.ngram_len = ngram_len
  2156. self.keys = torch.tensor(keys, device=device)
  2157. generator = torch.Generator(device=device).manual_seed(sampling_table_seed)
  2158. # A random sampling table is pre-computed and modulo table size is applied to map from a hash of ngram keys to
  2159. # g values, this is similar to the hashtable implementation used in
  2160. # https://github.com/facebookresearch/three_bricks. We note that the hashing employed in this repository is
  2161. # different from that used to watermark the Gemini App, and hence the detectors trained based on the
  2162. # hashing in this repository will not transfer to text generated by the Gemini App.
  2163. self.sampling_table = torch.randint(
  2164. low=0,
  2165. high=2,
  2166. size=(sampling_table_size,),
  2167. generator=generator,
  2168. device=device,
  2169. )
  2170. self.context_history_size = context_history_size
  2171. self.device = device
  2172. self.state = None
  2173. self.skip_first_ngram_calls = skip_first_ngram_calls
  2174. self.debug_mode = debug_mode
  2175. def _init_state(self, batch_size: int):
  2176. """Initializes the state."""
  2177. self.state = SynthIDTextWatermarkState(
  2178. batch_size=batch_size,
  2179. ngram_len=self.ngram_len,
  2180. context_history_size=self.context_history_size,
  2181. device=self.device,
  2182. )
  2183. def update_scores(self, scores: torch.FloatTensor, g_values: torch.FloatTensor) -> torch.FloatTensor:
  2184. """Updates scores using the g values.
  2185. We assume that the scores are in the log space.
  2186. Args:
  2187. scores (`torch.FloatTensor`): Scores (batch_size, vocab_size).
  2188. g_values (`torch.FloatTensor`): G values (batch_size, vocab_size, depth).
  2189. Returns:
  2190. Updated scores (batch_size, vocab_size).
  2191. """
  2192. _, _, depth = g_values.shape
  2193. probs = torch.softmax(scores, dim=1)
  2194. for i in range(depth):
  2195. g_values_at_depth = g_values[:, :, i]
  2196. g_mass_at_depth = (g_values_at_depth * probs).sum(axis=1, keepdims=True)
  2197. probs = probs * (1 + g_values_at_depth - g_mass_at_depth)
  2198. log_probs = torch.log(probs)
  2199. log_probs = torch.where(torch.isfinite(log_probs), log_probs, torch.finfo(log_probs.dtype).min)
  2200. return log_probs
  2201. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  2202. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  2203. self._check_input_ids_shape(input_ids)
  2204. batch_size, vocab_size = scores.shape
  2205. if self.debug_mode:
  2206. scores = torch.ones_like(scores)
  2207. # Currently indices is just a arange to compute watermarking on the dense logits.
  2208. all_indices = torch.stack([torch.arange(vocab_size, device=self.device) for _ in range(batch_size)])
  2209. if self.state is None:
  2210. # Initialize watermarking state if it does not exist.
  2211. self._init_state(batch_size)
  2212. else:
  2213. # Append last input id (which is the input id added in last call) to the
  2214. # previous context so we have the context to be used for current
  2215. # watermarking.
  2216. self.state.context = torch.concat(
  2217. (self.state.context, input_ids[:, -1:]),
  2218. dim=1,
  2219. )
  2220. self.state.context = self.state.context[:, 1:]
  2221. if self.state is None:
  2222. raise ValueError("self.state can't be None! Call `self._init_state` to initialize the state.")
  2223. self.state.num_calls += 1
  2224. # Don't watermark the first ngram_len - 1 tokens if set.
  2225. if self.skip_first_ngram_calls and self.state.num_calls < self.ngram_len:
  2226. return scores
  2227. # 2. Generate random keys for each ngram key combination.
  2228. ngram_keys, hash_result_with_just_context = self._compute_keys(self.state.context, all_indices)
  2229. # ngram_keys shape [batch_size, top_k, depth]
  2230. # 3. Sample g values.
  2231. g_values = self.sample_g_values(ngram_keys)
  2232. # g_values shape [batch_size, top_k, depth]
  2233. # 4. Modify scores.
  2234. updated_scores = self.update_scores(scores, g_values)
  2235. # updated scores shape [batch_size, top_k]
  2236. # 5. Check if the current watermarking context was previously used, if yes skip watermarking.
  2237. hash_result_with_just_context = hash_result_with_just_context[:, None]
  2238. is_repeated_context = (self.state.context_history == hash_result_with_just_context).any(
  2239. dim=1,
  2240. keepdim=True,
  2241. )
  2242. self.state.context_history = torch.concat(
  2243. (hash_result_with_just_context, self.state.context_history),
  2244. dim=1,
  2245. )[:, :-1]
  2246. updated_watermarked_scores = torch.where(
  2247. is_repeated_context,
  2248. input=scores,
  2249. other=updated_scores,
  2250. )
  2251. return updated_watermarked_scores
  2252. def accumulate_hash(
  2253. self,
  2254. current_hash: torch.LongTensor,
  2255. data: torch.LongTensor,
  2256. multiplier: int = 6364136223846793005,
  2257. increment: int = 1,
  2258. ) -> torch.LongTensor:
  2259. """
  2260. Accumulate hash of data on current hash.
  2261. Method uses adapted linear congruential generator with newlib/musl parameters.
  2262. This function has following property -
  2263. f(x, data[T]) = f(f(x, data[:T - 1]), data[T])
  2264. This function expects current_hash.shape and data.shape[:-1] to
  2265. match/broadcastable.
  2266. Args:
  2267. current_hash (`torch.LongTensor`):
  2268. (shape,)
  2269. data (`torch.LongTensor`):
  2270. (shape, tensor_len)
  2271. multiplier (`int`, optional, *optional*, defaults to 6364136223846793005):
  2272. multiplier of linear congruential generator
  2273. increment (`int`, optional, *optional*, defaults to 1):
  2274. increment of linear congruential generator
  2275. Returns:
  2276. updated hash (shape,)
  2277. """
  2278. for i in range(data.shape[-1]):
  2279. current_hash = torch.add(current_hash, data[..., i])
  2280. current_hash = torch.mul(current_hash, multiplier)
  2281. current_hash = torch.add(current_hash, increment)
  2282. return current_hash
  2283. def compute_ngram_keys(self, ngrams: torch.LongTensor) -> torch.LongTensor:
  2284. """Computes random keys for each ngram and depth.
  2285. Args:
  2286. ngrams (`torch.LongTensor`):
  2287. Ngrams (batch_size, num_ngrams, ngram_len).
  2288. Returns:
  2289. ngram keys (batch_size, num_ngrams, depth).
  2290. """
  2291. if len(ngrams.shape) != 3:
  2292. raise ValueError(f"Ngrams should be of shape (batch_size, num_ngrams, ngram_len), but is {ngrams.shape}")
  2293. if ngrams.shape[2] != self.ngram_len:
  2294. raise ValueError(
  2295. "Ngrams should be of shape (batch_size, num_ngrams, ngram_len),"
  2296. f" where ngram_len is {self.ngram_len}, but is {ngrams.shape}"
  2297. )
  2298. batch_size, _, _ = ngrams.shape
  2299. hash_result = torch.ones(batch_size, device=self.device, dtype=torch.long)
  2300. # hash_result shape [batch_size,]
  2301. # ngrams shape [batch_size, num_ngrams, ngram_len]
  2302. hash_result = torch.vmap(self.accumulate_hash, in_dims=(None, 1), out_dims=1)(hash_result, ngrams)
  2303. # hash_result shape [batch_size, num_ngrams]
  2304. keys = self.keys[None, None, :, None]
  2305. # hash_result shape [batch_size, num_ngrams]
  2306. # keys shape [1, 1, depth, 1]
  2307. hash_result = torch.vmap(self.accumulate_hash, in_dims=(None, 2), out_dims=2)(hash_result, keys)
  2308. # hash_result shape [batch_size, num_ngrams, depth]
  2309. return hash_result
  2310. def _compute_keys(
  2311. self, n_minus_1_grams: torch.LongTensor, indices: torch.LongTensor
  2312. ) -> tuple[torch.LongTensor, torch.LongTensor]:
  2313. """Computes random keys for each ngram and depth.
  2314. Args:
  2315. n_minus_1_grams (`torch.LongTensor`):
  2316. Ngrams (batch_size, ngram_len - 1).
  2317. indices (`torch.LongTensor`):
  2318. indices of the continuations (batch_size, num_indices)
  2319. Returns:
  2320. Ngram keys (batch_size, num_indices, depth).
  2321. """
  2322. batch_size, _ = n_minus_1_grams.shape
  2323. hash_result = torch.ones(batch_size, device=self.device, dtype=torch.long)
  2324. # First hash n_minus_1 gram, for each batch entry we have a single
  2325. # n_minus_1 gram context.
  2326. # hash_result shape [batch_size]
  2327. # n_minus_1_gram shape [batch_size, ngram_len - 1]
  2328. hash_result_with_just_context = self.accumulate_hash(hash_result, n_minus_1_grams)
  2329. # hash_result shape [batch_size,]
  2330. # Indices is of shape [batch_size, num_indices], so we make it
  2331. # [batch_size, num_indices, 1] so we can vmap over num_indices dim.
  2332. hash_result = torch.vmap(self.accumulate_hash, in_dims=(None, 1), out_dims=1)(
  2333. hash_result_with_just_context, indices[:, :, None]
  2334. )
  2335. # hash_result shape [batch_size, num_indices]
  2336. # Basically we have a hash for each batch entry and each indices
  2337. # Now we add watermarking keys to this hash.
  2338. # keys are of shape [depth,]
  2339. # We add batch, num_indices and data dimension to this making it
  2340. # [1, 1, depth, 1].
  2341. # So we can vmap over the depth dimension for compute_hash
  2342. keys = self.keys[None, None, :, None]
  2343. hash_result = torch.vmap(self.accumulate_hash, in_dims=(None, 2), out_dims=2)(hash_result, keys)
  2344. # hash_result shape should be [batch_size, num_indices, depth]
  2345. return hash_result, hash_result_with_just_context
  2346. def sample_g_values(self, ngram_keys: torch.LongTensor) -> torch.LongTensor:
  2347. """
  2348. Samples g values from Bernoulli distribution.
  2349. It is not possible to pass random keys in a vectorized way in torch. Instead
  2350. we pre-compute a random sampling table, and use apply modulo table size to
  2351. map from ngram keys (int64) to g values.
  2352. Args:
  2353. ngram_keys (`torch.LongTensor`):
  2354. Random keys (batch_size, num_ngrams, depth).
  2355. Returns:
  2356. G values (batch_size, num_ngrams, depth).
  2357. """
  2358. (sampling_table_size,) = self.sampling_table.shape
  2359. sampling_table = self.sampling_table.reshape((1, 1, sampling_table_size))
  2360. ngram_keys = ngram_keys % sampling_table_size
  2361. return torch.take_along_dim(sampling_table, indices=ngram_keys, dim=2)
  2362. def _check_input_ids_shape(self, input_ids: torch.LongTensor):
  2363. """Checks the shape of input ids."""
  2364. if len(input_ids.shape) != 2:
  2365. raise ValueError(f"Input ids should be of shape (batch_size, input_len), but is {input_ids.shape}")
  2366. def compute_g_values(self, input_ids: torch.LongTensor) -> torch.LongTensor:
  2367. """
  2368. Computes g values for each ngram from the given sequence of tokens.
  2369. Args:
  2370. input_ids (`torch.LongTensor`):
  2371. Input token ids (batch_size, input_len).
  2372. Returns:
  2373. G values (batch_size, input_len - (ngram_len - 1), depth).
  2374. """
  2375. self._check_input_ids_shape(input_ids)
  2376. ngrams = input_ids.unfold(dimension=1, size=self.ngram_len, step=1)
  2377. ngram_keys = self.compute_ngram_keys(ngrams)
  2378. return self.sample_g_values(ngram_keys)
  2379. def compute_context_repetition_mask(self, input_ids: torch.LongTensor) -> torch.LongTensor:
  2380. """
  2381. Computes repetition mask.
  2382. 0 and 1 stand for repeated and not repeated context n-1 grams respectively.
  2383. Args:
  2384. input_ids (`torch.LongTensor`):
  2385. Input token ids (batch_size, input_len).
  2386. Returns:
  2387. Repetitions mask (batch_size, input_len - (ngram_len - 1)).
  2388. """
  2389. self._check_input_ids_shape(input_ids)
  2390. batch_size, _ = input_ids.shape
  2391. state = SynthIDTextWatermarkState(
  2392. batch_size=batch_size,
  2393. ngram_len=self.ngram_len,
  2394. context_history_size=self.context_history_size,
  2395. device=self.device,
  2396. )
  2397. contexts = input_ids[:, :-1].unfold(
  2398. dimension=1,
  2399. size=self.ngram_len - 1,
  2400. step=1,
  2401. )
  2402. _, num_contexts, _ = contexts.shape
  2403. are_repeated_contexts = []
  2404. for i in range(num_contexts):
  2405. context = contexts[:, i, :]
  2406. hash_result = torch.ones(batch_size, device=self.device, dtype=torch.long)
  2407. context_hash = self.accumulate_hash(hash_result, context)[:, None]
  2408. is_repeated_context = (state.context_history == context_hash).any(
  2409. dim=1,
  2410. keepdim=True,
  2411. )
  2412. are_repeated_contexts.append(is_repeated_context)
  2413. state.context_history = torch.concat(
  2414. (context_hash, state.context_history),
  2415. dim=1,
  2416. )[:, :-1]
  2417. are_repeated_contexts = torch.concat(are_repeated_contexts, dim=1)
  2418. return torch.logical_not(are_repeated_contexts)
  2419. def compute_eos_token_mask(self, input_ids: torch.LongTensor, eos_token_id: int) -> torch.LongTensor:
  2420. """
  2421. Computes repetitions mask.
  2422. 1 stands for ngrams that don't contain EOS tokens and vice versa.
  2423. Args:
  2424. input_ids (`torch.LongTensor`):
  2425. Input token ids (batch_size, input_len).
  2426. eos_token_id (`int`):
  2427. EOS token ID.
  2428. Returns:
  2429. EOS token mask (batch_size, input_len).
  2430. """
  2431. self._check_input_ids_shape(input_ids)
  2432. noneos_masks = []
  2433. all_eos_equated = input_ids == eos_token_id
  2434. for eos_equated in all_eos_equated:
  2435. nonzero_idx = torch.nonzero(eos_equated)
  2436. noneos_mask = torch.ones_like(eos_equated)
  2437. if nonzero_idx.shape[0] != 0:
  2438. noneos_mask[nonzero_idx[0][0] :] = 0
  2439. noneos_masks.append(noneos_mask)
  2440. return torch.stack(noneos_masks, dim=0)
  2441. def expected_mean_g_value(self, vocab_size: int, coinflip_prob: float = 0.5) -> float:
  2442. """
  2443. Compute expected mean g-value after watermarking, assuming uniform LM dist.
  2444. This is the theoretical expected value for single-layer watermarking.
  2445. Args:
  2446. vocab_size (`int`):
  2447. The size of the vocabulary.
  2448. coinflip_prob arg_name (`float`, *optional*, defaults to 0.5):
  2449. Probability of 1 in boolean prf.
  2450. Returns:
  2451. The expected mean g-value for watermarked text.
  2452. """
  2453. return coinflip_prob + coinflip_prob * (1 - coinflip_prob) * (1 - (1 / vocab_size))
  2454. class DiaClassifierFreeGuidanceLogitsProcessor(LogitsProcessor):
  2455. r"""
  2456. [`LogitsProcessor`] for classifier free guidance (CFG). Similar to the original
  2457. `ClassifierFreeGuidanceLogitsProcessor` with some modifications on the overall
  2458. calculation, e.g. conditioned logits centered, and an additional top k selection
  2459. option.
  2460. <Tip warning={true}>
  2461. This logits processor is exclusively compatible with
  2462. [Dia](https://huggingface.co/docs/transformers/main/en/model_doc/dia)
  2463. </Tip>
  2464. Args:
  2465. guidance_scale (float):
  2466. The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`.
  2467. Higher guidance scale encourages the model to generate samples that are more closely linked to the input
  2468. prompt, usually at the expense of poorer quality.
  2469. guidance_top_k (int, *optional*):
  2470. The number of highest probability vocabulary tokens to keep for top-k-filtering. However, we do not keep
  2471. the logits of the combined CFG output, but the conditioned output only.
  2472. """
  2473. def __init__(self, guidance_scale: float, guidance_top_k: Optional[int] = None):
  2474. if guidance_scale > 1:
  2475. self.guidance_scale = guidance_scale
  2476. else:
  2477. raise ValueError(
  2478. "Require guidance scale >1 to use the classifier free guidance processor, got guidance scale "
  2479. f"{guidance_scale}."
  2480. )
  2481. self.guidance_top_k = guidance_top_k
  2482. if self.guidance_top_k is not None and self.guidance_top_k < 1:
  2483. raise ValueError(
  2484. f"`guidance_top_k` has to be a strictly positive integer if given, but is {self.guidance_top_k}"
  2485. )
  2486. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  2487. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  2488. # simple check to make sure we have compatible batch sizes between our
  2489. # logits scores (cond + uncond) and input ids (cond only)
  2490. if scores.shape[0] != 2 * input_ids.shape[0]:
  2491. raise ValueError(
  2492. f"Logits should have twice the batch size of the input ids, the first half of batches corresponding to "
  2493. f"the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got "
  2494. f"batch size {scores.shape[0]} for the logits and {input_ids.shape[0]} for the input ids."
  2495. )
  2496. # Base CFG with center on cond_logits
  2497. unguided_bsz = scores.shape[0] // 2
  2498. cond_logits, uncond_logits = scores.split(unguided_bsz, dim=0)
  2499. scores_processed = cond_logits + (cond_logits - uncond_logits) * self.guidance_scale
  2500. # Optional CFG top k filtering
  2501. if self.guidance_top_k is not None:
  2502. # Create top k based on the combined CFG output
  2503. _, top_k_indices = torch.topk(scores_processed, k=self.guidance_top_k, dim=-1)
  2504. top_k_mask = torch.ones_like(scores_processed, dtype=torch.bool)
  2505. top_k_mask = top_k_mask.scatter(dim=-1, index=top_k_indices, value=False)
  2506. # Only return conditioned logits with top k
  2507. scores_processed = cond_logits.masked_fill(top_k_mask, -float("inf"))
  2508. return scores_processed
  2509. class DiaEOSChannelFilterLogitsProcessor(LogitsProcessor):
  2510. r"""Specialized processor that ensures certain properties around EOS sampling:
  2511. 1. Only channel 0 can generate EOS
  2512. 2. If channel 0 has EOS with highest logit, it will be the only candidate
  2513. 3. If channel 0 has EOS not with highest logit, it will be suppressed
  2514. 2. and 3. are especially important in contexts where we allow sampling to guarantee the
  2515. respective tokens to be (not) sampled.
  2516. <Tip warning={true}>
  2517. This logits processor is exclusively compatible with
  2518. [Dia](https://huggingface.co/docs/transformers/en/model_doc/dia).
  2519. </Tip>
  2520. Args:
  2521. num_channels (`int`):
  2522. Number of audio codebooks. Simplifies access to the first channel on the logits.
  2523. eos_token_id (`int`):
  2524. The id of *end-of-sequence* token.
  2525. """
  2526. def __init__(self, num_channels: int, eos_token_id: int):
  2527. if num_channels < 1:
  2528. raise ValueError(f"Audio codebooks need at least one channel, but found {num_channels} channels.")
  2529. if eos_token_id < 1:
  2530. raise ValueError(f"Expected `eos_token_id` to be a positive integer, found {eos_token_id} instead.")
  2531. self.num_channels = num_channels
  2532. self.eos_id = eos_token_id
  2533. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  2534. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  2535. # Reshape for easier channel indexing [B, C, V]
  2536. scores = scores.reshape(-1, self.num_channels, scores.shape[-1])
  2537. # EOS filter
  2538. # 1. Condition: Only the first channel can generate the EOS token
  2539. # Side condition of disabling generation of special tokens (e.g. audio pad, bos, ...)
  2540. # (Assumes them to be greater than audio eos token position)
  2541. scores[:, 1:, self.eos_id :] = torch.full_like(
  2542. scores[:, 1:, self.eos_id :],
  2543. fill_value=-float("inf"),
  2544. )
  2545. scores[:, 0, self.eos_id + 1 :] = torch.full_like(
  2546. scores[:, 0, self.eos_id + 1 :],
  2547. fill_value=-float("inf"),
  2548. )
  2549. # 2+3 Conditions: Force/Suppress EOS if (not) highest logit
  2550. # Reshape back to original shape
  2551. scores = scores.view(-1, scores.shape[-1])
  2552. # Sample highest tokens
  2553. top_logit_indices = torch.argmax(scores, dim=-1)
  2554. # 2. Force EOS
  2555. eos_highest_mask = top_logit_indices == self.eos_id
  2556. mask_eos_highest = torch.zeros_like(scores, dtype=torch.bool)
  2557. mask_eos_highest[eos_highest_mask, : self.eos_id] = True
  2558. scores = scores.masked_fill(mask_eos_highest, -float("inf"))
  2559. # 3. Suppress EOS
  2560. eos_not_highest_mask = top_logit_indices != self.eos_id
  2561. mask_eos_unless_highest = torch.zeros_like(scores, dtype=torch.bool)
  2562. mask_eos_unless_highest[eos_not_highest_mask, self.eos_id] = True
  2563. scores = scores.masked_fill(mask_eos_unless_highest, -float("inf"))
  2564. return scores
  2565. class DiaEOSDelayPatternLogitsProcessor(LogitsProcessor):
  2566. r"""Special logits processor to handle the generation of the EOS token in Dia.
  2567. This is due to the fact that Dia does not allow the generation of EOS in all
  2568. channels except the first channel (C0).
  2569. Hence, based on the delay pattern, an EOS is forced after the respective delays
  2570. in the channels. For example, if the delay pattern is [0, 2, 3, 4]:
  2571. s s+1 s+2 s+3 s+4 s+5 ...
  2572. | | | | | |
  2573. C0: EOS PAD PAD PAD PAD PAD ...
  2574. C1: x x EOS PAD PAD PAD ...
  2575. C2: x x x EOS PAD PAD ...
  2576. C3: x x x x EOS PAD ...
  2577. If the first channel generated EOS at step s, channels Cx are forced to generate
  2578. theirs at the respective delays (s+2, s+3, s+4). Subsequent padding tokens are
  2579. handled by the `EosTokenCriteria` when an EOS has been detected.
  2580. <Tip warning={true}>
  2581. This logits processor is exclusively compatible with
  2582. [Dia](https://huggingface.co/docs/transformers/en/model_doc/dia).
  2583. </Tip>
  2584. Args:
  2585. delay_pattern (`List[int]`):
  2586. The delays per channel in the audio codebooks.
  2587. eos_token_id (`int`):
  2588. The id of *end-of-sequence* token.
  2589. max_generation_len (`int`):
  2590. The max sequence length that can be generated.
  2591. device (`str`, *optional*, defaults to `"cpu"`):
  2592. The device to allocate the tensors on.
  2593. """
  2594. def __init__(self, delay_pattern: list[int], eos_token_id: int, max_generation_len: int, device: str = "cpu"):
  2595. self.num_channels = len(delay_pattern)
  2596. # Update during first iteration
  2597. self.active_batches = None
  2598. self.delay_pattern = torch.tensor(delay_pattern, device=device, dtype=torch.int)[None, :]
  2599. self.eos_token_id = eos_token_id
  2600. self.max_generation_len = max_generation_len - max(delay_pattern) - 1
  2601. self.device = device
  2602. @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
  2603. def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
  2604. # Reshape for easier channel indexing [B, C, V]
  2605. scores = scores.reshape(-1, self.num_channels, scores.shape[-1])
  2606. # Initialize / expand values on first iteration
  2607. if self.active_batches is None:
  2608. self.delay_pattern = self.delay_pattern.repeat(scores.shape[0], 1)
  2609. self.active_batches = torch.zeros(size=(scores.shape[0],), device=self.device, dtype=torch.bool)
  2610. # Check if eos has been generated in any batch
  2611. channel_generated_eos = torch.argmax(scores, dim=-1)[:, 0] == self.eos_token_id
  2612. # Check if max len has been reached
  2613. reached_max_len = input_ids.shape[1] == self.max_generation_len
  2614. # Update active batches
  2615. self.active_batches |= channel_generated_eos
  2616. self.active_batches |= reached_max_len
  2617. # Find channels that need to force eos
  2618. forced_eos_channels = self.active_batches[:, None] & (self.delay_pattern == 0)
  2619. # Use indexing to avoid issues on all `False` by having empty tensors in that case
  2620. idx_bsz, idx_channel = forced_eos_channels.nonzero(as_tuple=True)
  2621. # Force eos if delay is kicking in
  2622. scores[idx_bsz, idx_channel, :] = -float("inf")
  2623. scores[idx_bsz, idx_channel, self.eos_token_id] = 0.0
  2624. # Reshape back to [B * C, V]
  2625. scores = scores.reshape(-1, scores.shape[-1])
  2626. # Update amount of delay left for each channel
  2627. self.delay_pattern -= self.active_batches[:, None].int()
  2628. return scores