multimodalart HF Staff commited on
Commit
8ba3a61
·
verified ·
1 Parent(s): 12cc049

Patch init_weights and _finalize_model_loading to avoid passing extra kwargs

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -21,11 +21,11 @@ NO_THINK = "/no think"
21
  # The custom TinyGPTForCausalLM (written for transformers 4.x) uses:
22
  # 1. tie_weights() without `recompute_mapping` / `missing_keys` kwargs
23
  # 2. self._tie_or_clone_weights() — removed in transformers 5.x
24
- # transformers 5.x calls tie_weights with these kwargs in two places:
25
- # - post_init -> init_weights -> tie_weights(recompute_mapping=False)
26
  # - _finalize_model_loading -> tie_weights(missing_keys=..., recompute_mapping=False)
27
- # We patch tie_weights universally to strip unknown kwargs, and add the
28
- # missing _tie_or_clone_weights method.
29
 
30
  # Add the missing _tie_or_clone_weights method (simple weight tying)
31
  def _tie_or_clone_weights(self, output_embeddings, input_embeddings):
@@ -36,23 +36,33 @@ def _tie_or_clone_weights(self, output_embeddings, input_embeddings):
36
  if not hasattr(PreTrainedModel, "_tie_or_clone_weights"):
37
  PreTrainedModel._tie_or_clone_weights = _tie_or_clone_weights
38
 
39
- # Wrap tie_weights to accept and strip unknown kwargs (transformers 5.x compat)
40
- _orig_tie_weights = PreTrainedModel.tie_weights
41
-
42
- def _kwargs_safe_tie_weights(self, *args, **kwargs):
43
- # Strip kwargs that transformers 5.x passes but the custom model doesn't accept
44
- kwargs.pop("recompute_mapping", None)
45
- kwargs.pop("missing_keys", None)
46
- # Call the bound method (which may be the custom override)
47
- # We use the MRO to find the actual implementation
48
- for cls in type(self).__mro__:
49
- if cls is PreTrainedModel:
50
- break
51
- if "tie_weights" in cls.__dict__:
52
- return cls.__dict__["tie_weights"](self, *args, **kwargs)
53
- return _orig_tie_weights(self, *args, **kwargs)
54
-
55
- PreTrainedModel.tie_weights = _kwargs_safe_tie_weights
 
 
 
 
 
 
 
 
 
 
56
 
57
  # ============================================================
58
  # Model + tokenizer loaded at module scope
 
21
  # The custom TinyGPTForCausalLM (written for transformers 4.x) uses:
22
  # 1. tie_weights() without `recompute_mapping` / `missing_keys` kwargs
23
  # 2. self._tie_or_clone_weights() — removed in transformers 5.x
24
+ # transformers 5.x calls tie_weights with extra kwargs in two places:
25
+ # - init_weights -> tie_weights(recompute_mapping=False)
26
  # - _finalize_model_loading -> tie_weights(missing_keys=..., recompute_mapping=False)
27
+ # We replace both callers to call tie_weights() without the extra kwargs,
28
+ # and add the missing _tie_or_clone_weights method.
29
 
30
  # Add the missing _tie_or_clone_weights method (simple weight tying)
31
  def _tie_or_clone_weights(self, output_embeddings, input_embeddings):
 
36
  if not hasattr(PreTrainedModel, "_tie_or_clone_weights"):
37
  PreTrainedModel._tie_or_clone_weights = _tie_or_clone_weights
38
 
39
+ # Replace init_weights: call tie_weights without recompute_mapping kwarg
40
+ def _patched_init_weights(self):
41
+ self.tie_weights()
42
+
43
+ PreTrainedModel.init_weights = _patched_init_weights
44
+
45
+ # Replace _finalize_model_loading: call tie_weights without extra kwargs
46
+ if hasattr(PreTrainedModel, "_finalize_model_loading"):
47
+ _orig_finalize = PreTrainedModel._finalize_model_loading
48
+
49
+ def _patched_finalize(self, model, load_config=None, loading_info=None, *args, **kwargs):
50
+ # Call tie_weights without missing_keys/recompute_mapping
51
+ model.tie_weights()
52
+ # Call the original without the tie_weights part
53
+ # Actually we need to call the original to handle everything else it does
54
+ # but we need to prevent it from calling tie_weights again.
55
+ # The simplest approach: temporarily swap tie_weights to a no-op,
56
+ # call the original, then restore.
57
+ _orig_tie = model.tie_weights
58
+ model.tie_weights = lambda *a, **kw: None
59
+ try:
60
+ result = _orig_finalize(self, model, load_config, loading_info, *args, **kwargs)
61
+ finally:
62
+ model.tie_weights = _orig_tie
63
+ return result
64
+
65
+ PreTrainedModel._finalize_model_loading = _patched_finalize
66
 
67
  # ============================================================
68
  # Model + tokenizer loaded at module scope