Repository navigation
MXFP4 torch decode: fp16 in fp32 on torch < 2.12, where ldexp overflows - #15121
Merged
Merged
Conversation
torch < 2.12's ldexp forms 2^e in the input dtype, so the fp16 torch path returned NaN for a zero nibble under a scale >= 2^16, inf for in-range products such as 2^-10 * 2^20, and 0 for subnormals. Decode in fp32 there and cast once, matching the zoo kernel and the float64 reference.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
danielhanchen
added a commit
that referenced
this pull request
Oct 30, 2026
Follow-up to #15121. ## Problem `_dequantize_torch` runs `torch.ldexp` without a device guard, so it launches on the current device. With packed weights on cuda:1 and cuda:0 current (a model split across cards), the fallback decodes garbage. float32 decodes always take this path, since the unsloth_zoo decoder serves only fp16 and bf16. This predates #15121. ## Change Guard on the operands' device, as unsloth_zoo's `mxfp4_dequantize_torch` already does (unsloth-zoo #2115). Compiled code keeps Inductor's own device guard. ## Tests `tests/test_mxfp4_compressed_linear.py`, torch 2.13: - CPU, main: 3 failed, 28 passed, 26 skipped. Failing: `test_torch_fallback_runs_ldexp_on_the_operands_gpu[dtype0]`, `[dtype1]`, `[dtype2]`. This branch: 31 passed, 26 skipped. - 2 GPUs, main: the new `test_torch_fallback_runs_ldexp_on_the_operands_gpu[dtype0-2]` and `test_torch_fallback_on_a_non_current_gpu_matches_cpu[dtype0-2]` fail. The cross-device decode hits an illegal memory access that also fails the 26 CUDA tests after it; with the 6 new tests deselected, main passes the other 56. This branch: 62 passed. - 2x T4, operands on cuda:1 with cuda:0 current, compared with the CPU decode, torch 2.11 and 2.14.1, float32 / float16 / bfloat16: before 6890 to 7676 of 8192 values wrong, after 0. ## Not covered No full `device_map` model run across two GPUs. The compiled path relies on Inductor's device guard and was not run on two GPUs.
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.
On torch < 2.12, the torch fallback MXFP4 decode (
_dequantize_torchinunsloth/models/mxfp4_compressed_linear.py) gives wrong fp16 results for large or small scales. This madetest_zoo_kernel_and_torch_path_agreefail on torch 2.10, while the zoo kernel was right.Cause
Before 2.12,
torch.ldexpforms2^ein the input dtype, so in fp16 it overflows from2^16up and flushes small powers:The zoo's own
mxfp4_dequant.pyalready notes this and works in fp32.Change
On torch < 2.12, fp16 is decoded in fp32 and cast once. Nothing changes on torch 2.12+ or for bf16 / fp32.
Tests
New
test_torch_fp16_decode_rounds_once_for_every_finite_scale[cpu|cuda]: the fp16 decode must match a float64 reference bit for bit over E8M0 scales 1..254, including the cases above.tests/test_mxfp4_compressed_linear.pytest_full_and_merged_saves_reload_in_plain_transformers, a bf16 / fp16 dtype assertion. It fails on main too and is not touched here.