Repository navigation
Description
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 modelOutput:
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 snapshottingmodel.all_tied_weights_keys(or checking whetherlm_head.weight is embed.weight) right afterfrom_pretrainedand re-applying exactly that mapping. Alternatively, resolve tying fromconfig.get_text_config().tie_word_embeddingsand from whether the checkpoint carries a distinct head tensor. - The writer should never drop
lm_head.weightwhen the source checkpoint has its own head tensor. - Audit the other places that read the top-level
config.tie_word_embeddingsthe same way:models/loader.py:3219,models/base.py:6690tied_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
Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-53ba17eb4d83finished 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.
fix(vl): keep an untied InternVL lm_head through lazy load and save (#…
fix(load): make explicit CPU-offload device maps survive the first fo…
fix(load): keep a repaired AWQ module in the model's mode (#4907) (#4949
fix(load): place a declared tied pair before the checkpoint tie (#4943)…
fix(vl): tie an InternVL head from the wrapper flag and checkpoint ev…
fix(tied): materialize a tie stored under either member (#5399) (#5490)
Replace LazyTurtle with weight_loader CheckpointSource (#6471)
Remove accelerate: explicit WeightLoader for weight loading, placemen…