Skip to content

MXFP4 torch decode: fp16 in fp32 on torch < 2.12, where ldexp overflows - #15121

Merged
danielhanchen merged 2 commits into
mainfrom
fix/mxfp4-test-oob-scales
Oct 24, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/mxfp4-test-oob-scales

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

On torch < 2.12, the torch fallback MXFP4 decode (_dequantize_torch in unsloth/models/mxfp4_compressed_linear.py) gives wrong fp16 results for large or small scales. This made test_zoo_kernel_and_torch_path_agree fail on torch 2.10, while the zoo kernel was right.

Cause

Before 2.12, torch.ldexp forms 2^e in the input dtype, so in fp16 it overflows from 2^16 up and flushes small powers:

input correct torch 2.10, fp16
0 x 2^16 0 NaN
2^-10 x 2^20 1024 inf
6 x 2^-25 1.79e-7 (subnormal) 0

The zoo's own mxfp4_dequant.py already 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.py main this PR
B200, torch 2.10 53 / 56 56 / 56
B200, torch 2.14 38 passed, 18 skipped same
T4 (Kaggle), torch 2.10.0 52 / 56 55 / 56
T4 (Kaggle), torch 2.14.1 55 / 56 55 / 56
  • Failure left on T4: test_full_and_merged_saves_reload_in_plain_transformers, a bf16 / fp16 dtype assertion. It fails on main too and is not touched here.
  • Not run: torch 2.11 to 2.13. The 2.12 cutoff follows the zoo's note.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T13:18:24.580698Z d911c2d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: d911c2d0c2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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
danielhanchen merged commit edf19f5 into main Oct 24, 2026
36 of 37 checks passed
@danielhanchen
danielhanchen deleted the fix/mxfp4-test-oob-scales branch October 24, 2026 13:24
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant