Skip to content

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

Closed

Description

@Qubitium

Describe the bug

A tied Llama checkpoint can store its shared tensor under either model.embed_tokens.weight or lm_head.weight. Transformers loads both forms and reconstructs the tie. GPT-QModel's lazy path discards the declared tie when the canonical embedding key is absent, even when the head key contains the shared tensor. The input embedding stays on meta and calibration fails.

To reproduce

At 2c8717e1 with Transformers 5.23.0, save a tiny local Llama with tie_word_embeddings=True. Make a second checkpoint with the same config, tokenizer and tensors, moving the sole stored embedding tensor from model.embed_tokens.weight to lm_head.weight. Load both and quantize with GPTQ on CPU using offload_to_disk=True.

Native Transformers loads the head-only variant with the same embeddings and the expected storage tie. GPT-QModel quantizes the embedding-only variant. The head-only variant fails during calibration with RuntimeError: Tensor.item() cannot be called on meta tensors. Its LazyTurtle tie snapshot is empty. Declared tie resolution, lazy snapshot.

Suggested mitigation

The tie resolver could choose the stored member of a declared tie as its materialization source, including the reverse direction. The existing comparison for checkpoints containing two different tensors can still preserve untied weights. A tiny fixture could exercise each alias as the only saved tensor and check that lazy calibration succeeds and both modules share the loaded data. Setting offload_to_disk=False is a verified workaround on the same fixture.

Activity

  1. added
    bug
    on Nov 19, 2026
  2. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-4e3a0639ecaf is working on this issue now.

    Started 2026-11-19T08:50:00Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    Make the declared-tie resolver pick whichever member the checkpoint actually stores (including the reverse direction) so the lazy snapshot is non-empty for head-only tied checkpoints, with a fixture for each alias.

  3. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Released: #5490 merged as cdd0b2011.

    Root cause

    • A tied Llama checkpoint may store the shared tensor only as lm_head.weight; Transformers reconstructs the embedding from it. resolve_tied_weights_keys() dropped the declared pair whenever the declared source (model.embed_tokens.weight) was absent, so the lazy snapshot was empty, the embedding stayed on meta, and calibration failed with RuntimeError: Tensor.item() cannot be called on meta tensors.

    Fix

    Verification

    • Tiny tied Llama with offload_to_disk=True: embedding-only stores QUANTIZED OK on both trees; head-only stores FAILED: RuntimeError Tensor.item() cannot be called on meta tensors on e84d249fd and QUANTIZED OK after the fix.
    • New tests/test_tied_lm_head_only_5399.py (8 tests). On e84d249fd: 3 failed / 5 passed -> 8 passed.
    • Regression: tied-head suites + new file 69 passed; test_structure.py + lazy-turtle suites 61 passed; ruff clean.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions