Skip to content

[BUG] #4890 (fix of #4866) incomplete: tied checkpoints that store both lm_head.weight and embed_tokens (AutoAWQ, GPTQModel 1.x) still crash at load with a CPU-offload map (tie_weights torch.equal across cpu/cuda) #4943

Closed

Description

@Qubitium

Found on main @ 3867c82, RTX 4090, torch 2.15, transformers 5.22, accelerate 1.15.

What is wrong

#4890 (fix of #4866) makes the four CPU-offload maps from #4866 work, but only for tied-embedding checkpoints that store model.embed_tokens.weight alone, which is what the current GPTQModel save writes. Some tied checkpoints store both lm_head.weight and model.embed_tokens.weight. That includes AutoAWQ-format checkpoints and GPTQModel <= 1.x checkpoints (tie_word_embeddings=True). On those, the same maps now fail earlier, inside load():

  • gptqmodel/utils/model.py:3169 _tie_weights_after_checkpoint_load calls model.tie_weights(missing_keys=...). Both keys are present, so transformers compares them with torch.equal(source_param, target_param) (transformers/modeling_utils.py:2567) to decide whether to tie.
  • With an explicit map that puts lm_head and embed_tokens on different devices, the checkpoint is loaded into a CPU lm_head and a CUDA embed_tokens, and torch.equal raises.
  • transformers avoids this because infer_auto_device_map always gives tied tensors the same device (the comment just above that line says so). fix(load): make explicit CPU-offload device maps survive the first forward (#4866) #4890's "keep a really-tied group on one device" rule runs only later, in simple_dispatch_model, after the checkpoint load and tie.

Repro (same maps as #4866, GPU = cuda:0)

import torch
from gptqmodel import GPTQModel, BACKEND
L, D = 16, "cuda:0"
def split(a, b):
    dm = {"model.embed_tokens": a, "model.rotary_emb": a, "model.norm": b, "lm_head": b}
    dm.update({f"model.layers.{i}": (a if i < L // 2 else b) for i in range(L)}); return dm
maps = {"head_cpu": {**split(D, D), "lm_head": "cpu"}, "1_then_cpu": split(D, "cpu"),
        "cpu_then1": split("cpu", D), "embed_cpu": {**split(D, D), "model.embed_tokens": "cpu"},
        "mid_cpu": {**split(D, D), "model.layers.2": "cpu", "model.layers.3": "cpu"}}
for ck, be in [("AWQ-Llama-3.2-1B-g128-gemm", BACKEND.AUTO),
               ("Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1", BACKEND.GPTQ_TRITON),
               ("Llama-3.2-1B-Instruct-GPTQ-4bits-gp128", BACKEND.GPTQ_TRITON)]:
    for name, dm in maps.items():
        q = GPTQModel.load(ck, backend=be, device_map=dm, dtype=torch.float16)   # raises for the first two ckpts
map GPTQ-4bits-gp128 (only embed_tokens stored) vortex-v1 GPTQ (both stored) AWQ-Llama-3.2-1B-g128-gemm (both stored)
head_cpu ok, bitwise = all-GPU 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) same
1_then_cpu ok same RuntimeError same
cpu_then1 ok ... got other is on cuda:0, different from other tensors on cpu same
embed_cpu ok same same
rot_cpu / mid_cpu / norm_cpu ok ok ok

The error is the same at a43e305^ (before #4890). So this is not a regression; it is the tied-head CPU-offload case that #4890 did not cover. The traceback is from_quantized -> load_checkpoint_in_model_then_tie_weights -> _tie_weights_after_checkpoint_load -> model.tie_weights -> torch.equal.

Suggested fix

Apply #4890's tied-group placement rule to the device map before the checkpoint load (load_checkpoint_in_model_then_tie_weights), not only in simple_dispatch_model. Or have _tie_weights_after_checkpoint_load compare the two tensors on one device before calling tie_weights, and then tie or skip based on that result, so a cross-device pair never reaches torch.equal.

Test that should have caught it

tests/test_cpu_offload_device_map_tied_head_4866.py uses only a checkpoint that stores embed_tokens alone. Parametrize it with a tied checkpoint that stores both keys (for example, save one with lm_head.weight duplicated, or use the AWQ gemm checkpoint).

Severity

Medium: a documented explicit CPU-offload placement crashes at load for tied-embedding AutoAWQ checkpoints and older GPTQModel checkpoints with the common "head on CPU" or "second half on CPU" maps. The same checkpoints load fine on all-GPU or mid_cpu maps.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4960

    Extend #4890: tied checkpoints that store both lm_head.weight and embed_tokens (AutoAWQ / GPTQModel 1.x) must load with a CPU-offload map instead of failing in tie_weights' torch.equal across cpu/cuda; add the fixture and the map matrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions