@@ -1161,6 +1161,17 @@ def _model_has_vision_config(model_name: str) -> bool:
11611161}
11621162
11631163
1164+ # Tokenizer repos to use when a canonical model repo is gated but an
1165+ # audited unrestricted mirror ships byte-identical tokenizer files and
1166+ # chat_template. The returned tokenizer keeps the caller's original
1167+ # ``name_or_path`` so exact-match renderer resolution still uses
1168+ # ``MODEL_RENDERER_MAP``.
1169+ TOKENIZER_SOURCE_OVERRIDES : dict [str , str ] = {
1170+ "meta-llama/Llama-3.2-1B-Instruct" : "unsloth/Llama-3.2-1B-Instruct" ,
1171+ "meta-llama/Llama-3.2-3B-Instruct" : "unsloth/Llama-3.2-3B-Instruct" ,
1172+ }
1173+
1174+
11641175# Models for which ``fastokens`` is known to diverge from vanilla
11651176# ``transformers.AutoTokenizer`` and therefore must NOT be patched.
11661177# Empirical audit ran each entry of ``MODEL_RENDERER_MAP`` through both
@@ -1184,6 +1195,42 @@ def _model_has_vision_config(model_name: str) -> bool:
11841195_FASTOKENS_ANNOUNCED = False
11851196
11861197
1198+ def _tokenizer_source_for (model_name_or_path : str ) -> str :
1199+ return TOKENIZER_SOURCE_OVERRIDES .get (model_name_or_path , model_name_or_path )
1200+
1201+
1202+ def _tokenizer_load_kwargs (model_name_or_path : str ) -> dict [str , Any ]:
1203+ revision = TRUSTED_REVISIONS .get (model_name_or_path )
1204+ if revision is not None :
1205+ return {"trust_remote_code" : True , "revision" : revision }
1206+ return {"trust_remote_code" : False }
1207+
1208+
1209+ def _preserve_requested_tokenizer_name (
1210+ tokenizer ,
1211+ * ,
1212+ requested_name_or_path : str ,
1213+ loaded_name_or_path : str ,
1214+ ):
1215+ if requested_name_or_path == loaded_name_or_path :
1216+ return tokenizer
1217+
1218+ try :
1219+ tokenizer .name_or_path = requested_name_or_path
1220+ except Exception :
1221+ init_kwargs = getattr (tokenizer , "init_kwargs" , None )
1222+ if isinstance (init_kwargs , dict ):
1223+ init_kwargs ["name_or_path" ] = requested_name_or_path
1224+
1225+ if getattr (tokenizer , "name_or_path" , "" ) != requested_name_or_path :
1226+ raise RuntimeError (
1227+ f"Loaded tokenizer for { requested_name_or_path !r} from "
1228+ f"{ loaded_name_or_path !r} , but could not preserve the requested "
1229+ "name_or_path for renderer auto-resolution."
1230+ )
1231+ return tokenizer
1232+
1233+
11871234def _patched_load (model_name_or_path : str , ** kwargs ):
11881235 """Run ``AutoTokenizer.from_pretrained`` with fastokens patched in
11891236 process-locally — patch around the load, unpatch right after.
@@ -1321,29 +1368,41 @@ def load_tokenizer(
13211368 validation for configs with nested ``rope_parameters``), we fall
13221369 back to loading the repo's self-contained ``tokenizer.json``
13231370 directly — see ``_load_tokenizer_via_auto``.
1324- """
1325- kwargs : dict [str , Any ] = {}
1326- revision = TRUSTED_REVISIONS .get (model_name_or_path )
1327- if revision is not None :
1328- kwargs = {"trust_remote_code" : True , "revision" : revision }
1329- else :
1330- kwargs = {"trust_remote_code" : False }
13311371
1332- if not use_fastokens or model_name_or_path in FASTOKENS_INCOMPATIBLE :
1333- return _load_tokenizer_via_auto (model_name_or_path , ** kwargs )
1372+ Canonical Meta Llama-3.2 Instruct repos are gated on HuggingFace. For
1373+ those exact IDs we load tokenizer files from the audited unrestricted
1374+ ``unsloth`` mirrors instead, then restore ``tokenizer.name_or_path`` to
1375+ the requested Meta ID so auto-resolution still selects ``Llama3Renderer``.
1376+ """
1377+ load_name_or_path = _tokenizer_source_for (model_name_or_path )
1378+ kwargs = _tokenizer_load_kwargs (load_name_or_path )
1379+
1380+ if not use_fastokens or load_name_or_path in FASTOKENS_INCOMPATIBLE :
1381+ tok = _load_tokenizer_via_auto (load_name_or_path , ** kwargs )
1382+ return _preserve_requested_tokenizer_name (
1383+ tok ,
1384+ requested_name_or_path = model_name_or_path ,
1385+ loaded_name_or_path = load_name_or_path ,
1386+ )
13341387
13351388 try :
1336- return _patched_load (model_name_or_path , ** kwargs )
1389+ tok = _patched_load (load_name_or_path , ** kwargs )
13371390 except Exception as exc :
13381391 logger .info (
13391392 "fastokens could not load %r (%s: %s); falling back to vanilla "
13401393 "AutoTokenizer. Add this model to FASTOKENS_INCOMPATIBLE in "
13411394 "renderers.base to suppress the retry." ,
1342- model_name_or_path ,
1395+ load_name_or_path ,
13431396 type (exc ).__name__ ,
13441397 str (exc )[:160 ],
13451398 )
1346- return _load_tokenizer_via_auto (model_name_or_path , ** kwargs )
1399+ tok = _load_tokenizer_via_auto (load_name_or_path , ** kwargs )
1400+
1401+ return _preserve_requested_tokenizer_name (
1402+ tok ,
1403+ requested_name_or_path = model_name_or_path ,
1404+ loaded_name_or_path = load_name_or_path ,
1405+ )
13471406
13481407
13491408def _populate_registry ():
@@ -1709,12 +1768,8 @@ def _get_offset_tokenizer(tokenizer):
17091768 if cached is not None :
17101769 return cached
17111770
1712- kwargs : dict [str , Any ] = {}
1713- revision = TRUSTED_REVISIONS .get (name_or_path )
1714- if revision is not None :
1715- kwargs = {"trust_remote_code" : True , "revision" : revision }
1716- else :
1717- kwargs = {"trust_remote_code" : False }
1771+ load_name_or_path = _tokenizer_source_for (name_or_path )
1772+ kwargs = _tokenizer_load_kwargs (load_name_or_path )
17181773
17191774 def _has_offsets (tok ) -> bool :
17201775 if not getattr (tok , "is_fast" , False ):
@@ -1734,7 +1789,12 @@ def _has_offsets(tok) -> bool:
17341789 # off — serialized against pool patch/unpatch via ``_FASTOKENS_PATCH_LOCK``
17351790 # so no concurrent window can swap the shim back in mid-load — then
17361791 # restore the prior patch state. Never cache a non-offset tokenizer.
1737- offset_tok = _load_tokenizer_via_auto (name_or_path , ** kwargs )
1792+ offset_tok = _load_tokenizer_via_auto (load_name_or_path , ** kwargs )
1793+ offset_tok = _preserve_requested_tokenizer_name (
1794+ offset_tok ,
1795+ requested_name_or_path = name_or_path ,
1796+ loaded_name_or_path = load_name_or_path ,
1797+ )
17381798 if not _has_offsets (offset_tok ):
17391799 import fastokens
17401800
@@ -1744,7 +1804,12 @@ def _has_offsets(tok) -> bool:
17441804 with contextlib .redirect_stdout (io .StringIO ()):
17451805 fastokens .unpatch_transformers ()
17461806 try :
1747- offset_tok = _load_tokenizer_via_auto (name_or_path , ** kwargs )
1807+ offset_tok = _load_tokenizer_via_auto (load_name_or_path , ** kwargs )
1808+ offset_tok = _preserve_requested_tokenizer_name (
1809+ offset_tok ,
1810+ requested_name_or_path = name_or_path ,
1811+ loaded_name_or_path = load_name_or_path ,
1812+ )
17481813 finally :
17491814 if was_patched :
17501815 with contextlib .redirect_stdout (io .StringIO ()):
0 commit comments