Skip to content

fix(tied): materialize a tie stored under either member (#5399) - #5490

Merged
Qubitium merged 1 commit into
mainfrom
fix/5399-tied-lm-head-only
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5399-tied-lm-head-only

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5399.

Root cause

A tied Llama checkpoint may store the shared tensor only as lm_head.weight; Transformers reconstructs model.embed_tokens.weight from it. resolve_tied_weights_keys() dropped the declared pair whenever the declared source was absent:

if checkpoint_has_tensor(source) is False:
    # Nothing to tie to: the source tensor is not in the checkpoint.
    continue

The lazy snapshot was therefore empty, the embedding stayed on meta, and calibration failed with RuntimeError: Tensor.item() cannot be called on meta tensors (from transformers' sdpa_mask while reading a meta tensor).

Reproduced with a tiny tied Llama (2 layers, tie_word_embeddings=True, tokenizer written locally) with offload_to_disk=True:

checkpoint stores pristine e84d249fd this branch
model.embed_tokens.weight only QUANTIZED OK QUANTIZED OK
lm_head.weight only FAILED: RuntimeError Tensor.item() cannot be called on meta tensors QUANTIZED OK

Fix

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 with offload_to_disk=True and the two names then expose the same loaded values.

One existing expectation in tests/test_internvl_untied_lm_head_4799.py encoded 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):

result
e84d249fd 3 failed, 5 passed (test_head_only_checkpoint_keeps_the_declared_tie, ..._still_reports_the_head_as_tied, test_lazy_calibration_materializes_either_stored_member[head])
this branch 8 passed

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.

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.
@Qubitium
Qubitium merged commit cdd0b20 into main Nov 19, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5399-tied-lm-head-only branch November 19, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Lazy quantization fails when a tied checkpoint stores only lm_head.weight

1 participant