Skip to content

[BUG] Regression from #4852 (fix of #4799): InternVL whose head is tied by transformers (wrapper tie=True, text_config tie=False, no lm_head tensor) now fails at the end of quantize(): lm_head left on meta #4973

Closed

Description

@Qubitium

Found on main @ 3867c82 (still on c52ae99), CPU path (RTX 4090 host, the same with no GPU visible).

What is wrong

#4852 (fix of #4799) resolves InternVL head tying "from evidence": a composite wrapper's head is tied only when both the wrapper tie_word_embeddings and text_config.tie_word_embeddings say so (gptqmodel/utils/tied_weights.py, word_embeddings_tied). That fixes the InternVL3-hf case, where the checkpoint stores a distinct language_model.lm_head.weight. The converse case now breaks. The config has wrapper True (the PretrainedConfig default) and text_config False (the Qwen2 default), and the checkpoint has no lm_head tensor because transformers tied the head and save_pretrained omitted it. GPT-QModel then treats the head as untied, nothing loads it, and quantize() fails at the end:

gptqmodel/looper/module_looper.py:1949 _materialize_remaining_meta_modules -> utils/offload.py:510
RuntimeError: Offload: modules remain on the meta device after quantization (lm_head). The in-session model cannot run inference or be moved with `.to()`; reload the saved checkpoint before using it.

This is exactly what transformers 5.22 produces for any InternVL built from InternVLConfig(text_config=<qwen2 dict>) and then saved, for example a model trained from config or a test fixture. transformers itself ties the head for this checkpoint, so it loads and runs fine there.

Repro (CPU, a few seconds)

import torch
from transformers import AutoModelForImageTextToText
from transformers.models.internvl.configuration_internvl import InternVLConfig
from gptqmodel import BACKEND, CalibrationConfig, GPTQModel, QuantizeConfig
from gptqmodel.models.auto import MODEL_MAP_CASEFOLD

text = dict(vocab_size=16, hidden_size=8, intermediate_size=16, num_hidden_layers=1,
            num_attention_heads=2, num_key_value_heads=1, max_position_embeddings=16)
vision = dict(hidden_size=8, intermediate_size=16, num_hidden_layers=1, num_attention_heads=2, image_size=4, patch_size=2)
cfg = InternVLConfig(text_config=text, vision_config=vision, image_token_id=15, image_seq_length=4, downsample_ratio=0.5)
hf = AutoModelForImageTextToText.from_config(cfg).eval()
print(cfg.tie_word_embeddings, cfg.text_config.tie_word_embeddings,
      hf.lm_head.weight.data_ptr() == hf.get_input_embeddings().weight.data_ptr())
hf.save_pretrained("dense")                      # writes embed_tokens only, no lm_head
MODEL_MAP_CASEFOLD["internvl"].require_load_processor = False   # tiny model has no processor assets
qm = GPTQModel.load("dense", quantize_config=QuantizeConfig(bits=4, group_size=-1, sym=True, desc_act=False, device="cpu"))
ids = torch.tensor([[1, 2, 3, 4, 5, 6, 7, 8]])
qm.quantize(CalibrationConfig(dataset=[{"input_ids": ids, "attention_mask": torch.ones_like(ids)}], batch_size=1, min_length=1),
            backend=BACKEND.GPTQ_TORCH)
tree result
57a6055 (parent of 0f14dd7) quantize OK; lm_head device: cpu, tied: True
0f14dd7 (#4852) RuntimeError: Offload: modules remain on the meta device after quantization (lm_head)
main 3867c82 / c52ae99 same RuntimeError

The first print shows True False True: transformers ties this head.

Suggested fix

In the tie resolution, when the checkpoint has no tensor for the head target but has the source (embed_tokens), and either level declares tying, fall back to the tie (this is what transformers does). The "both levels must agree" rule should only decide between two tensors that are both present. More generally, a head that the checkpoint does not provide and that _gptqmodel_tied_weights_keys does not tie should fail at load with a clear message, not at the end of a full quantize run.

Test that should have caught it

tests/test_vl_legacy_checkpoint_resave_4802.py::test_vl_legacy_checkpoint_resave_roundtrip[internvl] uses exactly this tiny InternVL and has been red since 0f14dd7 (bisected). tests/test_internvl_untied_lm_head_4799.py covers an untied head (checkpoint has lm_head) and a tied control in which both config levels say tied. It has no case with a mixed config and a head missing from the checkpoint.

Severity

Medium. It is a crash after the whole calibration/quantization run, on a checkpoint layout that transformers produces and loads. Real InternVL3-hf releases store lm_head and are not affected.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

    Pull request: #4991

    Fix the #4852 regression: InternVL whose head is tied by transformers (wrapper tie=True, text_config tie=False) is now left untied by my snapshot evidence rule; resolve the tie from the checkpoint/wrapper intent so a genuinely tied head is re-tied and saved without duplicating lm_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