Repository navigation
fix(tied): materialize a tie stored under either member (#5399) - #5490
Merged
Merged
Conversation
A tied Llama checkpoint may store the shared tensor only as `lm_head.weight`; Transformers reconstructs the embedding from it. GPT-QModel dropped the declared pair whenever the declared *source* (`model.embed_tokens.weight`) was absent, so the lazy snapshot stayed empty, the embedding stayed on meta and calibration failed with `Tensor.item() cannot be called on meta tensors`. - `resolve_tied_weights_keys` keeps the declared pair when the checkpoint stores the other member, and drops it only when neither is present. A checkpoint that stores both with different values is still refuted, so an untied head keeps its own weights (#4799). - `LazyTurtle._resolve_checkpoint_tensor_source` falls back to the partner of a declared tie, in either direction, so the omitted member is materialized from the stored one instead of failing to resolve.
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.
Fixes #5399.
Root cause
A tied Llama checkpoint may store the shared tensor only as
lm_head.weight; Transformers reconstructsmodel.embed_tokens.weightfrom it.resolve_tied_weights_keys()dropped the declared pair whenever the declared source was absent:The lazy snapshot was therefore empty, the embedding stayed on
meta, and calibration failed withRuntimeError: Tensor.item() cannot be called on meta tensors(from transformers'sdpa_maskwhile reading a meta tensor).Reproduced with a tiny tied Llama (2 layers,
tie_word_embeddings=True, tokenizer written locally) withoffload_to_disk=True:e84d249fdmodel.embed_tokens.weightonlyQUANTIZED OKQUANTIZED OKlm_head.weightonlyFAILED: RuntimeError Tensor.item() cannot be called on meta tensorsQUANTIZED OKFix
resolve_tied_weights_keys()keeps the declared pair when the checkpoint stores the other member, and drops it only when neither member is present. A checkpoint that stores both sides with different values is still refuted, so an untied head keeps its own weights ([BUG] InternVL3-hf: quantize() ties the untied lm_head to embed_tokens (LazyTurtle re-calls tie_weights() off the composite config's default tie_word_embeddings=True) and the checkpoint omits lm_head: PPL 23.59 -> 29.98 #4799).LazyTurtle._resolve_checkpoint_tensor_source()falls back to the partner of a declared tie, in either direction, so the omitted member is materialized from the stored one. The snapshot is still unset on the first resolution pass, which keeps the lookup inert there.Tests
New
tests/test_tied_lm_head_only_5399.py(8 tests): the resolver for each stored-member combination (head only, embed only, both equal, both distinct, neither, unknown),lm_head_is_tied, and the issue's end-to-end repro for both aliases - each quantizes withoffload_to_disk=Trueand the two names then expose the same loaded values.One existing expectation in
tests/test_internvl_untied_lm_head_4799.pyencoded the old "source missing means untied" rule; it now asserts the pair is kept, plus a new assertion that a checkpoint storing neither member drops it.Red -> green (new file run against a pristine worktree at
upstream/main e84d249fd):e84d249fdtest_head_only_checkpoint_keeps_the_declared_tie,..._still_reports_the_head_as_tied,test_lazy_calibration_materializes_either_stored_member[head])Regression (this branch): the tied-head suites (
test_internvl_tied_head_4973,test_lm_head_tied_detection,test_cpu_offload_tied_both_copies_4943,test_internvl_untied_lm_head_4799,test_weight_only_tied_lm_head_5027,test_cpu_offload_device_map_tied_head_4866) + the new file -> 69 passed;test_structure.py,test_lazy_turtle_materialize_map.py,test_lazy_turtle_conversion_mapping.py-> 61 passed;uvx [email protected] check gptqmodel docs/eora tests setup.py-> clean.CPU-only; no GPU needed.