Repository navigation
fix(vl): keep an untied InternVL lm_head through lazy load and save (#4799) - #4852
Merged
Merged
Conversation
…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.
This was referenced Nov 17, 2026
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.
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4799.
Root cause
InternVLConfigleaves the compositetie_word_embeddings=Truewhiletext_config.tie_word_embeddings=Falseand the checkpoint stores a distinctlanguage_model.lm_head.weight.LazyTurtle.materialize_submodule()calledtie_weights()after every materialization, so transformers replaced the real head with the embedding and the writer droppedlm_head.weightas a duplicate.Change
New
gptqmodel/utils/tied_weights.pyresolves 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. Alltie_weights()call sites re-apply the snapshot instead.get_state_dict_for_saveonly collapses snapshot-tied pairs.Tests
tests/test_internvl_untied_lm_head_4799.py: 6 passed (1 fails on parent)ruff check gptqmodel tests: cleanResidual risks
LazyTurtleis constructed.offload_to_disk=True; meta/CUDA paths share the helpers but were not run on GPU.