Skip to content

fix(vl): keep an untied InternVL lm_head through lazy load and save (#4799) - #4852

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4799
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4799

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4799.

Root cause

InternVLConfig leaves the composite tie_word_embeddings=True while text_config.tie_word_embeddings=False and the checkpoint stores a distinct language_model.lm_head.weight. LazyTurtle.materialize_submodule() called tie_weights() after every materialization, so transformers replaced the real head with the embedding and the writer dropped lm_head.weight as a duplicate.

Change

New gptqmodel/utils/tied_weights.py resolves tying from evidence: head is tied only when wrapper and text config agree; a declared pair is dropped if the checkpoint has no source tensor or stores both sides with different values (row-chunked compare); result stamped as _gptqmodel_tied_weights_keys. All tie_weights() call sites re-apply the snapshot instead. get_state_dict_for_save only collapses snapshot-tied pairs.

Tests

  • new tests/test_internvl_untied_lm_head_4799.py: 6 passed (1 fails on parent)
  • 13-file regression batch: 102 passed, 2 skipped
  • untied round-trip bit-equal (max|diff| 0.0)
  • ruff check gptqmodel tests: clean

Residual risks

  • No real InternVL3-1B checkpoint locally, so PPL was not re-measured; coverage is structural.
  • Snapshot is stamped only when LazyTurtle is constructed.
  • Exercised on CPU with offload_to_disk=True; meta/CUDA paths share the helpers but were not run on GPU.

…4799)

`InternVLConfig` leaves the composite `tie_word_embeddings` at the
`PretrainedConfig` default `True` while `text_config.tie_word_embeddings` is
`False` and the checkpoint stores a distinct
`language_model.lm_head.weight`. `LazyTurtle.materialize_submodule()` called
`model.tie_weights()` after every materialization, and transformers recomputed
the mapping from the wrapper flag: the real head was replaced by the input
embedding and the writer then omitted `lm_head.weight` as a duplicate, so the
quantized model silently changed its output layer (PPL 23.59 -> 29.98).

Resolve tying once from evidence instead of one flag, in a new
`gptqmodel/utils/tied_weights.py`:

* a composite wrapper's head is tied only when both the wrapper and its
  decoder text config declare it (`word_embeddings_tied`), so neither an
  InternVL wrapper default (#4799) nor a decoder default can override the
  other level (#4369);
* a declared pair is dropped when the checkpoint has no source tensor, or when
  the checkpoint stores both sides with different values (a real head); values
  are compared in row chunks so a large vocabulary is never materialized twice;
* the resolved `{target: source}` map is stamped on the model as
  `_gptqmodel_tied_weights_keys`.

The lazy loader, offload/restore, GGUF and W4A load paths now re-apply that
snapshot (`retie_tied_weights`) instead of calling `tie_weights()`, and the
writer/looper/base placement decisions prefer it over the config flag.
`get_state_dict_for_save` only collapses pairs the snapshot ties; a tensor that
merely shares storage is cloned and written rather than dropped.

Adds `tests/test_internvl_untied_lm_head_4799.py`, covering the untied InternVL
quantize+save+reload path (head bit-equal, `lm_head.weight` present), tied
InternVL and Qwen controls (head still omitted), an untied Qwen decoder, and
unit coverage for the resolution helpers.
@Qubitium
Qubitium merged commit 0f14dd7 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4799 branch November 17, 2026 17:30
Qubitium added a commit that referenced this pull request Nov 17, 2026
…idence (#4973) (#4993)

`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