Repository navigation
Studio diffusion: launch the VAE and MOSS Triton kernels on the input's own card - #17099
Conversation
Codex Review Summary: ✅ CompletedThis 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. |
[skip ci]
|
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". |
…rrent card [skip ci]
|
@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". |
… q/k RoPE int64 offsets, Gemma-4 band route int32 guard (unslothai#18141) Three fixes from a re-check of recent kernel PRs. 1. **Cold probes ask about capture on the operand's card (unslothai#17795, unslothai#17593, unslothai#17637).** The cold `verdict()` of the MiniMax-H3 row-mod kernel (`video_minimax_h3_rowmod.py`), the H3 q/k norm (`video_minimax_h3_qknorm.py`) and the FLUX.3 q/k RoPE (`diffusion_flux3_qk_rope.py`) read `torch.cuda.is_current_stream_capturing()` on the current card before switching to the operand's card. With another card current, the timing / equality probe could start inside a CUDA graph capture on the operand's card, then cache a failure or raise. The query now runs under `torch.cuda.device(x.device)`, with the probe. 2. **MOSS-Image fused q/k norm + RoPE: int64 row offsets (unslothai#17099).** The kernel multiplied the token index by the fused-QKV row stride in int32, which wraps past 2**31 (a 9216-wide row past about 233k tokens). The row index is now int64 before any stride product; output bits are unchanged. 3. **Block compile: the Gemma-4 sliding band route checks the interval kernel's int32 limit (unslothai#17817, unslothai#17860).** The long-band route in `unsloth/models/_block_compile.py` called `large_head_attention` directly and skipped the int32 span check the large-head router applies on its own path (`_shapes_qualify`), so a long enough sliding layer reached the int32-addressed kernel. The band route now keeps masked SDPA past that limit. ### GPU results, base (main 62d66cb) vs this branch Capture probes, Kaggle 2x T4 (sm75), torch 2.11.0+cu130: cuda:0 current, a `torch.cuda.graph` capture active on cuda:1, operand on cuda:1, cold call, probe runs counted. | probe | base | this branch | |---|---|---| | H3 row-mod `verdict` | probe ran inside the capture and cached `{exact: False, use: False}`; the next eager call still returns the cached failure | returns None, no probe, nothing cached; the next eager call computes `{exact: True, use: True}` | | H3 q/k norm `verdict` | probe ran inside the capture and the call raised `Cannot copy between CPU and CUDA tensors during CUDA graph capture` | returns None, no probe; the next eager call computes the real verdict | | FLUX.3 q/k RoPE `verdict` | probe ran inside the capture and cached a RuntimeError failure for cuda:1 for the process | returns None, nothing cached; the next eager call runs the real check | The captured graph itself replayed correctly in both arms; the defect is the probe running inside the capture and the cached failure or raise. MOSS and band route, L4 (sm89), Colab, torch 2.12.1+cu130, Triton 3.7.1: | fix | check | base | this branch | |---|---|---|---| | MOSS int64 offsets | fused kernel engaged (4 launches per arm); 1x4096x24, 2x1029x24, 1x16384x24, 1x37x4 | q / k / v hashes | identical to base; v equal to eager, q / k within 0.03125 of eager in both arms | | band route guard | S600 / w512 / hd256, S2048 / w512 / hd128, S4096 / w1024 / hd256 (kernel route), S256 / w512 (causal SDPA) | routes and output hashes | same routes, identical hashes; max error vs fp32 about 0.008 | ### Tests - `studio/backend/tests/test_diffusion_capture_probe_device.py` and `test_h3_row_mod.py::test_cold_verdict_asks_about_capture_on_the_operand_card`: on the T4 pair base 3 failed / 14 passed, this branch 17 passed (two-card tests included). - `studio/backend/tests/test_diffusion_moss_qk_rope_offsets.py` lowers the kernel to TTIR and rejects i32 row-stride products: fails on main; on the L4 base 1 failed / 8 passed, this branch 9 passed, and the existing MOSS fused GPU tests pass in both arms. - `tests/test_block_compile.py::test_gemma4_long_band_past_int32_keeps_the_plain_route`: fails on main; on the L4 base 1 failed / 83 passed, this branch 84 passed (4 skips, the same version gates in both arms). - CPU (torch 2.14.1) on this branch: 13 passed / 5 skipped in the probe and MOSS files, 74 passed / 14 skipped in `test_block_compile.py`. ### Not covered - The overflow sizes themselves (over 233k MOSS tokens, a band span past 2**31) cannot be allocated; the TTIR and CPU route tests are the evidence there. - A full MOSS-Image render with real weights; the op and its unit tests ran. - On fp16 / sm75 the FLUX.3 q/k RoPE check reports a real bit mismatch, so its fused path stays off on T4 regardless of this change; the capture fix is shown by the probe count, not by the fused path engaging there. - sm80 and sm120 cards; the capture probes ran on T4 only, MOSS and the band route on L4 only. - The GPU runs used main 62d66cb; the branch was then rebased onto main 142c398 and only the CPU tests were rerun there.
Triton launches a kernel on the thread's current CUDA device, not on the device of its tensor arguments. None of the nine launches in
diffusion_vae_fused.py(GroupNorm partials / combine / apply, RMS norm, softmax rows, bias + residual, DupUp add, nearest 2x upsample, the HunyuanVideo-1.5 upsample) nor the MOSS QK-norm RoPE launch indiffusion_moss_fused.pyset the device. When the input sits on another card than the calling thread's current device, the launch raisesValueError: Pointer argument (at 0) cannot be accessed from Triton (cpu tensor?)._guardthen marks the module failed and it stays on the stock path for the rest of the process.This PR wraps each launch in
_on_card(t). When the tensor is on another card than the current one, that istorch.cuda.device(t.device), the same guard the int8, NVFP4 and FLUX.2 RoPE launches already use. When the tensor is on the current card, as in every single-card render, it is a sharednullcontext, so there is no device switch. The guard is also skipped undertorch.compile, because Inductor sets the device for traced user kernels itself.UNSLOTH_DIFFUSION_TRITON_DEVICE_SCOPE=0restores the old behaviour.Studio pins its render and CFG replica threads to their card, so the default single-card render does not hit this. It affects any caller whose current device is a different card from the tensor's.
Evidence (Kaggle 2x T4, torch 2.11.0+cu130, Triton 3.6.0)
torch.cuda.set_device(0), then the same call oncuda:0inputs and oncuda:1inputs. The base arm is main at 6244269.group_norm_actwas checked through itson_kernelcallback, so the Triton path ran and not the stock fallback. Oncuda:0the outputs hash the same on base and head, so single-card numerics are unchanged.Same-card cost, measured in-process on a T4 over 10 interleaved rounds with the real
_on_cardagainst a stand-in that never scopes (main's behaviour):_on_cardper launchgroup_norm_act1x64x1x16x16group_norm_act1x128x4x64x64The roughly 1 us per launch is inside the round-to-round spread; the medians are not slower. The first version of this PR entered
torch.cuda.deviceon every launch, which cost about 3.6 us per launch and was measurably slower on the small GroupNorm, hence the same-card shortcut. The table at the top was re-run with the final code on both T4s and gives the same result: base fails, this PR runs with bitwise-equal outputs, the kill switch fails.New CPU test
tests/test_diffusion_triton_device_scope.py: an AST check that everykernel[grid](...)in both files sits insidewith _on_card(...)(9 + 1 launches), plus the other-card, same-card, kill-switch, CPU and traced cases. It passes 8/8 here and fails 8/8 on main, which has no_on_card.The int8 GEMV launch has the same problem and is fixed in its own PR, together with that file's tile changes.
Not covered here: real VAE decode timings on a second card, and ROCm. On ROCm,
torch.cuda.devicemaps to the HIP device in the same way, but no gfx card was run for this change.