Skip to content

fix(requantize): place embedding endpoints on the model device and dtype (#5116) - #5156

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-5116-requant-embed-placement
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-5116-requant-embed-placement

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5116. After a successful requantize(embed_quant_mode=...) the new endpoint modules were left on CPU and TorchQuantEmbeddings emitted the checkpoint dtype (FP16) into a BF16 model, so the in-session forward failed and an INPUT-requantized bf16 checkpoint could not run a single forward after reload.

What Changed

  • BaseQModel._requantize_embeddings: snapshot each leaf module's device before the run and restore it afterwards. The loop returns every processed layer to CPU (post_quantize), which is right for a fresh quantize but left a requantized loaded model unrunnable; the replaced endpoints are moved back to the device they occupied.
  • TorchQuantEmbeddings: record the dtype the embedding must emit (the replaced embedding's weight dtype, else the model dtype) and cast both the whole-table F.embedding and row-decode paths (plus the empty-input path) to it. Previously the forward returned scales.dtype (FP16 for GPTQ), which promoted a BF16 residual stream to FP32 and made a dense bf16 lm_head raise float != BFloat16.
  • create_quant_module: set the embedding output dtype from the constructor dtype or the replaced nn.Embedding weight dtype, so a fresh quantize(embed_quant_config=...) records it too.
  • New tests/test_requant_embed_endpoint_5116.py: recorded output dtype on both decode paths, bf16 in-session forward and save/reload, and the CUDA endpoint placement.

Out of scope: quantize(embed_quant_config=...) on an unquantized model still crashes at input capture / tied-head re-tying (that is #5111, a separate path).

Tests

python -m pytest tests/test_requant_embed_endpoint_5116.py -q
# 4 passed

python -m pytest tests/test_embed_quant_api.py tests/test_requant_embed_lm_head.py -q
# 62 passed, 1 skipped

python -m pytest tests/test_norm_requant.py tests/test_norm_guards.py tests/test_norm_lifecycle.py \
  tests/test_requant_embed_endpoint_5116.py -q
# 40 passed

python -m pytest tests/test_calibration_config.py -q
# 54 passed

python -m pytest tests/kernels/test_kernel_compile_levels_cpu.py -q -k embeddings
# 2 passed

ruff check gptqmodel tests
# All checks passed

Host: one NVIDIA GB10 (sm_121, aarch64), torch 2.11.0+cu130, transformers 5.17.0. Before the fix, the same repro left embed_tokens on CPU and raised Expected all tensors to be on the same device; after it the model stays on cuda:0, the forward returns bf16, and the saved INPUT checkpoint reloads and forwards. tests/test_save_loaded_quantized_model_torch_fused.py::TestSaveTorchFused::test_save fails identically on unmodified origin/main here (hub OSError) and is unrelated.

Kernel Accuracy Requirements

  • No arithmetic change: the embedding still dequantizes in the checkpoint dtype and only casts the visible output to the model dtype (FP16 -> BF16 is a lossless narrowing of mantissa for these values). Verified converted == default.to(bfloat16) exactly on both decode paths.
  • No dense/expanded weight cache added.

…ype (#5116)

Two defects made a successful embedding requantize leave an unusable model:

- The looper packs on CPU (`post_quantize` returns every processed layer to
  CPU), so after requantizing a loaded GPU model the in-session forward failed
  on a device mismatch. Snapshot each leaf module's device before the run and
  restore it afterwards, including the replaced endpoints.
- `TorchQuantEmbeddings.forward` returned `dequantize_weight()`/the row gather
  in the dtype of `scales` (FP16 for GPTQ). A BF16 model therefore got FP16
  embeddings, promoting the residual stream to FP32 and making a dense bf16
  `lm_head` raise `float != BFloat16`. The module now records the dtype it
  must emit (the replaced embedding's weight dtype, else the model dtype) and
  casts both the whole-table and row-decode paths to it.

Tests: `tests/test_requant_embed_endpoint_5116.py` covers the recorded output
dtype (both decode paths), the bf16 in-session forward and save/reload, and
the CUDA endpoint placement.

Verified on one NVIDIA GB10 (sm_121, aarch64, torch 2.11.0+cu130):
- new file: 4 passed
- tests/test_embed_quant_api.py + test_requant_embed_lm_head.py: 62 passed
- test_norm_requant/guards/lifecycle + new file: 40 passed
- tests/test_calibration_config.py: 54 passed
- embedding compile-level cases: 2 passed
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