Skip to content

Studio: keep MiniMax-H3 PDD float64 math on the host on a device without float64 - #14848

Merged
danielhanchen merged 4 commits into
mainfrom
studio-h3-pdd-no-device-fp64
Oct 25, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-h3-pdd-no-device-fp64

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

The MiniMax-H3 PDD head bank (#14410) builds the per-step [sigma, sigma_next] pair (h3_pdd_sigma_pair, .to(torch.float64)) and the per-head blend weights (pdd_head_weights: torch.linspace / torch.arange in float64, round, diff, divide) on the denoiser's device every step.

Intel Arc A-series GPUs have no native float64. A float64 kernel there raises RuntimeError: Required aspect fp64 is not supported on the device:

Studio accepts XPU as a diffusion device (_xpu_target), and a MiniMax-H3 ComfyUI PDD checkpoint is not refused there: only MPS is refused for the modular workflow, and on XPU the comfy int8 / fp8 layers dequantize to bf16. The stock H3 path only uses float64 for position ids that are cast to float32, while PDD adds real float64 arithmetic on the device. So a PDD render on an Arc A-series card fails loudly at the first denoiser call. Not tested on real Intel hardware.

Change

  • New device_has_fp64(device). It returns True for CPU and CUDA (ROCm included). On XPU it reads torch.xpu.get_device_properties(i).has_fp64, cached per device; an XPU whose properties cannot be read counts as without. Anything else, such as MPS, returns False.
  • On a device without float64, h3_pdd_sigma_pair returns the pair float64 on the host. The bank's pdd_state then stays on the host, and forward computes the weights in float64 there, casts them to the activation dtype and moves only those to the device. This mirrors ComfyUI, which builds its float64 grid on the CPU. The cost is one small device-to-host copy per step, on a device where Studio uses neither CUDA graphs nor default torch.compile.
  • CPU and CUDA run exactly the code they ran before.

Tests

  • test_device_has_fp64_reads_the_xpu_capability[False|True] and test_an_unreadable_xpu_counts_as_without_fp64: torch.xpu.get_device_properties is spoofed. Both fail on main.
  • test_the_denoiser_never_puts_float64_on_a_device_without_it: the CUDA card is spoofed as an fp64-less device. A dispatch mode raises the Arc error on any float64 tensor on the card. Over 8 steps the blend matches the ComfyUI port and the float64 oracle within 1e-5. On main it fails with Required aspect fp64 is not supported on the device (aten._to_copy.default).
  • tests/test_video_minimax_h3_pdd.py: 33 passed and 5 skipped on CPU, 38 passed on a B200. That includes the inductor compile, the fullgraph no-recompile test, CUDA graph replay with changing sigmas, group offload and model CPU offload.

Not covered here: a real Intel Arc run (this host has no XPU).

…out float64

The PDD head bank built its sigma pair and per-head weights in float64 on the denoiser's device. Intel Arc A-series
XPUs have no float64, and a float64 kernel there raises "Required aspect fp64 is not supported on the device", so a
PDD render on such a card failed at the first step. When the device reports no float64 (torch.xpu has_fp64, unreadable
XPU properties, MPS), the pair and the weights stay float64 on the host, as ComfyUI builds its grid, and only the
blended weights in the activation dtype go to the device. CPU and CUDA (ROCm included) are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-25T05:35:59.607188Z 34b2452 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: dc422ab633

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 34b2452bad

ℹ️ 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 0e9dd10 into main Oct 25, 2026
34 of 42 checks passed
@danielhanchen
danielhanchen deleted the studio-h3-pdd-no-device-fp64 branch October 25, 2026 09:01
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Nov 20, 2026
… XPU, fp16 accumulation fallback (unslothai#18629)

Follow-up fixes for unslothai#18515, found while re-checking the PRs merged on
2026-11-19. One commit per fix; every fix has a test that fails on main
and passes here.

| Fix | Follows up | Severity |
|---|---|---|
| DoRA norm: float64 rank terms only on CPU and on CUDA / ROCm up to
rank 32 | unslothai#18515 | P1 |
| DoRA norm: float32 fallback for float16 weights under
allow_fp16_accumulation | unslothai#18515 | P1 |

## Tests (this branch vs main `54af93780b`, CPU)

- New and changed tests: main 9 failed / 12 passed, this branch 21
passed. Each test loads the code of its own tree.
- Neighbouring suites on this branch: DoRA suites: 40 passed.
- `prepush_gate.py` passes.

## DoRA norm: float64 rank terms only on CPU and on CUDA / ROCm up to
rank 32 (follows up unslothai#18515)

#### DoRA factored norm: float64 rank terms only where float64 exists
and is cheap

##### What broke

unslothai#18515 computes the rank-sized terms of the factored DoRA norm (`A @
A.T`, `B @ (A @ A.T)`) in float64 on every device except MPS
(`_rank_dtype` in `unsloth/kernels/dora.py`). That causes two problems.

1. **Intel Arc XPUs have no float64.** PyTorch raises `Required aspect
fp64 is not supported on the device` there (intel/torch-xpu-ops#628,
pytorch/pytorch#147731; Unsloth already guards the same case in Studio,
unslothai#14848). The patch is installed at import with no fallback, so every
DoRA forward on such a device fails, where PEFT's dense path ran before.
2. **High-rank DoRA is slow on consumer NVIDIA cards.** The rank terms
cost `4 r^2 (d_in + d_out)` float64 FLOPs per call, and float64 runs at
1/64 rate on T4, L4 and RTX cards. At `d_in = d_out = 4096`:

| r | float64 FLOPs | cross-term bf16 GEMM FLOPs | float64 time on an L4
(about 0.49 TFLOPS) |
   |---|---|---|---|
   | 16 | 4.2 M | 1.07 G | about 9 us (launch bound) |
   | 64 | 67 M | 4.3 G | about 0.14 ms |
   | 256 | 1.07 G | 17.2 G | about 2.2 ms |

The cross-term GEMM reads W once (about 0.1 ms on an L4). At rank 256
the float64 part is about 20x that. A reported RTX 3090 measurement of a
rank-256 Qwen3-0.6B DoRA step was 261 ms on this code vs 95 ms with
`UNSLOTH_DORA_FACTORED_NORM=0`, with 183 ms of it in the float64 GEMMs.

##### Fix

`_rank_dtype(device, rank)`:
- float64 on CPU.
- float64 on CUDA (including ROCm) up to rank 32.
- float32 everywhere else: MPS, XPU, and CUDA above rank 32.

The rank is a static weight shape, so the choice adds no guard,
recompile or host query. float32 rank terms lose precision only when the
user enables TF32. With realistic updates they stay within 1e-5 of fp64
at rank 64 and 256.

##### Tests

New `tests/test_dora_factored_norm_fp64_devices.py` (CPU, prints the
`dora.py` it loaded):
- The rank dtype per device and rank.
- The float32 rank path produces no float64 tensor (a TorchFunctionMode
raises on any float64 output, as the device would).
- Accuracy against an fp64 oracle: realistic, 97% cancellation, and
ranks 64 / 256.

| Arm | Result (new file + `tests/test_dora_factored_norm.py`) |
|---|---|
| origin/main 635532f | 7 failed, 28 passed |
| this branch | 35 passed |

##### Not covered

- Not run on a real Intel Arc GPU.
- The step-time change was not timed here. The float64 cost above is a
FLOP estimate plus the reported RTX 3090 measurement.

## DoRA norm: float32 fallback for float16 weights under
allow_fp16_accumulation (follows up unslothai#18515)

#### DoRA factored norm: chunked float32 cross term for float16 under
allow_fp16_accumulation

##### What broke

unslothai#18515 computes the DoRA cross term with `torch.mm(A_2, W.t(), out_dtype
= torch.float32)` on CUDA. cuBLAS refuses float16 inputs with a float32
output when `torch.backends.cuda.matmul.allow_fp16_accumulation` is on.
The check is in `aten/src/ATen/cuda/CUDABlas.cpp`,
`gemm_internal<at::Half, float>` and `gemm_and_bias` (torch 2.10 and
2.14):

`TORCH_CHECK(false, "gemm input type at::Half and output type float is
not supported with allowFP16AccumulationCuBLAS")`

So every float16 DoRA forward crashed with that flag set. A Colab L4 and
A100 (torch 2.14.1) run of the factored norm reproduced the RuntimeError
on origin/main.

##### Fix

Only when the weight is float16 and the flag is on, the cross term takes
the existing chunked float32 GEMM: W upcast in 64 MiB row chunks, which
is exact for float16 values. Every other case keeps `mm.dtype`
unchanged. The common path gains one host-side flag read and no kernels.

Reading the flag inside a trace is a graph break
(`torch._C._get_cublas_allow_fp16_accumulation`). The helper therefore
goes through `torch.compiler.assume_constant_result`, so a compiled
graph takes the value at trace time. It is compile-clean under
`fullgraph = True` on torch 2.10 and 2.14.

##### Tests

New `tests/test_dora_factored_norm_fp16_accumulation.py` (CPU; the CUDA
route is selected with a patched `is_cuda` and a spy on `torch.mm`;
prints the `dora.py` it loaded):
- float16 / bfloat16 weight, with the flag off and on. Only float16 with
the flag on skips `mm(..., out_dtype = ...)`. The norm stays within 1e-5
of fp64 in all four cells.
- Reading the flag compiles under `fullgraph = True` with no graph
break.

| Arm | Result (new file + `tests/test_dora_factored_norm.py`) |
|---|---|
| origin/main 635532f | 2 failed, 22 passed |
| this branch | 24 passed |

##### Not covered

- Not run on a GPU here.
- Two edge cases are left as they are on main:
- a float32 `lora_A` entry above 65504 with a float16 base gives a NaN
norm;
- the low half of the float16 split is subnormal at Kaiming scale. The
Colab L4 / A100 runs showed no loss from that (fp64-oracle error about
1.5e-7 on both arms); T4 and RTX cards were not run.

## Not proven here

Intel Arc XPU was not run (no XPU available); the float64 crash is shown
with a device stub. The fp16 subnormal precision concern did not show on
T4, L4 or A100 and is untested on consumer Ampere cards. A float32
lora_A above 65504 with a float16 base still gives a NaN norm (unchanged
from main).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant