Repository navigation
MXFP4 torch decode fallback: run ldexp on the operands' GPU - #16062
Merged
Merged
Conversation
torch.ldexp (2.11+, native integer-exponent kernel) launches on the current device, so packed weights on cuda:1 with cuda:0 current (a model split across cards) decoded garbage in _dequantize_torch. float32 decodes always take this path (the zoo decoder serves only fp16 / bf16). Guard on the operands' device, as unsloth_zoo's mxfp4_dequantize_torch already does (zoo #2115); compiled code keeps Inductor's own device guard. Kaggle 2x T4, operands on cuda:1, cuda:0 current, vs the CPU decode: torch 2.11.0 and 2.14.1, float32 / float16 / bfloat16: before 6890-7676 of 8192 values wrong, after 0.
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. Bravo. 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". |
[skip ci]
This was referenced Oct 30, 2026
danielhanchen
added a commit
that referenced
this pull request
Oct 30, 2026
…#16088) `Repo tests (CPU, rest-2)` is red on main since #16062 (MXFP4 torch decode fallback: run ldexp on the operands' GPU). It fails on unrelated PRs such as #16085 and #16074: ``` tests/test_mxfp4_compressed_linear.py::test_torch_fallback_runs_ldexp_on_the_operands_gpu[dtype0..2] unsloth/models/mxfp4_compressed_linear.py:99: in _dequantize_torch_impl out[..., 0::2] = lut[(blocks & 0x0F).long()] RuntimeError: PyTorch is not linked with support for cuda devices ``` The test builds its operands on `cuda:1` inside `FakeTensorMode`. That works on any CUDA-linked torch, with or without a GPU, but a CPU-only build cannot dispatch a fake CUDA tensor. The CPU shards install CPU-only torch. I checked the same few lines on both builds: `2.13.0+cu130` with no visible GPU runs them, and `2.10.0+cpu` raises the error above. The product's device guard is fine; the test assumed a CUDA build. ## Changes - `test_torch_fallback_runs_ldexp_on_the_operands_gpu` now skips when `torch.backends.cuda.is_built()` is false. It still runs on CUDA builds, GPU or not. - New `test_the_torch_decode_runs_inside_its_operands_device_guard` runs on any build, so the CPU shards keep covering the guard. It checks three things: `_dequantize_torch` decodes with the operand's device current, puts the previous device back, and leaves the current device alone for a CPU operand or an index-less `cuda` operand. - The fake `torch.cuda.device` guard is shared by both tests. ## Verification - CPU-only torch 2.10: the file has 20 passed and 38 skipped, and the fake-tensor test skips with the reason above. On main it had 3 failures. - CUDA torch 2.13 with no visible GPU: the three fake-tensor cases and the new test pass (4 passed). - Sabotage: dropping `_operand_device_guard` from `_dequantize_torch` fails the new test on CPU-only torch.
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.
Follow-up to #15121.
Problem
_dequantize_torchrunstorch.ldexpwithout 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_torchalready does (unsloth-zoo #2115). Compiled code keeps Inductor's own device guard.Tests
tests/test_mxfp4_compressed_linear.py, torch 2.13:test_torch_fallback_runs_ldexp_on_the_operands_gpu[dtype0],[dtype1],[dtype2]. This branch: 31 passed, 26 skipped.test_torch_fallback_runs_ldexp_on_the_operands_gpu[dtype0-2]andtest_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.Not covered
No full
device_mapmodel run across two GPUs. The compiled path relies on Inductor's device guard and was not run on two GPUs.