Skip to content

Studio: probe the FLUX.2 GPU reference preprocess at load, Lanczos 2**31 guard, CUTLASS race key covers its helpers - #18968

Merged
danielhanchen merged 4 commits into
mainfrom
studio-int8-attn-audit-fixes
Nov 23, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-int8-attn-audit-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-ups from a kernel-rules pass over the recent INT8 attention, CUTLASS race cache and FLUX.2 reference preprocessing PRs (#18917, #18797, #18786). Three commits, one per fix:

  1. FLUX.2 edits: the GPU reference preprocess probe runs at load, not inside the first edit request (Studio: prepare FLUX.2 edit references on the GPU, byte-identical to the CPU path #18917).
    install() patches the image processor before placement, so the per-device probe (Triton compile of the two Lanczos kernels plus two Pillow comparison images) ran on the first edit request under the module lock. A new diffusion_ref_preprocess.warm(pipe) runs the same probe for the pipe's execution device right after placement (after the graph arming, before the load record). It is a no-op for families without the installed preprocess. The lazy probe stays as the fallback for a device warm() never saw.
  2. FLUX.2 edits: references past 231 elements stay off the 32-bit Lanczos kernels (Studio: prepare FLUX.2 edit references on the GPU, byte-identical to the CPU path #18917).**
    The kernels index one image with int32 flat offsets. lanczos_resize now refuses a resize whose source, intermediate (either pass order) or output reaches 2**31 elements, about 715 MP. The FLUX.2 preprocess checks before it uploads the pixels, so such an image takes the CPU path.
  3. INT8 GEMM: CUTLASS race verdicts are keyed on the helpers the race code calls (Studio: key the int8 CUTLASS race cache on the race code, not the whole GEMM file #18797).
    _RACE_SOURCES hashed the listed race functions but not the helpers they call. An edit to _xs_dtype (the activation-scale dtype the race times), _payload, the act quant, the row-bucket bookkeeping or the race switches therefore kept stale verdicts.
    • Those helpers are now keyed.
    • The few left out on purpose are listed in _RACE_UNKEYED: the key builders, the library loader (the library is hashed by its bytes) and the tile probe's own persistence.
    • A new test walks the call closure across the three keyed modules, including cut.x calls, and fails on any helper that is in neither list.
    • Performance only: tile correctness is still the per-card bit-exact probe. Every card re-races once after this update.

Evidence

1. First edit request, L4 (Colab, torch 2.14.1+cu130, Triton 3.8.0, diffusers 0.43.0)

Each run is a fresh process with base (main) and head interleaved, 5 runs per arm. The images are 2048x1536 and then 1800x2400, resized to 1 MP and cropped. "Cold" uses an empty Triton cache per process; "warm" uses a pre-populated one.

Triton cache Arm Load step (install + warm), ms First request, ms Second request, ms
cold main 0.68 [0.64, 0.70] 2601.6 [2577.2, 2609.3] 28.2 [27.4, 28.7]
cold head 2564.8 [2553.5, 2569.1] 33.5 [32.9, 34.7] 28.3 [27.9, 30.0]
warm main 0.65 [0.63, 0.68] 1639.4 [1637.0, 1646.5] 28.1 [27.6, 30.5]
warm head 1606.5 [1604.1, 1614.7] 32.7 [32.3, 34.7] 27.5 [27.2, 27.8]
  • The first request drops from 2.6 s to 34 ms with a cold Triton cache, and from 1.6 s to 33 ms with a warm one. The base spread is 1.2%, so this is far outside the noise.
  • The same time now lands in the load step.
  • The second request is a tie (A/A control).
  • Output tensors hash the same on main, on head and on the stock CPU path, for both images and all 10 runs per arm. STATS shows gpu 2, fallback 0 in every run, so the GPU path ran.

2. Folded sink kernel at head dim 128 on sm80 and sm120 (#18786)

The question was whether the folded sink path is right on cards where it had only been checked offline. It is, so the code is unchanged.

  • Setup: current main, fold on vs UNSLOTH_DIFFUSION_INT8_ATTENTION_RESID_FOLD=0, each arm twice. 15 cases: Flux shape, 40 sink keys, long calls, fp16 / fp32, strided, batch 2, no-clip, hd64. Compared against fp32 reference attention.
  • A100-40GB (sm80, qreg 1):
    • Covers both hd128 configs: wpc 4 below 4608 keys and wpc 8 from 4608 on.
    • The fold engaged on every clipping hd128 case: one launch instead of two.
    • Fold on vs off differs by at most 0.125 in bf16. The 40-key case reaches 0.5, which is one bf16 step at those magnitudes.
    • The folded output is nearer fp32 in every bf16 clipping case. Flux shape rel_l2 0.0130402 -> 0.0129988; 8192 keys 0.0085737 -> 0.0085072.
    • Non-clipping and non-sink cases are bit-identical.
  • RTX PRO 6000 (sm120, qreg 0, native probability pack from Studio diffusion: native probability round and pack in the INT8 attention kernel on sm89 / sm120 #18870):
    • Same picture. Flux shape rel_l2 0.0144564 -> 0.0144301; 8192 keys 0.0160356 -> 0.0160310.
    • The 40-key case is a tie: 0.0208967 vs 0.0209146, with mean abs error lower.
  • GPU pytest subset: 57 passed on both cards in both arms, plus one failure that is also there with the fold off: test_a_plan_whose_sink_path_loses_its_race_runs_torch_attention.
  • sm86 has no cloud tier. Its hd128 config compiles the same defines as the A100 long-call config: HD 128, BN 128, NQ 1, WPC 8, STAGES 2, QREG 1. The only arch branch in the kernel source is __CUDA_ARCH__ < 800, so sm86 runs the same fold code path that passed on A100. It stays enabled.

Tests

  • tests/test_diffusion_ref_preprocess.py:
    • The probe runs in warm() and not on the first request (the probe is mocked to raise).
    • An unwarmed device still probes lazily.
    • warm() is a no-op without the install.
    • The load calls warm after placement.
    • The int32 size limit, including that it is checked before the pixel upload.
  • tests/test_diffusion_int8_cutlass_race_key.py:
    • Every function the keyed race code reaches is keyed or listed as deliberately unkeyed.
    • The unkeyed names still exist.
  • Each new test fails on main.
  • Targeted CPU files pass: test_diffusion_ref_preprocess.py 34, test_diffusion_ref_preprocess_gate.py 5, test_diffusion_int8_cutlass_race_key.py 29, test_diffusion_int8_cutlass_lib_digest.py 5, test_diffusion_int8_cutlass_probe_persist.py 8.

Not covered

  • No real FLUX.2 model load was timed end to end. The L4 numbers come from the image processor with the real kernels, on a pipe placed on the GPU.
  • sm86 was not run on hardware. The case for it rests on the identical compiled defines above.
  • Windows was not run. Nothing here is OS specific.

…on the first edit request

install() runs before placement, so the per-device probe (Triton compile of the
Lanczos kernels plus two Pillow comparison images, about 1.2 s cold) ran inside
the first edit request under the module lock. warm(pipe) now runs the same
probe for the pipe's execution device once placement is done; the lazy probe
stays as the fallback for a device warm() never saw.
…it Lanczos kernels

The horizontal and vertical Lanczos kernels index one image with int32 flat
offsets; only the batch offset is int64. A reference whose source, pass
intermediate or output reaches 2**31 elements (about 715 MP) would wrap.
lanczos_resize now refuses such sizes, and the FLUX.2 preprocess checks before
uploading the pixels, so that image takes the CPU path.
…ode calls

_RACE_SOURCES hashed the listed race functions but not the same-module helpers
they call, so an edit to _xs_dtype (the activation-scale dtype the race times),
_payload, the act quant, the row-bucket bookkeeping or the race switches kept
stale verdicts. Those helpers are now keyed; the few left out on purpose (key
builders, library loader, tile-probe persistence) are listed in _RACE_UNKEYED,
and a test walks the call closure across the three keyed modules and fails on
any helper that is in neither. Cards re-race once after this update.
@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:15.699905Z 69f4700 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.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 69f4700151

ℹ️ 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 b3ecd8d into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the studio-int8-attn-audit-fixes branch November 23, 2026 03:37
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant