tokenization_utils_base.py 213 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361
  1. # coding=utf-8
  2. # Copyright 2020 The HuggingFace Inc. team.
  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. """
  16. Base classes common to both the slow and the fast tokenization classes: PreTrainedTokenizerBase (host all the user
  17. fronting encoding methods) Special token mixing (host the special tokens logic) and BatchEncoding (wrap the dictionary
  18. of output with special method for the Fast tokenizers)
  19. """
  20. import copy
  21. import json
  22. import os
  23. import re
  24. import warnings
  25. from collections import UserDict
  26. from collections.abc import Mapping, Sequence, Sized
  27. from contextlib import contextmanager
  28. from dataclasses import dataclass
  29. from pathlib import Path
  30. from typing import TYPE_CHECKING, Any, Callable, NamedTuple, Optional, Union
  31. import numpy as np
  32. from huggingface_hub import list_repo_files
  33. from packaging import version
  34. from . import __version__
  35. from .dynamic_module_utils import custom_object_save
  36. from .utils import (
  37. CHAT_TEMPLATE_DIR,
  38. CHAT_TEMPLATE_FILE,
  39. ExplicitEnum,
  40. PaddingStrategy,
  41. PushToHubMixin,
  42. TensorType,
  43. add_end_docstrings,
  44. cached_file,
  45. copy_func,
  46. download_url,
  47. extract_commit_hash,
  48. is_flax_available,
  49. is_jax_tensor,
  50. is_mlx_available,
  51. is_numpy_array,
  52. is_offline_mode,
  53. is_protobuf_available,
  54. is_remote_url,
  55. is_tf_available,
  56. is_tf_tensor,
  57. is_tokenizers_available,
  58. is_torch_available,
  59. is_torch_device,
  60. is_torch_tensor,
  61. list_repo_templates,
  62. logging,
  63. requires_backends,
  64. to_py_obj,
  65. )
  66. from .utils.chat_template_utils import render_jinja_template
  67. from .utils.import_utils import PROTOBUF_IMPORT_ERROR
  68. if TYPE_CHECKING:
  69. if is_torch_available():
  70. import torch
  71. if is_tf_available():
  72. import tensorflow as tf
  73. if is_flax_available():
  74. import jax.numpy as jnp # noqa: F401
  75. def import_protobuf_decode_error(error_message=""):
  76. if is_protobuf_available():
  77. from google.protobuf.message import DecodeError
  78. return DecodeError
  79. else:
  80. raise ImportError(PROTOBUF_IMPORT_ERROR.format(error_message))
  81. def flatten(arr: list):
  82. res = []
  83. if len(arr) > 0:
  84. for sub_arr in arr:
  85. if isinstance(arr[0], (list, tuple)):
  86. res.extend(flatten(sub_arr))
  87. else:
  88. res.append(sub_arr)
  89. return res
  90. if is_tokenizers_available() or TYPE_CHECKING:
  91. from tokenizers import Encoding as EncodingFast
  92. if is_tokenizers_available():
  93. from tokenizers import AddedToken
  94. else:
  95. @dataclass(frozen=False, eq=True)
  96. class AddedToken:
  97. """
  98. AddedToken represents a token to be added to a Tokenizer An AddedToken can have special options defining the
  99. way it should behave.
  100. The `normalized` will default to `not special` if it is not specified, similarly to the definition in
  101. `tokenizers`.
  102. """
  103. def __init__(
  104. self, content: str, single_word=False, lstrip=False, rstrip=False, special=False, normalized=None
  105. ):
  106. self.content = content
  107. self.single_word = single_word
  108. self.lstrip = lstrip
  109. self.rstrip = rstrip
  110. self.special = special
  111. self.normalized = normalized if normalized is not None else not special
  112. def __getstate__(self):
  113. return self.__dict__
  114. def __str__(self):
  115. return self.content
  116. logger = logging.get_logger(__name__)
  117. VERY_LARGE_INTEGER = int(1e30) # This is used to set the max input length for a model with infinite size input
  118. LARGE_INTEGER = int(1e20) # This is used when we need something big but slightly smaller than VERY_LARGE_INTEGER
  119. # Define type aliases and NamedTuples
  120. TextInput = str
  121. PreTokenizedInput = list[str]
  122. EncodedInput = list[int]
  123. TextInputPair = tuple[str, str]
  124. PreTokenizedInputPair = tuple[list[str], list[str]]
  125. EncodedInputPair = tuple[list[int], list[int]]
  126. # Define type aliases for text-related non-text modalities
  127. AudioInput = Union[np.ndarray, "torch.Tensor", list[np.ndarray], list["torch.Tensor"]]
  128. # Slow tokenizers used to be saved in three separated files
  129. SPECIAL_TOKENS_MAP_FILE = "special_tokens_map.json"
  130. ADDED_TOKENS_FILE = "added_tokens.json"
  131. TOKENIZER_CONFIG_FILE = "tokenizer_config.json"
  132. # Fast tokenizers (provided by HuggingFace tokenizer's library) can be saved in a single file
  133. FULL_TOKENIZER_FILE = "tokenizer.json"
  134. _re_tokenizer_file = re.compile(r"tokenizer\.(.*)\.json")
  135. class TruncationStrategy(ExplicitEnum):
  136. """
  137. Possible values for the `truncation` argument in [`PreTrainedTokenizerBase.__call__`]. Useful for tab-completion in
  138. an IDE.
  139. """
  140. ONLY_FIRST = "only_first"
  141. ONLY_SECOND = "only_second"
  142. LONGEST_FIRST = "longest_first"
  143. DO_NOT_TRUNCATE = "do_not_truncate"
  144. class CharSpan(NamedTuple):
  145. """
  146. Character span in the original string.
  147. Args:
  148. start (`int`): Index of the first character in the original string.
  149. end (`int`): Index of the character following the last character in the original string.
  150. """
  151. start: int
  152. end: int
  153. class TokenSpan(NamedTuple):
  154. """
  155. Token span in an encoded string (list of tokens).
  156. Args:
  157. start (`int`): Index of the first token in the span.
  158. end (`int`): Index of the token following the last token in the span.
  159. """
  160. start: int
  161. end: int
  162. class BatchEncoding(UserDict):
  163. """
  164. Holds the output of the [`~tokenization_utils_base.PreTrainedTokenizerBase.__call__`],
  165. [`~tokenization_utils_base.PreTrainedTokenizerBase.encode_plus`] and
  166. [`~tokenization_utils_base.PreTrainedTokenizerBase.batch_encode_plus`] methods (tokens, attention_masks, etc).
  167. This class is derived from a python dictionary and can be used as a dictionary. In addition, this class exposes
  168. utility methods to map from word/character space to token space.
  169. Args:
  170. data (`dict`, *optional*):
  171. Dictionary of lists/arrays/tensors returned by the `__call__`/`encode_plus`/`batch_encode_plus` methods
  172. ('input_ids', 'attention_mask', etc.).
  173. encoding (`tokenizers.Encoding` or `Sequence[tokenizers.Encoding]`, *optional*):
  174. If the tokenizer is a fast tokenizer which outputs additional information like mapping from word/character
  175. space to token space the `tokenizers.Encoding` instance or list of instance (for batches) hold this
  176. information.
  177. tensor_type (`Union[None, str, TensorType]`, *optional*):
  178. You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at
  179. initialization.
  180. prepend_batch_axis (`bool`, *optional*, defaults to `False`):
  181. Whether or not to add a batch axis when converting to tensors (see `tensor_type` above). Note that this
  182. parameter has an effect if the parameter `tensor_type` is set, *otherwise has no effect*.
  183. n_sequences (`Optional[int]`, *optional*):
  184. You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at
  185. initialization.
  186. """
  187. def __init__(
  188. self,
  189. data: Optional[dict[str, Any]] = None,
  190. encoding: Optional[Union[EncodingFast, Sequence[EncodingFast]]] = None,
  191. tensor_type: Union[None, str, TensorType] = None,
  192. prepend_batch_axis: bool = False,
  193. n_sequences: Optional[int] = None,
  194. ):
  195. super().__init__(data)
  196. # If encoding is not None, the fast tokenization is used
  197. if encoding is not None and isinstance(encoding, EncodingFast):
  198. encoding = [encoding]
  199. self._encodings = encoding
  200. if n_sequences is None and encoding is not None and encoding:
  201. n_sequences = encoding[0].n_sequences
  202. self._n_sequences = n_sequences
  203. self.convert_to_tensors(tensor_type=tensor_type, prepend_batch_axis=prepend_batch_axis)
  204. @property
  205. def n_sequences(self) -> Optional[int]:
  206. """
  207. `Optional[int]`: The number of sequences used to generate each sample from the batch encoded in this
  208. [`BatchEncoding`]. Currently can be one of `None` (unknown), `1` (a single sentence) or `2` (a pair of
  209. sentences)
  210. """
  211. return self._n_sequences
  212. @property
  213. def is_fast(self) -> bool:
  214. """
  215. `bool`: Indicate whether this [`BatchEncoding`] was generated from the result of a [`PreTrainedTokenizerFast`]
  216. or not.
  217. """
  218. return self._encodings is not None
  219. def __getitem__(self, item: Union[int, str]) -> Union[Any, EncodingFast]:
  220. """
  221. If the key is a string, returns the value of the dict associated to `key` ('input_ids', 'attention_mask',
  222. etc.).
  223. If the key is an integer, get the `tokenizers.Encoding` for batch item with index `key`.
  224. If the key is a slice, returns the value of the dict associated to `key` ('input_ids', 'attention_mask', etc.)
  225. with the constraint of slice.
  226. """
  227. if isinstance(item, str):
  228. return self.data[item]
  229. elif self._encodings is not None:
  230. return self._encodings[item]
  231. elif isinstance(item, slice):
  232. return {key: self.data[key][item] for key in self.data}
  233. else:
  234. raise KeyError(
  235. "Invalid key. Only three types of key are available: "
  236. "(1) string, (2) integers for backend Encoding, and (3) slices for data subsetting."
  237. )
  238. def __getattr__(self, item: str):
  239. try:
  240. return self.data[item]
  241. except KeyError:
  242. raise AttributeError
  243. def __getstate__(self):
  244. return {"data": self.data, "encodings": self._encodings}
  245. def __setstate__(self, state):
  246. if "data" in state:
  247. self.data = state["data"]
  248. if "encodings" in state:
  249. self._encodings = state["encodings"]
  250. # After this point:
  251. # Extended properties and methods only available for fast (Rust-based) tokenizers
  252. # provided by HuggingFace tokenizers library.
  253. @property
  254. def encodings(self) -> Optional[list[EncodingFast]]:
  255. """
  256. `Optional[list[tokenizers.Encoding]]`: The list all encodings from the tokenization process. Returns `None` if
  257. the input was tokenized through Python (i.e., not a fast) tokenizer.
  258. """
  259. return self._encodings
  260. def tokens(self, batch_index: int = 0) -> list[str]:
  261. """
  262. Return the list of tokens (sub-parts of the input strings after word/subword splitting and before conversion to
  263. integer indices) at a given batch index (only works for the output of a fast tokenizer).
  264. Args:
  265. batch_index (`int`, *optional*, defaults to 0): The index to access in the batch.
  266. Returns:
  267. `list[str]`: The list of tokens at that index.
  268. """
  269. if not self._encodings:
  270. raise ValueError(
  271. "tokens() is not available when using non-fast tokenizers (e.g. instance of a `XxxTokenizerFast`"
  272. " class)."
  273. )
  274. return self._encodings[batch_index].tokens
  275. def sequence_ids(self, batch_index: int = 0) -> list[Optional[int]]:
  276. """
  277. Return a list mapping the tokens to the id of their original sentences:
  278. - `None` for special tokens added around or between sequences,
  279. - `0` for tokens corresponding to words in the first sequence,
  280. - `1` for tokens corresponding to words in the second sequence when a pair of sequences was jointly
  281. encoded.
  282. Args:
  283. batch_index (`int`, *optional*, defaults to 0): The index to access in the batch.
  284. Returns:
  285. `list[Optional[int]]`: A list indicating the sequence id corresponding to each token. Special tokens added
  286. by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding
  287. sequence.
  288. """
  289. if not self._encodings:
  290. raise ValueError(
  291. "sequence_ids() is not available when using non-fast tokenizers (e.g. instance of a `XxxTokenizerFast`"
  292. " class)."
  293. )
  294. return self._encodings[batch_index].sequence_ids
  295. def words(self, batch_index: int = 0) -> list[Optional[int]]:
  296. """
  297. Return a list mapping the tokens to their actual word in the initial sentence for a fast tokenizer.
  298. Args:
  299. batch_index (`int`, *optional*, defaults to 0): The index to access in the batch.
  300. Returns:
  301. `list[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the
  302. tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding word
  303. (several tokens will be mapped to the same word index if they are parts of that word).
  304. """
  305. if not self._encodings:
  306. raise ValueError(
  307. "words() is not available when using non-fast tokenizers (e.g. instance of a `XxxTokenizerFast`"
  308. " class)."
  309. )
  310. warnings.warn(
  311. "`BatchEncoding.words()` property is deprecated and should be replaced with the identical, "
  312. "but more self-explanatory `BatchEncoding.word_ids()` property.",
  313. FutureWarning,
  314. )
  315. return self.word_ids(batch_index)
  316. def word_ids(self, batch_index: int = 0) -> list[Optional[int]]:
  317. """
  318. Return a list mapping the tokens to their actual word in the initial sentence for a fast tokenizer.
  319. Args:
  320. batch_index (`int`, *optional*, defaults to 0): The index to access in the batch.
  321. Returns:
  322. `list[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the
  323. tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding word
  324. (several tokens will be mapped to the same word index if they are parts of that word).
  325. """
  326. if not self._encodings:
  327. raise ValueError(
  328. "word_ids() is not available when using non-fast tokenizers (e.g. instance of a `XxxTokenizerFast`"
  329. " class)."
  330. )
  331. return self._encodings[batch_index].word_ids
  332. def token_to_sequence(self, batch_or_token_index: int, token_index: Optional[int] = None) -> int:
  333. """
  334. Get the index of the sequence represented by the given token. In the general use case, this method returns `0`
  335. for a single sequence or the first sequence of a pair, and `1` for the second sequence of a pair
  336. Can be called as:
  337. - `self.token_to_sequence(token_index)` if batch size is 1
  338. - `self.token_to_sequence(batch_index, token_index)` if batch size is greater than 1
  339. This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e.,
  340. words are defined by the user). In this case it allows to easily associate encoded tokens with provided
  341. tokenized words.
  342. Args:
  343. batch_or_token_index (`int`):
  344. Index of the sequence in the batch. If the batch only comprises one sequence, this can be the index of
  345. the token in the sequence.
  346. token_index (`int`, *optional*):
  347. If a batch index is provided in *batch_or_token_index*, this can be the index of the token in the
  348. sequence.
  349. Returns:
  350. `int`: Index of the word in the input sequence.
  351. """
  352. if not self._encodings:
  353. raise ValueError("token_to_sequence() is not available when using Python based tokenizers")
  354. if token_index is not None:
  355. batch_index = batch_or_token_index
  356. else:
  357. batch_index = 0
  358. token_index = batch_or_token_index
  359. if batch_index < 0:
  360. batch_index = self._batch_size + batch_index
  361. if token_index < 0:
  362. token_index = self._seq_len + token_index
  363. return self._encodings[batch_index].token_to_sequence(token_index)
  364. def token_to_word(self, batch_or_token_index: int, token_index: Optional[int] = None) -> int:
  365. """
  366. Get the index of the word corresponding (i.e. comprising) to an encoded token in a sequence of the batch.
  367. Can be called as:
  368. - `self.token_to_word(token_index)` if batch size is 1
  369. - `self.token_to_word(batch_index, token_index)` if batch size is greater than 1
  370. This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e.,
  371. words are defined by the user). In this case it allows to easily associate encoded tokens with provided
  372. tokenized words.
  373. Args:
  374. batch_or_token_index (`int`):
  375. Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of
  376. the token in the sequence.
  377. token_index (`int`, *optional*):
  378. If a batch index is provided in *batch_or_token_index*, this can be the index of the token in the
  379. sequence.
  380. Returns:
  381. `int`: Index of the word in the input sequence.
  382. """
  383. if not self._encodings:
  384. raise ValueError("token_to_word() is not available when using Python based tokenizers")
  385. if token_index is not None:
  386. batch_index = batch_or_token_index
  387. else:
  388. batch_index = 0
  389. token_index = batch_or_token_index
  390. if batch_index < 0:
  391. batch_index = self._batch_size + batch_index
  392. if token_index < 0:
  393. token_index = self._seq_len + token_index
  394. return self._encodings[batch_index].token_to_word(token_index)
  395. def word_to_tokens(
  396. self, batch_or_word_index: int, word_index: Optional[int] = None, sequence_index: int = 0
  397. ) -> Optional[TokenSpan]:
  398. """
  399. Get the encoded token span corresponding to a word in a sequence of the batch.
  400. Token spans are returned as a [`~tokenization_utils_base.TokenSpan`] with:
  401. - **start** -- Index of the first token.
  402. - **end** -- Index of the token following the last token.
  403. Can be called as:
  404. - `self.word_to_tokens(word_index, sequence_index: int = 0)` if batch size is 1
  405. - `self.word_to_tokens(batch_index, word_index, sequence_index: int = 0)` if batch size is greater or equal to
  406. 1
  407. This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e. words
  408. are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized
  409. words.
  410. Args:
  411. batch_or_word_index (`int`):
  412. Index of the sequence in the batch. If the batch only comprises one sequence, this can be the index of
  413. the word in the sequence.
  414. word_index (`int`, *optional*):
  415. If a batch index is provided in *batch_or_token_index*, this can be the index of the word in the
  416. sequence.
  417. sequence_index (`int`, *optional*, defaults to 0):
  418. If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0
  419. or 1) the provided word index belongs to.
  420. Returns:
  421. ([`~tokenization_utils_base.TokenSpan`], *optional*): Span of tokens in the encoded sequence. Returns
  422. `None` if no tokens correspond to the word. This can happen especially when the token is a special token
  423. that has been used to format the tokenization. For example when we add a class token at the very beginning
  424. of the tokenization.
  425. """
  426. if not self._encodings:
  427. raise ValueError("word_to_tokens() is not available when using Python based tokenizers")
  428. if word_index is not None:
  429. batch_index = batch_or_word_index
  430. else:
  431. batch_index = 0
  432. word_index = batch_or_word_index
  433. if batch_index < 0:
  434. batch_index = self._batch_size + batch_index
  435. if word_index < 0:
  436. word_index = self._seq_len + word_index
  437. span = self._encodings[batch_index].word_to_tokens(word_index, sequence_index)
  438. return TokenSpan(*span) if span is not None else None
  439. def token_to_chars(self, batch_or_token_index: int, token_index: Optional[int] = None) -> Optional[CharSpan]:
  440. """
  441. Get the character span corresponding to an encoded token in a sequence of the batch.
  442. Character spans are returned as a [`~tokenization_utils_base.CharSpan`] with:
  443. - **start** -- Index of the first character in the original string associated to the token.
  444. - **end** -- Index of the character following the last character in the original string associated to the
  445. token.
  446. Can be called as:
  447. - `self.token_to_chars(token_index)` if batch size is 1
  448. - `self.token_to_chars(batch_index, token_index)` if batch size is greater or equal to 1
  449. Args:
  450. batch_or_token_index (`int`):
  451. Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of
  452. the token in the sequence.
  453. token_index (`int`, *optional*):
  454. If a batch index is provided in *batch_or_token_index*, this can be the index of the token or tokens in
  455. the sequence.
  456. Returns:
  457. [`~tokenization_utils_base.CharSpan`]: Span of characters in the original string, or None, if the token
  458. (e.g. <s>, </s>) doesn't correspond to any chars in the origin string.
  459. """
  460. if not self._encodings:
  461. raise ValueError("token_to_chars() is not available when using Python based tokenizers")
  462. if token_index is not None:
  463. batch_index = batch_or_token_index
  464. else:
  465. batch_index = 0
  466. token_index = batch_or_token_index
  467. span_indices = self._encodings[batch_index].token_to_chars(token_index)
  468. return CharSpan(*span_indices) if span_indices is not None else None
  469. def char_to_token(
  470. self, batch_or_char_index: int, char_index: Optional[int] = None, sequence_index: int = 0
  471. ) -> int:
  472. """
  473. Get the index of the token in the encoded output comprising a character in the original string for a sequence
  474. of the batch.
  475. Can be called as:
  476. - `self.char_to_token(char_index)` if batch size is 1
  477. - `self.char_to_token(batch_index, char_index)` if batch size is greater or equal to 1
  478. This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e. words
  479. are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized
  480. words.
  481. Args:
  482. batch_or_char_index (`int`):
  483. Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of
  484. the word in the sequence
  485. char_index (`int`, *optional*):
  486. If a batch index is provided in *batch_or_token_index*, this can be the index of the word in the
  487. sequence.
  488. sequence_index (`int`, *optional*, defaults to 0):
  489. If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0
  490. or 1) the provided character index belongs to.
  491. Returns:
  492. `int`: Index of the token, or None if the char index refers to a whitespace only token and whitespace is
  493. trimmed with `trim_offsets=True`.
  494. """
  495. if not self._encodings:
  496. raise ValueError("char_to_token() is not available when using Python based tokenizers")
  497. if char_index is not None:
  498. batch_index = batch_or_char_index
  499. else:
  500. batch_index = 0
  501. char_index = batch_or_char_index
  502. return self._encodings[batch_index].char_to_token(char_index, sequence_index)
  503. def word_to_chars(
  504. self, batch_or_word_index: int, word_index: Optional[int] = None, sequence_index: int = 0
  505. ) -> CharSpan:
  506. """
  507. Get the character span in the original string corresponding to given word in a sequence of the batch.
  508. Character spans are returned as a CharSpan NamedTuple with:
  509. - start: index of the first character in the original string
  510. - end: index of the character following the last character in the original string
  511. Can be called as:
  512. - `self.word_to_chars(word_index)` if batch size is 1
  513. - `self.word_to_chars(batch_index, word_index)` if batch size is greater or equal to 1
  514. Args:
  515. batch_or_word_index (`int`):
  516. Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of
  517. the word in the sequence
  518. word_index (`int`, *optional*):
  519. If a batch index is provided in *batch_or_token_index*, this can be the index of the word in the
  520. sequence.
  521. sequence_index (`int`, *optional*, defaults to 0):
  522. If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0
  523. or 1) the provided word index belongs to.
  524. Returns:
  525. `CharSpan` or `list[CharSpan]`: Span(s) of the associated character or characters in the string. CharSpan
  526. are NamedTuple with:
  527. - start: index of the first character associated to the token in the original string
  528. - end: index of the character following the last character associated to the token in the original
  529. string
  530. """
  531. if not self._encodings:
  532. raise ValueError("word_to_chars() is not available when using Python based tokenizers")
  533. if word_index is not None:
  534. batch_index = batch_or_word_index
  535. else:
  536. batch_index = 0
  537. word_index = batch_or_word_index
  538. return CharSpan(*(self._encodings[batch_index].word_to_chars(word_index, sequence_index)))
  539. def char_to_word(self, batch_or_char_index: int, char_index: Optional[int] = None, sequence_index: int = 0) -> int:
  540. """
  541. Get the word in the original string corresponding to a character in the original string of a sequence of the
  542. batch.
  543. Can be called as:
  544. - `self.char_to_word(char_index)` if batch size is 1
  545. - `self.char_to_word(batch_index, char_index)` if batch size is greater than 1
  546. This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e. words
  547. are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized
  548. words.
  549. Args:
  550. batch_or_char_index (`int`):
  551. Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of
  552. the character in the original string.
  553. char_index (`int`, *optional*):
  554. If a batch index is provided in *batch_or_token_index*, this can be the index of the character in the
  555. original string.
  556. sequence_index (`int`, *optional*, defaults to 0):
  557. If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0
  558. or 1) the provided character index belongs to.
  559. Returns:
  560. `int` or `list[int]`: Index or indices of the associated encoded token(s).
  561. """
  562. if not self._encodings:
  563. raise ValueError("char_to_word() is not available when using Python based tokenizers")
  564. if char_index is not None:
  565. batch_index = batch_or_char_index
  566. else:
  567. batch_index = 0
  568. char_index = batch_or_char_index
  569. return self._encodings[batch_index].char_to_word(char_index, sequence_index)
  570. def convert_to_tensors(
  571. self, tensor_type: Optional[Union[str, TensorType]] = None, prepend_batch_axis: bool = False
  572. ):
  573. """
  574. Convert the inner content to tensors.
  575. Args:
  576. tensor_type (`str` or [`~utils.TensorType`], *optional*):
  577. The type of tensors to use. If `str`, should be one of the values of the enum [`~utils.TensorType`]. If
  578. `None`, no modification is done.
  579. prepend_batch_axis (`int`, *optional*, defaults to `False`):
  580. Whether or not to add the batch dimension during the conversion.
  581. """
  582. if tensor_type is None:
  583. return self
  584. # Convert to TensorType
  585. if not isinstance(tensor_type, TensorType):
  586. tensor_type = TensorType(tensor_type)
  587. # Get a function reference for the correct framework
  588. if tensor_type == TensorType.TENSORFLOW:
  589. if not is_tf_available():
  590. raise ImportError(
  591. "Unable to convert output to TensorFlow tensors format, TensorFlow is not installed."
  592. )
  593. import tensorflow as tf
  594. def as_tensor(value, dtype=None):
  595. if len(flatten(value)) == 0 and dtype is None:
  596. dtype = tf.int32
  597. return tf.constant(value, dtype=dtype)
  598. is_tensor = tf.is_tensor
  599. elif tensor_type == TensorType.PYTORCH:
  600. if not is_torch_available():
  601. raise ImportError("Unable to convert output to PyTorch tensors format, PyTorch is not installed.")
  602. import torch
  603. def as_tensor(value, dtype=None):
  604. if isinstance(value, list) and len(value) > 0 and isinstance(value[0], np.ndarray):
  605. return torch.from_numpy(np.array(value))
  606. if len(flatten(value)) == 0 and dtype is None:
  607. dtype = torch.int64
  608. return torch.tensor(value, dtype=dtype)
  609. is_tensor = torch.is_tensor
  610. elif tensor_type == TensorType.JAX:
  611. if not is_flax_available():
  612. raise ImportError("Unable to convert output to JAX tensors format, JAX is not installed.")
  613. import jax.numpy as jnp # noqa: F811
  614. def as_tensor(value, dtype=None):
  615. if len(flatten(value)) == 0 and dtype is None:
  616. dtype = jnp.int32
  617. return jnp.array(value, dtype=dtype)
  618. is_tensor = is_jax_tensor
  619. elif tensor_type == TensorType.MLX:
  620. if not is_mlx_available():
  621. raise ImportError("Unable to convert output to MLX tensors format, MLX is not installed.")
  622. import mlx.core as mx
  623. def as_tensor(value, dtype=None):
  624. if len(flatten(value)) == 0 and dtype is None:
  625. dtype = mx.int32
  626. return mx.array(value, dtype=dtype)
  627. def is_tensor(obj):
  628. return isinstance(obj, mx.array)
  629. else:
  630. def as_tensor(value, dtype=None):
  631. if (
  632. isinstance(value, (list, tuple))
  633. and len(value) > 0
  634. and isinstance(value[0], (list, tuple, np.ndarray))
  635. ):
  636. value_lens = [len(val) for val in value]
  637. if len(set(value_lens)) > 1 and dtype is None:
  638. # we have a ragged list so handle explicitly
  639. value = as_tensor([np.asarray(val) for val in value], dtype=object)
  640. if len(flatten(value)) == 0 and dtype is None:
  641. dtype = np.int64
  642. return np.asarray(value, dtype=dtype)
  643. is_tensor = is_numpy_array
  644. # Do the tensor conversion in batch
  645. for key, value in self.items():
  646. try:
  647. if prepend_batch_axis:
  648. value = [value]
  649. if not is_tensor(value):
  650. tensor = as_tensor(value)
  651. # Removing this for now in favor of controlling the shape with `prepend_batch_axis`
  652. # # at-least2d
  653. # if tensor.ndim > 2:
  654. # tensor = tensor.squeeze(0)
  655. # elif tensor.ndim < 2:
  656. # tensor = tensor[None, :]
  657. self[key] = tensor
  658. except Exception as e:
  659. if key == "overflowing_tokens":
  660. raise ValueError(
  661. "Unable to create tensor returning overflowing tokens of different lengths. "
  662. "Please see if a fast version of this tokenizer is available to have this feature available."
  663. ) from e
  664. raise ValueError(
  665. "Unable to create tensor, you should probably activate truncation and/or padding with"
  666. " 'padding=True' 'truncation=True' to have batched tensors with the same length. Perhaps your"
  667. f" features (`{key}` in this case) have excessive nesting (inputs type `list` where type `int` is"
  668. " expected)."
  669. ) from e
  670. return self
  671. def to(self, device: Union[str, "torch.device"], *, non_blocking: bool = False) -> "BatchEncoding":
  672. """
  673. Send all values to device by calling `v.to(device, non_blocking=non_blocking)` (PyTorch only).
  674. Args:
  675. device (`str` or `torch.device`): The device to put the tensors on.
  676. non_blocking (`bool`): Whether to perform the copy asynchronously.
  677. Returns:
  678. [`BatchEncoding`]: The same instance after modification.
  679. """
  680. requires_backends(self, ["torch"])
  681. # This check catches things like APEX blindly calling "to" on all inputs to a module
  682. # Otherwise it passes the casts down and casts the LongTensor containing the token idxs
  683. # into a HalfTensor
  684. if isinstance(device, str) or is_torch_device(device) or isinstance(device, int):
  685. self.data = {
  686. k: v.to(device=device, non_blocking=non_blocking) if hasattr(v, "to") and callable(v.to) else v
  687. for k, v in self.data.items()
  688. }
  689. else:
  690. logger.warning(f"Attempting to cast a BatchEncoding to type {str(device)}. This is not supported.")
  691. return self
  692. class SpecialTokensMixin:
  693. """
  694. A mixin derived by [`PreTrainedTokenizer`] and [`PreTrainedTokenizerFast`] to handle specific behaviors related to
  695. special tokens. In particular, this class hold the attributes which can be used to directly access these special
  696. tokens in a model-independent manner and allow to set and update the special tokens.
  697. Args:
  698. bos_token (`str` or `tokenizers.AddedToken`, *optional*):
  699. A special token representing the beginning of a sentence.
  700. eos_token (`str` or `tokenizers.AddedToken`, *optional*):
  701. A special token representing the end of a sentence.
  702. unk_token (`str` or `tokenizers.AddedToken`, *optional*):
  703. A special token representing an out-of-vocabulary token.
  704. sep_token (`str` or `tokenizers.AddedToken`, *optional*):
  705. A special token separating two different sentences in the same input (used by BERT for instance).
  706. pad_token (`str` or `tokenizers.AddedToken`, *optional*):
  707. A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by
  708. attention mechanisms or loss computation.
  709. cls_token (`str` or `tokenizers.AddedToken`, *optional*):
  710. A special token representing the class of the input (used by BERT for instance).
  711. mask_token (`str` or `tokenizers.AddedToken`, *optional*):
  712. A special token representing a masked token (used by masked-language modeling pretraining objectives, like
  713. BERT).
  714. additional_special_tokens (tuple or list of `str` or `tokenizers.AddedToken`, *optional*):
  715. A tuple or a list of additional tokens, which will be marked as `special`, meaning that they will be
  716. skipped when decoding if `skip_special_tokens` is set to `True`.
  717. """
  718. SPECIAL_TOKENS_ATTRIBUTES = [
  719. "bos_token",
  720. "eos_token",
  721. "unk_token",
  722. "sep_token",
  723. "pad_token",
  724. "cls_token",
  725. "mask_token",
  726. "additional_special_tokens",
  727. ]
  728. def __init__(self, verbose=False, **kwargs):
  729. self._pad_token_type_id = 0
  730. self.verbose = verbose
  731. self._special_tokens_map = dict.fromkeys(self.SPECIAL_TOKENS_ATTRIBUTES)
  732. self._special_tokens_map["additional_special_tokens"] = [] # for BC where it defaults to empty list
  733. # We directly set the hidden value to allow initialization with special tokens
  734. # which are not yet in the vocabulary. Necessary for serialization/de-serialization
  735. # TODO clean this up at some point (probably by switching to fast tokenizers)
  736. for key, value in kwargs.items():
  737. if value is None:
  738. continue
  739. if key in self.SPECIAL_TOKENS_ATTRIBUTES:
  740. if key == "additional_special_tokens":
  741. assert isinstance(value, (list, tuple)), f"Value {value} is not a list or tuple"
  742. assert all(isinstance(t, (str, AddedToken)) for t in value), (
  743. "One of the tokens is not a string or an AddedToken"
  744. )
  745. setattr(self, key, value)
  746. elif isinstance(value, (str, AddedToken)):
  747. setattr(self, key, value)
  748. else:
  749. raise TypeError(f"Special token {key} has to be either str or AddedToken but got: {type(value)}")
  750. def sanitize_special_tokens(self) -> int:
  751. """
  752. The `sanitize_special_tokens` is now deprecated kept for backward compatibility and will be removed in
  753. transformers v5.
  754. """
  755. logger.warning_once("The `sanitize_special_tokens` will be removed in transformers v5.")
  756. return self.add_tokens(self.all_special_tokens_extended, special_tokens=True)
  757. def add_special_tokens(
  758. self,
  759. special_tokens_dict: dict[str, Union[str, AddedToken, Sequence[Union[str, AddedToken]]]],
  760. replace_additional_special_tokens=True,
  761. ) -> int:
  762. """
  763. Add a dictionary of special tokens (eos, pad, cls, etc.) to the encoder and link them to class attributes. If
  764. special tokens are NOT in the vocabulary, they are added to it (indexed starting from the last index of the
  765. current vocabulary).
  766. When adding new tokens to the vocabulary, you should make sure to also resize the token embedding matrix of the
  767. model so that its embedding matrix matches the tokenizer.
  768. In order to do that, please use the [`~PreTrainedModel.resize_token_embeddings`] method.
  769. Using `add_special_tokens` will ensure your special tokens can be used in several ways:
  770. - Special tokens can be skipped when decoding using `skip_special_tokens = True`.
  771. - Special tokens are carefully handled by the tokenizer (they are never split), similar to `AddedTokens`.
  772. - You can easily refer to special tokens using tokenizer class attributes like `tokenizer.cls_token`. This
  773. makes it easy to develop model-agnostic training and fine-tuning scripts.
  774. When possible, special tokens are already registered for provided pretrained models (for instance
  775. [`BertTokenizer`] `cls_token` is already registered to be `'[CLS]'` and XLM's one is also registered to be
  776. `'</s>'`).
  777. Args:
  778. special_tokens_dict (dictionary *str* to *str*, `tokenizers.AddedToken`, or `Sequence[Union[str, AddedToken]]`):
  779. Keys should be in the list of predefined special attributes: [`bos_token`, `eos_token`, `unk_token`,
  780. `sep_token`, `pad_token`, `cls_token`, `mask_token`, `additional_special_tokens`].
  781. Tokens are only added if they are not already in the vocabulary (tested by checking if the tokenizer
  782. assign the index of the `unk_token` to them).
  783. replace_additional_special_tokens (`bool`, *optional*,, defaults to `True`):
  784. If `True`, the existing list of additional special tokens will be replaced by the list provided in
  785. `special_tokens_dict`. Otherwise, `self._special_tokens_map["additional_special_tokens"]` is just extended. In the former
  786. case, the tokens will NOT be removed from the tokenizer's full vocabulary - they are only being flagged
  787. as non-special tokens. Remember, this only affects which tokens are skipped during decoding, not the
  788. `added_tokens_encoder` and `added_tokens_decoder`. This means that the previous
  789. `additional_special_tokens` are still added tokens, and will not be split by the model.
  790. Returns:
  791. `int`: Number of tokens added to the vocabulary.
  792. Examples:
  793. ```python
  794. # Let's see how to add a new classification token to GPT-2
  795. tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2")
  796. model = GPT2Model.from_pretrained("openai-community/gpt2")
  797. special_tokens_dict = {"cls_token": "<CLS>"}
  798. num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
  799. print("We have added", num_added_toks, "tokens")
  800. # Notice: resize_token_embeddings expect to receive the full size of the new vocabulary, i.e., the length of the tokenizer.
  801. model.resize_token_embeddings(len(tokenizer))
  802. assert tokenizer.cls_token == "<CLS>"
  803. ```"""
  804. if not special_tokens_dict:
  805. return 0
  806. added_tokens = []
  807. for key, value in special_tokens_dict.items():
  808. assert key in self.SPECIAL_TOKENS_ATTRIBUTES, f"Key {key} is not a special token"
  809. if self.verbose:
  810. logger.info(f"Assigning {value} to the {key} key of the tokenizer")
  811. if key == "additional_special_tokens":
  812. assert isinstance(value, (list, tuple)) and all(isinstance(t, (str, AddedToken)) for t in value), (
  813. f"Tokens {value} for key {key} should all be str or AddedToken instances"
  814. )
  815. to_add = []
  816. for token in value:
  817. if isinstance(token, str):
  818. # for legacy purpose we default to stripping. `test_add_tokens_tokenizer` depends on this
  819. token = AddedToken(token, rstrip=False, lstrip=False, normalized=False, special=True)
  820. if not replace_additional_special_tokens and str(token) in self.additional_special_tokens:
  821. continue
  822. to_add.append(token)
  823. if replace_additional_special_tokens and len(to_add) > 0:
  824. setattr(self, key, list(to_add))
  825. else:
  826. self._special_tokens_map["additional_special_tokens"].extend(to_add)
  827. added_tokens += to_add
  828. else:
  829. if not isinstance(value, (str, AddedToken)):
  830. raise ValueError(f"Token {value} for key {key} should be a str or an AddedToken instance")
  831. if isinstance(value, (str)):
  832. # for legacy purpose we default to stripping. `False` depends on this
  833. value = AddedToken(value, rstrip=False, lstrip=False, normalized=False, special=True)
  834. if isinstance(value, AddedToken):
  835. setattr(self, key, value)
  836. if value not in added_tokens:
  837. added_tokens.append(value)
  838. # if we are adding tokens that were not part of the vocab, we ought to add them
  839. added_tokens = self.add_tokens(added_tokens, special_tokens=True)
  840. return added_tokens
  841. def add_tokens(
  842. self, new_tokens: Union[str, AddedToken, Sequence[Union[str, AddedToken]]], special_tokens: bool = False
  843. ) -> int:
  844. """
  845. Add a list of new tokens to the tokenizer class. If the new tokens are not in the vocabulary, they are added to
  846. it with indices starting from length of the current vocabulary and will be isolated before the tokenization
  847. algorithm is applied. Added tokens and tokens from the vocabulary of the tokenization algorithm are therefore
  848. not treated in the same way.
  849. Note, when adding new tokens to the vocabulary, you should make sure to also resize the token embedding matrix
  850. of the model so that its embedding matrix matches the tokenizer.
  851. In order to do that, please use the [`~PreTrainedModel.resize_token_embeddings`] method.
  852. Args:
  853. new_tokens (`str`, `tokenizers.AddedToken` or a sequence of *str* or `tokenizers.AddedToken`):
  854. Tokens are only added if they are not already in the vocabulary. `tokenizers.AddedToken` wraps a string
  855. token to let you personalize its behavior: whether this token should only match against a single word,
  856. whether this token should strip all potential whitespaces on the left side, whether this token should
  857. strip all potential whitespaces on the right side, etc.
  858. special_tokens (`bool`, *optional*, defaults to `False`):
  859. Can be used to specify if the token is a special token. This mostly change the normalization behavior
  860. (special tokens like CLS or [MASK] are usually not lower-cased for instance).
  861. See details for `tokenizers.AddedToken` in HuggingFace tokenizers library.
  862. Returns:
  863. `int`: Number of tokens added to the vocabulary.
  864. Examples:
  865. ```python
  866. # Let's see how to increase the vocabulary of Bert model and tokenizer
  867. tokenizer = BertTokenizerFast.from_pretrained("google-bert/bert-base-uncased")
  868. model = BertModel.from_pretrained("google-bert/bert-base-uncased")
  869. num_added_toks = tokenizer.add_tokens(["new_tok1", "my_new-tok2"])
  870. print("We have added", num_added_toks, "tokens")
  871. # Notice: resize_token_embeddings expect to receive the full size of the new vocabulary, i.e., the length of the tokenizer.
  872. model.resize_token_embeddings(len(tokenizer))
  873. ```"""
  874. if not new_tokens:
  875. return 0
  876. if not isinstance(new_tokens, (list, tuple)):
  877. new_tokens = [new_tokens]
  878. return self._add_tokens(new_tokens, special_tokens=special_tokens)
  879. def _add_tokens(self, new_tokens: Union[list[str], list[AddedToken]], special_tokens: bool = False) -> int:
  880. raise NotImplementedError
  881. @property
  882. def pad_token_type_id(self) -> int:
  883. """
  884. `int`: Id of the padding token type in the vocabulary.
  885. """
  886. return self._pad_token_type_id
  887. def __setattr__(self, key, value):
  888. key_without_id = key
  889. key_is_special_id = key.endswith("_id") or key.endswith("_ids")
  890. if key_is_special_id:
  891. key_without_id = key[:-3] if not key.endswith("_ids") else key[:-4]
  892. if self.__dict__.get("_special_tokens_map", None) is not None and any(
  893. name in self.__dict__["_special_tokens_map"] for name in [key, key_without_id]
  894. ):
  895. if key_is_special_id:
  896. if value is not None:
  897. value = (
  898. self.convert_ids_to_tokens(value)
  899. if key != "additional_special_tokens"
  900. else [self.convert_ids_to_tokens(val) for val in value]
  901. )
  902. key = key_without_id
  903. if key != "additional_special_tokens" and not isinstance(value, (str, AddedToken)) and value is not None:
  904. raise ValueError(f"Cannot set a non-string value as the {key}")
  905. self._special_tokens_map[key] = value
  906. else:
  907. super().__setattr__(key, value)
  908. def __getattr__(self, key):
  909. key_without_id = key
  910. key_is_special_id = key.endswith("_id") or key.endswith("_ids")
  911. if key_is_special_id:
  912. key_without_id = key[:-3] if not key.endswith("_ids") else key[:-4]
  913. if self.__dict__.get("_special_tokens_map", None) is not None and any(
  914. name in self.__dict__["_special_tokens_map"] for name in [key, key_without_id]
  915. ):
  916. _special_tokens_map = self.__dict__["_special_tokens_map"]
  917. if not key_is_special_id:
  918. if _special_tokens_map[key] is None:
  919. if self.verbose:
  920. logger.error(f"Using {key}, but it is not set yet.")
  921. return None
  922. value = _special_tokens_map[key]
  923. return str(value) if key != "additional_special_tokens" else [str(tok) for tok in value]
  924. else:
  925. attr_as_tokens = getattr(self, key_without_id)
  926. return self.convert_tokens_to_ids(attr_as_tokens) if attr_as_tokens is not None else None
  927. if key not in self.__dict__:
  928. raise AttributeError(f"{self.__class__.__name__} has no attribute {key}")
  929. else:
  930. return super().__getattr__(key)
  931. @property
  932. def special_tokens_map(self) -> dict[str, Union[str, list[str]]]:
  933. """
  934. `dict[str, Union[str, list[str]]]`: A dictionary mapping special token class attributes (`cls_token`,
  935. `unk_token`, etc.) to their values (`'<unk>'`, `'<cls>'`, etc.).
  936. Convert potential tokens of `tokenizers.AddedToken` type to string.
  937. """
  938. set_attr = {}
  939. for attr in self.SPECIAL_TOKENS_ATTRIBUTES:
  940. attr_value = getattr(self, attr)
  941. if attr_value:
  942. set_attr[attr] = attr_value
  943. return set_attr
  944. @property
  945. def special_tokens_map_extended(self) -> dict[str, Union[str, AddedToken, list[Union[str, AddedToken]]]]:
  946. """
  947. `dict[str, Union[str, tokenizers.AddedToken, list[Union[str, tokenizers.AddedToken]]]]`: A dictionary mapping
  948. special token class attributes (`cls_token`, `unk_token`, etc.) to their values (`'<unk>'`, `'<cls>'`, etc.).
  949. Don't convert tokens of `tokenizers.AddedToken` type to string so they can be used to control more finely how
  950. special tokens are tokenized.
  951. """
  952. set_attr = {}
  953. for attr in self.SPECIAL_TOKENS_ATTRIBUTES:
  954. attr_value = self._special_tokens_map[attr]
  955. if attr_value:
  956. set_attr[attr] = attr_value
  957. return set_attr
  958. @property
  959. def all_special_tokens_extended(self) -> list[Union[str, AddedToken]]:
  960. """
  961. `list[Union[str, tokenizers.AddedToken]]`: All the special tokens (`'<unk>'`, `'<cls>'`, etc.), the order has
  962. nothing to do with the index of each tokens. If you want to know the correct indices, check
  963. `self.added_tokens_encoder`. We can't create an order anymore as the keys are `AddedTokens` and not `Strings`.
  964. Don't convert tokens of `tokenizers.AddedToken` type to string so they can be used to control more finely how
  965. special tokens are tokenized.
  966. """
  967. all_tokens = []
  968. seen = set()
  969. for value in self.special_tokens_map_extended.values():
  970. if isinstance(value, (list, tuple)):
  971. tokens_to_add = [token for token in value if str(token) not in seen]
  972. else:
  973. tokens_to_add = [value] if str(value) not in seen else []
  974. seen.update(map(str, tokens_to_add))
  975. all_tokens.extend(tokens_to_add)
  976. return all_tokens
  977. @property
  978. def all_special_tokens(self) -> list[str]:
  979. """
  980. `list[str]`: A list of the unique special tokens (`'<unk>'`, `'<cls>'`, ..., etc.).
  981. Convert tokens of `tokenizers.AddedToken` type to string.
  982. """
  983. all_toks = [str(s) for s in self.all_special_tokens_extended]
  984. return all_toks
  985. @property
  986. def all_special_ids(self) -> list[int]:
  987. """
  988. `list[int]`: List the ids of the special tokens(`'<unk>'`, `'<cls>'`, etc.) mapped to class attributes.
  989. """
  990. all_toks = self.all_special_tokens
  991. all_ids = self.convert_tokens_to_ids(all_toks)
  992. return all_ids
  993. def _set_model_specific_special_tokens(self, special_tokens: list[str]):
  994. """
  995. Adds new special tokens to the "SPECIAL_TOKENS_ATTRIBUTES" list which will be part
  996. of "self.special_tokens" and saved as a special token in tokenizer's config.
  997. This allows us to dynamically add new model-type specific tokens after initializing the tokenizer.
  998. For example: if the model tokenizers is multimodal, we can support special image or audio tokens.
  999. """
  1000. self.SPECIAL_TOKENS_ATTRIBUTES = self.SPECIAL_TOKENS_ATTRIBUTES + list(special_tokens.keys())
  1001. for key, value in special_tokens.items():
  1002. if isinstance(value, (str, AddedToken)):
  1003. self._special_tokens_map[key] = value
  1004. else:
  1005. raise TypeError(f"Special token {key} has to be either str or AddedToken but got: {type(value)}")
  1006. ENCODE_KWARGS_DOCSTRING = r"""
  1007. add_special_tokens (`bool`, *optional*, defaults to `True`):
  1008. Whether or not to add special tokens when encoding the sequences. This will use the underlying
  1009. `PretrainedTokenizerBase.build_inputs_with_special_tokens` function, which defines which tokens are
  1010. automatically added to the input ids. This is useful if you want to add `bos` or `eos` tokens
  1011. automatically.
  1012. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
  1013. Activates and controls padding. Accepts the following values:
  1014. - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
  1015. sequence is provided).
  1016. - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
  1017. acceptable input length for the model if that argument is not provided.
  1018. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
  1019. lengths).
  1020. truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):
  1021. Activates and controls truncation. Accepts the following values:
  1022. - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or
  1023. to the maximum acceptable input length for the model if that argument is not provided. This will
  1024. truncate token by token, removing a token from the longest sequence in the pair if a pair of
  1025. sequences (or a batch of pairs) is provided.
  1026. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the
  1027. maximum acceptable input length for the model if that argument is not provided. This will only
  1028. truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
  1029. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the
  1030. maximum acceptable input length for the model if that argument is not provided. This will only
  1031. truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
  1032. - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths
  1033. greater than the model maximum admissible input size).
  1034. max_length (`int`, *optional*):
  1035. Controls the maximum length to use by one of the truncation/padding parameters.
  1036. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length
  1037. is required by one of the truncation/padding parameters. If the model has no specific maximum input
  1038. length (like XLNet) truncation/padding to a maximum length will be deactivated.
  1039. stride (`int`, *optional*, defaults to 0):
  1040. If set to a number along with `max_length`, the overflowing tokens returned when
  1041. `return_overflowing_tokens=True` will contain some tokens from the end of the truncated sequence
  1042. returned to provide some overlap between truncated and overflowing sequences. The value of this
  1043. argument defines the number of overlapping tokens.
  1044. is_split_into_words (`bool`, *optional*, defaults to `False`):
  1045. Whether or not the input is already pre-tokenized (e.g., split into words). If set to `True`, the
  1046. tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
  1047. which it will tokenize. This is useful for NER or token classification.
  1048. pad_to_multiple_of (`int`, *optional*):
  1049. If set will pad the sequence to a multiple of the provided value. Requires `padding` to be activated.
  1050. This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
  1051. `>= 7.5` (Volta).
  1052. padding_side (`str`, *optional*):
  1053. The side on which the model should have padding applied. Should be selected between ['right', 'left'].
  1054. Default value is picked from the class attribute of the same name.
  1055. return_tensors (`str` or [`~utils.TensorType`], *optional*):
  1056. If set, will return tensors instead of list of python integers. Acceptable values are:
  1057. - `'tf'`: Return TensorFlow `tf.constant` objects.
  1058. - `'pt'`: Return PyTorch `torch.Tensor` objects.
  1059. - `'np'`: Return Numpy `np.ndarray` objects.
  1060. """
  1061. ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING = r"""
  1062. return_token_type_ids (`bool`, *optional*):
  1063. Whether to return token type IDs. If left to the default, will return the token type IDs according to
  1064. the specific tokenizer's default, defined by the `return_outputs` attribute.
  1065. [What are token type IDs?](../glossary#token-type-ids)
  1066. return_attention_mask (`bool`, *optional*):
  1067. Whether to return the attention mask. If left to the default, will return the attention mask according
  1068. to the specific tokenizer's default, defined by the `return_outputs` attribute.
  1069. [What are attention masks?](../glossary#attention-mask)
  1070. return_overflowing_tokens (`bool`, *optional*, defaults to `False`):
  1071. Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
  1072. of pairs) is provided with `truncation_strategy = longest_first` or `True`, an error is raised instead
  1073. of returning overflowing tokens.
  1074. return_special_tokens_mask (`bool`, *optional*, defaults to `False`):
  1075. Whether or not to return special tokens mask information.
  1076. return_offsets_mapping (`bool`, *optional*, defaults to `False`):
  1077. Whether or not to return `(char_start, char_end)` for each token.
  1078. This is only available on fast tokenizers inheriting from [`PreTrainedTokenizerFast`], if using
  1079. Python's tokenizer, this method will raise `NotImplementedError`.
  1080. return_length (`bool`, *optional*, defaults to `False`):
  1081. Whether or not to return the lengths of the encoded inputs.
  1082. verbose (`bool`, *optional*, defaults to `True`):
  1083. Whether or not to print more information and warnings.
  1084. **kwargs: passed to the `self.tokenize()` method
  1085. Return:
  1086. [`BatchEncoding`]: A [`BatchEncoding`] with the following fields:
  1087. - **input_ids** -- List of token ids to be fed to a model.
  1088. [What are input IDs?](../glossary#input-ids)
  1089. - **token_type_ids** -- List of token type ids to be fed to a model (when `return_token_type_ids=True` or
  1090. if *"token_type_ids"* is in `self.model_input_names`).
  1091. [What are token type IDs?](../glossary#token-type-ids)
  1092. - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
  1093. `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names`).
  1094. [What are attention masks?](../glossary#attention-mask)
  1095. - **overflowing_tokens** -- List of overflowing tokens sequences (when a `max_length` is specified and
  1096. `return_overflowing_tokens=True`).
  1097. - **num_truncated_tokens** -- Number of tokens truncated (when a `max_length` is specified and
  1098. `return_overflowing_tokens=True`).
  1099. - **special_tokens_mask** -- List of 0s and 1s, with 1 specifying added special tokens and 0 specifying
  1100. regular sequence tokens (when `add_special_tokens=True` and `return_special_tokens_mask=True`).
  1101. - **length** -- The length of the inputs (when `return_length=True`)
  1102. """
  1103. INIT_TOKENIZER_DOCSTRING = r"""
  1104. Class attributes (overridden by derived classes)
  1105. - **vocab_files_names** (`dict[str, str]`) -- A dictionary with, as keys, the `__init__` keyword name of each
  1106. vocabulary file required by the model, and as associated values, the filename for saving the associated file
  1107. (string).
  1108. - **pretrained_vocab_files_map** (`dict[str, dict[str, str]]`) -- A dictionary of dictionaries, with the
  1109. high-level keys being the `__init__` keyword name of each vocabulary file required by the model, the
  1110. low-level being the `short-cut-names` of the pretrained models with, as associated values, the `url` to the
  1111. associated pretrained vocabulary file.
  1112. - **model_input_names** (`list[str]`) -- A list of inputs expected in the forward pass of the model.
  1113. - **padding_side** (`str`) -- The default value for the side on which the model should have padding applied.
  1114. Should be `'right'` or `'left'`.
  1115. - **truncation_side** (`str`) -- The default value for the side on which the model should have truncation
  1116. applied. Should be `'right'` or `'left'`.
  1117. Args:
  1118. model_max_length (`int`, *optional*):
  1119. The maximum length (in number of tokens) for the inputs to the transformer model. When the tokenizer is
  1120. loaded with [`~tokenization_utils_base.PreTrainedTokenizerBase.from_pretrained`], this will be set to the
  1121. value stored for the associated model in `max_model_input_sizes` (see above). If no value is provided, will
  1122. default to VERY_LARGE_INTEGER (`int(1e30)`).
  1123. padding_side (`str`, *optional*):
  1124. The side on which the model should have padding applied. Should be selected between ['right', 'left'].
  1125. Default value is picked from the class attribute of the same name.
  1126. truncation_side (`str`, *optional*):
  1127. The side on which the model should have truncation applied. Should be selected between ['right', 'left'].
  1128. Default value is picked from the class attribute of the same name.
  1129. chat_template (`str`, *optional*):
  1130. A Jinja template string that will be used to format lists of chat messages. See
  1131. https://huggingface.co/docs/transformers/chat_templating for a full description.
  1132. model_input_names (`list[string]`, *optional*):
  1133. The list of inputs accepted by the forward pass of the model (like `"token_type_ids"` or
  1134. `"attention_mask"`). Default value is picked from the class attribute of the same name.
  1135. bos_token (`str` or `tokenizers.AddedToken`, *optional*):
  1136. A special token representing the beginning of a sentence. Will be associated to `self.bos_token` and
  1137. `self.bos_token_id`.
  1138. eos_token (`str` or `tokenizers.AddedToken`, *optional*):
  1139. A special token representing the end of a sentence. Will be associated to `self.eos_token` and
  1140. `self.eos_token_id`.
  1141. unk_token (`str` or `tokenizers.AddedToken`, *optional*):
  1142. A special token representing an out-of-vocabulary token. Will be associated to `self.unk_token` and
  1143. `self.unk_token_id`.
  1144. sep_token (`str` or `tokenizers.AddedToken`, *optional*):
  1145. A special token separating two different sentences in the same input (used by BERT for instance). Will be
  1146. associated to `self.sep_token` and `self.sep_token_id`.
  1147. pad_token (`str` or `tokenizers.AddedToken`, *optional*):
  1148. A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by
  1149. attention mechanisms or loss computation. Will be associated to `self.pad_token` and `self.pad_token_id`.
  1150. cls_token (`str` or `tokenizers.AddedToken`, *optional*):
  1151. A special token representing the class of the input (used by BERT for instance). Will be associated to
  1152. `self.cls_token` and `self.cls_token_id`.
  1153. mask_token (`str` or `tokenizers.AddedToken`, *optional*):
  1154. A special token representing a masked token (used by masked-language modeling pretraining objectives, like
  1155. BERT). Will be associated to `self.mask_token` and `self.mask_token_id`.
  1156. additional_special_tokens (tuple or list of `str` or `tokenizers.AddedToken`, *optional*):
  1157. A tuple or a list of additional special tokens. Add them here to ensure they are skipped when decoding with
  1158. `skip_special_tokens` is set to True. If they are not part of the vocabulary, they will be added at the end
  1159. of the vocabulary.
  1160. clean_up_tokenization_spaces (`bool`, *optional*, defaults to `True`):
  1161. Whether or not the model should cleanup the spaces that were added when splitting the input text during the
  1162. tokenization process.
  1163. split_special_tokens (`bool`, *optional*, defaults to `False`):
  1164. Whether or not the special tokens should be split during the tokenization process. Passing will affect the
  1165. internal state of the tokenizer. The default behavior is to not split special tokens. This means that if
  1166. `<s>` is the `bos_token`, then `tokenizer.tokenize("<s>") = ['<s>`]. Otherwise, if
  1167. `split_special_tokens=True`, then `tokenizer.tokenize("<s>")` will be give `['<','s', '>']`.
  1168. """
  1169. @add_end_docstrings(INIT_TOKENIZER_DOCSTRING)
  1170. class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
  1171. """
  1172. Base class for [`PreTrainedTokenizer`] and [`PreTrainedTokenizerFast`].
  1173. Handles shared (mostly boiler plate) methods for those two classes.
  1174. """
  1175. vocab_files_names: dict[str, str] = {}
  1176. pretrained_vocab_files_map: dict[str, dict[str, str]] = {}
  1177. _auto_class: Optional[str] = None
  1178. # first name has to correspond to main model input name
  1179. # to make sure `tokenizer.pad(...)` works correctly
  1180. model_input_names: list[str] = ["input_ids", "token_type_ids", "attention_mask"]
  1181. padding_side: str = "right"
  1182. truncation_side: str = "right"
  1183. slow_tokenizer_class = None
  1184. def __init__(self, **kwargs):
  1185. # inputs and kwargs for saving and re-loading (see ``from_pretrained`` and ``save_pretrained``)
  1186. self.init_inputs = ()
  1187. for key in kwargs:
  1188. if hasattr(self, key) and callable(getattr(self, key)):
  1189. raise AttributeError(f"{key} conflicts with the method {key} in {self.__class__.__name__}")
  1190. self.init_kwargs = copy.deepcopy(kwargs)
  1191. self.name_or_path = kwargs.pop("name_or_path", "")
  1192. self._processor_class = kwargs.pop("processor_class", None)
  1193. # For backward compatibility we fallback to set model_max_length from max_len if provided
  1194. model_max_length = kwargs.pop("model_max_length", kwargs.pop("max_len", None))
  1195. self.model_max_length = model_max_length if model_max_length is not None else VERY_LARGE_INTEGER
  1196. # Padding and truncation side are right by default and overridden in subclasses. If specified in the kwargs, it
  1197. # is changed.
  1198. self.padding_side = kwargs.pop("padding_side", self.padding_side)
  1199. if self.padding_side not in ["right", "left"]:
  1200. raise ValueError(
  1201. f"Padding side should be selected between 'right' and 'left', current value: {self.padding_side}"
  1202. )
  1203. self.truncation_side = kwargs.pop("truncation_side", self.truncation_side)
  1204. if self.truncation_side not in ["right", "left"]:
  1205. raise ValueError(
  1206. f"Truncation side should be selected between 'right' and 'left', current value: {self.truncation_side}"
  1207. )
  1208. self.model_input_names = kwargs.pop("model_input_names", self.model_input_names)
  1209. # By default, cleaning tokenization spaces for both fast and slow tokenizers
  1210. self.clean_up_tokenization_spaces = kwargs.pop("clean_up_tokenization_spaces", False)
  1211. # By default, do not split special tokens for both fast and slow tokenizers
  1212. self.split_special_tokens = kwargs.pop("split_special_tokens", False)
  1213. self.deprecation_warnings = {} # Use to store when we have already noticed a deprecation warning (avoid overlogging).
  1214. self._in_target_context_manager = False
  1215. # Stores a Jinja template that formats chat histories into tokenizable strings
  1216. self.chat_template = kwargs.pop("chat_template", None)
  1217. if isinstance(self.chat_template, (list, tuple)):
  1218. # Chat templates are stored as lists of dicts with fixed key names,
  1219. # we reconstruct that into a single dict while loading them.
  1220. self.chat_template = {template["name"]: template["template"] for template in self.chat_template}
  1221. super().__init__(**kwargs)
  1222. self.extra_special_tokens = kwargs.pop("extra_special_tokens", {})
  1223. self._set_model_specific_special_tokens(special_tokens=self.extra_special_tokens)
  1224. @property
  1225. def max_len_single_sentence(self) -> int:
  1226. """
  1227. `int`: The maximum length of a sentence that can be fed to the model.
  1228. """
  1229. return self.model_max_length - self.num_special_tokens_to_add(pair=False)
  1230. @property
  1231. def max_len_sentences_pair(self) -> int:
  1232. """
  1233. `int`: The maximum combined length of a pair of sentences that can be fed to the model.
  1234. """
  1235. return self.model_max_length - self.num_special_tokens_to_add(pair=True)
  1236. @max_len_single_sentence.setter
  1237. def max_len_single_sentence(self, value) -> int:
  1238. # For backward compatibility, allow to try to setup 'max_len_single_sentence'.
  1239. if value == self.model_max_length - self.num_special_tokens_to_add(pair=False) and self.verbose:
  1240. if not self.deprecation_warnings.get("max_len_single_sentence", False):
  1241. logger.warning(
  1242. "Setting 'max_len_single_sentence' is now deprecated. This value is automatically set up."
  1243. )
  1244. self.deprecation_warnings["max_len_single_sentence"] = True
  1245. else:
  1246. raise ValueError(
  1247. "Setting 'max_len_single_sentence' is now deprecated. This value is automatically set up."
  1248. )
  1249. @max_len_sentences_pair.setter
  1250. def max_len_sentences_pair(self, value) -> int:
  1251. # For backward compatibility, allow to try to setup 'max_len_sentences_pair'.
  1252. if value == self.model_max_length - self.num_special_tokens_to_add(pair=True) and self.verbose:
  1253. if not self.deprecation_warnings.get("max_len_sentences_pair", False):
  1254. logger.warning(
  1255. "Setting 'max_len_sentences_pair' is now deprecated. This value is automatically set up."
  1256. )
  1257. self.deprecation_warnings["max_len_sentences_pair"] = True
  1258. else:
  1259. raise ValueError("Setting 'max_len_sentences_pair' is now deprecated. This value is automatically set up.")
  1260. def _set_processor_class(self, processor_class: str):
  1261. """Sets processor class as an attribute."""
  1262. self._processor_class = processor_class
  1263. @property
  1264. def added_tokens_decoder(self) -> dict[int, AddedToken]:
  1265. raise NotImplementedError()
  1266. def __repr__(self) -> str:
  1267. added_tokens_decoder_rep = "\n\t".join([f"{k}: {v.__repr__()}," for k, v in self.added_tokens_decoder.items()])
  1268. return (
  1269. f"{self.__class__.__name__}(name_or_path='{self.name_or_path}',"
  1270. f" vocab_size={self.vocab_size}, model_max_length={self.model_max_length}, is_fast={self.is_fast},"
  1271. f" padding_side='{self.padding_side}', truncation_side='{self.truncation_side}',"
  1272. f" special_tokens={self.special_tokens_map}, clean_up_tokenization_spaces={self.clean_up_tokenization_spaces},"
  1273. " added_tokens_decoder={\n\t" + added_tokens_decoder_rep + "\n}\n)"
  1274. )
  1275. def __len__(self) -> int:
  1276. raise NotImplementedError()
  1277. def get_vocab(self) -> dict[str, int]:
  1278. """
  1279. Returns the vocabulary as a dictionary of token to index.
  1280. `tokenizer.get_vocab()[token]` is equivalent to `tokenizer.convert_tokens_to_ids(token)` when `token` is in the
  1281. vocab.
  1282. Returns:
  1283. `dict[str, int]`: The vocabulary.
  1284. """
  1285. raise NotImplementedError()
  1286. def apply_chat_template(
  1287. self,
  1288. conversation: Union[list[dict[str, str]], list[list[dict[str, str]]]],
  1289. tools: Optional[list[Union[dict, Callable]]] = None,
  1290. documents: Optional[list[dict[str, str]]] = None,
  1291. chat_template: Optional[str] = None,
  1292. add_generation_prompt: bool = False,
  1293. continue_final_message: bool = False,
  1294. tokenize: bool = True,
  1295. padding: Union[bool, str, PaddingStrategy] = False,
  1296. truncation: bool = False,
  1297. max_length: Optional[int] = None,
  1298. return_tensors: Optional[Union[str, TensorType]] = None,
  1299. return_dict: bool = False,
  1300. return_assistant_tokens_mask: bool = False,
  1301. tokenizer_kwargs: Optional[dict[str, Any]] = None,
  1302. **kwargs,
  1303. ) -> Union[str, list[int], list[str], list[list[int]], BatchEncoding]:
  1304. """
  1305. Converts a list of dictionaries with `"role"` and `"content"` keys to a list of token
  1306. ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to
  1307. determine the format and control tokens to use when converting.
  1308. Args:
  1309. conversation (Union[list[dict[str, str]], list[list[dict[str, str]]]]): A list of dicts
  1310. with "role" and "content" keys, representing the chat history so far.
  1311. tools (`list[Union[Dict, Callable]]`, *optional*):
  1312. A list of tools (callable functions) that will be accessible to the model. If the template does not
  1313. support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema,
  1314. giving the name, description and argument types for the tool. See our
  1315. [tool use guide](https://huggingface.co/docs/transformers/en/chat_extras#passing-tools)
  1316. for more information.
  1317. documents (`list[dict[str, str]]`, *optional*):
  1318. A list of dicts representing documents that will be accessible to the model if it is performing RAG
  1319. (retrieval-augmented generation). If the template does not support RAG, this argument will have no
  1320. effect. We recommend that each document should be a dict containing "title" and "text" keys.
  1321. chat_template (`str`, *optional*):
  1322. A Jinja template to use for this conversion. It is usually not necessary to pass anything to this
  1323. argument, as the model's template will be used by default.
  1324. add_generation_prompt (bool, *optional*):
  1325. If this is set, a prompt with the token(s) that indicate
  1326. the start of an assistant message will be appended to the formatted output. This is useful when you want to generate a response from the model.
  1327. Note that this argument will be passed to the chat template, and so it must be supported in the
  1328. template for this argument to have any effect.
  1329. continue_final_message (bool, *optional*):
  1330. If this is set, the chat will be formatted so that the final
  1331. message in the chat is open-ended, without any EOS tokens. The model will continue this message
  1332. rather than starting a new one. This allows you to "prefill" part of
  1333. the model's response for it. Cannot be used at the same time as `add_generation_prompt`.
  1334. tokenize (`bool`, defaults to `True`):
  1335. Whether to tokenize the output. If `False`, the output will be a string.
  1336. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
  1337. Select a strategy to pad the returned sequences (according to the model's padding side and padding
  1338. index) among:
  1339. - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
  1340. sequence if provided).
  1341. - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
  1342. acceptable input length for the model if that argument is not provided.
  1343. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
  1344. lengths).
  1345. truncation (`bool`, defaults to `False`):
  1346. Whether to truncate sequences at the maximum length. Has no effect if tokenize is `False`.
  1347. max_length (`int`, *optional*):
  1348. Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is `False`. If
  1349. not specified, the tokenizer's `max_length` attribute will be used as a default.
  1350. return_tensors (`str` or [`~utils.TensorType`], *optional*):
  1351. If set, will return tensors of a particular framework. Has no effect if tokenize is `False`. Acceptable
  1352. values are:
  1353. - `'tf'`: Return TensorFlow `tf.Tensor` objects.
  1354. - `'pt'`: Return PyTorch `torch.Tensor` objects.
  1355. - `'np'`: Return NumPy `np.ndarray` objects.
  1356. - `'jax'`: Return JAX `jnp.ndarray` objects.
  1357. return_dict (`bool`, defaults to `False`):
  1358. Whether to return a dictionary with named outputs. Has no effect if tokenize is `False`.
  1359. tokenizer_kwargs (`dict[str: Any]`, *optional*): Additional kwargs to pass to the tokenizer.
  1360. return_assistant_tokens_mask (`bool`, defaults to `False`):
  1361. Whether to return a mask of the assistant generated tokens. For tokens generated by the assistant,
  1362. the mask will contain 1. For user and system tokens, the mask will contain 0.
  1363. This functionality is only available for chat templates that support it via the `{% generation %}` keyword.
  1364. **kwargs: Additional kwargs to pass to the template renderer. Will be accessible by the chat template.
  1365. Returns:
  1366. `Union[list[int], Dict]`: A list of token ids representing the tokenized chat so far, including control tokens. This
  1367. output is ready to pass to the model, either directly or via methods like `generate()`. If `return_dict` is
  1368. set, will return a dict of tokenizer outputs instead.
  1369. """
  1370. if return_dict and not tokenize:
  1371. raise ValueError(
  1372. "`return_dict=True` is incompatible with `tokenize=False`, because there is no dict "
  1373. "of tokenizer outputs to return."
  1374. )
  1375. if return_assistant_tokens_mask and not return_dict:
  1376. raise ValueError("`return_assistant_tokens_mask=True` is incompatible with `return_dict=False`")
  1377. if tokenizer_kwargs is None:
  1378. tokenizer_kwargs = {}
  1379. chat_template = self.get_chat_template(chat_template, tools)
  1380. if isinstance(conversation, (list, tuple)) and (
  1381. isinstance(conversation[0], (list, tuple)) or hasattr(conversation[0], "messages")
  1382. ):
  1383. conversations = conversation
  1384. is_batched = True
  1385. else:
  1386. conversations = [conversation]
  1387. is_batched = False
  1388. if continue_final_message:
  1389. if add_generation_prompt:
  1390. raise ValueError(
  1391. "continue_final_message and add_generation_prompt are not compatible. Use continue_final_message when you want the model to continue the final message, and add_generation_prompt when you want to add a header that will prompt it to start a new assistant message instead."
  1392. )
  1393. if return_assistant_tokens_mask:
  1394. raise ValueError("continue_final_message is not compatible with return_assistant_tokens_mask.")
  1395. template_kwargs = {**self.special_tokens_map, **kwargs} # kwargs overwrite special tokens if both are present
  1396. rendered_chat, generation_indices = render_jinja_template(
  1397. conversations=conversations,
  1398. tools=tools,
  1399. documents=documents,
  1400. chat_template=chat_template,
  1401. return_assistant_tokens_mask=return_assistant_tokens_mask,
  1402. continue_final_message=continue_final_message,
  1403. add_generation_prompt=add_generation_prompt,
  1404. **template_kwargs,
  1405. )
  1406. if not is_batched:
  1407. rendered_chat = rendered_chat[0]
  1408. if tokenize:
  1409. out = self(
  1410. rendered_chat,
  1411. padding=padding,
  1412. truncation=truncation,
  1413. max_length=max_length,
  1414. add_special_tokens=False,
  1415. return_tensors=return_tensors,
  1416. **tokenizer_kwargs,
  1417. )
  1418. if return_dict:
  1419. if return_assistant_tokens_mask:
  1420. assistant_masks = []
  1421. if is_batched or return_tensors:
  1422. input_ids = out["input_ids"]
  1423. else:
  1424. input_ids = [out["input_ids"]]
  1425. for i in range(len(input_ids)):
  1426. current_mask = [0] * len(input_ids[i])
  1427. for assistant_start_char, assistant_end_char in generation_indices[i]:
  1428. start_token = out.char_to_token(i, assistant_start_char)
  1429. end_token = out.char_to_token(i, assistant_end_char - 1)
  1430. if start_token is None:
  1431. # start_token is out of bounds maybe due to truncation.
  1432. break
  1433. for token_id in range(start_token, end_token + 1 if end_token else len(input_ids[i])):
  1434. current_mask[token_id] = 1
  1435. assistant_masks.append(current_mask)
  1436. if not is_batched and not return_tensors:
  1437. assistant_masks = assistant_masks[0]
  1438. out["assistant_masks"] = assistant_masks
  1439. if return_tensors:
  1440. out.convert_to_tensors(tensor_type=return_tensors)
  1441. return out
  1442. else:
  1443. return out["input_ids"]
  1444. else:
  1445. return rendered_chat
  1446. def encode_message_with_chat_template(
  1447. self,
  1448. message: dict[str, str],
  1449. conversation_history: Optional[list[dict[str, str]]] = None,
  1450. **kwargs,
  1451. ) -> list[int]:
  1452. """
  1453. Tokenize a single message. This method is a convenience wrapper around `apply_chat_template` that allows you
  1454. to tokenize messages one by one. This is useful for things like token-by-token streaming.
  1455. This method is not guaranteed to be perfect. For some models, it may be impossible to robustly tokenize
  1456. single messages. For example, if the chat template adds tokens after each message, but also has a prefix that
  1457. is added to the entire chat, it will be impossible to distinguish a chat-start-token from a message-start-token.
  1458. In these cases, this method will do its best to find the correct tokenization, but it may not be perfect.
  1459. **Note:** This method does not support `add_generation_prompt`. If you want to add a generation prompt,
  1460. you should do it separately after tokenizing the conversation.
  1461. Args:
  1462. message (`dict`):
  1463. A dictionary with "role" and "content" keys, representing the message to tokenize.
  1464. conversation_history (`list[dict]`, *optional*):
  1465. A list of dicts with "role" and "content" keys, representing the chat history so far. If you are
  1466. tokenizing messages one by one, you should pass the previous messages in the conversation here.
  1467. **kwargs:
  1468. Additional kwargs to pass to the `apply_chat_template` method.
  1469. Returns:
  1470. `list[int]`: A list of token ids representing the tokenized message.
  1471. """
  1472. if "add_generation_prompt" in kwargs:
  1473. raise ValueError(
  1474. "`encode_message_with_chat_template` does not support `add_generation_prompt`. Please add the generation prompt "
  1475. "separately."
  1476. )
  1477. if conversation_history is None or len(conversation_history) == 0:
  1478. return self.apply_chat_template([message], add_generation_prompt=False, tokenize=True, **kwargs)
  1479. conversation = conversation_history + [message]
  1480. tokens = self.apply_chat_template(conversation, add_generation_prompt=False, tokenize=True, **kwargs)
  1481. prefix_tokens = self.apply_chat_template(
  1482. conversation_history, add_generation_prompt=False, tokenize=True, **kwargs
  1483. )
  1484. # It's possible that the prefix tokens are not a prefix of the full list of tokens.
  1485. # For example, if the prefix is `<s>User: Hi` and the full conversation is `<s>User: Hi</s><s>Assistant: Hello`.
  1486. # In this case, we can't simply find the prefix, so we have to do something a bit more subtle.
  1487. # We look for the first place where the tokens differ, and that's our split point.
  1488. # This is not perfect, but it's the best we can do without a token-level API.
  1489. # To make this more robust, we could do a diff and find the longest common subsequence, but this is
  1490. # a good first approximation.
  1491. # This is particularly important for models like Llama3 that have changed their chat template to include
  1492. # EOS tokens after user messages.
  1493. min_len = min(len(prefix_tokens), len(tokens))
  1494. for i in range(min_len):
  1495. if prefix_tokens[i] != tokens[i]:
  1496. return tokens[i:]
  1497. return tokens[min_len:]
  1498. def get_chat_template(self, chat_template: Optional[str] = None, tools: Optional[list[dict]] = None) -> str:
  1499. """
  1500. Retrieve the chat template string used for tokenizing chat messages. This template is used
  1501. internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat
  1502. template for better generation tracking.
  1503. Args:
  1504. chat_template (`str`, *optional*):
  1505. A Jinja template or the name of a template to use for this conversion.
  1506. It is usually not necessary to pass anything to this argument,
  1507. as the model's template will be used by default.
  1508. tools (`list[Dict]`, *optional*):
  1509. A list of tools (callable functions) that will be accessible to the model. If the template does not
  1510. support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema,
  1511. giving the name, description and argument types for the tool. See our
  1512. [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use)
  1513. for more information.
  1514. Returns:
  1515. `str`: The chat template string.
  1516. """
  1517. # First, handle the cases when the model has a dict of multiple templates
  1518. if isinstance(self.chat_template, dict):
  1519. template_dict = self.chat_template
  1520. if chat_template is not None and chat_template in template_dict:
  1521. # The user can pass the name of a template to the chat template argument instead of an entire template
  1522. chat_template = template_dict[chat_template]
  1523. elif chat_template is None:
  1524. if tools is not None and "tool_use" in template_dict:
  1525. chat_template = template_dict["tool_use"]
  1526. elif "default" in template_dict:
  1527. chat_template = template_dict["default"]
  1528. else:
  1529. raise ValueError(
  1530. "This model has multiple chat templates with no default specified! Please either pass a chat "
  1531. "template or the name of the template you wish to use to the `chat_template` argument. Available "
  1532. f"template names are {sorted(template_dict.keys())}."
  1533. )
  1534. elif chat_template is None:
  1535. # These are the cases when the model has a single template
  1536. # priority: `chat_template` argument > `tokenizer.chat_template`
  1537. if self.chat_template is not None:
  1538. chat_template = self.chat_template
  1539. else:
  1540. raise ValueError(
  1541. "Cannot use chat template functions because tokenizer.chat_template is not set and no template "
  1542. "argument was passed! For information about writing templates and setting the "
  1543. "tokenizer.chat_template attribute, please see the documentation at "
  1544. "https://huggingface.co/docs/transformers/main/en/chat_templating"
  1545. )
  1546. return chat_template
  1547. @classmethod
  1548. def from_pretrained(
  1549. cls,
  1550. pretrained_model_name_or_path: Union[str, os.PathLike],
  1551. *init_inputs,
  1552. cache_dir: Optional[Union[str, os.PathLike]] = None,
  1553. force_download: bool = False,
  1554. local_files_only: bool = False,
  1555. token: Optional[Union[str, bool]] = None,
  1556. revision: str = "main",
  1557. trust_remote_code=False,
  1558. **kwargs,
  1559. ):
  1560. r"""
  1561. Instantiate a [`~tokenization_utils_base.PreTrainedTokenizerBase`] (or a derived class) from a predefined
  1562. tokenizer.
  1563. Args:
  1564. pretrained_model_name_or_path (`str` or `os.PathLike`):
  1565. Can be either:
  1566. - A string, the *model id* of a predefined tokenizer hosted inside a model repo on huggingface.co.
  1567. - A path to a *directory* containing vocabulary files required by the tokenizer, for instance saved
  1568. using the [`~tokenization_utils_base.PreTrainedTokenizerBase.save_pretrained`] method, e.g.,
  1569. `./my_model_directory/`.
  1570. - (**Deprecated**, not applicable to all derived classes) A path or url to a single saved vocabulary
  1571. file (if and only if the tokenizer only requires a single vocabulary file like Bert or XLNet), e.g.,
  1572. `./my_model_directory/vocab.txt`.
  1573. cache_dir (`str` or `os.PathLike`, *optional*):
  1574. Path to a directory in which a downloaded predefined tokenizer vocabulary files should be cached if the
  1575. standard cache should not be used.
  1576. force_download (`bool`, *optional*, defaults to `False`):
  1577. Whether or not to force the (re-)download the vocabulary files and override the cached versions if they
  1578. exist.
  1579. resume_download:
  1580. Deprecated and ignored. All downloads are now resumed by default when possible.
  1581. Will be removed in v5 of Transformers.
  1582. proxies (`dict[str, str]`, *optional*):
  1583. A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
  1584. 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
  1585. token (`str` or *bool*, *optional*):
  1586. The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
  1587. when running `hf auth login` (stored in `~/.huggingface`).
  1588. local_files_only (`bool`, *optional*, defaults to `False`):
  1589. Whether or not to only rely on local files and not to attempt to download any files.
  1590. revision (`str`, *optional*, defaults to `"main"`):
  1591. The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
  1592. git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
  1593. identifier allowed by git.
  1594. subfolder (`str`, *optional*):
  1595. In case the relevant files are located inside a subfolder of the model repo on huggingface.co (e.g. for
  1596. facebook/rag-token-base), specify it here.
  1597. inputs (additional positional arguments, *optional*):
  1598. Will be passed along to the Tokenizer `__init__` method.
  1599. trust_remote_code (`bool`, *optional*, defaults to `False`):
  1600. Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
  1601. should only be set to `True` for repositories you trust and in which you have read the code, as it will
  1602. execute code present on the Hub on your local machine.
  1603. kwargs (additional keyword arguments, *optional*):
  1604. Will be passed to the Tokenizer `__init__` method. Can be used to set special tokens like `bos_token`,
  1605. `eos_token`, `unk_token`, `sep_token`, `pad_token`, `cls_token`, `mask_token`,
  1606. `additional_special_tokens`. See parameters in the `__init__` for more details.
  1607. <Tip>
  1608. Passing `token=True` is required when you want to use a private model.
  1609. </Tip>
  1610. Examples:
  1611. ```python
  1612. # We can't instantiate directly the base class *PreTrainedTokenizerBase* so let's show our examples on a derived class: BertTokenizer
  1613. # Download vocabulary from huggingface.co and cache.
  1614. tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
  1615. # Download vocabulary from huggingface.co (user-uploaded) and cache.
  1616. tokenizer = BertTokenizer.from_pretrained("dbmdz/bert-base-german-cased")
  1617. # If vocabulary files are in a directory (e.g. tokenizer was saved using *save_pretrained('./test/saved_model/')*)
  1618. tokenizer = BertTokenizer.from_pretrained("./test/saved_model/")
  1619. # If the tokenizer uses a single vocabulary file, you can point directly to this file
  1620. tokenizer = BertTokenizer.from_pretrained("./test/saved_model/my_vocab.txt")
  1621. # You can link tokens to special vocabulary when instantiating
  1622. tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased", unk_token="<unk>")
  1623. # You should be sure '<unk>' is in the vocabulary when doing that.
  1624. # Otherwise use tokenizer.add_special_tokens({'unk_token': '<unk>'}) instead)
  1625. assert tokenizer.unk_token == "<unk>"
  1626. ```"""
  1627. resume_download = kwargs.pop("resume_download", None)
  1628. proxies = kwargs.pop("proxies", None)
  1629. use_auth_token = kwargs.pop("use_auth_token", None)
  1630. subfolder = kwargs.pop("subfolder", None)
  1631. from_pipeline = kwargs.pop("_from_pipeline", None)
  1632. from_auto_class = kwargs.pop("_from_auto", False)
  1633. commit_hash = kwargs.pop("_commit_hash", None)
  1634. gguf_file = kwargs.get("gguf_file")
  1635. if use_auth_token is not None:
  1636. warnings.warn(
  1637. "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.",
  1638. FutureWarning,
  1639. )
  1640. if token is not None:
  1641. raise ValueError(
  1642. "`token` and `use_auth_token` are both specified. Please set only the argument `token`."
  1643. )
  1644. token = use_auth_token
  1645. user_agent = {"file_type": "tokenizer", "from_auto_class": from_auto_class, "is_fast": "Fast" in cls.__name__}
  1646. if from_pipeline is not None:
  1647. user_agent["using_pipeline"] = from_pipeline
  1648. if is_offline_mode() and not local_files_only:
  1649. logger.info("Offline mode: forcing local_files_only=True")
  1650. local_files_only = True
  1651. pretrained_model_name_or_path = str(pretrained_model_name_or_path)
  1652. vocab_files = {}
  1653. init_configuration = {}
  1654. is_local = os.path.isdir(pretrained_model_name_or_path)
  1655. single_file_id = None
  1656. if os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path):
  1657. if len(cls.vocab_files_names) > 1 and not gguf_file:
  1658. raise ValueError(
  1659. f"Calling {cls.__name__}.from_pretrained() with the path to a single file or url is not "
  1660. "supported for this tokenizer. Use a model identifier or the path to a directory instead."
  1661. )
  1662. warnings.warn(
  1663. f"Calling {cls.__name__}.from_pretrained() with the path to a single file or url is deprecated and "
  1664. "won't be possible anymore in v5. Use a model identifier or the path to a directory instead.",
  1665. FutureWarning,
  1666. )
  1667. file_id = list(cls.vocab_files_names.keys())[0]
  1668. vocab_files[file_id] = pretrained_model_name_or_path
  1669. single_file_id = file_id
  1670. else:
  1671. if gguf_file:
  1672. vocab_files["vocab_file"] = gguf_file
  1673. else:
  1674. # At this point pretrained_model_name_or_path is either a directory or a model identifier name
  1675. additional_files_names = {
  1676. "added_tokens_file": ADDED_TOKENS_FILE, # kept only for legacy
  1677. "special_tokens_map_file": SPECIAL_TOKENS_MAP_FILE, # kept only for legacy
  1678. "tokenizer_config_file": TOKENIZER_CONFIG_FILE,
  1679. # tokenizer_file used to initialize a slow from a fast. Properly copy the `addedTokens` instead of adding in random orders
  1680. "tokenizer_file": FULL_TOKENIZER_FILE,
  1681. "chat_template_file": CHAT_TEMPLATE_FILE,
  1682. }
  1683. vocab_files = {**cls.vocab_files_names, **additional_files_names}
  1684. if "tokenizer_file" in vocab_files:
  1685. # Try to get the tokenizer config to see if there are versioned tokenizer files.
  1686. fast_tokenizer_file = FULL_TOKENIZER_FILE
  1687. try:
  1688. resolved_config_file = cached_file(
  1689. pretrained_model_name_or_path,
  1690. TOKENIZER_CONFIG_FILE,
  1691. cache_dir=cache_dir,
  1692. force_download=force_download,
  1693. resume_download=resume_download,
  1694. proxies=proxies,
  1695. token=token,
  1696. revision=revision,
  1697. local_files_only=local_files_only,
  1698. subfolder=subfolder,
  1699. user_agent=user_agent,
  1700. _raise_exceptions_for_missing_entries=False,
  1701. _commit_hash=commit_hash,
  1702. )
  1703. except OSError:
  1704. # Re-raise any error raised by cached_file in order to get a helpful error message
  1705. raise
  1706. except Exception:
  1707. # For any other exception, we throw a generic error.
  1708. raise OSError(
  1709. f"Can't load tokenizer for '{pretrained_model_name_or_path}'. If you were trying to load it from "
  1710. "'https://huggingface.co/models', make sure you don't have a local directory with the same name. "
  1711. f"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory "
  1712. f"containing all relevant files for a {cls.__name__} tokenizer."
  1713. )
  1714. commit_hash = extract_commit_hash(resolved_config_file, commit_hash)
  1715. if resolved_config_file is not None:
  1716. with open(resolved_config_file, encoding="utf-8") as reader:
  1717. tokenizer_config = json.load(reader)
  1718. if "fast_tokenizer_files" in tokenizer_config:
  1719. fast_tokenizer_file = get_fast_tokenizer_file(tokenizer_config["fast_tokenizer_files"])
  1720. vocab_files["tokenizer_file"] = fast_tokenizer_file
  1721. # This block looks for any extra chat template files
  1722. if is_local:
  1723. template_dir = Path(pretrained_model_name_or_path, CHAT_TEMPLATE_DIR)
  1724. if template_dir.is_dir():
  1725. for template_file in template_dir.glob("*.jinja"):
  1726. template_name = template_file.name.removesuffix(".jinja")
  1727. vocab_files[f"chat_template_{template_name}"] = (
  1728. f"{CHAT_TEMPLATE_DIR}/{template_file.name}"
  1729. )
  1730. else:
  1731. for template in list_repo_templates(
  1732. pretrained_model_name_or_path,
  1733. local_files_only=local_files_only,
  1734. revision=revision,
  1735. cache_dir=cache_dir,
  1736. token=token,
  1737. ):
  1738. template = template.removesuffix(".jinja")
  1739. vocab_files[f"chat_template_{template}"] = f"{CHAT_TEMPLATE_DIR}/{template}.jinja"
  1740. remote_files = []
  1741. if not is_local and not local_files_only:
  1742. try:
  1743. remote_files = list_repo_files(pretrained_model_name_or_path)
  1744. except Exception:
  1745. remote_files = []
  1746. elif pretrained_model_name_or_path and os.path.isdir(pretrained_model_name_or_path):
  1747. remote_files = os.listdir(pretrained_model_name_or_path)
  1748. if "tokenizer_file" in vocab_files and not re.search(vocab_files["tokenizer_file"], "".join(remote_files)):
  1749. # mistral tokenizer names are different, but we can still convert them if
  1750. # mistral common is not there
  1751. other_pattern = r"tekken\.json|tokenizer\.model\.*"
  1752. if match := re.search(other_pattern, "\n".join(remote_files)):
  1753. vocab_files["vocab_file"] = match.group()
  1754. resolved_vocab_files = {}
  1755. for file_id, file_path in vocab_files.items():
  1756. if file_path is None:
  1757. resolved_vocab_files[file_id] = None
  1758. elif single_file_id == file_id:
  1759. if os.path.isfile(file_path):
  1760. resolved_vocab_files[file_id] = file_path
  1761. elif is_remote_url(file_path):
  1762. resolved_vocab_files[file_id] = download_url(file_path, proxies=proxies)
  1763. else:
  1764. try:
  1765. resolved_vocab_files[file_id] = cached_file(
  1766. pretrained_model_name_or_path,
  1767. file_path,
  1768. cache_dir=cache_dir,
  1769. force_download=force_download,
  1770. proxies=proxies,
  1771. resume_download=resume_download,
  1772. local_files_only=local_files_only,
  1773. token=token,
  1774. user_agent=user_agent,
  1775. revision=revision,
  1776. subfolder=subfolder,
  1777. _raise_exceptions_for_missing_entries=False,
  1778. _commit_hash=commit_hash,
  1779. )
  1780. except OSError:
  1781. # Re-raise any error raised by cached_file in order to get a helpful error message
  1782. raise
  1783. except Exception:
  1784. # For any other exception, we throw a generic error.
  1785. raise OSError(
  1786. f"Can't load tokenizer for '{pretrained_model_name_or_path}'. If you were trying to load it from "
  1787. "'https://huggingface.co/models', make sure you don't have a local directory with the same name. "
  1788. f"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory "
  1789. f"containing all relevant files for a {cls.__name__} tokenizer."
  1790. )
  1791. commit_hash = extract_commit_hash(resolved_vocab_files[file_id], commit_hash)
  1792. for file_id, file_path in vocab_files.items():
  1793. if file_id not in resolved_vocab_files:
  1794. continue
  1795. if is_local:
  1796. logger.info(f"loading file {file_path}")
  1797. else:
  1798. logger.info(f"loading file {file_path} from cache at {resolved_vocab_files[file_id]}")
  1799. return cls._from_pretrained(
  1800. resolved_vocab_files,
  1801. pretrained_model_name_or_path,
  1802. init_configuration,
  1803. *init_inputs,
  1804. token=token,
  1805. cache_dir=cache_dir,
  1806. local_files_only=local_files_only,
  1807. _commit_hash=commit_hash,
  1808. _is_local=is_local,
  1809. trust_remote_code=trust_remote_code,
  1810. **kwargs,
  1811. )
  1812. @classmethod
  1813. def _from_pretrained(
  1814. cls,
  1815. resolved_vocab_files,
  1816. pretrained_model_name_or_path,
  1817. init_configuration,
  1818. *init_inputs,
  1819. token=None,
  1820. cache_dir=None,
  1821. local_files_only=False,
  1822. _commit_hash=None,
  1823. _is_local=False,
  1824. trust_remote_code=False,
  1825. **kwargs,
  1826. ):
  1827. # We instantiate fast tokenizers based on a slow tokenizer if we don't have access to the tokenizer.json
  1828. # file or if `from_slow` is set to True.
  1829. from_slow = kwargs.get("from_slow", False)
  1830. gguf_file = kwargs.get("gguf_file")
  1831. has_tokenizer_file = resolved_vocab_files.get("tokenizer_file", None) is not None
  1832. # If one passes a GGUF file path to `gguf_file` there is no need for this check as the tokenizer will be
  1833. # loaded directly from the GGUF file.
  1834. if (from_slow or not has_tokenizer_file) and cls.slow_tokenizer_class is not None and not gguf_file:
  1835. slow_tokenizer = (cls.slow_tokenizer_class)._from_pretrained(
  1836. copy.deepcopy(resolved_vocab_files),
  1837. pretrained_model_name_or_path,
  1838. copy.deepcopy(init_configuration),
  1839. *init_inputs,
  1840. token=token,
  1841. cache_dir=cache_dir,
  1842. local_files_only=local_files_only,
  1843. _commit_hash=_commit_hash,
  1844. **(copy.deepcopy(kwargs)),
  1845. )
  1846. else:
  1847. slow_tokenizer = None
  1848. # Prepare tokenizer initialization kwargs
  1849. # Did we saved some inputs and kwargs to reload ?
  1850. tokenizer_config_file = resolved_vocab_files.pop("tokenizer_config_file", None)
  1851. if tokenizer_config_file is not None:
  1852. with open(tokenizer_config_file, encoding="utf-8") as tokenizer_config_handle:
  1853. init_kwargs = json.load(tokenizer_config_handle)
  1854. # First attempt. We get tokenizer_class from tokenizer_config to check mismatch between tokenizers.
  1855. config_tokenizer_class = init_kwargs.get("tokenizer_class")
  1856. init_kwargs.pop("tokenizer_class", None)
  1857. if not has_tokenizer_file:
  1858. init_kwargs.pop("tokenizer_file", None)
  1859. saved_init_inputs = init_kwargs.pop("init_inputs", ())
  1860. if not init_inputs:
  1861. init_inputs = saved_init_inputs
  1862. else:
  1863. config_tokenizer_class = None
  1864. init_kwargs = init_configuration
  1865. # If independent chat template file(s) exist, they take priority over template entries in the tokenizer config
  1866. chat_templates = {}
  1867. chat_template_file = resolved_vocab_files.pop("chat_template_file", None)
  1868. extra_chat_templates = [key for key in resolved_vocab_files if key.startswith("chat_template_")]
  1869. if chat_template_file is not None:
  1870. with open(chat_template_file, encoding="utf-8") as chat_template_handle:
  1871. chat_templates["default"] = chat_template_handle.read()
  1872. for extra_chat_template in extra_chat_templates:
  1873. template_file = resolved_vocab_files.pop(extra_chat_template, None)
  1874. if template_file is None:
  1875. continue # I think this should never happen, but just in case
  1876. template_name = extra_chat_template.removeprefix("chat_template_")
  1877. with open(template_file, encoding="utf8") as chat_template_handle:
  1878. chat_templates[template_name] = chat_template_handle.read()
  1879. if len(chat_templates) == 1 and "default" in chat_templates:
  1880. init_kwargs["chat_template"] = chat_templates["default"]
  1881. elif chat_templates:
  1882. init_kwargs["chat_template"] = chat_templates
  1883. if not _is_local:
  1884. if "auto_map" in init_kwargs:
  1885. # For backward compatibility with odl format.
  1886. if isinstance(init_kwargs["auto_map"], (tuple, list)):
  1887. init_kwargs["auto_map"] = {"AutoTokenizer": init_kwargs["auto_map"]}
  1888. if config_tokenizer_class is None:
  1889. # Matt: This entire block is only used to decide if the tokenizer class matches the class in the repo.
  1890. # If not, it raises a warning, but otherwise continues. Since we mostly load tokenizers with
  1891. # AutoTokenizer these days, it seems like a lot of work (and a source of bugs) for little gain.
  1892. # Maybe we can just remove this entirely?
  1893. from .models.auto.configuration_auto import AutoConfig # tests_ignore
  1894. # Second attempt. If we have not yet found tokenizer_class, let's try to use the config.
  1895. try:
  1896. config = AutoConfig.from_pretrained(
  1897. pretrained_model_name_or_path,
  1898. token=token,
  1899. cache_dir=cache_dir,
  1900. local_files_only=local_files_only,
  1901. trust_remote_code=trust_remote_code,
  1902. _commit_hash=_commit_hash,
  1903. )
  1904. config_tokenizer_class = config.tokenizer_class
  1905. except (OSError, ValueError, KeyError):
  1906. # skip if an error occurred.
  1907. config = None
  1908. if config_tokenizer_class is None:
  1909. # Third attempt. If we have not yet found the original type of the tokenizer,
  1910. # we are loading we see if we can infer it from the type of the configuration file
  1911. from .models.auto.tokenization_auto import TOKENIZER_MAPPING_NAMES # tests_ignore
  1912. if hasattr(config, "model_type"):
  1913. model_type = config.model_type
  1914. else:
  1915. # Fallback: use pattern matching on the string.
  1916. model_type = None
  1917. for pattern in TOKENIZER_MAPPING_NAMES:
  1918. if pattern in str(pretrained_model_name_or_path):
  1919. model_type = pattern
  1920. break
  1921. if model_type is not None:
  1922. config_tokenizer_class, config_tokenizer_class_fast = TOKENIZER_MAPPING_NAMES.get(
  1923. model_type, (None, None)
  1924. )
  1925. if config_tokenizer_class is None:
  1926. config_tokenizer_class = config_tokenizer_class_fast
  1927. if config_tokenizer_class is not None:
  1928. if cls.__name__.replace("Fast", "") != config_tokenizer_class.replace("Fast", ""):
  1929. logger.warning(
  1930. "The tokenizer class you load from this checkpoint is not the same type as the class this"
  1931. " function is called from. It may result in unexpected tokenization. \nThe tokenizer class you"
  1932. f" load from this checkpoint is '{config_tokenizer_class}'. \nThe class this function is called"
  1933. f" from is '{cls.__name__}'."
  1934. )
  1935. # Update with newly provided kwargs
  1936. init_kwargs.update(kwargs)
  1937. # Merge resolved_vocab_files arguments in init_kwargs.
  1938. added_tokens_file = resolved_vocab_files.pop("added_tokens_file", None)
  1939. special_tokens_map_file = resolved_vocab_files.pop("special_tokens_map_file", None)
  1940. for args_name, file_path in resolved_vocab_files.items():
  1941. if args_name not in init_kwargs:
  1942. init_kwargs[args_name] = file_path
  1943. tokenizer_file = resolved_vocab_files.pop("tokenizer_file", None)
  1944. if slow_tokenizer is not None:
  1945. init_kwargs["__slow_tokenizer"] = slow_tokenizer
  1946. init_kwargs["name_or_path"] = pretrained_model_name_or_path
  1947. #### Handle tokenizer serialization of added and special tokens
  1948. added_tokens_decoder: dict[int, AddedToken] = {}
  1949. added_tokens_map: dict[str, AddedToken] = {}
  1950. # if we have info on the slow added tokens
  1951. if "added_tokens_decoder" in init_kwargs:
  1952. for idx, token in init_kwargs["added_tokens_decoder"].items():
  1953. if isinstance(token, dict):
  1954. token = AddedToken(**token)
  1955. if isinstance(token, AddedToken):
  1956. added_tokens_decoder[int(idx)] = token
  1957. added_tokens_map[str(token)] = token
  1958. else:
  1959. raise TypeError(
  1960. f"Found a {token.__class__} in the saved `added_tokens_decoder`, should be a dictionary or an AddedToken instance"
  1961. )
  1962. else:
  1963. # begin legacy: read the added_tokens_file and update kwargs with special_tokens_map if modified
  1964. if special_tokens_map_file is not None:
  1965. with open(special_tokens_map_file, encoding="utf-8") as special_tokens_map_handle:
  1966. special_tokens_map = json.load(special_tokens_map_handle)
  1967. for key, value in special_tokens_map.items():
  1968. if key in kwargs and kwargs[key]:
  1969. # This value has already been redefined by the kwargs
  1970. # We keep this new value and ignore the one stored in the special_tokens_map_file
  1971. continue
  1972. if isinstance(value, dict):
  1973. value["special"] = True
  1974. value = AddedToken(**value)
  1975. elif key == "additional_special_tokens" and isinstance(value, list):
  1976. additional_special_tokens = init_kwargs.pop("additional_special_tokens", []) or []
  1977. for token in value:
  1978. if isinstance(token, dict):
  1979. token["special"] = True
  1980. token = AddedToken(**token)
  1981. if token not in additional_special_tokens:
  1982. additional_special_tokens.append(token)
  1983. value = additional_special_tokens
  1984. init_kwargs[key] = value
  1985. # slow -> slow|fast, legacy: convert the `"added_tokens.json"` file to `added_tokens_decoder`.
  1986. # this is for legacy purpose. We don't add the tokens after init for efficiency.
  1987. if added_tokens_file is not None:
  1988. special_tokens = []
  1989. for key in cls.SPECIAL_TOKENS_ATTRIBUTES & init_kwargs.keys():
  1990. if init_kwargs[key] is not None:
  1991. if key == "additional_special_tokens":
  1992. special_tokens += [str(token) for token in init_kwargs[key]]
  1993. else:
  1994. special_tokens.append(str(init_kwargs[key]))
  1995. with open(added_tokens_file, encoding="utf-8") as added_tokens_handle:
  1996. added_tok_encoder = json.load(added_tokens_handle)
  1997. for str_token, index in added_tok_encoder.items():
  1998. # if index not in added_tokens_decoder and str_token not in added_tokens_map:
  1999. special = str_token in special_tokens
  2000. added_tokens_decoder[index] = AddedToken(
  2001. str_token, rstrip=False, lstrip=False, normalized=not special, special=special
  2002. )
  2003. added_tokens_map[str(token)] = added_tokens_decoder[index]
  2004. # allows converting a fast -> slow: add the `tokenizer.json`'s `"added_tokens"` to the slow tokenizer
  2005. # if `tokenizer_config.json` is `None`
  2006. if tokenizer_file is not None:
  2007. # This is for slow so can be done before
  2008. with open(tokenizer_file, encoding="utf-8") as tokenizer_file_handle:
  2009. tokenizer_file_handle = json.load(tokenizer_file_handle)
  2010. added_tokens = tokenizer_file_handle.pop("added_tokens")
  2011. for serialized_tokens in added_tokens:
  2012. idx = serialized_tokens.pop("id")
  2013. added_tokens_decoder[idx] = AddedToken(**serialized_tokens)
  2014. added_tokens_map[str(added_tokens_decoder[idx])] = added_tokens_decoder[idx]
  2015. # end legacy
  2016. # Passing AddedTokens and not strings to the class to prevent it from casting the string to a different AddedToken
  2017. # convert {'__type': 'AddedToken', 'content': '<ent>', 'lstrip': False, 'normalized': True, ...} to AddedTokens
  2018. init_kwargs["added_tokens_decoder"] = added_tokens_decoder
  2019. init_kwargs = cls.convert_added_tokens(init_kwargs, save=False)
  2020. for key in cls.SPECIAL_TOKENS_ATTRIBUTES & init_kwargs.keys():
  2021. if added_tokens_map != {} and init_kwargs[key] is not None:
  2022. if key != "additional_special_tokens":
  2023. init_kwargs[key] = added_tokens_map.get(str(init_kwargs[key]), init_kwargs[key])
  2024. # Instantiate the tokenizer.
  2025. try:
  2026. tokenizer = cls(*init_inputs, **init_kwargs)
  2027. except import_protobuf_decode_error():
  2028. logger.info(
  2029. "Unable to load tokenizer model from SPM, loading from TikToken will be attempted instead."
  2030. "(Google protobuf error: Tried to load SPM model with non-SPM vocab file).",
  2031. )
  2032. return False
  2033. except RuntimeError as e:
  2034. if "sentencepiece_processor.cc" in str(e):
  2035. logger.info(
  2036. "Unable to load tokenizer model from SPM, loading from TikToken will be attempted instead."
  2037. "(SentencePiece RuntimeError: Tried to load SPM model with non-SPM vocab file).",
  2038. )
  2039. return False
  2040. except OSError:
  2041. raise OSError(
  2042. "Unable to load vocabulary from file. "
  2043. "Please check that the provided vocabulary is accessible and not corrupted."
  2044. )
  2045. if added_tokens_decoder != {} and max(list(added_tokens_decoder.keys())[-1], 0) > tokenizer.vocab_size:
  2046. logger.info(
  2047. "Special tokens have been added in the vocabulary, make sure the associated word embeddings are"
  2048. " fine-tuned or trained."
  2049. )
  2050. try:
  2051. vocab_size = tokenizer.vocab_size
  2052. except NotImplementedError:
  2053. vocab_size = 0
  2054. # Optionally patches mistral tokenizers with wrong regex
  2055. if (
  2056. vocab_size > 100000
  2057. and hasattr(tokenizer, "_tokenizer")
  2058. and getattr(tokenizer._tokenizer, "pre_tokenizer", None) is not None
  2059. ):
  2060. tokenizer = cls._patch_mistral_regex(
  2061. tokenizer,
  2062. pretrained_model_name_or_path,
  2063. token=token,
  2064. cache_dir=cache_dir,
  2065. local_files_only=local_files_only,
  2066. _commit_hash=_commit_hash,
  2067. _is_local=_is_local,
  2068. init_kwargs=init_kwargs,
  2069. fix_mistral_regex=kwargs.get("fix_mistral_regex"),
  2070. )
  2071. return tokenizer
  2072. @classmethod
  2073. def _patch_mistral_regex(
  2074. cls,
  2075. tokenizer,
  2076. pretrained_model_name_or_path,
  2077. token=None,
  2078. cache_dir=None,
  2079. local_files_only=False,
  2080. _commit_hash=None,
  2081. _is_local=False,
  2082. init_kwargs=None,
  2083. fix_mistral_regex=None,
  2084. ):
  2085. """
  2086. Patches mistral related tokenizers with incorrect regex if detected
  2087. 1) Local file with an associated config saved next to it
  2088. >> Model type one of the mistral models (on older versions)
  2089. 2) Remote models on the hub from official mistral models
  2090. >> Tags including `base_model:.*mistralai`
  2091. """
  2092. from huggingface_hub import model_info
  2093. def is_base_mistral(model_id: str) -> bool:
  2094. model = model_info(model_id)
  2095. if model.tags is not None:
  2096. if re.search("base_model:.*mistralai", "".join(model.tags)):
  2097. return True
  2098. return False
  2099. if _is_local or is_base_mistral(pretrained_model_name_or_path):
  2100. _config_file = cached_file(
  2101. pretrained_model_name_or_path,
  2102. "config.json",
  2103. cache_dir=cache_dir,
  2104. token=token,
  2105. local_files_only=local_files_only,
  2106. _raise_exceptions_for_missing_entries=False,
  2107. _raise_exceptions_for_connection_errors=False,
  2108. _commit_hash=_commit_hash,
  2109. )
  2110. # Detected using a (local) mistral tokenizer
  2111. mistral_config_detected = False
  2112. if _config_file is not None:
  2113. with open(_config_file, encoding="utf-8") as f:
  2114. _config = json.load(f)
  2115. transformers_version = _config.get("transformers_version")
  2116. transformers_model_type = _config.get("model_type")
  2117. # Detect if we can skip the mistral fix by
  2118. # a) having a non-mistral tokenizer
  2119. # b) fixed version of transformers
  2120. if transformers_version and version.parse(transformers_version) <= version.parse("4.57.2"):
  2121. if (
  2122. _is_local
  2123. and transformers_model_type is not None
  2124. and transformers_model_type
  2125. not in [
  2126. "mistral",
  2127. "mistral3",
  2128. "voxtral",
  2129. "ministral",
  2130. "pixtral",
  2131. ]
  2132. ):
  2133. return tokenizer
  2134. elif transformers_version and version.parse(transformers_version) >= version.parse("5.0.0"):
  2135. return tokenizer
  2136. mistral_config_detected = True
  2137. if mistral_config_detected or (not _is_local and is_base_mistral(pretrained_model_name_or_path)):
  2138. # Expose the `fix_mistral_regex` flag on the tokenizer when provided, even if no correction is applied.
  2139. if init_kwargs and "fix_mistral_regex" in init_kwargs:
  2140. setattr(tokenizer, "fix_mistral_regex", init_kwargs["fix_mistral_regex"])
  2141. # only warn if its not explicitly passed
  2142. if fix_mistral_regex is None and not getattr(tokenizer, "fix_mistral_regex", False):
  2143. setattr(tokenizer, "fix_mistral_regex", False)
  2144. logger.warning(
  2145. f"The tokenizer you are loading from '{pretrained_model_name_or_path}'"
  2146. f" with an incorrect regex pattern: https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503/discussions/84#69121093e8b480e709447d5e."
  2147. " This will lead to incorrect tokenization. You should set the `fix_mistral_regex=True` flag when loading this tokenizer to fix this issue."
  2148. )
  2149. elif fix_mistral_regex is True or getattr(tokenizer, "fix_mistral_regex", False):
  2150. setattr(tokenizer, "fix_mistral_regex", True)
  2151. import tokenizers
  2152. tokenizer.backend_tokenizer.pre_tokenizer[0] = tokenizers.pre_tokenizers.Split(
  2153. pattern=tokenizers.Regex(
  2154. r"[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+"
  2155. ),
  2156. behavior="isolated",
  2157. )
  2158. return tokenizer
  2159. @staticmethod
  2160. def _eventually_correct_t5_max_length(pretrained_model_name_or_path, max_model_length, init_max_model_length):
  2161. # This method should be deleted in Transformers v5
  2162. # Its only purpose is to potentially throw a warning
  2163. # that incorrectly defined max lengths of T5's tokenizer are used
  2164. # which we will correct in Transformers v5.
  2165. return max_model_length
  2166. @classmethod
  2167. def convert_added_tokens(cls, obj: Union[AddedToken, Any], save=False, add_type_field=True):
  2168. if isinstance(obj, dict) and "__type" in obj and obj["__type"] == "AddedToken":
  2169. obj.pop("__type")
  2170. return AddedToken(**obj)
  2171. if isinstance(obj, AddedToken) and save:
  2172. obj = obj.__getstate__()
  2173. if add_type_field:
  2174. obj["__type"] = "AddedToken"
  2175. else:
  2176. # Don't save "special" for previous tokenizers
  2177. obj.pop("special")
  2178. return obj
  2179. elif isinstance(obj, (list, tuple)):
  2180. return [cls.convert_added_tokens(o, save=save, add_type_field=add_type_field) for o in obj]
  2181. elif isinstance(obj, dict):
  2182. return {k: cls.convert_added_tokens(v, save=save, add_type_field=add_type_field) for k, v in obj.items()}
  2183. return obj
  2184. def save_chat_templates(
  2185. self,
  2186. save_directory: Union[str, os.PathLike],
  2187. tokenizer_config: dict,
  2188. filename_prefix: Optional[str],
  2189. save_jinja_files: bool,
  2190. ):
  2191. """
  2192. Writes chat templates out to the save directory if we're using the new format, and removes them from
  2193. the tokenizer config if present. If we're using the legacy format, it doesn't write any files, and instead
  2194. writes the templates to the tokenizer config in the correct format.
  2195. """
  2196. chat_template_file = os.path.join(
  2197. save_directory, (filename_prefix + "-" if filename_prefix else "") + CHAT_TEMPLATE_FILE
  2198. )
  2199. chat_template_dir = os.path.join(
  2200. save_directory, (filename_prefix + "-" if filename_prefix else "") + CHAT_TEMPLATE_DIR
  2201. )
  2202. saved_raw_chat_template_files = []
  2203. if save_jinja_files and isinstance(self.chat_template, str):
  2204. # New format for single templates is to save them as chat_template.jinja
  2205. with open(chat_template_file, "w", encoding="utf-8") as f:
  2206. f.write(self.chat_template)
  2207. logger.info(f"chat template saved in {chat_template_file}")
  2208. saved_raw_chat_template_files.append(chat_template_file)
  2209. if "chat_template" in tokenizer_config:
  2210. tokenizer_config.pop("chat_template") # To ensure it doesn't somehow end up in the config too
  2211. elif save_jinja_files and isinstance(self.chat_template, dict):
  2212. # New format for multiple templates is to save the default as chat_template.jinja
  2213. # and the other templates in the chat_templates/ directory
  2214. for template_name, template in self.chat_template.items():
  2215. if template_name == "default":
  2216. with open(chat_template_file, "w", encoding="utf-8") as f:
  2217. f.write(self.chat_template["default"])
  2218. logger.info(f"chat template saved in {chat_template_file}")
  2219. saved_raw_chat_template_files.append(chat_template_file)
  2220. else:
  2221. Path(chat_template_dir).mkdir(exist_ok=True)
  2222. template_filepath = os.path.join(chat_template_dir, f"{template_name}.jinja")
  2223. with open(template_filepath, "w", encoding="utf-8") as f:
  2224. f.write(template)
  2225. logger.info(f"chat template saved in {template_filepath}")
  2226. saved_raw_chat_template_files.append(template_filepath)
  2227. if "chat_template" in tokenizer_config:
  2228. tokenizer_config.pop("chat_template") # To ensure it doesn't somehow end up in the config too
  2229. elif isinstance(self.chat_template, dict):
  2230. # Legacy format for multiple templates:
  2231. # chat template dicts are saved to the config as lists of dicts with fixed key names.
  2232. tokenizer_config["chat_template"] = [{"name": k, "template": v} for k, v in self.chat_template.items()]
  2233. elif self.chat_template is not None:
  2234. # Legacy format for single templates: Just make them a key in tokenizer_config.json
  2235. tokenizer_config["chat_template"] = self.chat_template
  2236. return tokenizer_config, saved_raw_chat_template_files
  2237. def save_pretrained(
  2238. self,
  2239. save_directory: Union[str, os.PathLike],
  2240. legacy_format: Optional[bool] = None,
  2241. filename_prefix: Optional[str] = None,
  2242. push_to_hub: bool = False,
  2243. **kwargs,
  2244. ) -> tuple[str, ...]:
  2245. """
  2246. Save the full tokenizer state.
  2247. This method make sure the full tokenizer can then be re-loaded using the
  2248. [`~tokenization_utils_base.PreTrainedTokenizer.from_pretrained`] class method..
  2249. Warning,None This won't save modifications you may have applied to the tokenizer after the instantiation (for
  2250. instance, modifying `tokenizer.do_lower_case` after creation).
  2251. Args:
  2252. save_directory (`str` or `os.PathLike`): The path to a directory where the tokenizer will be saved.
  2253. legacy_format (`bool`, *optional*):
  2254. Only applicable for a fast tokenizer. If unset (default), will save the tokenizer in the unified JSON
  2255. format as well as in legacy format if it exists, i.e. with tokenizer specific vocabulary and a separate
  2256. added_tokens files.
  2257. If `False`, will only save the tokenizer in the unified JSON format. This format is incompatible with
  2258. "slow" tokenizers (not powered by the *tokenizers* library), so the tokenizer will not be able to be
  2259. loaded in the corresponding "slow" tokenizer.
  2260. If `True`, will save the tokenizer in legacy format. If the "slow" tokenizer doesn't exits, a value
  2261. error is raised.
  2262. filename_prefix (`str`, *optional*):
  2263. A prefix to add to the names of the files saved by the tokenizer.
  2264. push_to_hub (`bool`, *optional*, defaults to `False`):
  2265. Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
  2266. repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
  2267. namespace).
  2268. kwargs (`dict[str, Any]`, *optional*):
  2269. Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
  2270. Returns:
  2271. A tuple of `str`: The files saved.
  2272. """
  2273. use_auth_token = kwargs.pop("use_auth_token", None)
  2274. if use_auth_token is not None:
  2275. warnings.warn(
  2276. "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.",
  2277. FutureWarning,
  2278. )
  2279. if kwargs.get("token") is not None:
  2280. raise ValueError(
  2281. "`token` and `use_auth_token` are both specified. Please set only the argument `token`."
  2282. )
  2283. kwargs["token"] = use_auth_token
  2284. if os.path.isfile(save_directory):
  2285. logger.error(f"Provided path ({save_directory}) should be a directory, not a file")
  2286. return
  2287. os.makedirs(save_directory, exist_ok=True)
  2288. if push_to_hub:
  2289. commit_message = kwargs.pop("commit_message", None)
  2290. repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1])
  2291. repo_id = self._create_repo(repo_id, **kwargs)
  2292. files_timestamps = self._get_files_timestamps(save_directory)
  2293. special_tokens_map_file = os.path.join(
  2294. save_directory, (filename_prefix + "-" if filename_prefix else "") + SPECIAL_TOKENS_MAP_FILE
  2295. )
  2296. tokenizer_config_file = os.path.join(
  2297. save_directory, (filename_prefix + "-" if filename_prefix else "") + TOKENIZER_CONFIG_FILE
  2298. )
  2299. tokenizer_config = copy.deepcopy(self.init_kwargs)
  2300. # Let's save the init kwargs
  2301. target_keys = set(self.init_kwargs.keys())
  2302. # Let's save the special tokens map (only the strings)
  2303. target_keys.update(["model_max_length", "clean_up_tokenization_spaces"])
  2304. for k in target_keys:
  2305. if hasattr(self, k):
  2306. tokenizer_config[k] = getattr(self, k)
  2307. # Let's make sure we properly save the special tokens
  2308. tokenizer_config.update(self.special_tokens_map)
  2309. if "extra_special_tokens" not in tokenizer_config:
  2310. tokenizer_config["extra_special_tokens"] = self.extra_special_tokens
  2311. tokenizer_config.update(self.extra_special_tokens)
  2312. save_jinja_files = kwargs.get("save_jinja_files", True)
  2313. tokenizer_config, saved_raw_chat_template_files = self.save_chat_templates(
  2314. save_directory, tokenizer_config, filename_prefix, save_jinja_files
  2315. )
  2316. if len(self.init_inputs) > 0:
  2317. tokenizer_config["init_inputs"] = copy.deepcopy(self.init_inputs)
  2318. for file_id in self.vocab_files_names:
  2319. tokenizer_config.pop(file_id, None)
  2320. # no typefields, this way old fast and slow can load it
  2321. tokenizer_config = self.convert_added_tokens(tokenizer_config, add_type_field=True, save=True)
  2322. # Process added tokens separately: allows previous versions to ignore it!
  2323. added_tokens = {}
  2324. for key, value in self.added_tokens_decoder.items():
  2325. added_tokens[key] = value.__getstate__()
  2326. tokenizer_config["added_tokens_decoder"] = added_tokens
  2327. # Add tokenizer class to the tokenizer config to be able to reload it with from_pretrained
  2328. tokenizer_class = self.__class__.__name__
  2329. # Remove the Fast at the end if we can save the slow tokenizer
  2330. if tokenizer_class.endswith("Fast") and getattr(self, "can_save_slow_tokenizer", False):
  2331. tokenizer_class = tokenizer_class[:-4]
  2332. tokenizer_config["tokenizer_class"] = tokenizer_class
  2333. if getattr(self, "_auto_map", None) is not None:
  2334. tokenizer_config["auto_map"] = self._auto_map
  2335. if getattr(self, "_processor_class", None) is not None:
  2336. tokenizer_config["processor_class"] = self._processor_class
  2337. # If we have a custom model, we copy the file defining it in the folder and set the attributes so it can be
  2338. # loaded from the Hub.
  2339. if self._auto_class is not None:
  2340. custom_object_save(self, save_directory, config=tokenizer_config)
  2341. # remove private information
  2342. if "name_or_path" in tokenizer_config:
  2343. tokenizer_config.pop("name_or_path")
  2344. tokenizer_config.pop("special_tokens_map_file", None)
  2345. tokenizer_config.pop("tokenizer_file", None)
  2346. if "device_map" in tokenizer_config:
  2347. tokenizer_config.pop("device_map")
  2348. with open(tokenizer_config_file, "w", encoding="utf-8") as f:
  2349. out_str = json.dumps(tokenizer_config, indent=2, sort_keys=True, ensure_ascii=False) + "\n"
  2350. f.write(out_str)
  2351. logger.info(f"tokenizer config file saved in {tokenizer_config_file}")
  2352. # Sanitize AddedTokens in special_tokens_map
  2353. # kept for forward compatibility, will be removed in transoformers 5. Typefields are not saved for FC, special should not be save either
  2354. write_dict = self.convert_added_tokens(self.special_tokens_map_extended, save=True, add_type_field=False)
  2355. with open(special_tokens_map_file, "w", encoding="utf-8") as f:
  2356. out_str = json.dumps(write_dict, indent=2, sort_keys=True, ensure_ascii=False) + "\n"
  2357. f.write(out_str)
  2358. logger.info(f"Special tokens file saved in {special_tokens_map_file}")
  2359. file_names = (tokenizer_config_file, special_tokens_map_file, *saved_raw_chat_template_files)
  2360. save_files = self._save_pretrained(
  2361. save_directory=save_directory,
  2362. file_names=file_names,
  2363. legacy_format=legacy_format,
  2364. filename_prefix=filename_prefix,
  2365. )
  2366. if push_to_hub:
  2367. self._upload_modified_files(
  2368. save_directory,
  2369. repo_id,
  2370. files_timestamps,
  2371. commit_message=commit_message,
  2372. token=kwargs.get("token"),
  2373. )
  2374. return save_files
  2375. def _save_pretrained(
  2376. self,
  2377. save_directory: Union[str, os.PathLike],
  2378. file_names: tuple[str, ...],
  2379. legacy_format: Optional[bool] = None,
  2380. filename_prefix: Optional[str] = None,
  2381. ) -> tuple[str, ...]:
  2382. """
  2383. Save a tokenizer using the slow-tokenizer/legacy format: vocabulary + added tokens.
  2384. Fast tokenizers can also be saved in a unique JSON file containing {config + vocab + added-tokens} using the
  2385. specific [`~tokenization_utils_fast.PreTrainedTokenizerFast._save_pretrained`]
  2386. """
  2387. if legacy_format is False:
  2388. raise ValueError(
  2389. "Only fast tokenizers (instances of PreTrainedTokenizerFast) can be saved in non legacy format."
  2390. )
  2391. save_directory = str(save_directory)
  2392. added_tokens_file = os.path.join(
  2393. save_directory, (filename_prefix + "-" if filename_prefix else "") + ADDED_TOKENS_FILE
  2394. )
  2395. # the new get_added_vocab() also returns special tokens and tokens that have an index < vocab_size
  2396. added_vocab = {tok: index for tok, index in self.added_tokens_encoder.items() if index >= self.vocab_size}
  2397. if added_vocab:
  2398. with open(added_tokens_file, "w", encoding="utf-8") as f:
  2399. out_str = json.dumps(added_vocab, indent=2, sort_keys=True, ensure_ascii=False) + "\n"
  2400. f.write(out_str)
  2401. logger.info(f"added tokens file saved in {added_tokens_file}")
  2402. vocab_files = self.save_vocabulary(save_directory, filename_prefix=filename_prefix)
  2403. return file_names + vocab_files + (added_tokens_file,)
  2404. def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str, ...]:
  2405. """
  2406. Save only the vocabulary of the tokenizer (vocabulary + added tokens).
  2407. This method won't save the configuration and special token mappings of the tokenizer. Use
  2408. [`~PreTrainedTokenizerFast._save_pretrained`] to save the whole state of the tokenizer.
  2409. Args:
  2410. save_directory (`str`):
  2411. The directory in which to save the vocabulary.
  2412. filename_prefix (`str`, *optional*):
  2413. An optional prefix to add to the named of the saved files.
  2414. Returns:
  2415. `tuple(str)`: Paths to the files saved.
  2416. """
  2417. raise NotImplementedError
  2418. def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]:
  2419. """
  2420. Converts a string into a sequence of tokens, replacing unknown tokens with the `unk_token`.
  2421. Args:
  2422. text (`str`):
  2423. The sequence to be encoded.
  2424. pair (`str`, *optional*):
  2425. A second sequence to be encoded with the first.
  2426. add_special_tokens (`bool`, *optional*, defaults to `False`):
  2427. Whether or not to add the special tokens associated with the corresponding model.
  2428. kwargs (additional keyword arguments, *optional*):
  2429. Will be passed to the underlying model specific encode method. See details in
  2430. [`~PreTrainedTokenizerBase.__call__`]
  2431. Returns:
  2432. `list[str]`: The list of tokens.
  2433. """
  2434. raise NotImplementedError
  2435. @add_end_docstrings(
  2436. ENCODE_KWARGS_DOCSTRING,
  2437. """
  2438. **kwargs: Passed along to the `.tokenize()` method.
  2439. """,
  2440. """
  2441. Returns:
  2442. `list[int]`, `torch.Tensor`, `tf.Tensor` or `np.ndarray`: The tokenized ids of the text.
  2443. """,
  2444. )
  2445. def encode(
  2446. self,
  2447. text: Union[TextInput, PreTokenizedInput, EncodedInput],
  2448. text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None,
  2449. add_special_tokens: bool = True,
  2450. padding: Union[bool, str, PaddingStrategy] = False,
  2451. truncation: Union[bool, str, TruncationStrategy, None] = None,
  2452. max_length: Optional[int] = None,
  2453. stride: int = 0,
  2454. padding_side: Optional[str] = None,
  2455. return_tensors: Optional[Union[str, TensorType]] = None,
  2456. **kwargs,
  2457. ) -> list[int]:
  2458. """
  2459. Converts a string to a sequence of ids (integer), using the tokenizer and vocabulary.
  2460. Same as doing `self.convert_tokens_to_ids(self.tokenize(text))`.
  2461. Args:
  2462. text (`str`, `list[str]` or `list[int]`):
  2463. The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the
  2464. `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids`
  2465. method).
  2466. text_pair (`str`, `list[str]` or `list[int]`, *optional*):
  2467. Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using
  2468. the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids`
  2469. method).
  2470. """
  2471. encoded_inputs = self.encode_plus(
  2472. text,
  2473. text_pair=text_pair,
  2474. add_special_tokens=add_special_tokens,
  2475. padding=padding,
  2476. truncation=truncation,
  2477. max_length=max_length,
  2478. stride=stride,
  2479. padding_side=padding_side,
  2480. return_tensors=return_tensors,
  2481. **kwargs,
  2482. )
  2483. return encoded_inputs["input_ids"]
  2484. def num_special_tokens_to_add(self, pair: bool = False) -> int:
  2485. raise NotImplementedError
  2486. def _get_padding_truncation_strategies(
  2487. self, padding=False, truncation=None, max_length=None, pad_to_multiple_of=None, verbose=True, **kwargs
  2488. ):
  2489. """
  2490. Find the correct padding/truncation strategy
  2491. """
  2492. # Backward compatibility for previous behavior, maybe we should deprecate it:
  2493. # If you only set max_length, it activates truncation for max_length
  2494. if max_length is not None and padding is False and truncation is None:
  2495. if verbose:
  2496. if not self.deprecation_warnings.get("Truncation-not-explicitly-activated", False):
  2497. logger.warning(
  2498. "Truncation was not explicitly activated but `max_length` is provided a specific value, please"
  2499. " use `truncation=True` to explicitly truncate examples to max length. Defaulting to"
  2500. " 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the"
  2501. " tokenizer you can select this strategy more precisely by providing a specific strategy to"
  2502. " `truncation`."
  2503. )
  2504. self.deprecation_warnings["Truncation-not-explicitly-activated"] = True
  2505. truncation = "longest_first"
  2506. # Get padding strategy
  2507. if padding is not False:
  2508. if padding is True:
  2509. if verbose:
  2510. if max_length is not None and (
  2511. truncation is None or truncation is False or truncation == "do_not_truncate"
  2512. ):
  2513. warnings.warn(
  2514. "`max_length` is ignored when `padding`=`True` and there is no truncation strategy. "
  2515. "To pad to max length, use `padding='max_length'`."
  2516. )
  2517. padding_strategy = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch
  2518. elif not isinstance(padding, PaddingStrategy):
  2519. padding_strategy = PaddingStrategy(padding)
  2520. elif isinstance(padding, PaddingStrategy):
  2521. padding_strategy = padding
  2522. else:
  2523. padding_strategy = PaddingStrategy.DO_NOT_PAD
  2524. # Get truncation strategy
  2525. if truncation is not False and truncation is not None:
  2526. if truncation is True:
  2527. truncation_strategy = (
  2528. TruncationStrategy.LONGEST_FIRST
  2529. ) # Default to truncate the longest sequences in pairs of inputs
  2530. elif not isinstance(truncation, TruncationStrategy):
  2531. truncation_strategy = TruncationStrategy(truncation)
  2532. elif isinstance(truncation, TruncationStrategy):
  2533. truncation_strategy = truncation
  2534. else:
  2535. truncation_strategy = TruncationStrategy.DO_NOT_TRUNCATE
  2536. # Set max length if needed
  2537. if max_length is None:
  2538. if padding_strategy == PaddingStrategy.MAX_LENGTH:
  2539. if self.model_max_length > LARGE_INTEGER:
  2540. if verbose:
  2541. if not self.deprecation_warnings.get("Asking-to-pad-to-max_length", False):
  2542. logger.warning(
  2543. "Asking to pad to max_length but no maximum length is provided and the model has no"
  2544. " predefined maximum length. Default to no padding."
  2545. )
  2546. self.deprecation_warnings["Asking-to-pad-to-max_length"] = True
  2547. padding_strategy = PaddingStrategy.DO_NOT_PAD
  2548. else:
  2549. max_length = self.model_max_length
  2550. if truncation_strategy != TruncationStrategy.DO_NOT_TRUNCATE:
  2551. if self.model_max_length > LARGE_INTEGER:
  2552. if verbose:
  2553. if not self.deprecation_warnings.get("Asking-to-truncate-to-max_length", False):
  2554. logger.warning(
  2555. "Asking to truncate to max_length but no maximum length is provided and the model has"
  2556. " no predefined maximum length. Default to no truncation."
  2557. )
  2558. self.deprecation_warnings["Asking-to-truncate-to-max_length"] = True
  2559. truncation_strategy = TruncationStrategy.DO_NOT_TRUNCATE
  2560. else:
  2561. max_length = self.model_max_length
  2562. # Test if we have a padding token
  2563. if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.pad_token is None or self.pad_token_id < 0):
  2564. raise ValueError(
  2565. "Asking to pad but the tokenizer does not have a padding token. "
  2566. "Please select a token to use as `pad_token` `(tokenizer.pad_token = tokenizer.eos_token e.g.)` "
  2567. "or add a new pad token via `tokenizer.add_special_tokens({'pad_token': '[PAD]'})`."
  2568. )
  2569. # Check that we will truncate to a multiple of pad_to_multiple_of if both are provided
  2570. if (
  2571. truncation_strategy != TruncationStrategy.DO_NOT_TRUNCATE
  2572. and padding_strategy != PaddingStrategy.DO_NOT_PAD
  2573. and pad_to_multiple_of is not None
  2574. and max_length is not None
  2575. and (max_length % pad_to_multiple_of != 0)
  2576. ):
  2577. raise ValueError(
  2578. "Truncation and padding are both activated but "
  2579. f"truncation length ({max_length}) is not a multiple of pad_to_multiple_of ({pad_to_multiple_of})."
  2580. )
  2581. return padding_strategy, truncation_strategy, max_length, kwargs
  2582. @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
  2583. def __call__(
  2584. self,
  2585. text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput], None] = None,
  2586. text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None,
  2587. text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput], None] = None,
  2588. text_pair_target: Optional[
  2589. Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]
  2590. ] = None,
  2591. add_special_tokens: bool = True,
  2592. padding: Union[bool, str, PaddingStrategy] = False,
  2593. truncation: Union[bool, str, TruncationStrategy, None] = None,
  2594. max_length: Optional[int] = None,
  2595. stride: int = 0,
  2596. is_split_into_words: bool = False,
  2597. pad_to_multiple_of: Optional[int] = None,
  2598. padding_side: Optional[str] = None,
  2599. return_tensors: Optional[Union[str, TensorType]] = None,
  2600. return_token_type_ids: Optional[bool] = None,
  2601. return_attention_mask: Optional[bool] = None,
  2602. return_overflowing_tokens: bool = False,
  2603. return_special_tokens_mask: bool = False,
  2604. return_offsets_mapping: bool = False,
  2605. return_length: bool = False,
  2606. verbose: bool = True,
  2607. **kwargs,
  2608. ) -> BatchEncoding:
  2609. """
  2610. Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of
  2611. sequences.
  2612. Args:
  2613. text (`str`, `list[str]`, `list[list[str]]`, *optional*):
  2614. The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
  2615. (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
  2616. `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
  2617. text_pair (`str`, `list[str]`, `list[list[str]]`, *optional*):
  2618. The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
  2619. (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
  2620. `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
  2621. text_target (`str`, `list[str]`, `list[list[str]]`, *optional*):
  2622. The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
  2623. list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized),
  2624. you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
  2625. text_pair_target (`str`, `list[str]`, `list[list[str]]`, *optional*):
  2626. The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
  2627. list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized),
  2628. you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
  2629. """
  2630. # To avoid duplicating
  2631. all_kwargs = {
  2632. "add_special_tokens": add_special_tokens,
  2633. "padding": padding,
  2634. "truncation": truncation,
  2635. "max_length": max_length,
  2636. "stride": stride,
  2637. "is_split_into_words": is_split_into_words,
  2638. "pad_to_multiple_of": pad_to_multiple_of,
  2639. "padding_side": padding_side,
  2640. "return_tensors": return_tensors,
  2641. "return_token_type_ids": return_token_type_ids,
  2642. "return_attention_mask": return_attention_mask,
  2643. "return_overflowing_tokens": return_overflowing_tokens,
  2644. "return_special_tokens_mask": return_special_tokens_mask,
  2645. "return_offsets_mapping": return_offsets_mapping,
  2646. "return_length": return_length,
  2647. "split_special_tokens": kwargs.pop("split_special_tokens", self.split_special_tokens),
  2648. "verbose": verbose,
  2649. }
  2650. if return_tensors in ("tf", "jax"):
  2651. logger.warning_once(
  2652. "TensorFlow and JAX classes are deprecated and will be removed in Transformers v5. We "
  2653. "recommend migrating to PyTorch classes or pinning your version of Transformers."
  2654. )
  2655. all_kwargs.update(kwargs)
  2656. if text is None and text_target is None:
  2657. raise ValueError("You need to specify either `text` or `text_target`.")
  2658. if text is not None:
  2659. # The context manager will send the inputs as normal texts and not text_target, but we shouldn't change the
  2660. # input mode in this case.
  2661. if not self._in_target_context_manager:
  2662. self._switch_to_input_mode()
  2663. encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs)
  2664. if text_target is not None:
  2665. self._switch_to_target_mode()
  2666. target_encodings = self._call_one(text=text_target, text_pair=text_pair_target, **all_kwargs)
  2667. # Leave back tokenizer in input mode
  2668. self._switch_to_input_mode()
  2669. if text_target is None:
  2670. return encodings
  2671. elif text is None:
  2672. return target_encodings
  2673. else:
  2674. encodings["labels"] = target_encodings["input_ids"]
  2675. return encodings
  2676. def _call_one(
  2677. self,
  2678. text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]],
  2679. text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None,
  2680. add_special_tokens: bool = True,
  2681. padding: Union[bool, str, PaddingStrategy] = False,
  2682. truncation: Union[bool, str, TruncationStrategy, None] = None,
  2683. max_length: Optional[int] = None,
  2684. stride: int = 0,
  2685. is_split_into_words: bool = False,
  2686. pad_to_multiple_of: Optional[int] = None,
  2687. padding_side: Optional[str] = None,
  2688. return_tensors: Optional[Union[str, TensorType]] = None,
  2689. return_token_type_ids: Optional[bool] = None,
  2690. return_attention_mask: Optional[bool] = None,
  2691. return_overflowing_tokens: bool = False,
  2692. return_special_tokens_mask: bool = False,
  2693. return_offsets_mapping: bool = False,
  2694. return_length: bool = False,
  2695. verbose: bool = True,
  2696. split_special_tokens: bool = False,
  2697. **kwargs,
  2698. ) -> BatchEncoding:
  2699. # Input type checking for clearer error
  2700. def _is_valid_text_input(t):
  2701. if isinstance(t, str):
  2702. # Strings are fine
  2703. return True
  2704. elif isinstance(t, (list, tuple)):
  2705. # List are fine as long as they are...
  2706. if len(t) == 0:
  2707. # ... empty
  2708. return True
  2709. elif isinstance(t[0], str):
  2710. # ... list of strings
  2711. return True
  2712. elif isinstance(t[0], (list, tuple)):
  2713. # ... list with an empty list or with a list of strings
  2714. return len(t[0]) == 0 or isinstance(t[0][0], str)
  2715. else:
  2716. return False
  2717. else:
  2718. return False
  2719. if not _is_valid_text_input(text):
  2720. raise ValueError(
  2721. "text input must be of type `str` (single example), `list[str]` (batch or single pretokenized example) "
  2722. "or `list[list[str]]` (batch of pretokenized examples)."
  2723. )
  2724. if text_pair is not None and not _is_valid_text_input(text_pair):
  2725. raise ValueError(
  2726. "text input must be of type `str` (single example), `list[str]` (batch or single pretokenized example) "
  2727. "or `list[list[str]]` (batch of pretokenized examples)."
  2728. )
  2729. if is_split_into_words:
  2730. is_batched = isinstance(text, (list, tuple)) and text and isinstance(text[0], (list, tuple))
  2731. else:
  2732. is_batched = isinstance(text, (list, tuple))
  2733. if is_batched:
  2734. if isinstance(text_pair, str):
  2735. raise TypeError(
  2736. "when tokenizing batches of text, `text_pair` must be a list or tuple with the same length as"
  2737. " `text`."
  2738. )
  2739. if text_pair is not None and len(text) != len(text_pair):
  2740. raise ValueError(
  2741. f"batch length of `text`: {len(text)} does not match batch length of `text_pair`:"
  2742. f" {len(text_pair)}."
  2743. )
  2744. batch_text_or_text_pairs = list(zip(text, text_pair)) if text_pair is not None else text
  2745. return self.batch_encode_plus(
  2746. batch_text_or_text_pairs=batch_text_or_text_pairs,
  2747. add_special_tokens=add_special_tokens,
  2748. padding=padding,
  2749. truncation=truncation,
  2750. max_length=max_length,
  2751. stride=stride,
  2752. is_split_into_words=is_split_into_words,
  2753. pad_to_multiple_of=pad_to_multiple_of,
  2754. padding_side=padding_side,
  2755. return_tensors=return_tensors,
  2756. return_token_type_ids=return_token_type_ids,
  2757. return_attention_mask=return_attention_mask,
  2758. return_overflowing_tokens=return_overflowing_tokens,
  2759. return_special_tokens_mask=return_special_tokens_mask,
  2760. return_offsets_mapping=return_offsets_mapping,
  2761. return_length=return_length,
  2762. verbose=verbose,
  2763. split_special_tokens=split_special_tokens,
  2764. **kwargs,
  2765. )
  2766. else:
  2767. return self.encode_plus(
  2768. text=text,
  2769. text_pair=text_pair,
  2770. add_special_tokens=add_special_tokens,
  2771. padding=padding,
  2772. truncation=truncation,
  2773. max_length=max_length,
  2774. stride=stride,
  2775. is_split_into_words=is_split_into_words,
  2776. pad_to_multiple_of=pad_to_multiple_of,
  2777. padding_side=padding_side,
  2778. return_tensors=return_tensors,
  2779. return_token_type_ids=return_token_type_ids,
  2780. return_attention_mask=return_attention_mask,
  2781. return_overflowing_tokens=return_overflowing_tokens,
  2782. return_special_tokens_mask=return_special_tokens_mask,
  2783. return_offsets_mapping=return_offsets_mapping,
  2784. return_length=return_length,
  2785. verbose=verbose,
  2786. split_special_tokens=split_special_tokens,
  2787. **kwargs,
  2788. )
  2789. @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
  2790. def encode_plus(
  2791. self,
  2792. text: Union[TextInput, PreTokenizedInput, EncodedInput],
  2793. text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None,
  2794. add_special_tokens: bool = True,
  2795. padding: Union[bool, str, PaddingStrategy] = False,
  2796. truncation: Union[bool, str, TruncationStrategy, None] = None,
  2797. max_length: Optional[int] = None,
  2798. stride: int = 0,
  2799. is_split_into_words: bool = False,
  2800. pad_to_multiple_of: Optional[int] = None,
  2801. padding_side: Optional[str] = None,
  2802. return_tensors: Optional[Union[str, TensorType]] = None,
  2803. return_token_type_ids: Optional[bool] = None,
  2804. return_attention_mask: Optional[bool] = None,
  2805. return_overflowing_tokens: bool = False,
  2806. return_special_tokens_mask: bool = False,
  2807. return_offsets_mapping: bool = False,
  2808. return_length: bool = False,
  2809. verbose: bool = True,
  2810. **kwargs,
  2811. ) -> BatchEncoding:
  2812. """
  2813. Tokenize and prepare for the model a sequence or a pair of sequences.
  2814. <Tip warning={true}>
  2815. This method is deprecated, `__call__` should be used instead.
  2816. </Tip>
  2817. Args:
  2818. text (`str`, `list[str]` or (for non-fast tokenizers) `list[int]`):
  2819. The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the
  2820. `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids`
  2821. method).
  2822. text_pair (`str`, `list[str]` or `list[int]`, *optional*):
  2823. Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using
  2824. the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids`
  2825. method).
  2826. """
  2827. padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
  2828. padding=padding,
  2829. truncation=truncation,
  2830. max_length=max_length,
  2831. pad_to_multiple_of=pad_to_multiple_of,
  2832. verbose=verbose,
  2833. **kwargs,
  2834. )
  2835. return self._encode_plus(
  2836. text=text,
  2837. text_pair=text_pair,
  2838. add_special_tokens=add_special_tokens,
  2839. padding_strategy=padding_strategy,
  2840. truncation_strategy=truncation_strategy,
  2841. max_length=max_length,
  2842. stride=stride,
  2843. is_split_into_words=is_split_into_words,
  2844. pad_to_multiple_of=pad_to_multiple_of,
  2845. padding_side=padding_side,
  2846. return_tensors=return_tensors,
  2847. return_token_type_ids=return_token_type_ids,
  2848. return_attention_mask=return_attention_mask,
  2849. return_overflowing_tokens=return_overflowing_tokens,
  2850. return_special_tokens_mask=return_special_tokens_mask,
  2851. return_offsets_mapping=return_offsets_mapping,
  2852. return_length=return_length,
  2853. verbose=verbose,
  2854. split_special_tokens=kwargs.pop("split_special_tokens", self.split_special_tokens),
  2855. **kwargs,
  2856. )
  2857. def _encode_plus(
  2858. self,
  2859. text: Union[TextInput, PreTokenizedInput, EncodedInput],
  2860. text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None,
  2861. add_special_tokens: bool = True,
  2862. padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
  2863. truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
  2864. max_length: Optional[int] = None,
  2865. stride: int = 0,
  2866. is_split_into_words: bool = False,
  2867. pad_to_multiple_of: Optional[int] = None,
  2868. padding_side: Optional[str] = None,
  2869. return_tensors: Optional[Union[str, TensorType]] = None,
  2870. return_token_type_ids: Optional[bool] = None,
  2871. return_attention_mask: Optional[bool] = None,
  2872. return_overflowing_tokens: bool = False,
  2873. return_special_tokens_mask: bool = False,
  2874. return_offsets_mapping: bool = False,
  2875. return_length: bool = False,
  2876. verbose: bool = True,
  2877. split_special_tokens: bool = False,
  2878. **kwargs,
  2879. ) -> BatchEncoding:
  2880. raise NotImplementedError
  2881. @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
  2882. def batch_encode_plus(
  2883. self,
  2884. batch_text_or_text_pairs: Union[
  2885. list[TextInput],
  2886. list[TextInputPair],
  2887. list[PreTokenizedInput],
  2888. list[PreTokenizedInputPair],
  2889. list[EncodedInput],
  2890. list[EncodedInputPair],
  2891. ],
  2892. add_special_tokens: bool = True,
  2893. padding: Union[bool, str, PaddingStrategy] = False,
  2894. truncation: Union[bool, str, TruncationStrategy, None] = None,
  2895. max_length: Optional[int] = None,
  2896. stride: int = 0,
  2897. is_split_into_words: bool = False,
  2898. pad_to_multiple_of: Optional[int] = None,
  2899. padding_side: Optional[str] = None,
  2900. return_tensors: Optional[Union[str, TensorType]] = None,
  2901. return_token_type_ids: Optional[bool] = None,
  2902. return_attention_mask: Optional[bool] = None,
  2903. return_overflowing_tokens: bool = False,
  2904. return_special_tokens_mask: bool = False,
  2905. return_offsets_mapping: bool = False,
  2906. return_length: bool = False,
  2907. verbose: bool = True,
  2908. split_special_tokens: bool = False,
  2909. **kwargs,
  2910. ) -> BatchEncoding:
  2911. """
  2912. Tokenize and prepare for the model a list of sequences or a list of pairs of sequences.
  2913. <Tip warning={true}>
  2914. This method is deprecated, `__call__` should be used instead.
  2915. </Tip>
  2916. Args:
  2917. batch_text_or_text_pairs (`list[str]`, `list[tuple[str, str]]`, `list[list[str]]`, `list[tuple[list[str], list[str]]]`, and for not-fast tokenizers, also `list[list[int]]`, `list[tuple[list[int], list[int]]]`):
  2918. Batch of sequences or pair of sequences to be encoded. This can be a list of
  2919. string/string-sequences/int-sequences or a list of pair of string/string-sequences/int-sequence (see
  2920. details in `encode_plus`).
  2921. """
  2922. # Backward compatibility for 'truncation_strategy', 'pad_to_max_length'
  2923. padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
  2924. padding=padding,
  2925. truncation=truncation,
  2926. max_length=max_length,
  2927. pad_to_multiple_of=pad_to_multiple_of,
  2928. verbose=verbose,
  2929. **kwargs,
  2930. )
  2931. return self._batch_encode_plus(
  2932. batch_text_or_text_pairs=batch_text_or_text_pairs,
  2933. add_special_tokens=add_special_tokens,
  2934. padding_strategy=padding_strategy,
  2935. truncation_strategy=truncation_strategy,
  2936. max_length=max_length,
  2937. stride=stride,
  2938. is_split_into_words=is_split_into_words,
  2939. pad_to_multiple_of=pad_to_multiple_of,
  2940. padding_side=padding_side,
  2941. return_tensors=return_tensors,
  2942. return_token_type_ids=return_token_type_ids,
  2943. return_attention_mask=return_attention_mask,
  2944. return_overflowing_tokens=return_overflowing_tokens,
  2945. return_special_tokens_mask=return_special_tokens_mask,
  2946. return_offsets_mapping=return_offsets_mapping,
  2947. return_length=return_length,
  2948. verbose=verbose,
  2949. split_special_tokens=split_special_tokens,
  2950. **kwargs,
  2951. )
  2952. def _batch_encode_plus(
  2953. self,
  2954. batch_text_or_text_pairs: Union[
  2955. list[TextInput],
  2956. list[TextInputPair],
  2957. list[PreTokenizedInput],
  2958. list[PreTokenizedInputPair],
  2959. list[EncodedInput],
  2960. list[EncodedInputPair],
  2961. ],
  2962. add_special_tokens: bool = True,
  2963. padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
  2964. truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
  2965. max_length: Optional[int] = None,
  2966. stride: int = 0,
  2967. is_split_into_words: bool = False,
  2968. pad_to_multiple_of: Optional[int] = None,
  2969. padding_side: Optional[str] = None,
  2970. return_tensors: Optional[Union[str, TensorType]] = None,
  2971. return_token_type_ids: Optional[bool] = None,
  2972. return_attention_mask: Optional[bool] = None,
  2973. return_overflowing_tokens: bool = False,
  2974. return_special_tokens_mask: bool = False,
  2975. return_offsets_mapping: bool = False,
  2976. return_length: bool = False,
  2977. verbose: bool = True,
  2978. split_special_tokens: bool = False,
  2979. **kwargs,
  2980. ) -> BatchEncoding:
  2981. raise NotImplementedError
  2982. def pad(
  2983. self,
  2984. encoded_inputs: Union[
  2985. BatchEncoding,
  2986. list[BatchEncoding],
  2987. dict[str, EncodedInput],
  2988. dict[str, list[EncodedInput]],
  2989. list[dict[str, EncodedInput]],
  2990. ],
  2991. padding: Union[bool, str, PaddingStrategy] = True,
  2992. max_length: Optional[int] = None,
  2993. pad_to_multiple_of: Optional[int] = None,
  2994. padding_side: Optional[str] = None,
  2995. return_attention_mask: Optional[bool] = None,
  2996. return_tensors: Optional[Union[str, TensorType]] = None,
  2997. verbose: bool = True,
  2998. ) -> BatchEncoding:
  2999. """
  3000. Pad a single encoded input or a batch of encoded inputs up to predefined length or to the max sequence length
  3001. in the batch.
  3002. Padding side (left/right) padding token ids are defined at the tokenizer level (with `self.padding_side`,
  3003. `self.pad_token_id` and `self.pad_token_type_id`).
  3004. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the
  3005. text followed by a call to the `pad` method to get a padded encoding.
  3006. <Tip>
  3007. If the `encoded_inputs` passed are dictionary of numpy arrays, PyTorch tensors or TensorFlow tensors, the
  3008. result will use the same type unless you provide a different tensor type with `return_tensors`. In the case of
  3009. PyTorch tensors, you will lose the specific device of your tensors however.
  3010. </Tip>
  3011. Args:
  3012. encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `dict[str, list[int]]`, `dict[str, list[list[int]]` or `list[dict[str, list[int]]]`):
  3013. Tokenized inputs. Can represent one input ([`BatchEncoding`] or `dict[str, list[int]]`) or a batch of
  3014. tokenized inputs (list of [`BatchEncoding`], *dict[str, list[list[int]]]* or *list[dict[str,
  3015. list[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader
  3016. collate function.
  3017. Instead of `list[int]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see
  3018. the note above for the return type.
  3019. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`):
  3020. Select a strategy to pad the returned sequences (according to the model's padding side and padding
  3021. index) among:
  3022. - `True` or `'longest'` (default): Pad to the longest sequence in the batch (or no padding if only a single
  3023. sequence if provided).
  3024. - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
  3025. acceptable input length for the model if that argument is not provided.
  3026. - `False` or `'do_not_pad'`: No padding (i.e., can output a batch with sequences of different
  3027. lengths).
  3028. max_length (`int`, *optional*):
  3029. Maximum length of the returned list and optionally padding length (see above).
  3030. pad_to_multiple_of (`int`, *optional*):
  3031. If set will pad the sequence to a multiple of the provided value.
  3032. This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
  3033. `>= 7.5` (Volta).
  3034. padding_side (`str`, *optional*):
  3035. The side on which the model should have padding applied. Should be selected between ['right', 'left'].
  3036. Default value is picked from the class attribute of the same name.
  3037. return_attention_mask (`bool`, *optional*):
  3038. Whether to return the attention mask. If left to the default, will return the attention mask according
  3039. to the specific tokenizer's default, defined by the `return_outputs` attribute.
  3040. [What are attention masks?](../glossary#attention-mask)
  3041. return_tensors (`str` or [`~utils.TensorType`], *optional*):
  3042. If set, will return tensors instead of list of python integers. Acceptable values are:
  3043. - `'tf'`: Return TensorFlow `tf.constant` objects.
  3044. - `'pt'`: Return PyTorch `torch.Tensor` objects.
  3045. - `'np'`: Return Numpy `np.ndarray` objects.
  3046. verbose (`bool`, *optional*, defaults to `True`):
  3047. Whether or not to print more information and warnings.
  3048. """
  3049. if self.__class__.__name__.endswith("Fast"):
  3050. if not self.deprecation_warnings.get("Asking-to-pad-a-fast-tokenizer", False):
  3051. logger.warning_advice(
  3052. f"You're using a {self.__class__.__name__} tokenizer. Please note that with a fast tokenizer,"
  3053. " using the `__call__` method is faster than using a method to encode the text followed by a call"
  3054. " to the `pad` method to get a padded encoding."
  3055. )
  3056. self.deprecation_warnings["Asking-to-pad-a-fast-tokenizer"] = True
  3057. # If we have a list of dicts, let's convert it in a dict of lists
  3058. # We do this to allow using this method as a collate_fn function in PyTorch Dataloader
  3059. if isinstance(encoded_inputs, (list, tuple)) and isinstance(encoded_inputs[0], Mapping):
  3060. encoded_inputs = {key: [example[key] for example in encoded_inputs] for key in encoded_inputs[0]}
  3061. # The model's main input name, usually `input_ids`, has been passed for padding
  3062. if self.model_input_names[0] not in encoded_inputs:
  3063. raise ValueError(
  3064. "You should supply an encoding or a list of encodings to this method "
  3065. f"that includes {self.model_input_names[0]}, but you provided {list(encoded_inputs.keys())}"
  3066. )
  3067. required_input = encoded_inputs[self.model_input_names[0]]
  3068. if required_input is None or (isinstance(required_input, Sized) and len(required_input) == 0):
  3069. if return_attention_mask:
  3070. encoded_inputs["attention_mask"] = []
  3071. return encoded_inputs
  3072. # If we have PyTorch/TF/NumPy tensors/arrays as inputs, we cast them as python objects
  3073. # and rebuild them afterwards if no return_tensors is specified
  3074. # Note that we lose the specific device the tensor may be on for PyTorch
  3075. first_element = required_input[0]
  3076. if isinstance(first_element, (list, tuple)):
  3077. # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
  3078. for item in required_input:
  3079. if len(item) != 0:
  3080. first_element = item[0]
  3081. break
  3082. # At this state, if `first_element` is still a list/tuple, it's an empty one so there is nothing to do.
  3083. if not isinstance(first_element, (int, list, tuple)):
  3084. if is_tf_tensor(first_element):
  3085. return_tensors = "tf" if return_tensors is None else return_tensors
  3086. elif is_torch_tensor(first_element):
  3087. return_tensors = "pt" if return_tensors is None else return_tensors
  3088. elif isinstance(first_element, np.ndarray):
  3089. return_tensors = "np" if return_tensors is None else return_tensors
  3090. else:
  3091. raise ValueError(
  3092. f"type of {first_element} unknown: {type(first_element)}. "
  3093. "Should be one of a python, numpy, pytorch or tensorflow object."
  3094. )
  3095. for key, value in encoded_inputs.items():
  3096. encoded_inputs[key] = to_py_obj(value)
  3097. # Convert padding_strategy in PaddingStrategy
  3098. padding_strategy, _, max_length, _ = self._get_padding_truncation_strategies(
  3099. padding=padding, max_length=max_length, verbose=verbose
  3100. )
  3101. required_input = encoded_inputs[self.model_input_names[0]]
  3102. if required_input and not isinstance(required_input[0], (list, tuple)):
  3103. encoded_inputs = self._pad(
  3104. encoded_inputs,
  3105. max_length=max_length,
  3106. padding_strategy=padding_strategy,
  3107. pad_to_multiple_of=pad_to_multiple_of,
  3108. padding_side=padding_side,
  3109. return_attention_mask=return_attention_mask,
  3110. )
  3111. return BatchEncoding(encoded_inputs, tensor_type=return_tensors)
  3112. batch_size = len(required_input)
  3113. assert all(len(v) == batch_size for v in encoded_inputs.values()), (
  3114. "Some items in the output dictionary have a different batch size than others."
  3115. )
  3116. if padding_strategy == PaddingStrategy.LONGEST:
  3117. max_length = max(len(inputs) for inputs in required_input)
  3118. padding_strategy = PaddingStrategy.MAX_LENGTH
  3119. batch_outputs = {}
  3120. for i in range(batch_size):
  3121. inputs = {k: v[i] for k, v in encoded_inputs.items()}
  3122. outputs = self._pad(
  3123. inputs,
  3124. max_length=max_length,
  3125. padding_strategy=padding_strategy,
  3126. pad_to_multiple_of=pad_to_multiple_of,
  3127. padding_side=padding_side,
  3128. return_attention_mask=return_attention_mask,
  3129. )
  3130. for key, value in outputs.items():
  3131. if key not in batch_outputs:
  3132. batch_outputs[key] = []
  3133. batch_outputs[key].append(value)
  3134. return BatchEncoding(batch_outputs, tensor_type=return_tensors)
  3135. def create_token_type_ids_from_sequences(
  3136. self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None
  3137. ) -> list[int]:
  3138. """
  3139. Create the token type IDs corresponding to the sequences passed. [What are token type
  3140. IDs?](../glossary#token-type-ids)
  3141. Should be overridden in a subclass if the model has a special way of building those.
  3142. Args:
  3143. token_ids_0 (`list[int]`): The first tokenized sequence.
  3144. token_ids_1 (`list[int]`, *optional*): The second tokenized sequence.
  3145. Returns:
  3146. `list[int]`: The token type ids.
  3147. """
  3148. cls_len = int(getattr(self, "cls_token_id", None) is not None)
  3149. sep_len = int(getattr(self, "sep_token_id", None) is not None)
  3150. if token_ids_1 is None:
  3151. return [0] * (cls_len + len(token_ids_0) + sep_len)
  3152. return [0] * (cls_len + len(token_ids_0) + sep_len) + [1] * (len(token_ids_1) + sep_len)
  3153. def build_inputs_with_special_tokens(
  3154. self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None
  3155. ) -> list[int]:
  3156. """
  3157. Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
  3158. adding special tokens.
  3159. This implementation does not add special tokens and this method should be overridden in a subclass.
  3160. Args:
  3161. token_ids_0 (`list[int]`): The first tokenized sequence.
  3162. token_ids_1 (`list[int]`, *optional*): The second tokenized sequence.
  3163. Returns:
  3164. `list[int]`: The model input with special tokens.
  3165. """
  3166. if token_ids_1 is None:
  3167. return token_ids_0
  3168. return token_ids_0 + token_ids_1
  3169. @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
  3170. def prepare_for_model(
  3171. self,
  3172. ids: list[int],
  3173. pair_ids: Optional[list[int]] = None,
  3174. add_special_tokens: bool = True,
  3175. padding: Union[bool, str, PaddingStrategy] = False,
  3176. truncation: Union[bool, str, TruncationStrategy, None] = None,
  3177. max_length: Optional[int] = None,
  3178. stride: int = 0,
  3179. pad_to_multiple_of: Optional[int] = None,
  3180. padding_side: Optional[str] = None,
  3181. return_tensors: Optional[Union[str, TensorType]] = None,
  3182. return_token_type_ids: Optional[bool] = None,
  3183. return_attention_mask: Optional[bool] = None,
  3184. return_overflowing_tokens: bool = False,
  3185. return_special_tokens_mask: bool = False,
  3186. return_offsets_mapping: bool = False,
  3187. return_length: bool = False,
  3188. verbose: bool = True,
  3189. prepend_batch_axis: bool = False,
  3190. **kwargs,
  3191. ) -> BatchEncoding:
  3192. """
  3193. Prepares a sequence of input id, or a pair of sequences of inputs ids so that it can be used by the model. It
  3194. adds special tokens, truncates sequences if overflowing while taking into account the special tokens and
  3195. manages a moving window (with user defined stride) for overflowing tokens. Please Note, for *pair_ids*
  3196. different than `None` and *truncation_strategy = longest_first* or `True`, it is not possible to return
  3197. overflowing tokens. Such a combination of arguments will raise an error.
  3198. Args:
  3199. ids (`list[int]`):
  3200. Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and
  3201. `convert_tokens_to_ids` methods.
  3202. pair_ids (`list[int]`, *optional*):
  3203. Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize`
  3204. and `convert_tokens_to_ids` methods.
  3205. """
  3206. # Backward compatibility for 'truncation_strategy', 'pad_to_max_length'
  3207. padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
  3208. padding=padding,
  3209. truncation=truncation,
  3210. max_length=max_length,
  3211. pad_to_multiple_of=pad_to_multiple_of,
  3212. verbose=verbose,
  3213. **kwargs,
  3214. )
  3215. pair = pair_ids is not None
  3216. len_ids = len(ids)
  3217. len_pair_ids = len(pair_ids) if pair else 0
  3218. if return_token_type_ids and not add_special_tokens:
  3219. raise ValueError(
  3220. "Asking to return token_type_ids while setting add_special_tokens to False "
  3221. "results in an undefined behavior. Please set add_special_tokens to True or "
  3222. "set return_token_type_ids to None."
  3223. )
  3224. if (
  3225. return_overflowing_tokens
  3226. and truncation_strategy == TruncationStrategy.LONGEST_FIRST
  3227. and pair_ids is not None
  3228. ):
  3229. raise ValueError(
  3230. "Not possible to return overflowing tokens for pair of sequences with the "
  3231. "`longest_first`. Please select another truncation strategy than `longest_first`, "
  3232. "for instance `only_second` or `only_first`."
  3233. )
  3234. # Load from model defaults
  3235. if return_token_type_ids is None:
  3236. return_token_type_ids = "token_type_ids" in self.model_input_names
  3237. if return_attention_mask is None:
  3238. return_attention_mask = "attention_mask" in self.model_input_names
  3239. encoded_inputs = {}
  3240. # Compute the total size of the returned encodings
  3241. total_len = len_ids + len_pair_ids + (self.num_special_tokens_to_add(pair=pair) if add_special_tokens else 0)
  3242. # Truncation: Handle max sequence length
  3243. overflowing_tokens = []
  3244. if truncation_strategy != TruncationStrategy.DO_NOT_TRUNCATE and max_length and total_len > max_length:
  3245. ids, pair_ids, overflowing_tokens = self.truncate_sequences(
  3246. ids,
  3247. pair_ids=pair_ids,
  3248. num_tokens_to_remove=total_len - max_length,
  3249. truncation_strategy=truncation_strategy,
  3250. stride=stride,
  3251. )
  3252. if return_overflowing_tokens:
  3253. encoded_inputs["overflowing_tokens"] = overflowing_tokens
  3254. encoded_inputs["num_truncated_tokens"] = total_len - max_length
  3255. # Add special tokens
  3256. if add_special_tokens:
  3257. sequence = self.build_inputs_with_special_tokens(ids, pair_ids)
  3258. token_type_ids = self.create_token_type_ids_from_sequences(ids, pair_ids)
  3259. else:
  3260. sequence = ids + pair_ids if pair else ids
  3261. token_type_ids = [0] * len(ids) + ([0] * len(pair_ids) if pair else [])
  3262. # Build output dictionary
  3263. encoded_inputs["input_ids"] = sequence
  3264. if return_token_type_ids:
  3265. encoded_inputs["token_type_ids"] = token_type_ids
  3266. if return_special_tokens_mask:
  3267. if add_special_tokens:
  3268. encoded_inputs["special_tokens_mask"] = self.get_special_tokens_mask(ids, pair_ids)
  3269. else:
  3270. encoded_inputs["special_tokens_mask"] = [0] * len(sequence)
  3271. # Check lengths
  3272. self._eventual_warn_about_too_long_sequence(encoded_inputs["input_ids"], max_length, verbose)
  3273. # Padding
  3274. if padding_strategy != PaddingStrategy.DO_NOT_PAD or return_attention_mask:
  3275. encoded_inputs = self.pad(
  3276. encoded_inputs,
  3277. max_length=max_length,
  3278. padding=padding_strategy.value,
  3279. pad_to_multiple_of=pad_to_multiple_of,
  3280. padding_side=padding_side,
  3281. return_attention_mask=return_attention_mask,
  3282. )
  3283. if return_length:
  3284. encoded_inputs["length"] = len(encoded_inputs["input_ids"])
  3285. batch_outputs = BatchEncoding(
  3286. encoded_inputs, tensor_type=return_tensors, prepend_batch_axis=prepend_batch_axis
  3287. )
  3288. return batch_outputs
  3289. def truncate_sequences(
  3290. self,
  3291. ids: list[int],
  3292. pair_ids: Optional[list[int]] = None,
  3293. num_tokens_to_remove: int = 0,
  3294. truncation_strategy: Union[str, TruncationStrategy] = "longest_first",
  3295. stride: int = 0,
  3296. ) -> tuple[list[int], list[int], list[int]]:
  3297. """
  3298. Truncates a sequence pair in-place following the strategy.
  3299. Args:
  3300. ids (`list[int]`):
  3301. Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and
  3302. `convert_tokens_to_ids` methods.
  3303. pair_ids (`list[int]`, *optional*):
  3304. Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize`
  3305. and `convert_tokens_to_ids` methods.
  3306. num_tokens_to_remove (`int`, *optional*, defaults to 0):
  3307. Number of tokens to remove using the truncation strategy.
  3308. truncation_strategy (`str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `'longest_first'`):
  3309. The strategy to follow for truncation. Can be:
  3310. - `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to the
  3311. maximum acceptable input length for the model if that argument is not provided. This will truncate
  3312. token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a
  3313. batch of pairs) is provided.
  3314. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the
  3315. maximum acceptable input length for the model if that argument is not provided. This will only
  3316. truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
  3317. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the
  3318. maximum acceptable input length for the model if that argument is not provided. This will only
  3319. truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
  3320. - `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths greater
  3321. than the model maximum admissible input size).
  3322. stride (`int`, *optional*, defaults to 0):
  3323. If set to a positive number, the overflowing tokens returned will contain some tokens from the main
  3324. sequence returned. The value of this argument defines the number of additional tokens.
  3325. Returns:
  3326. `tuple[list[int], list[int], list[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of
  3327. overflowing tokens. Note: The *longest_first* strategy returns empty list of overflowing tokens if a pair
  3328. of sequences (or a batch of pairs) is provided.
  3329. """
  3330. if num_tokens_to_remove <= 0:
  3331. return ids, pair_ids, []
  3332. if not isinstance(truncation_strategy, TruncationStrategy):
  3333. truncation_strategy = TruncationStrategy(truncation_strategy)
  3334. overflowing_tokens = []
  3335. if truncation_strategy == TruncationStrategy.ONLY_FIRST or (
  3336. truncation_strategy == TruncationStrategy.LONGEST_FIRST and pair_ids is None
  3337. ):
  3338. if len(ids) > num_tokens_to_remove:
  3339. window_len = min(len(ids), stride + num_tokens_to_remove)
  3340. if self.truncation_side == "left":
  3341. overflowing_tokens = ids[:window_len]
  3342. ids = ids[num_tokens_to_remove:]
  3343. elif self.truncation_side == "right":
  3344. overflowing_tokens = ids[-window_len:]
  3345. ids = ids[:-num_tokens_to_remove]
  3346. else:
  3347. raise ValueError(f"invalid truncation strategy: {self.truncation_side}, use 'left' or 'right'.")
  3348. else:
  3349. error_msg = (
  3350. f"We need to remove {num_tokens_to_remove} to truncate the input "
  3351. f"but the first sequence has a length {len(ids)}. "
  3352. )
  3353. if truncation_strategy == TruncationStrategy.ONLY_FIRST:
  3354. error_msg = (
  3355. error_msg + "Please select another truncation strategy than "
  3356. f"{truncation_strategy}, for instance 'longest_first' or 'only_second'."
  3357. )
  3358. logger.error(error_msg)
  3359. elif truncation_strategy == TruncationStrategy.LONGEST_FIRST:
  3360. logger.warning(
  3361. "Be aware, overflowing tokens are not returned for the setting you have chosen,"
  3362. f" i.e. sequence pairs with the '{TruncationStrategy.LONGEST_FIRST.value}' "
  3363. "truncation strategy. So the returned list will always be empty even if some "
  3364. "tokens have been removed."
  3365. )
  3366. len_pair_ids = len(pair_ids) if pair_ids is not None else 0
  3367. len_ids = len(ids)
  3368. first_remove = min(abs(len_pair_ids - len_ids), num_tokens_to_remove)
  3369. second_remove = num_tokens_to_remove - first_remove
  3370. if len_ids > len_pair_ids:
  3371. ids_to_move = first_remove + second_remove // 2
  3372. pair_ids_to_move = second_remove - second_remove // 2
  3373. else:
  3374. ids_to_move = second_remove // 2
  3375. pair_ids_to_move = first_remove + second_remove - (second_remove // 2)
  3376. if self.truncation_side == "right":
  3377. ids = ids[:-ids_to_move] if ids_to_move > 0 else ids
  3378. pair_ids = pair_ids[:-pair_ids_to_move] if pair_ids is not None and pair_ids_to_move > 0 else pair_ids
  3379. elif self.truncation_side == "left":
  3380. ids = ids[ids_to_move:]
  3381. pair_ids = pair_ids[pair_ids_to_move:] if pair_ids is not None else None
  3382. else:
  3383. raise ValueError(f"invalid truncation strategy:{self.truncation_side}")
  3384. elif truncation_strategy == TruncationStrategy.ONLY_SECOND and pair_ids is not None:
  3385. if len(pair_ids) > num_tokens_to_remove:
  3386. window_len = min(len(pair_ids), stride + num_tokens_to_remove)
  3387. if self.truncation_side == "right":
  3388. overflowing_tokens = pair_ids[-window_len:]
  3389. pair_ids = pair_ids[:-num_tokens_to_remove]
  3390. elif self.truncation_side == "left":
  3391. overflowing_tokens = pair_ids[:window_len]
  3392. pair_ids = pair_ids[num_tokens_to_remove:]
  3393. else:
  3394. raise ValueError(f"invalid truncation strategy:{self.truncation_side}")
  3395. else:
  3396. logger.error(
  3397. f"We need to remove {num_tokens_to_remove} to truncate the input "
  3398. f"but the second sequence has a length {len(pair_ids)}. "
  3399. f"Please select another truncation strategy than {truncation_strategy}, "
  3400. "for instance 'longest_first' or 'only_first'."
  3401. )
  3402. return (ids, pair_ids, overflowing_tokens)
  3403. def _pad(
  3404. self,
  3405. encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding],
  3406. max_length: Optional[int] = None,
  3407. padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
  3408. pad_to_multiple_of: Optional[int] = None,
  3409. padding_side: Optional[str] = None,
  3410. return_attention_mask: Optional[bool] = None,
  3411. ) -> dict:
  3412. """
  3413. Pad encoded inputs (on left/right and up to predefined length or max length in the batch)
  3414. Args:
  3415. encoded_inputs:
  3416. Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`).
  3417. max_length: maximum length of the returned list and optionally padding length (see below).
  3418. Will truncate by taking into account the special tokens.
  3419. padding_strategy: PaddingStrategy to use for padding.
  3420. - PaddingStrategy.LONGEST Pad to the longest sequence in the batch
  3421. - PaddingStrategy.MAX_LENGTH: Pad to the max length (default)
  3422. - PaddingStrategy.DO_NOT_PAD: Do not pad
  3423. The tokenizer padding sides are defined in `padding_side` argument:
  3424. - 'left': pads on the left of the sequences
  3425. - 'right': pads on the right of the sequences
  3426. pad_to_multiple_of: (optional) Integer if set will pad the sequence to a multiple of the provided value.
  3427. This is especially useful to enable the use of Tensor Core on NVIDIA hardware with compute capability
  3428. `>= 7.5` (Volta).
  3429. padding_side:
  3430. The side on which the model should have padding applied. Should be selected between ['right', 'left'].
  3431. Default value is picked from the class attribute of the same name.
  3432. return_attention_mask:
  3433. (optional) Set to False to avoid returning attention mask (default: set to model specifics)
  3434. """
  3435. # Load from model defaults
  3436. if return_attention_mask is None:
  3437. return_attention_mask = "attention_mask" in self.model_input_names
  3438. required_input = encoded_inputs[self.model_input_names[0]]
  3439. if padding_strategy == PaddingStrategy.LONGEST:
  3440. max_length = len(required_input)
  3441. if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
  3442. max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
  3443. needs_to_be_padded = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(required_input) != max_length
  3444. # Initialize attention mask if not present.
  3445. if return_attention_mask and "attention_mask" not in encoded_inputs:
  3446. encoded_inputs["attention_mask"] = [1] * len(required_input)
  3447. if needs_to_be_padded:
  3448. difference = max_length - len(required_input)
  3449. padding_side = padding_side if padding_side is not None else self.padding_side
  3450. if padding_side == "right":
  3451. if return_attention_mask:
  3452. encoded_inputs["attention_mask"] = encoded_inputs["attention_mask"] + [0] * difference
  3453. if "token_type_ids" in encoded_inputs:
  3454. encoded_inputs["token_type_ids"] = (
  3455. encoded_inputs["token_type_ids"] + [self.pad_token_type_id] * difference
  3456. )
  3457. if "special_tokens_mask" in encoded_inputs:
  3458. encoded_inputs["special_tokens_mask"] = encoded_inputs["special_tokens_mask"] + [1] * difference
  3459. encoded_inputs[self.model_input_names[0]] = required_input + [self.pad_token_id] * difference
  3460. elif padding_side == "left":
  3461. if return_attention_mask:
  3462. encoded_inputs["attention_mask"] = [0] * difference + encoded_inputs["attention_mask"]
  3463. if "token_type_ids" in encoded_inputs:
  3464. encoded_inputs["token_type_ids"] = [self.pad_token_type_id] * difference + encoded_inputs[
  3465. "token_type_ids"
  3466. ]
  3467. if "special_tokens_mask" in encoded_inputs:
  3468. encoded_inputs["special_tokens_mask"] = [1] * difference + encoded_inputs["special_tokens_mask"]
  3469. encoded_inputs[self.model_input_names[0]] = [self.pad_token_id] * difference + required_input
  3470. else:
  3471. raise ValueError(f"Invalid padding strategy:{padding_side}")
  3472. return encoded_inputs
  3473. def convert_tokens_to_string(self, tokens: list[str]) -> str:
  3474. """
  3475. Converts a sequence of tokens in a single string. The most simple way to do it is `" ".join(tokens)` but we
  3476. often want to remove sub-word tokenization artifacts at the same time.
  3477. Args:
  3478. tokens (`list[str]`): The token to join in a string.
  3479. Returns:
  3480. `str`: The joined tokens.
  3481. """
  3482. raise NotImplementedError
  3483. def batch_decode(
  3484. self,
  3485. sequences: Union[list[int], list[list[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"],
  3486. skip_special_tokens: bool = False,
  3487. clean_up_tokenization_spaces: Optional[bool] = None,
  3488. **kwargs,
  3489. ) -> list[str]:
  3490. """
  3491. Convert a list of lists of token ids into a list of strings by calling decode.
  3492. Args:
  3493. sequences (`Union[list[int], list[list[int]], np.ndarray, torch.Tensor, tf.Tensor]`):
  3494. List of tokenized input ids. Can be obtained using the `__call__` method.
  3495. skip_special_tokens (`bool`, *optional*, defaults to `False`):
  3496. Whether or not to remove special tokens in the decoding.
  3497. clean_up_tokenization_spaces (`bool`, *optional*):
  3498. Whether or not to clean up the tokenization spaces. If `None`, will default to
  3499. `self.clean_up_tokenization_spaces`.
  3500. kwargs (additional keyword arguments, *optional*):
  3501. Will be passed to the underlying model specific decode method.
  3502. Returns:
  3503. `list[str]`: The list of decoded sentences.
  3504. """
  3505. return [
  3506. self.decode(
  3507. seq,
  3508. skip_special_tokens=skip_special_tokens,
  3509. clean_up_tokenization_spaces=clean_up_tokenization_spaces,
  3510. **kwargs,
  3511. )
  3512. for seq in sequences
  3513. ]
  3514. def decode(
  3515. self,
  3516. token_ids: Union[int, list[int], np.ndarray, "torch.Tensor"],
  3517. skip_special_tokens: bool = False,
  3518. clean_up_tokenization_spaces: Optional[bool] = None,
  3519. **kwargs,
  3520. ) -> str:
  3521. """
  3522. Converts a sequence of ids in a string, using the tokenizer and vocabulary with options to remove special
  3523. tokens and clean up tokenization spaces.
  3524. Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`.
  3525. Args:
  3526. token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`):
  3527. List of tokenized input ids. Can be obtained using the `__call__` method.
  3528. skip_special_tokens (`bool`, *optional*, defaults to `False`):
  3529. Whether or not to remove special tokens in the decoding.
  3530. clean_up_tokenization_spaces (`bool`, *optional*):
  3531. Whether or not to clean up the tokenization spaces. If `None`, will default to
  3532. `self.clean_up_tokenization_spaces`.
  3533. kwargs (additional keyword arguments, *optional*):
  3534. Will be passed to the underlying model specific decode method.
  3535. Returns:
  3536. `str`: The decoded sentence.
  3537. """
  3538. # Convert inputs to python lists
  3539. token_ids = to_py_obj(token_ids)
  3540. return self._decode(
  3541. token_ids=token_ids,
  3542. skip_special_tokens=skip_special_tokens,
  3543. clean_up_tokenization_spaces=clean_up_tokenization_spaces,
  3544. **kwargs,
  3545. )
  3546. def _decode(
  3547. self,
  3548. token_ids: Union[int, list[int]],
  3549. skip_special_tokens: bool = False,
  3550. clean_up_tokenization_spaces: Optional[bool] = None,
  3551. **kwargs,
  3552. ) -> str:
  3553. raise NotImplementedError
  3554. def get_special_tokens_mask(
  3555. self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False
  3556. ) -> list[int]:
  3557. """
  3558. Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
  3559. special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods.
  3560. Args:
  3561. token_ids_0 (`list[int]`):
  3562. List of ids of the first sequence.
  3563. token_ids_1 (`list[int]`, *optional*):
  3564. List of ids of the second sequence.
  3565. already_has_special_tokens (`bool`, *optional*, defaults to `False`):
  3566. Whether or not the token list is already formatted with special tokens for the model.
  3567. Returns:
  3568. A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
  3569. """
  3570. assert already_has_special_tokens and token_ids_1 is None, (
  3571. "You cannot use ``already_has_special_tokens=False`` with this tokenizer. "
  3572. "Please use a slow (full python) tokenizer to activate this argument. "
  3573. "Or set `return_special_tokens_mask=True` when calling the encoding method "
  3574. "to get the special tokens mask in any tokenizer. "
  3575. )
  3576. all_special_ids = self.all_special_ids # cache the property
  3577. special_tokens_mask = [1 if token in all_special_ids else 0 for token in token_ids_0]
  3578. return special_tokens_mask
  3579. @staticmethod
  3580. def clean_up_tokenization(out_string: str) -> str:
  3581. """
  3582. Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms.
  3583. Args:
  3584. out_string (`str`): The text to clean up.
  3585. Returns:
  3586. `str`: The cleaned-up string.
  3587. """
  3588. out_string = (
  3589. out_string.replace(" .", ".")
  3590. .replace(" ?", "?")
  3591. .replace(" !", "!")
  3592. .replace(" ,", ",")
  3593. .replace(" ' ", "'")
  3594. .replace(" n't", "n't")
  3595. .replace(" 'm", "'m")
  3596. .replace(" 's", "'s")
  3597. .replace(" 've", "'ve")
  3598. .replace(" 're", "'re")
  3599. )
  3600. return out_string
  3601. def _eventual_warn_about_too_long_sequence(self, ids: list[int], max_length: Optional[int], verbose: bool):
  3602. """
  3603. Depending on the input and internal state we might trigger a warning about a sequence that is too long for its
  3604. corresponding model
  3605. Args:
  3606. ids (`list[str]`): The ids produced by the tokenization
  3607. max_length (`int`, *optional*): The max_length desired (does not trigger a warning if it is set)
  3608. verbose (`bool`): Whether or not to print more information and warnings.
  3609. """
  3610. if max_length is None and len(ids) > self.model_max_length and verbose and self.model_max_length != 0:
  3611. if not self.deprecation_warnings.get("sequence-length-is-longer-than-the-specified-maximum", False):
  3612. logger.warning(
  3613. "Token indices sequence length is longer than the specified maximum sequence length "
  3614. f"for this model ({len(ids)} > {self.model_max_length}). Running this sequence through the model "
  3615. "will result in indexing errors"
  3616. )
  3617. self.deprecation_warnings["sequence-length-is-longer-than-the-specified-maximum"] = True
  3618. def _switch_to_input_mode(self):
  3619. """
  3620. Private method to put the tokenizer in input mode (when it has different modes for input/outputs)
  3621. """
  3622. pass
  3623. def _switch_to_target_mode(self):
  3624. """
  3625. Private method to put the tokenizer in target mode (when it has different modes for input/outputs)
  3626. """
  3627. pass
  3628. @contextmanager
  3629. def as_target_tokenizer(self):
  3630. """
  3631. Temporarily sets the tokenizer for encoding the targets. Useful for tokenizer associated to
  3632. sequence-to-sequence models that need a slightly different processing for the labels.
  3633. """
  3634. warnings.warn(
  3635. "`as_target_tokenizer` is deprecated and will be removed in v5 of Transformers. You can tokenize your "
  3636. "labels by using the argument `text_target` of the regular `__call__` method (either in the same call as "
  3637. "your input texts if you use the same keyword arguments, or in a separate call."
  3638. )
  3639. self._switch_to_target_mode()
  3640. self._in_target_context_manager = True
  3641. yield
  3642. self._in_target_context_manager = False
  3643. self._switch_to_input_mode()
  3644. @classmethod
  3645. def register_for_auto_class(cls, auto_class="AutoTokenizer"):
  3646. """
  3647. Register this class with a given auto class. This should only be used for custom tokenizers as the ones in the
  3648. library are already mapped with `AutoTokenizer`.
  3649. Args:
  3650. auto_class (`str` or `type`, *optional*, defaults to `"AutoTokenizer"`):
  3651. The auto class to register this new tokenizer with.
  3652. """
  3653. if not isinstance(auto_class, str):
  3654. auto_class = auto_class.__name__
  3655. import transformers.models.auto as auto_module
  3656. if not hasattr(auto_module, auto_class):
  3657. raise ValueError(f"{auto_class} is not a valid auto class.")
  3658. cls._auto_class = auto_class
  3659. def prepare_seq2seq_batch(
  3660. self,
  3661. src_texts: list[str],
  3662. tgt_texts: Optional[list[str]] = None,
  3663. max_length: Optional[int] = None,
  3664. max_target_length: Optional[int] = None,
  3665. padding: str = "longest",
  3666. return_tensors: Optional[str] = None,
  3667. truncation: bool = True,
  3668. **kwargs,
  3669. ) -> BatchEncoding:
  3670. """
  3671. Prepare model inputs for translation. For best performance, translate one sentence at a time.
  3672. Arguments:
  3673. src_texts (`list[str]`):
  3674. List of documents to summarize or source language texts.
  3675. tgt_texts (`list`, *optional*):
  3676. List of summaries or target language texts.
  3677. max_length (`int`, *optional*):
  3678. Controls the maximum length for encoder inputs (documents to summarize or source language texts) If
  3679. left unset or set to `None`, this will use the predefined model maximum length if a maximum length is
  3680. required by one of the truncation/padding parameters. If the model has no specific maximum input length
  3681. (like XLNet) truncation/padding to a maximum length will be deactivated.
  3682. max_target_length (`int`, *optional*):
  3683. Controls the maximum length of decoder inputs (target language texts or summaries) If left unset or set
  3684. to `None`, this will use the max_length value.
  3685. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
  3686. Activates and controls padding. Accepts the following values:
  3687. - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
  3688. sequence if provided).
  3689. - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
  3690. acceptable input length for the model if that argument is not provided.
  3691. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
  3692. lengths).
  3693. return_tensors (`str` or [`~utils.TensorType`], *optional*):
  3694. If set, will return tensors instead of list of python integers. Acceptable values are:
  3695. - `'tf'`: Return TensorFlow `tf.constant` objects.
  3696. - `'pt'`: Return PyTorch `torch.Tensor` objects.
  3697. - `'np'`: Return Numpy `np.ndarray` objects.
  3698. truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `True`):
  3699. Activates and controls truncation. Accepts the following values:
  3700. - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or
  3701. to the maximum acceptable input length for the model if that argument is not provided. This will
  3702. truncate token by token, removing a token from the longest sequence in the pair if a pair of
  3703. sequences (or a batch of pairs) is provided.
  3704. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the
  3705. maximum acceptable input length for the model if that argument is not provided. This will only
  3706. truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
  3707. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the
  3708. maximum acceptable input length for the model if that argument is not provided. This will only
  3709. truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
  3710. - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths
  3711. greater than the model maximum admissible input size).
  3712. **kwargs:
  3713. Additional keyword arguments passed along to `self.__call__`.
  3714. Return:
  3715. [`BatchEncoding`]: A [`BatchEncoding`] with the following fields:
  3716. - **input_ids** -- List of token ids to be fed to the encoder.
  3717. - **attention_mask** -- List of indices specifying which tokens should be attended to by the model.
  3718. - **labels** -- List of token ids for tgt_texts.
  3719. The full set of keys `[input_ids, attention_mask, labels]`, will only be returned if tgt_texts is passed.
  3720. Otherwise, input_ids, attention_mask will be the only keys.
  3721. """
  3722. # docstyle-ignore
  3723. formatted_warning = """
  3724. `prepare_seq2seq_batch` is deprecated and will be removed in version 5 of HuggingFace Transformers. Use the regular
  3725. `__call__` method to prepare your inputs and targets.
  3726. Here is a short example:
  3727. model_inputs = tokenizer(src_texts, text_target=tgt_texts, ...)
  3728. If you either need to use different keyword arguments for the source and target texts, you should do two calls like
  3729. this:
  3730. model_inputs = tokenizer(src_texts, ...)
  3731. labels = tokenizer(text_target=tgt_texts, ...)
  3732. model_inputs["labels"] = labels["input_ids"]
  3733. See the documentation of your specific tokenizer for more details on the specific arguments to the tokenizer of choice.
  3734. For a more complete example, see the implementation of `prepare_seq2seq_batch`.
  3735. """
  3736. warnings.warn(formatted_warning, FutureWarning)
  3737. # mBART-specific kwargs that should be ignored by other models.
  3738. kwargs.pop("src_lang", None)
  3739. kwargs.pop("tgt_lang", None)
  3740. if max_length is None:
  3741. max_length = self.model_max_length
  3742. model_inputs = self(
  3743. src_texts,
  3744. add_special_tokens=True,
  3745. return_tensors=return_tensors,
  3746. max_length=max_length,
  3747. padding=padding,
  3748. truncation=truncation,
  3749. **kwargs,
  3750. )
  3751. if tgt_texts is None:
  3752. return model_inputs
  3753. # Process tgt_texts
  3754. if max_target_length is None:
  3755. max_target_length = max_length
  3756. with self.as_target_tokenizer():
  3757. labels = self(
  3758. tgt_texts,
  3759. add_special_tokens=True,
  3760. return_tensors=return_tensors,
  3761. padding=padding,
  3762. max_length=max_target_length,
  3763. truncation=truncation,
  3764. **kwargs,
  3765. )
  3766. model_inputs["labels"] = labels["input_ids"]
  3767. return model_inputs
  3768. def get_fast_tokenizer_file(tokenization_files: list[str]) -> str:
  3769. """
  3770. Get the tokenization file to use for this version of transformers.
  3771. Args:
  3772. tokenization_files (`list[str]`): The list of available configuration files.
  3773. Returns:
  3774. `str`: The tokenization file to use.
  3775. """
  3776. tokenizer_files_map = {}
  3777. for file_name in tokenization_files:
  3778. search = _re_tokenizer_file.search(file_name)
  3779. if search is not None:
  3780. v = search.groups()[0]
  3781. tokenizer_files_map[v] = file_name
  3782. available_versions = sorted(tokenizer_files_map.keys())
  3783. # Defaults to FULL_TOKENIZER_FILE and then try to look at some newer versions.
  3784. tokenizer_file = FULL_TOKENIZER_FILE
  3785. transformers_version = version.parse(__version__)
  3786. for v in available_versions:
  3787. if version.parse(v) <= transformers_version:
  3788. tokenizer_file = tokenizer_files_map[v]
  3789. else:
  3790. # No point going further since the versions are sorted.
  3791. break
  3792. return tokenizer_file
  3793. # To update the docstring, we need to copy the method, otherwise we change the original docstring.
  3794. PreTrainedTokenizerBase.push_to_hub = copy_func(PreTrainedTokenizerBase.push_to_hub)
  3795. if PreTrainedTokenizerBase.push_to_hub.__doc__ is not None:
  3796. PreTrainedTokenizerBase.push_to_hub.__doc__ = PreTrainedTokenizerBase.push_to_hub.__doc__.format(
  3797. object="tokenizer", object_class="AutoTokenizer", object_files="tokenizer files"
  3798. )