Repository navigation
fix(requantize): place embedding endpoints on the model device and dtype (#5116) - #5156
Merged
Merged
Conversation
…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
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.
Summary
Fixes #5116. After a successful
requantize(embed_quant_mode=...)the new endpoint modules were left on CPU andTorchQuantEmbeddingsemitted the checkpoint dtype (FP16) into a BF16 model, so the in-session forward failed and anINPUT-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-tableF.embeddingand row-decode paths (plus the empty-input path) to it. Previously the forward returnedscales.dtype(FP16 for GPTQ), which promoted a BF16 residual stream to FP32 and made a dense bf16lm_headraisefloat != BFloat16.create_quant_module: set the embedding output dtype from the constructordtypeor the replacednn.Embeddingweight dtype, so a freshquantize(embed_quant_config=...)records it too.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
Host: one NVIDIA GB10 (sm_121, aarch64), torch 2.11.0+cu130, transformers 5.17.0. Before the fix, the same repro left
embed_tokenson CPU and raisedExpected all tensors to be on the same device; after it the model stays oncuda:0, the forward returns bf16, and the savedINPUTcheckpoint reloads and forwards.tests/test_save_loaded_quantized_model_torch_fused.py::TestSaveTorchFused::test_savefails identically on unmodifiedorigin/mainhere (hubOSError) and is unrelated.Kernel Accuracy Requirements
converted == default.to(bfloat16)exactly on both decode paths.