Spaces:
Running on Zero
Running on Zero
Patch init_weights and _finalize_model_loading to avoid passing extra kwargs
Browse files
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
|
| 25 |
-
# -
|
| 26 |
# - _finalize_model_loading -> tie_weights(missing_keys=..., recompute_mapping=False)
|
| 27 |
-
# We
|
| 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 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|