2727log = setup_logger ()
2828
2929
30- # Older remote model files sometimes store tied-weight metadata as a plain list
31- # like `["lm_head.weight"]`, but transformers 5.x now expects `{target: source}`.
30+ # Older remote model files sometimes store `_tied_weights_keys` as a plain list
31+ # like `["lm_head.weight"]`. transformers 5.x now expects `{target: source}`,
32+ # and otherwise later save/load helpers fail with `'list' object has no attribute 'keys'`.
3233def _resolve_legacy_tied_weights_mapping (model : PreTrainedModel , tied_mapping ) -> dict [str , str ]:
3334 if not isinstance (tied_mapping , (list , tuple , set )):
3435 return {}
@@ -59,8 +60,9 @@ def _resolve_legacy_tied_weights_mapping(model: PreTrainedModel, tied_mapping) -
5960 }
6061
6162
62- # Rewrite legacy list-based `_tied_weights_keys` in-place so newer HF save/load
63- # helpers stop tripping over remote code that still uses the old format.
63+ # Rewrite legacy list-based `_tied_weights_keys` in-place so transformers 5.x
64+ # save/load code stops crashing on older trust_remote_code models that still use
65+ # the pre-5.x list format.
6466def _normalize_legacy_tied_weights_keys (model : PreTrainedModel ) -> None :
6567 for _name , submodule in model .named_modules (remove_duplicate = False ):
6668 tied_mapping = getattr (submodule , "_tied_weights_keys" , None )
@@ -73,17 +75,17 @@ def _normalize_legacy_tied_weights_keys(model: PreTrainedModel) -> None:
7375 submodule ._tied_weights_keys = {}
7476
7577
76- # Bridge a few transformers 5.x API/config changes so older trust_remote_code
77- # model files still import and initialize without patching their cached source.
78+ # Bridge a few transformers 5.x API changes so older trust_remote_code model
79+ # files still import and initialize without editing the cached remote source.
7880def _patch_transformers_remote_code_compat () -> None :
7981 try :
8082 from transformers .utils import import_utils
8183 except Exception :
8284 return
8385
8486 if not hasattr (import_utils , "is_torch_fx_available" ):
85- # transformers 5.x dropped this helper , but a number of remote model
86- # implementations still import it from transformers.utils.import_utils .
87+ # transformers 5.x removed `import_utils.is_torch_fx_available` , but
88+ # older remote model files still import it during module import .
8789 def is_torch_fx_available () -> bool :
8890 return hasattr (torch , "fx" )
8991
@@ -94,7 +96,8 @@ def is_torch_fx_available() -> bool:
9496
9597 def get_expanded_tied_weights_keys (self , all_submodels : bool = False ) -> dict :
9698 # transformers 5.x expects `_tied_weights_keys` to be a dict, while
97- # older trust_remote_code models still declare it as `["lm_head.weight"]`.
99+ # older trust_remote_code models still declare `["lm_head.weight"]`.
100+ # Handle the legacy form here so HF tied-weight expansion still works.
98101 tied_mapping = getattr (self , "_tied_weights_keys" , None )
99102 if not isinstance (tied_mapping , (list , tuple , set )):
100103 return original_get_expanded_tied_weights_keys (self , all_submodels = all_submodels )
@@ -120,11 +123,11 @@ def get_expanded_tied_weights_keys(self, all_submodels: bool = False) -> dict:
120123 PreTrainedModel ._gptqmodel_legacy_tied_weights_patch = True
121124
122125
123- # Restore the pre-transformers-5 RoPE config shape expected by older remote
124- # MiniCPM code before HF instantiates the architecture from config.
126+ # Restore the pre-transformers-5 RoPE config shape expected by older MiniCPM
127+ # remote code before HF instantiates the architecture from config.
125128def _normalize_remote_code_config_compat (config : Any ) -> None :
126129 # transformers 5.x normalizes RoPE config to `rope_type`, but older
127- # remote MiniCPM code still expects `rope_scaling["type"]` or `None`.
130+ # MiniCPM remote code still reads `rope_scaling["type"]` or expects `None`.
128131 rope_scaling = getattr (config , "rope_scaling" , None )
129132 if not isinstance (rope_scaling , dict ) or "type" in rope_scaling :
130133 return
0 commit comments