Prevent Reinitialization of Resized LM Head When tie_word_embeddings is False #35141#36221
Open
sambhavnoobcoder wants to merge 12 commits intohuggingface:mainfrom
Open
Prevent Reinitialization of Resized LM Head When tie_word_embeddings is False #35141#36221sambhavnoobcoder wants to merge 12 commits intohuggingface:mainfrom
tie_word_embeddings is False #35141#36221sambhavnoobcoder wants to merge 12 commits intohuggingface:mainfrom
Conversation
4 tasks
Member
|
Hi @sambhavnoobcoder, sorry for missing this one earlier! The solution seems good, but can we move the test into an existing file rather than a separate file for just that test? I think it might fit in the |
ca71974 to
fc7c069
Compare
4 tasks
6 tasks
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.
Issue Description
When using models with
tie_word_embeddings=False, callingresize_token_embeddings()followed bypost_init()causes unintended reinitialization of the output embeddings (LM head). This occurs because the newly created LM head during resizing lacks the_is_hf_initializedflag, causingpost_init()to treat it as uninitialized and reinitialize its weights.Solution
Added
_is_hf_initialized = Trueflag to the new LM head in_get_resized_lm_head(). This ensures thatpost_init()recognizes the module as already initialized and skips reinitialization, preserving the intended weights.The change is minimal and targeted:
tie_word_embeddings=FalseTest Coverage
Core Test: test_model_resize_embeddings.py
test_resize_embeddings_no_reinit
This test verifies:
torch.allclose()for exact weight comparisontest_new_tokens_initialization
This test verifies:
test_resize_embeddings_with_bias
This test verifies:
Test Results
All tests pass successfully, confirming:
Implementation Details
The fix is implemented in
src/transformers/modeling_utils.py, adding a single line to mark the new LM head as initialized immediately after creation.Backwards Compatibility
This change:
Fixes : #35141
cc: @ArthurZucker @Rocketknight1