Repository navigation
fix(load): place a declared tied pair before the checkpoint tie (#4943) - #4960
Merged
Merged
Conversation
`load_checkpoint_in_model_then_tie_weights` hands the explicit `device_map` to accelerate and then calls `tie_weights(missing_keys=...)`. When the checkpoint stores both `lm_head.weight` and `model.embed_tokens.weight` -- AutoAWQ format and GPTQModel <= 1.x tied checkpoints -- transformers compares the two with `torch.equal`. A map that puts one side on CPU and the other on the accelerator made that comparison raise before `simple_dispatch_model`, which #4890 taught to keep a really-tied group on one device, ever ran: RuntimeError: Expected all tensors to be on the same device, but got other is on cpu, different from other tensors on cuda:0 (wrapper_CUDA__equal) The model is not tied yet at that point, so `find_tied_parameters` sees no group. Apply the same placement rule to the load map from the model's *declared* `{target: source}` mapping -- the exact list `tie_weights` iterates -- plus the identity groups when the model already ties at construction. Every pair transformers may compare is then on one device, so it reaches its own "both present but different, so do not tie" path instead of raising. The untied head keeps its own placement: with `tie_word_embeddings=False` there is no declared pair, and a tied config that stores a distinct head now compares on one device and is left untied (#4799). Measured with a tiny quantized tied Qwen2 (2 layers, g32) loaded through `GPTQModel.load(..., device_map=...)`: tied checkpoint, both copies stored (parent) head_cpu / 1_then_cpu / cpu_then1: RuntimeError (wrapper_CUDA__equal) rotary_cpu: ok same checkpoint (fixed) all four maps: load, forward finite, `lm_head.weight is embed_tokens.weight`, both on one device, logits bitwise equal across the four maps The same checkpoint that stores only `embed_tokens` (the #4866 shape) and the untied control are unchanged. Tests: `tests/test_cpu_offload_tied_both_copies_4943.py` loads the real quantized checkpoint in three published shapes (embed only, both copies equal, both copies different) through the four #4866 maps and asserts the load, a finite forward, the surviving tie and the untied control, plus bitwise equality between the two tied shapes. 13 passed; on the parent 7 fail with the cross-device `torch.equal` error. The #4866 file, the #4799 InternVL file, the HF load bridge, the loader offload staging and the rehome hook tests stay green (94 passed, 3 skipped).
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 #4943 — the follow-up to my #4890.
Root cause
load_checkpoint_in_model_then_tie_weightspasses the explicitdevice_mapto accelerate, then callstie_weights(missing_keys=...). When the checkpoint stores bothlm_head.weightandmodel.embed_tokens.weight(AutoAWQ, GPTQModel <= 1.x), transformers compares the pair withtorch.equal. A map that puts one side on CPU and the other on the accelerator raises inside that comparison — beforesimple_dispatch_model, where #4890's placement rule lives. At that point the model is not yet tied, sofind_tied_parametersreturns no group and the identity-based rule cannot see the pair.Fix
gptqmodel/utils/model.py: apply the same placement rule to the load map, before the checkpoint read, using the model's declared{target: source}mapping (get_expanded_tied_weights_keys(all_submodels=True), falling back toall_tied_weights_keys/_tied_weights_keys) — the exact listtie_weightsiterates — plus identity groups for models already tied at construction. Every pair transformers may compare is then on one device, so it reaches its own "both present but different -> do not tie" path instead of raising. Untied heads keep their placement: no declared pair whentie_word_embeddings=False, and a tied config storing a distinct head now compares legally and is left untied (#4799).Evidence (tiny quantized tied Qwen2, 2 layers, g32, via
GPTQModel.load(..., device_map=...))head_cpu/1_then_cpu/cpu_then1RuntimeError ... wrapper_CUDA__equalrotary_cpuembed_tokens(#4866)RuntimeError ... wrapper_CUDA__equalAll four maps give
lm_head.weight is embed_tokens.weighton one device with logits bitwise equal across maps.Tests
New
tests/test_cpu_offload_tied_both_copies_4943.py: 13 passed; on parent 7 failed with the exact cross-devicetorch.equalerror. Requested suites (test_cpu_offload_device_map_tied_head_4866,test_internvl_untied_lm_head_4799,test_hf_gptqmodel_load_bridge,test_loader_offload_staging_4385,test_rehome_hook_realign) + new file: 94 passed, 3 skipped. Extra tied/embedding suites: 20 passed, 1 skipped.ruff check gptqmodel tests: clean.Residual risk
lm_headmay load on the accelerator instead of CPU; final placement matches fix(load): make explicit CPU-offload device maps survive the first forward (#4866) #4890's post-tie result (verified for the embed-only shape).tie_weights(missing_keys=...)is itself a transformers-5 API, so that combination is unreachable on a supported version.