Skip to content

fix(vl): tie an InternVL head from the wrapper flag and checkpoint evidence (#4973) - #4993

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4973
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4973

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4973
Regression from #4852 (the fix for #4799).

Root cause — three compounding defects

  1. resolve_tied_weights_keys gated on word_embeddings_tied, which is the wrapper and text flag. For InternVL the wrapper says True and text_config says False, so the declared pair was discarded, the snapshot came back {}, lm_head was never materialized, and quantize ended with RuntimeError: Offload: modules remain on the meta device ... (lm_head).
  2. LazyTurtle.__init__ initialized _lock / _active_source_root after snapshot_tied_weights_keys. _checkpoint_parameters_equal → _checkpoint_shard_path raised AttributeError, which a defensive except turned into None ("unknown"). The distinct-head evidence had therefore never actually been consulted; [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 only passed because the both-flags gate short-circuited whenever the text flag was False. Once the evidence became load-bearing, this had to be fixed.
  3. _retie_shared_embeddings also gated on word_embeddings_tied, so even a correct snapshot would not re-tie after materialization.

Decision table

resolve_tied_weights_keys now uses the wrapper flag plus the declared all_tied_weights_keys (the same source tie_weights() uses); the checkpoint can only refute a pair:

declared pair wrapper flag ckpt source ckpt target values equal verdict
no – – – – untied
yes False – – – untied (#4369)
yes True missing – – untied
yes True present absent – tied (#4973)
yes True present present yes tied
yes True present present no untied (#4799)

text_config.tie_word_embeddings is inert for the wrapper's head. _retie_shared_embeddings is snapshot-first. word_embeddings_tied is retained as the documented config-only fallback for callers with no snapshot. Row-chunked tensors_equal_in_chunks is unchanged.

Red evidence (parent cdeb00e4, final test file)

git checkout cdeb00e4 -- <3 source files> then restore from HEAD (no stash):

EXIT=1   5 failed, 12 passed
FAILED test_resolution_matrix[True-False-absent]      assert False is True
FAILED test_resolution_matrix[True-False-equal]       assert False is True
FAILED test_text_flag_alone_does_not_untie...
FAILED test_text_untied_head_absent_survives...       assert {} == {'lm_head.weight': ...}
FAILED test_text_untied_head_equal_stays_tied         assert {} == {'lm_head.weight': ...}

The two consumer failures are the empty snapshot. /tmp/repro_4973.py additionally reproduces the end-of-quantize RuntimeError ... (lm_head) on the parent; it now prints SNAPSHOT {'lm_head.weight': ...} / QUANTIZE_OK tied: True.

Green evidence (after merging current main)

50 passed   test_internvl_tied_head_4973.py test_internvl_untied_lm_head_4799.py
            test_cpu_offload_tied_both_copies_4943.py test_lm_head_tied_detection.py
            test_embedding_save_lifecycle.py
EXIT=0      test_hf_gptqmodel_load_bridge.py 30 passed, test_vl_legacy_checkpoint_resave_4802.py 9 passed,
            test_cpu_offload_device_map_tied_head_4866.py 12 passed (pre-merge run)
ruff check gptqmodel tests   clean

The #4799 test file needed one stale unit assertion updated because it encoded the old both-levels rule; every #4799 consumer-level tied/untied test stays green.

Residual risk

  • Other mismatched-flag families: Qwen3-VL (wrapper=False, text=True) still yields an empty declared map, hence untied; covered by the [False-*] matrix rows and the consumer wrapper-untied control.
  • The distinct-head compare now genuinely runs at load for checkpoints that store both copies (AutoAWQ / .bin / GPTQModel ≤1.x). It is row-chunked (8 rows) so memory stays bounded, but it adds I/O proportional to vocab×hidden when both copies exist. Load-time cost on a real large model was not measured here (tiny CPU fixtures only; no large-model validation on this host).
  • text_config/wrapper inconsistencies that make Transformers write a nested language_model.lm_head.weight alias are a save-layout quirk, not a tie decision; the resolver matrix covers that shape at the unit level.
  • test_diffusion_gemma_support.py has 2 pre-existing environment failures (missing /monster/data/model/diffusiongemma-26B-A4B-it), unrelated.

…idence (#4973)

`InternVLConfig(text_config=<qwen2 dict>)` leaves the wrapper
`tie_word_embeddings` at the `PretrainedConfig` default `True` while the nested
`text_config.tie_word_embeddings` defaults to `False`. Transformers ties
`lm_head.weight` to `model.language_model.embed_tokens.weight` from the wrapper
flag and its declared `_tied_weights_keys`, so `save_pretrained` writes only the
embedding.

#4852 required *both* levels to declare a tie before honouring the declared
pair, so the load-time snapshot came out empty. The lazy loader never
materialized the head and a full quantize run ended with:

    RuntimeError: Offload: modules remain on the meta device after
    quantization (lm_head).

Resolve tying from evidence instead of flag agreement:

* the wrapper flag and its declared `all_tied_weights_keys` decide which pairs
  may be tied, matching Transformers' `tie_weights()`. A nested decoder flag no
  longer unties the wrapper's head, and a wrapper that says `False` still ties
  nothing (#4369);
* the checkpoint only refutes a declared pair: a stored target whose values
  differ is a real head (#4799); an omitted target is the normal tied-save
  layout and stays tied (#4973). The row-chunked comparison is unchanged.

`LazyTurtle.__init__` initialized `_lock`/`_active_source_root` after the
snapshot call, so `_checkpoint_parameters_equal` raised `AttributeError` and its
defensive wrapper reported "unknown"; the distinct-head evidence had never
actually been consulted. Initialize them before resolving the snapshot.

`_retie_shared_embeddings` now trusts the resolved snapshot instead of the
config-only both-levels gate, so the tied head is re-established after
materialization.

Adds `tests/test_internvl_tied_head_4973.py` with the full
{wrapper T/F} x {text T/F} x {head absent/equal/distinct} matrix, the exact
quantize+save+reload repro, tied/equal controls, and the wrapper-untied control.
The #4799 consumer test now also exercises the live distinct-head comparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant