Skip to content

Studio INT8 attention: keep the native fallback in the op's declared layout - #17798

Merged
danielhanchen merged 1 commit into
mainfrom
studio-int8-attention-fallback-layout
Nov 15, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-int8-attention-fallback-layout

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #17718. The INT8 attention op unsloth_studio::int8_attention_nhd registers a fake that returns v.new_empty(q.shape), a contiguous [B, S, H, D] tensor. Inductor treats the fake's strides as part of the op contract and asserts them on the real output. The kernels allocate a fresh contiguous [B, S, H, D] output, so they match. The native fallback did not: it returns scaled_dot_product_attention(...).permute(0, 2, 1, 3), which keeps SDPA's [B, H, S, D] layout whenever SDPA returns one (math SDPA, or inputs that are views of a heads-first buffer).

So a compiled graph that reached the fallback failed at runtime:

AssertionError: expected size 11==11, stride 64==192 at dim=1; expected size 3==3, stride 704==64 at dim=2
Error in op: torch.ops.unsloth_studio.int8_attention_nhd.default

This reproduces with default torch.compile, fullgraph=True, and fullgraph=True, dynamic=True. torch.library.opcheck passes, because it never runs a compiled graph end to end, so only a real Inductor execution catches it.

Fix

The op body now returns int8_attention_impl(q, k, v).contiguous(). This makes every real output use the fake's layout:

  • Kernel outputs (CUDA and Triton) are already contiguous, so the call is a no-op for them.
  • SDPA backends that hand back a BSHD-contiguous result (flash, efficient or cuDNN on contiguous BSHD inputs, which is how the processors call the op) also skip the copy.
  • The copy only happens when SDPA returned the permuted layout.

The fake is unchanged. The dispatch route in diffusion_int8_attention_dispatch.py calls the same op and registers no op or fake of its own, so this fix covers it too.

The copy goes in the op body rather than only in compiled graphs. Eager and compiled calls both run the same body, and the body cannot tell which one called it. Keeping one layout for every real output is the only way the real and fake outputs stay consistent.

This adds no work in eager either. Every caller flattens the result right away (out.flatten(2, 3) in the Flux3 and H3 strided processors, and in the diffusers processors behind the dispatch route). On the permuted layout that flatten already did the copy, as an aten.clone. With this change .contiguous() does the copy and flatten becomes a view. A dispatch-mode op count on CPU shows exactly one copy before and after, for every combination of SDPA backend and input layout.

Tests

test_the_native_fallback_keeps_the_fake_layout_under_inductor (CPU) compiles a graph that calls the op on the native fallback and runs it with Inductor. It covers fullgraph=True with static and dynamic shapes, contiguous BSHD inputs and heads-first-backed strided inputs, and default SDPA and math SDPA, at several sequence lengths. It checks that the fallback really ran, that the compiled output matches eager, and that the eager op output is contiguous and equal to native_attention.

  • On main: 6 of 8 cases fail with the Inductor stride assertion above. The 2 cases that pass are contiguous BSHD inputs with default SDPA, where the CPU SDPA result is already BSHD-contiguous.
  • On this branch: 8 of 8 pass.
  • INT8 suites on CPU (test_diffusion_int8_attention.py, test_diffusion_int8_attention_dispatch.py, test_diffusion_int8_attention_triton_fallback.py): 125 passed, 76 skipped (the CUDA-only tests).

On an NVIDIA L4 (torch 2.14.1+cu130, diffusers 0.42.1), comparing main (f490069) with main plus this patch:

  • INT8 suites: 188 passed, 12 skipped, 1 failed on this branch, and 180 passed, 12 skipped, 1 failed on main (the 8 extra passes are the new test). The one failure is test_video_load_passes_the_request on both sides, a missing structlog in the test venv, and it is unrelated to this change.
  • The same compiled fallback graph on CUDA (head dim 80, so no kernel config and the native path runs), fullgraph=True, static and dynamic shapes: 6 of 8 cases fail on main with the same stride assertion, and 8 of 8 pass here.
  • A tiny Wan transformer with the dispatch route engaged (4 modules routed, the kernel ran on all 4 attention calls), compiled with the default settings, fullgraph=True, and fullgraph=True, dynamic=True: 0 graph breaks, and the output is bit-identical to main (eager and compiled).
  • Eager cost of op(q, k, v).flatten(2, 3) on the forced native fallback, median of 3 x 300 timed calls per arm, two passes per arm: with default SDPA every delta is inside the run-to-run spread, for both input layouts, at 126-token audio, 512-token text, 4096x512 text cross-attention and 1024-token image shapes. With math SDPA pinned, deltas are -11 to +126 us on 0.5 to 20 ms calls (under 1%), and the ranges mostly overlap.

The op's fake promises a contiguous [B, S, H, D] output and Inductor asserts
those strides on the real output. Native attention can return SDPA's permuted
[B, H, S, D] layout (math SDPA, or heads-first-backed inputs), so a compiled
graph that reached the fallback raised a stride assertion. Copy the op's
result to the fake's layout; the kernels already write a contiguous output,
so this is a no-op for them and for SDPA backends that keep the BSHD layout.

Adds a CPU test running real Inductor execution (fullgraph, static and
dynamic shapes) on both input layouts with default and math SDPA.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T06:30:23.834695Z b35fd66 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. More of your lovely PRs please.

Reviewed commit: b35fd66b97

ℹ️ 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 beb99c4 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the studio-int8-attention-fallback-layout branch November 15, 2026 08:46
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