Skip to content

[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

Closed

Description

@Qubitium

Found on main @ dba142d, RTX 5090 (CUDA 13.2, torch 2.15, transformers 5.22).

What is wrong

InternVL3-1B-hf has an untied lm_head. The checkpoint stores language_model.lm_head.weight, which differs from embed_tokens.weight by up to 0.039. Its text_config.tie_word_embeddings is False, but the composite InternVLConfig.tie_word_embeddings keeps the PretrainedConfig default of True. Plain from_pretrained loads the real head: lm_head == checkpoint lm_head, and the two tensors are separate objects.

GPT-QModel instead ties lm_head to embed_tokens during quantize(). LazyTurtle.materialize_submodule() calls target_model.tie_weights() after every lazy materialization (gptqmodel/utils/structure.py:1529-1530, reached from stage_inputs_capture.py:241/419/486, module_looper.py:1895, stage_layer.py:727, models/definitions/internvl.py:84-88). Transformers' tie_weights() recomputes the mapping from the top-level config.tie_word_embeddings, so lm_head.weight is replaced by the input embedding. The writer then treats the head as tied and omits lm_head.weight from the checkpoint. Every later load, in GPT-QModel or in transformers, uses embed_tokens as the output projection.

The quantized model gets a different, wrong output layer, and no error is reported.

Repro (full InternVL3-1B-hf, GPTQ 4-bit g128, 64 C4 rows, bf16, default offload)

import torch, glob
from safetensors import safe_open
from gptqmodel import GPTQModel
from gptqmodel.quantization.config import CalibrationConfig, GPTQConfig

src = "OpenGVLab/InternVL3-1B-hf"
m = GPTQModel.load(src, GPTQConfig(bits=4, group_size=128), dtype=torch.bfloat16)
m.quantize(CalibrationConfig(dataset=texts))
print(m.model.lm_head.weight is m.model.get_input_embeddings().weight)   # True
m.save("ivl_q")
print([k for f in glob.glob("ivl_q/*.safetensors") for k in safe_open(f, "pt").keys() if "lm_head" in k])  # []
q = GPTQModel.load("ivl_q", dtype=torch.bfloat16)   # lm_head == embed_tokens
# wikitext-2 PPL on 2048 tokens, then with the source lm_head copied back into the same quantized model

Output:

in-session lm==ckpt_lm False lm==embed True   (tied obj True)
saved lm_head keys: []
reloaded lm==ckpt_lm False lm==embed True
reloaded GPTQ ppl 29.981 | same ckpt with the source lm_head restored 23.586

On the unquantized model, transformers with the correct head gives PPL 20.133. Copying embed_tokens into lm_head gives 25.778. So this tying alone costs about +28% PPL, which is more than the 4-bit quantization costs.

I instrumented PreTrainedModel.tie_weights and confirmed that the first call comes from stage_inputs_capture.py:241 -> base.py:3829 -> structure.py:4370 -> structure.py:1530 while lm_head is still on meta. Every later lazy materialization re-ties it (after: tied True).

Plain transformers, with or without import gptqmodel, loads the saved checkpoint with the same wrong head. The other VL models I checked do not have a distinct untied head, so they are unaffected: Qwen2-VL-2B, Qwen2.5-VL-3B, Qwen3-VL-2B and gemma-3-4b-it have all non-quantized tensors bit-exact after reload.

Suggested fix

  • Do not call tie_weights() blindly after lazy materialization. Tie only when transformers itself tied the pair at load time, for example by snapshotting model.all_tied_weights_keys (or checking whether lm_head.weight is embed.weight) right after from_pretrained and re-applying exactly that mapping. Alternatively, resolve tying from config.get_text_config().tie_word_embeddings and from whether the checkpoint carries a distinct head tensor.
  • The writer should never drop lm_head.weight when the source checkpoint has its own head tensor.
  • Audit the other places that read the top-level config.tie_word_embeddings the same way: models/loader.py:3219, models/base.py:6690 tied_word_embedding(), models/writer.py:747, utils/model.py:3964.

Test that should have caught it

A VL reload test that compares every non-quantized tensor (towers, norms, lm_head) against from_pretrained of the source. A tiny InternVL config with text_config.tie_word_embeddings=False and a distinct head would be enough.

Severity

High: every GPT-QModel quantization of InternVL3-hf checkpoints with an untied head (and likely other composite configs that leave the top-level tie_word_embeddings at its default) silently replaces the output layer. The cost is +27% PPL on InternVL3-1B.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4852

    Fixing: stop LazyTurtle re-tying an untied lm_head; snapshot the load-time tying and keep the writer from dropping a distinct head.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions