Skip to content

[BUG] requantize(embed_quant_mode=...) leaves the new endpoints on CPU (in-session forward fails) and TorchQuantEmbeddings emits fp16 in a bf16 model (INPUT-requantized bf16 checkpoint cannot run: 'float != BFloat16' at lm_head) #5116

Closed

Description

@Qubitium

Found on main @ 7b8f127, ROCm (R9700, gfx1201). Qwen2.5-0.5B-Instruct GPTQ 4-bit (gptq_v2, bf16) quantized on this box.

What is wrong

After a successful requantize(embed_quant_mode=...) on a model loaded on the GPU, the new endpoint modules do not match the model's device and dtype:

  1. Device (in-session). The new TorchQuantEmbeddings (and, for BOTH, the new TorchLinear head) is left on CPU while the model is on cuda:0. The first in-session forward or generate() fails with a device mismatch. quantize() calls rehome_quantized_kernel(self) (models/base.py:2834), but nothing moves a requantized endpoint back to the device of the module it replaced. (This was noted in passing under "Plausible, not verified" in [BUG] Follow-up on #4069/#4084: hf_repack_layer zero points off by one for GPTQ v1, hf_pack_layer(post_init=True) saves v2 words as gptq, Phi-tiny-MoE AWQ still crashes (out_features % group_size) #4095; it still reproduces.)
  2. Dtype (also after reload). TorchQuantEmbeddings.forward (nn_modules/qlinear/torch.py:1410-1429) returns F.embedding(ids, self.dequantize_weight()), which has the dtype of scales. GPTQ stores scales as F16 even for a bf16 model, so a bf16 model gets an fp16 embedding output. Adding it to the bf16 norm/residual math promotes the residual stream to fp32. With INPUT alone, the dense bf16 lm_head then raises expected mat1 and mat2 to have the same dtype, but got: float != c10::BFloat16 on every forward of the saved and reloaded checkpoint, with or without dtype=torch.bfloat16. With BOTH, the quantized head accepts the fp32 activations, so it runs, but the whole decoder computes in fp32 instead of bf16.

tests/test_requant_embed_lm_head_e2e.py misses both problems. It never runs an in-session forward after requantize(), it only covers BOTH, and its checkpoint (Qwen2.5-0.5B-Instruct-gptq-4bit) is fp16, so the dtypes happen to match.

Repro

import torch
from gptqmodel import GPTQModel, QuantizeEmbed, CalibrationConfig
m = GPTQModel.load(ckpt, device_map={"": "cuda:0"}, dtype=torch.bfloat16)   # bf16 GPTQ ckpt
dyn = dict(m.quantize_config.dynamic or {})
for n in (m.get_input_embeddings_name(), m.get_output_embeddings_name() or m.lm_head):
    dyn[n] = {"bits": 4, "group_size": 32, "sym": True, "desc_act": False}
m.quantize_config.dynamic = dyn
m.requantize(CalibrationConfig(dataset=c4_rows[:8]), embed_quant_mode=QuantizeEmbed.INPUT)   # or BOTH
m.model(ids.cuda())                      # in-session
m.save(out); r = GPTQModel.load(out, device_map={"": "cuda:0"}); r.model(ids.cuda())   # reload

Output:

INPUT:
  in-session embed ('TorchQuantEmbeddings', ['cpu/torch.float16', 'cpu/torch.int32', 'cpu/torch.int64'])  lm_head ('Linear', ['cuda:0/torch.bfloat16'])
  in-session forward ERR RuntimeError Expected all tensors to be on the same device, but got index is on cuda:0, different from other tensors on cpu
  saved: model.embed_tokens.scales F16, lm_head.weight BF16
  reload {}                      -> RuntimeError expected mat1 and mat2 to have the same dtype, but got: float != c10::BFloat16   (at lm_head)
  reload {dtype: torch.bfloat16} -> RuntimeError expected mat1 and mat2 to have the same dtype, but got: float != c10::BFloat16
BOTH:
  in-session embed and lm_head on cpu/float16 -> same device-mismatch error
  reload -> runs ("The capital of France is Paris. Which of the following"), activations fp32

Suggested fix

  • At the end of _requantize_embeddings, place each new endpoint on the device of the module it replaced (or run rehome_quantized_kernel over the endpoints).
  • Have TorchQuantEmbeddings return the model compute dtype instead of scales.dtype: record the replaced embedding's weight dtype on the module, or cast in forward, the way TorchLinear casts its output to the input dtype.
  • Extend the e2e test with a bf16 checkpoint, INPUT mode, and an in-session forward before save().

Severity

Medium. On a bf16 model, INPUT requantize produces a checkpoint that cannot run a single forward. The in-session model is unusable after any successful embedding requantize.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-f60b36e15e6d finished this issue.

    Pull request: #5156

    Fixing requantize endpoint device/dtype placement so the in-session model runs and a bf16 INPUT checkpoint reloads

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