Skip to content

Studio INT8 attention and GEMM: CFG replicas keep the sink mode, strided cuDNN admits only the probed dtype, fused int8 takes plain SYMMETRIC only, _int_mm fallback stays on the operand's card - #18140

Merged
danielhanchen merged 6 commits into
mainfrom
studio-int8-attention-fixes
Nov 17, 2026

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes from a re-check of recent Studio INT8 kernel PRs.

  1. CFG replicas lose the INT8 sink mode (Studio: INT8 attention on by default where it wins per card (T4 Klein 18% and Z-Image 14% faster, G4 Klein 6%) #18004, route from Studio: INT8 attention for diffusers-dispatch DiTs (Qwen-Image-2.1, Wan 2.2) #17758). Int8Route.__reduce__ (studio/backend/core/inference/diffusion_int8_attention_dispatch.py) rebuilt the route from its constructor arguments only, so copy.deepcopy (Studio's CFG-parallel replica builder), copy.copy and pickle reset the sink kernel mode chosen at install to 0. With CFG split across two cards, the replica ran the plain INT8 path while the primary ran the sink path, so the guided output depended on the card count. The reduce tuple now carries the mode as state.
  2. Qwen-Image-2.1 strided cuDNN (Studio Qwen-Image-2.1: pass cuDNN attention the strided Q/K/V instead of contiguous copies #18083). The StridedCudnn marker kept only the probed card ids, so a bf16 probe also sent fp16 calls (an autocast, or a later cast of the pipe) down the strided path. The marker now carries the probed dtype, plain_qkv declines other dtypes, and a re-install at a new dtype probes that dtype. The INT8 dispatch race named the strided incumbent dispatch:_native_cudnn, the same key as the contiguous cuDNN call, so cached verdicts crossed over; it now races as dispatch:_native_cudnn:strided. The GPU no-copy check asserted flatten(2, 3).is_contiguous(), which also holds when flatten copies; it now checks the flatten shares storage.
  3. Fused act quant and exact ConvRot take plain SYMMETRIC weights only (Studio int8: fused MLPs and v2 Linears quantize their input with the probed one-pass kernel #17114, Studio: run the bit-identical int8 GEMM routes on uncompiled int8 denoisers #17617). _plain_int8_weight accepted any mapping containing SYMMETRIC, so a torchao weight with SYMMETRIC_NO_CLIPPING_ERR activation quant reached kernels that always divide amax by 127.5. Studio's own loaders build plain SYMMETRIC only, so this narrows a path no Studio load takes.
  4. _int_mm off-grid fallback returns on the operand's card (Studio int8: keep torchao's transposed weight in the TN layout for _int_mm (fixes an illegal memory access on L4) #17613, inherited from torchao). For K or N not a multiple of 8 the safe _int_mm wrapper (Studio diffusion_torchao_patches.py and the copy in unsloth/import_fixes.py) ran the matmul on the CPU and moved the result with .to(input.device.type), which is the current card. With the operands on cuda:1 and cuda:0 current, the int32 result landed on cuda:0. It now moves back to input.device.
  5. FLUX.3 strided attention: a test for the no-copy claim (Studio FLUX.3: pass cuDNN attention the projections' strided Q/K/V instead of contiguous copies #17180). No test checked it: restoring diffusers' .contiguous() copies passed every test. The new test spies on scaled_dot_product_attention and asserts Q / K / V arrive as the permuted views. Test only.

GPU results, base (main 62d66cb) vs this branch

CFG sink mode, Kaggle 2x T4 (sm75), torch 2.11.0+cu130, diffusers 0.42.1. Tiny FLUX.1 transformer with random weights at the real head dim (128), fp16, one attention-sink key; Studio's CfgParallelRouter with the replica built by build_replica on cuda:1; INT8 attention installed with the sink kernels (mode 2, NVRTC CUDA kernel) on both cards; guided 3-step render, cond on cuda:0 and uncond on the replica.

base this branch
sink mode, primary (cuda:0) 2, 2 2, 2
sink mode, replica (cuda:1) 0, 0 2, 2
kernel path on cuda:1 plain x4 sink x4
final latent, CFG split vs single card rel L2 4.82e-4, max abs 3.9e-3 bitwise equal
uncond step 2, CFG split vs native attention rel L2 6.43e-4 (single card 6.11e-4) 6.11e-4, same as single card

_int_mm fallback, same Kaggle 2x T4: int8 operands on cuda:1, cuda:0 current, both copies of _make_safe_int_mm with the real out_dtype.

case (Studio and core copies alike) base this branch
K=65 (off grid) result on cuda:0 (values exact); the next op with a cuda:1 tensor raises "Expected all tensors to be on the same device" result on cuda:1, exact
N=36 (off grid) result on cuda:0, same error result on cuda:1, exact
K=64, N=32 (cuBLAS, control) cuda:1 cuda:1

L4 (sm89), Colab, torch 2.12.1+cu130, cuDNN 9.20.0, torchao 0.17.0:

fix check base this branch
strided cuDNN tiny QwenImage21, bf16 probe at install, then the pipe cast to fp16 fp16 call takes the strided path (marker has no dtype) fp16 call goes to diffusers; marker records bf16
strided cuDNN fp16 output vs diffusers cuDNN equal (on L4 the fp16 strided call happens to be correct; the defect is the admission) equal
plain SYMMETRIC Int8Tensor FeedForward 256 to 1024, SYMMETRIC_NO_CLIPPING_ERR weights fused, identical to the SYMMETRIC result, differs from torchao in 106941 of 153600 elements (max abs 0.086) stays on torchao, bitwise equal
plain SYMMETRIC plain SYMMETRIC weights fused, bitwise equal to torchao same, same hash as base
no-copy test real cuDNN, bf16, head dim 128 SDPA gets the views SDPA gets the views; with the copies restored it gets contiguous tensors and the new test fails

Tests

  • studio/backend/tests/test_diffusion_int8_attention_dispatch.py::test_route_copies_keep_the_sink_mode[1|2]: fails on main.
  • On the T4 pair: test_diffusion_int8_attention_dispatch.py base 2 failed (test_route_copies_keep_the_sink_mode[1|2]) / 46 passed, this branch 48 passed with the two-card tests included.
  • studio/backend/tests/test_torchao_int_mm_transposed.py::test_off_grid_fallback_returns_on_the_operand_card: fails on main; on the T4 pair base 1 failed / 9 passed, this branch 10 passed (including the CUDA transposed _int_mm exactness cases).
  • studio/backend/tests/test_diffusion_qwenimage21_strided_attn.py: test_marker_admits_only_its_probed_dtype, test_reinstall_at_a_new_dtype_probes_it, test_strided_incumbent_races_under_its_own_name fail on main; on the L4 base 3 failed / 14 passed, this branch 17 passed, and the GPU bitwise test ran in both arms.
  • studio/backend/tests/test_diffusion_int8_fused.py::test_only_plain_symmetric_activation_quant_is_eligible[SYMMETRIC_NO_CLIPPING_ERR-False]: fails on main; on the L4 this branch 101 passed / 1 skipped (a torchao >= 0.18 case).
  • studio/backend/tests/test_diffusion_flux3_strided_attn.py::test_strided_processor_hands_sdpa_the_projection_views: passes on main, fails with the copies restored.
  • CPU (torch 2.14.1) on this branch: the five files pass (100 passed, 78 skipped, running the Qwen-Image-2.1 file in its own process). Running the Qwen-Image-2.1 file in one pytest process with the others, test_real_processor_decode_call_takes_the_strided_path hits a RecursionError on main and on this branch alike (test-order interaction, not touched here).

Not covered

  • No real checkpoint or decoded image: the CFG check compares DiT latents of a tiny random model, and the auto (raced) route was not exercised (the dropped field is the same for any non-zero mode).
  • sm80 and sm120 cards; the strided cuDNN and SYMMETRIC fixes ran on L4 only, the CFG and _int_mm checks on T4 only.
  • torchao 0.18 for the SYMMETRIC narrowing.
  • The GPU runs used main 62d66cb; the branch was then rebased onto main 142c398 and only the CPU tests were rerun there.

…e under its own name

The StridedCudnn marker kept only the probed card ids, so a bf16 probe also sent fp16 calls (an autocast or
a later cast of the pipe) down the strided path. The marker now carries the probed dtype, plain_qkv
declines other dtypes, and a re-install at a new dtype probes that dtype instead of keeping the old marker.

The INT8 dispatch race named the strided incumbent dispatch:_native_cudnn, the same key as the contiguous
cuDNN call, so verdicts cached for one were reused for the other. It now races as
dispatch:_native_cudnn:strided.

The GPU no-copy check asserted flatten(2, 3).is_contiguous(), which also holds when flatten copies; it now
checks the output is contiguous and the flatten shares its storage.
…ETRIC weights only

_plain_int8_weight accepted any mapping containing SYMMETRIC, so a torchao weight with
SYMMETRIC_NO_CLIPPING_ERR activation quant reached kernels that always divide amax by 127.5 (torchao 0.17
codes and scales differ for that mode). Studio's own loaders only build SYMMETRIC weights, so this only
narrows a path no Studio load takes. The new test fails on main.
…iews, not copies

No test checked the no-copy claim of #17180: putting diffusers' .contiguous() copies back into the strided
call passed every test. The new test spies on scaled_dot_product_attention and asserts Q / K / V arrive as
the permuted (non-contiguous) views; it fails with the copies restored and passes on main.
… replicas)

Int8Route.__reduce__ rebuilt the route from its constructor arguments only, so copy.deepcopy (Studio's CFG
replica builder), copy.copy and pickle reset the sink kernel mode chosen by _install to 0. The replica's
attention then ran the plain INT8 path while the source ran the sink path. The reduce tuple now carries
the mode as state; the new test fails on main for modes 1 and 2.
… card

For K or N not a multiple of 8 the safe _int_mm wrapper (Studio and core copies, inherited from torchao)
ran the matmul on the CPU and moved the result with .to(input.device.type), which is the current card.
With the operands on cuda:1 and cuda:0 current the int32 result landed on cuda:0. It now moves back to
input.device. The new test fails on main.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T12:21:21.918747Z ee3a335 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. Delightful!

Reviewed commit: ee3a3358d2

ℹ️ 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 361ee4e into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the studio-int8-attention-fixes branch November 17, 2026 13:45
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…rd times (#18584)

#18140 gave a strided cuDNN incumbent its own INT8 race verdict name,
`dispatch:_native_cudnn:strided`, because the strided call differs from
diffusers' contiguous `_native_cudnn` call. But the name was taken from
the marker's class alone. The marker only takes the strided call on the
cards it probed and at the dtype it probed; on any other card or dtype
`plain_qkv` declines and the race times diffusers' call. The dispatch
install races each module on the load card and on every secondary card
(layer split, CFG replica), so a marker for `{0}` raced on `cuda:1`
timed the contiguous call and cached that verdict as `cuda:1 ...
:strided`. A marker for `{0, 1}` then read that verdict on `cuda:1`,
although its real call there is the strided one, and the reverse order
had the same mix-up. The INT8 token floor on that card could then come
from a different incumbent call than the one the route replaces.

## Change

`_race_name` in `diffusion_int8_attention_dispatch.py` takes optional
`card` and `dtype` and keeps the `:strided` suffix only when the
marker's `cards` contains the card and its probed dtype matches (or is
unset). Elsewhere it returns the plain `dispatch:_native_cudnn` name, so
those races share the verdict of the call they actually time. The
dispatch install computes the name per raced card with the race dtype.
Called without the new arguments, `_race_name` returns what it did
before, and the persisted verdict names are unchanged.

## Tests

New `tests/test_diffusion_int8_attention_strided_race_key.py`, CPU only:

- `test_strided_race_name_follows_the_call_the_card_times`: a `{0}` bf16
marker is `:strided` on card 0 at bf16, plain on card 1 and plain at
fp16; a `{0, 1}` marker is `:strided` on card 1. Fails on main, passes
here.
- `test_dispatch_race_keys_each_card_by_its_timed_call` (3 cases): runs
the real dispatch install with a load card and a secondary card, records
the verdict name each card races under. `{0}` bf16 gives strided on 0
and plain on 1; `{0, 1}` fp16 under a bf16 load gives plain on both.
Both fail on main (every card raced as `:strided`) and pass here. `{0,
1}` bf16 gives strided on both and passes on main and here.

Differential (`test_differential.py`, the new file plus
`test_diffusion_int8_attention_persisted.py` and
`test_diffusion_int8_attention_dispatch.py`): DISCRIMINATING, fixed=3,
unchanged=68, head_fail=0.

Suite (`test_diffusion_qwenimage21_strided_attn.py`,
`test_diffusion_flux3_strided_attn.py`,
`test_diffusion_int8_attention_auto.py`,
`test_diffusion_int8_attention_dispatch.py`,
`test_diffusion_int8_attention_persisted.py`,
`test_diffusion_attention.py`,
`test_diffusion_attention_honest_status.py`,
`test_diffusion_attention_cudnn_head_dim.py`): main 272 passed, 1
skipped, 1 failed; this branch 276 passed (the 4 new), 1 skipped, 1
failed. The failing ID is the same on both,
`test_diffusion_qwenimage21_strided_attn.py::test_attention_hook_installs_on_cudnn_pins`,
which greps moved source and is fixed separately.

## Not proven here

No GPU run. The tests stub the race, so they show which name each card
is raced under, not a changed token floor on real hardware. That would
take Qwen-Image-2.1 with a cuDNN pin split across two NVIDIA cards, with
the strided probe passing on only one of them, comparing the per-card
`diffusion.int8_attention` race log lines on main and here.
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