Repository navigation
Studio INT8 attention: keep the native fallback in the op's declared layout - #17798
Merged
Merged
Conversation
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.
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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #17718. The INT8 attention op
unsloth_studio::int8_attention_nhdregisters a fake that returnsv.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 returnsscaled_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:
This reproduces with default
torch.compile,fullgraph=True, andfullgraph=True, dynamic=True.torch.library.opcheckpasses, 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:The fake is unchanged. The dispatch route in
diffusion_int8_attention_dispatch.pycalls 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 thatflattenalready did the copy, as anaten.clone. With this change.contiguous()does the copy andflattenbecomes 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 coversfullgraph=Truewith 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 tonative_attention.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.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) withmainplus this patch:main(the 8 extra passes are the new test). The one failure istest_video_load_passes_the_requeston both sides, a missingstructlogin the test venv, and it is unrelated to this change.fullgraph=True, static and dynamic shapes: 6 of 8 cases fail onmainwith the same stride assertion, and 8 of 8 pass here.fullgraph=True, andfullgraph=True, dynamic=True: 0 graph breaks, and the output is bit-identical tomain(eager and compiled).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.