Skip to content

MXFP4 torch decode fallback: run ldexp on the operands' GPU - #16062

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s9-15121-mxfp4-fallback-device-guard
Oct 30, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s9-15121-mxfp4-fallback-device-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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.

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.
@danielhanchen danielhanchen self-assigned this Oct 30, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 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-30T03:40:34.883242Z 6431601 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. Bravo.

Reviewed commit: 64316018aa

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit eb6f104 into main Oct 30, 2026
@danielhanchen
danielhanchen deleted the dh/s9-15121-mxfp4-fallback-device-guard branch October 30, 2026 05:09
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.
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