Fix #45127: Auto-fix diverged tie_word_embeddings config on save to prevent silent weight corruption#45136
Closed
Cursx wants to merge 2 commits intohuggingface:mainfrom
Closed
Fix #45127: Auto-fix diverged tie_word_embeddings config on save to prevent silent weight corruption#45136Cursx wants to merge 2 commits intohuggingface:mainfrom
Cursx wants to merge 2 commits intohuggingface:mainfrom
Conversation
…re independently modified outside of Transformers (e.g., via PEFT)
9ad243f to
3f68e2c
Compare
Member
|
Please don't tick the "I confirm this is not a code agent PR" box and then send me a code agent PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR fixes a bug in
PreTrainedModel.save_pretrained()whereconfig.tie_word_embeddingscan be inconsistent with the actual weight state, leading to silent model corruption for downstream consumers.Problem
After PEFT
merge_and_unload()(typical scenario: Qwen, Llama, Mistral, etc.),embed_tokensand lm_head weights are separated in memory with different values, butconfig.tie_word_embeddingsremainsTrue. Currently, save_pretrained() performs no validation oftie_word_embeddingsagainst the actual weight state — the incorrect config is written toconfig.jsonas-is.This causes two issues:
tie_word_embeddings: trueinconfig.jsondirectly, potentially causing silent weight corruption — one tensor overwrites the other, producing completely degraded outputs.Fix
In save_pretrained(), before writing the config to disk, we detect whether the input/output embeddings have diverged. If so, we automatically set
config.tie_word_embeddings = Falseand emit a warning.Key safety considerations:
lm_head.weight) is explicitly declared in the model's _tied_weights_keys mapping as tied to the input embedding. This prevents false positives on models like Pop2Piano, which usestie_word_embeddings=Truefor decoder output scaling but does not declarelm_head.weightin its _tied_weights_keys (it only tiesencoder.embed_tokensanddecoder.embed_tokensto shared).scale_decoder_outputsfield (configuration_t5.py:82-83) andtie_word_embeddingsis forced toTrue. Not affected.tie_word_embeddings = True— even if saved asFalse, it's restored on load. Not affected.tie_word_embeddingsfor scaling in forward, but these are guarded by the _tied_weights_keys check described above.Changes
modeling_utils.py:
NotImplementedError: silently ignored (expected for vision/speech backbones)logger.debugtest_modeling_utils.py:
Fixes # #45127
Code Agent Policy
The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.
PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.
This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read
CONTRIBUTING.md.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.