Skip to content

Studio kernels: recheck fixes for FLUX.1 GELU gate tracing, INT8 attention plan keys, CUTLASS live race key, step-skip sample= shape - #18981

Merged
danielhanchen merged 5 commits into
mainfrom
studio-recheck-kernel-fixes
Nov 23, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-recheck-kernel-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Four small fixes from a recheck of the merged FLUX.1 ConvRot GELU (#18766), INT8 attention sink fold (#18786), CUTLASS race key (#18797) and SDXL static step skip (#18826) changes. Each one is its own commit with a test that fails before it and passes after.

Changes

  1. FLUX.1 ConvRot GELU gate traces on torch 2.10 / 2.11 (diffusion_convrot_quant.py). convrot_gelu_act_quant chose its row threshold with rows[prefix is None]. Dynamo on torch 2.10 and 2.11 cannot trace a tuple indexed by a bool. With fullgraph=True the compile raises Unsupported; without it, every armed FF / single-block down projection graph-breaks. torch 2.6 and 2.12+ accept it. The index is now 1 if prefix is None else 0, which selects the same entry.
  2. INT8 attention launch plans follow the kernel switches (diffusion_int8_attention.py). _plan reads UNSLOTH_DIFFUSION_INT8_ATTENTION_RESID_FOLD, and kernel_variant reads the sink order and native P switches, when a plan picks its kernels. The warm lookup in _find_plan did not key on them. Flipping the fold off in a running process therefore left warm shapes on the folded kernel until forget_plans. The plan key now includes the three switches as set. That costs three env reads, about 1.7 us of host time per call, and nothing under CUDA graph replay.
  3. CUTLASS race key covers the live row race (diffusion_int8_gemm.py). _live_row_race decides which calls race and applies the force pick. It also writes its winner into the same disk entry that _cutlass_plan reads back on the next load. Neither it nor live_race_enabled was in _RACE_SOURCES, so an edit confined to the live race kept old verdicts. Both are keyed now. This applies cleanly on top of the open race-key closure change, whose closure test still passes with both applied.
  4. Step skip reads sample= (diffusion_step_skip.py). The reuse key took the latent from hidden_states or the first positional argument. A UNet2DConditionModel called with sample= was therefore keyed with shape None, and a skipped step after a latent shape change returned the previous output at the old size. The stock SDXL pipeline passes the latent positionally, so Studio's own renders were not affected.

Tests (CPU, base = parent commit, head = this branch)

Test Base Head
test_diffusion_convrot_gelu_route_trace.py on torch 2.10.0 / 2.11.0 2 failed, 1 passed 3 passed
same on torch 2.6.0 / 2.12.1 / 2.13.0 / 2.14.1 n/a 3 passed
test_diffusion_int8_attention_resid_fold.py::test_a_warm_plan_follows_the_kill_switch failed (warm plan reused) passed
test_diffusion_int8_cutlass_race_key.py (3 new live race cases) 3 failed, 27 passed 30 passed
test_diffusion_step_skip_unet.py::test_a_skipped_step_never_returns_another_latent_shape[True] failed (width 8 for a width 12 latent) passed

The surrounding INT8 attention, CUTLASS, GEMM, step skip and FLUX.1 GELU suites have the same results on both arms. 8 FLUX.1 GELU tests fail on both base and head when they run in the same process after the step skip and INT8 suites, and they pass when that file runs alone. That failure predates this change.

Not covered here

  • No GPU run. The four changes are host-side keys and gates, and each one is checked on CPU against the real module code.
  • The +1.7 us host cost per INT8 attention call was measured on CPU, not as end-to-end render time.

… 2.11 trace it

convrot_gelu_act_quant picked its (with prefix, without prefix) row threshold
with a bool index. Dynamo on torch 2.10 and 2.11 rejects a tuple indexed by a
bool: fullgraph compiles of the FLUX.1 transformer raise Unsupported and the
default compile graph-breaks at every armed FF / single-block down projection.
torch 2.6 and 2.12+ accept it, so the existing tiny-model compile test (which
also stubs the route out) did not see it.
…ir kernels

_plan reads UNSLOTH_DIFFUSION_INT8_ATTENTION_RESID_FOLD (and the sink order /
native P switches through kernel_variant) when it builds a plan, but the warm
lookup in _find_plan keyed only shapes, strides and config. Turning the fold
off in a running process left every warm shape on the folded kernel until
forget_plans; the kernel cache already keyed fold. The plan key now carries
the three switches as set (three env reads, about 1.7 us per call on the
host; nothing under CUDA graph replay).
_live_row_race (the first-call race of a new row bucket) chooses which calls
race, applies the force pick and writes its winner into the same disk entry
_cutlass_plan reads back on the next load, but neither it nor
live_race_enabled was in _RACE_SOURCES. An edit confined to the live race kept
the digest, so verdicts written under the old policy stayed in use. Both are
keyed now; three race-changing cases cover them.
_call_signature took the latent from hidden_states or the first positional
argument, so a UNet2DConditionModel called with sample= keyed every step with
shape None. A step skipped after the latent changed shape returned the
previous step's output at the old size. The stock SDXL pipeline passes the
latent positionally; the keyword now keys the same way.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T02:37:20.480780Z d6429c4 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: d6429c4e64

ℹ️ 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 a62bb71 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the studio-recheck-kernel-fixes branch November 23, 2026 04:17
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…cess, INT8 attention and MPS guard changes (#19005)

## Summary

Follow-ups to #18965, #18968, #18979 and #18981 from a recheck of the
merged kernel and compile changes. There is one commit per PR. Each fix
is small and backwards compatible. The default bf16 / fp32 paths produce
the same outputs as before.

### #18965: Windows ROCm per-tap Conv3d
- Under CPU bf16 autocast, fp32 inputs came back as fp32 instead of the
autocast dtype. Autocast calls now stay on stock `Conv3d`; bf16, fp16
and fp32 calls outside autocast keep the per-tap path. fp16 stays on it
on purpose: on Windows gfx1151 the real LTX-2.3 fp16 decode is finite
there, and stock `Conv3d` was 2.4x slower (35.7 s vs 87.0 s) with a 7.1
GiB higher peak.
- `torch.compiler.is_compiling()` is now checked before the env switch.
Before this, flipping `UNSLOTH_VIDEO_VAE_ROCM_PERTAP` inside a compiled
region added an env guard and forced a recompile.

### #18968: Lanczos reference preprocess
- The int32 guard bounds a single image. The identity-resize and
vertical-first normalize launches run flat over the whole NHWC batch, so
a `[24, 8192, 8192, 3]` batch wrapped the int32 index. `_normalize_rgb`
now widens the program offset to int64 before the multiply. The TTIR now
shows an `i64` multiply and `i64` pointer offsets.
- If the load-time probe hit a transient OOM, GPU reference
preprocessing stayed off on that device until restart. Now an OOM takes
the CPU path for that call only, and the next request probes again.
Other probe failures are still cached.

### #18981: INT8 attention kernel switches
- Flipping the residual-fold, sink-order or native-P switch built a new
launch plan. The plan's `decision` key left the switches out, so the new
kernel reused the old kernel's accuracy and timing verdict without
racing. The decision key now carries the same switch tuple as the plan
key.

### #18979: MPS large-attention guard
- The prefill check parsed `torch.__version__` with `re.match`. On a
cold regex cache, Dynamo traced into the pattern compiler and failed
`fullgraph=True`. The pattern is now compiled at import.

## Tests

Each new test fails on `origin/main` and passes here:
- `test_video_vae_rocm_pertap.py`:
`test_autocast_keeps_stock_output_dtype`,
`test_compiled_kill_switch_flip_does_not_recompile` (plus
`test_fp16_outside_autocast_still_takes_pertap`)
- `test_diffusion_ref_preprocess.py`:
`test_batched_normalize_uses_64_bit_offsets`,
`test_probe_oom_is_not_cached`
- `test_diffusion_int8_attention_resid_fold.py`:
`test_a_flipped_switch_races_its_own_kernel`
- `test_mps_large_attention_guard.py`:
`test_cold_prefill_check_traces_under_fullgraph`

The touched suites and their neighbours (per-tap VAE, reference
preprocess, MPS guard and families, ROCm contiguous q/k/v and k/v,
CUTLASS race key, attention install, every INT8 attention suite) give
657 passed, 175 skipped (GPU / Mac only) on Linux CPU, torch 2.12.1,
Triton 3.7.1.

## Not changed

- #18966: the 2,560-token q/k/v gate splits a dynamic range into two
graphs. That is bounded: 8 lengths from 1,024 to 4,096 under
`fullgraph=True, dynamic=True` compiled 2 graphs. Studio's trainer never
marks a dynamic range across the threshold.
- #18979: over-cap query lengths still specialize the chunk loop under
compile. Studio never compiles on MPS (training compiles on CUDA only,
and the MPS target defaults to eager).


## GPU, Windows ROCm and Mac checks

Same host and job for both arms, base = the merge base.

| platform | check | base | this PR |
|---|---|---|---|
| A100 40 GB | Lanczos normalize of a `[24, 8192, 8192, 3]` uint8 batch
| illegal memory access | 24 of 24 images equal to the torch reference |
| A100 40 GB | real OOM in the load-time probe, then memory freed | GPU
path off for the process | probes again, GPU path on |
| A100 40 GB | GPU Pillow parity, 6 sizes; INT8 `-m gpu` tests | 6/6; 6
passed | 6/6; 6 passed |
| L4 | flip the residual-fold switch in one loaded process | 1 race,
same decision key | 2 races, keys differ |
| Windows gfx1151 | bf16 1920x1088x121 LTX-2.3 decode | 10 per-tap calls
| 10 per-tap calls, bit-identical, time within the base A/A spread |
| macos-15 | MPS guard suites with `UNSLOTH_MPS_LARGE_ATTENTION=1` | 4
failed, 90 passed | same 4 failed (stock attention past the threshold,
the bug the guard exists for), 92 passed |

On Metal, a V batch wider than Q and K does not broaden the attention,
so the guard keeps counting Q and K only.
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