Repository navigation
fix(vl): tie an InternVL head from the wrapper flag and checkpoint evidence (#4973) - #4993
Merged
Merged
Conversation
…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.
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 #4973
Regression from #4852 (the fix for #4799).
Root cause — three compounding defects
resolve_tied_weights_keysgated onword_embeddings_tied, which is the wrapper and text flag. For InternVL the wrapper saysTrueandtext_configsaysFalse, so the declared pair was discarded, the snapshot came back{},lm_headwas never materialized, and quantize ended withRuntimeError: Offload: modules remain on the meta device ... (lm_head).LazyTurtle.__init__initialized_lock/_active_source_rootaftersnapshot_tied_weights_keys._checkpoint_parameters_equal→_checkpoint_shard_pathraisedAttributeError, which a defensiveexceptturned intoNone("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 wasFalse. Once the evidence became load-bearing, this had to be fixed._retie_shared_embeddingsalso gated onword_embeddings_tied, so even a correct snapshot would not re-tie after materialization.Decision table
resolve_tied_weights_keysnow uses the wrapper flag plus the declaredall_tied_weights_keys(the same sourcetie_weights()uses); the checkpoint can only refute a pair:text_config.tie_word_embeddingsis inert for the wrapper's head._retie_shared_embeddingsis snapshot-first.word_embeddings_tiedis retained as the documented config-only fallback for callers with no snapshot. Row-chunkedtensors_equal_in_chunksis unchanged.Red evidence (parent
cdeb00e4, final test file)git checkout cdeb00e4 -- <3 source files>then restore from HEAD (no stash):The two consumer failures are the empty snapshot.
/tmp/repro_4973.pyadditionally reproduces the end-of-quantizeRuntimeError ... (lm_head)on the parent; it now printsSNAPSHOT {'lm_head.weight': ...}/QUANTIZE_OK tied: True.Green evidence (after merging current
main)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
wrapper=False, text=True) still yields an empty declared map, hence untied; covered by the[False-*]matrix rows and the consumer wrapper-untied control..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 nestedlanguage_model.lm_head.weightalias are a save-layout quirk, not a tie decision; the resolver matrix covers that shape at the unit level.test_diffusion_gemma_support.pyhas 2 pre-existing environment failures (missing/monster/data/model/diffusiongemma-26B-A4B-it), unrelated.