Skip to content

fix(load): place a declared tied pair before the checkpoint tie (#4943) - #4960

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

Qubitium merged 2 commits into
mainfrom
fix/issue-4943

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4943 — the follow-up to my #4890.

Root cause

load_checkpoint_in_model_then_tie_weights passes the explicit device_map to accelerate, then calls tie_weights(missing_keys=...). When the checkpoint stores both lm_head.weight and model.embed_tokens.weight (AutoAWQ, GPTQModel <= 1.x), transformers compares the pair with torch.equal. A map that puts one side on CPU and the other on the accelerator raises inside that comparison — before simple_dispatch_model, where #4890's placement rule lives. At that point the model is not yet tied, so find_tied_parameters returns 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 to all_tied_weights_keys/_tied_weights_keys) — the exact list tie_weights iterates — 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 when tie_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=...))

checkpoint shape map parent fixed
both copies, equal head_cpu / 1_then_cpu / cpu_then1 RuntimeError ... wrapper_CUDA__equal load + finite forward
both copies, equal rotary_cpu ok ok
only embed_tokens (#4866) all 4 ok ok
distinct head, tied config 3 of 4 RuntimeError ... wrapper_CUDA__equal loads, stays untied

All four maps give lm_head.weight is embed_tokens.weight on 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-device torch.equal error. 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

  • The load map is normalized for declared pairs even when the checkpoint stores only one side, so lm_head may 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).
  • A model that declares no tied mapping would not be normalized; tie_weights(missing_keys=...) is itself a transformers-5 API, so that combination is unreachable on a supported version.
  • Tested with a 2-layer quantized Qwen2 on one GPU + CPU; real AutoAWQ/Llama checkpoints and multi-GPU maps were not exercised here.

`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).
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