Spaces:
Running on Zero
Running on Zero
Universal tie_weights kwargs stripping for transformers 5.x
Browse files18 unmodified lines1920212223242526274 unmodified lines32333435363738394041424344454647484950515218 unmodified lines# Patch for transformers 5.x + custom model compatibility# ============================================================# The custom TinyGPTForCausalLM (written for transformers 4.x) uses:# 1. tie_weights() without the `recompute_mapping` kwarg# 2. self._tie_or_clone_weights() — removed in transformers 5.x# We patch both to make the custom code work with transformers 5.x.# Add the missing _tie_or_clone_weights method (simple weight tying)def _tie_or_clone_weights(self, output_embeddings, input_embeddings):4 unmodified linesif not hasattr(PreTrainedModel, "_tie_or_clone_weights"):PreTrainedModel._tie_or_clone_weights = _tie_or_clone_weights# Patch init_weights to strip the recompute_mapping kwarg_orig_init_weights = PreTrainedModel.init_weightsdef _patched_init_weights(self):original_tie_weights = self.tie_weightsdef _safe_tie_weights(*args, **kwargs):kwargs.pop("recompute_mapping", None)return original_tie_weights(*args, **kwargs)self.tie_weights = _safe_tie_weightstry:_orig_init_weights(self)finally:self.tie_weights = original_tie_weightsPreTrainedModel.init_weights = _patched_init_weights# ============================================================# Model + tokenizer loaded at module scope18 unmodified lines192021222324252627282930314 unmodified lines363738394041424344454647484950515253545556575818 unmodified lines# Patch for transformers 5.x + custom model compatibility# ============================================================# The custom TinyGPTForCausalLM (written for transformers 4.x) uses:# 1. tie_weights() without `recompute_mapping` / `missing_keys` kwargs# 2. self._tie_or_clone_weights() — removed in transformers 5.x# transformers 5.x calls tie_weights with these kwargs in two places:# - post_init -> init_weights -> tie_weights(recompute_mapping=False)# - _finalize_model_loading -> tie_weights(missing_keys=..., recompute_mapping=False)# We patch tie_weights universally to strip unknown kwargs, and add the# missing _tie_or_clone_weights method.# Add the missing _tie_or_clone_weights method (simple weight tying)def _tie_or_clone_weights(self, output_embeddings, input_embeddings):4 unmodified linesif not hasattr(PreTrainedModel, "_tie_or_clone_weights"):PreTrainedModel._tie_or_clone_weights = _tie_or_clone_weights# Wrap tie_weights to accept and strip unknown kwargs (transformers 5.x compat)_orig_tie_weights = PreTrainedModel.tie_weightsdef _kwargs_safe_tie_weights(self, *args, **kwargs):# Strip kwargs that transformers 5.x passes but the custom model doesn't acceptkwargs.pop("recompute_mapping", None)kwargs.pop("missing_keys", None)# Call the bound method (which may be the custom override)# We use the MRO to find the actual implementationfor cls in type(self).__mro__:if cls is PreTrainedModel:breakif "tie_weights" in cls.__dict__:return cls.__dict__["tie_weights"](self, *args, **kwargs)return _orig_tie_weights(self, *args, **kwargs)PreTrainedModel.tie_weights = _kwargs_safe_tie_weights# ============================================================# Model + tokenizer loaded at module scope
app.py+23-17