Skip to content

Studio: flash attention by default for FLUX.2-klein on ROCm gfx11 - #12703

Merged
danielhanchen merged 22 commits into
mainfrom
studio/amd-klein-flash-default
Oct 5, 2026
Merged

danielhanchen merged 22 commits into
mainfrom
studio/amd-klein-flash-default

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Stacked on #12701 (fused RoPE on ROCm). Merge that first.

Problem

On a Strix Halo (Radeon 8060S, gfx1151, Linux, ROCm), FLUX.2-klein-4B with #12701's fused RoPE takes 7.37, 7.40 and 7.40 s per 1024x1024 image with attention_backend=flash (DAO-AILab flash-attention, Triton AMD backend). The same runner takes 8.04 to 8.09 s on the default SDPA path, so flash is about 8.5% faster. The two outputs differ by LPIPS 0.008.

That meets the rule for turning flash on by default:

  • the gain is larger than the A/A spread of either arm;
  • LPIPS is at most 0.02.

But Studio's auto attention never picks flash on ROCm (ROCM_AUTO_FLASH = False). It only would under a speed profile, and on ROCm an unset speed resolves to off. FLUX.1 has no valid flash measurement on this runner yet.

Change

  • New rule: select_attention_backend takes the load's family. auto now picks verified ROCm flash when all of these hold:
    • the family is in ROCM_AUTO_FLASH_FAMILIES (only flux.2-klein);
    • the card's gfx arch starts with gfx11;
    • flash_attn is installed and passes the existing per-device check (_rocm_flash_attn_runs).
  • When: under a speed profile, or with the speed left unset (speed_unset, which is what the UI sends; ROCm resolves it to off).
  • Unchanged:
    • an explicit speed_mode=off;
    • an explicit native / sdpa;
    • every other family, including FLUX.1, which stays off until it is measured;
    • other ROCm arches and every NVIDIA path;
    • the video backend.
  • No new noise: whether flash_attn is installed is checked without importing it, so an auto load on a card without it logs nothing new.
  • Wiring: diffusion.py passes family and speed_unset at the load-time pick, and family at the deferred re-pick.

Results

gfx1151: Strix Halo CI runner, Radeon 8060S, Linux, torch 2.11.0+rocm7.13.0, flash_attn installed with FLASH_ATTENTION_TRITON_AMD_ENABLE=TRUE. Base is main plus #12701, head is base plus this change, same runner, same job. The pre-run check found 62.54 GiB of the pool free. 1024x1024, 3 renders per arm.

cell base s/image head s/image head s/step
klein, auto 8.00 / 8.01 / 7.99 7.30 / 7.33 / 7.32 1.51 to 1.54
klein, SDPA pinned 8.00 / 8.01 / 8.00 7.98 / 7.99 / 8.01 1.70 to 1.71
FLUX.1-schnell, auto 16.03 16.04 3.73
FLUX.1-schnell, SDPA pinned 16.06 16.03 3.71
  • klein auto: the head is 8.5% faster than pinned SDPA (ratio 0.915). Each arm's three renders sit within 0.03 s of each other.
  • Gate: passes. Base auto equals pinned SDPA, so the default only changed in the head. LPIPS of head auto vs pinned SDPA is 0.010, inside the 0.02 limit. The run's differential verdict is CONFIRMED.
  • FLUX.1 auto: stays on SDPA in the head (16.04 vs 16.03 s), as intended.

FLUX.1 stays off the list. On the base with flash requested explicitly, FLUX.1 is 8.8% faster (14.58 / 14.58 / 14.59 s against 16.00 / 15.98 / 16.00 s on SDPA, 3.35 vs 3.69 s/step). But its output moves by LPIPS 0.048 against SDPA, above the 0.02 limit, so it does not meet the bar for a default. Users can still request flash explicitly.

  • With the attention, sage gate, xformers stub, fused ROCm and backend suites: 851 passed, 11 skipped. Two mocks in test_diffusion_backend.py take the new keywords.

Not covered

  • Only FLUX.2-klein-4B was measured. klein 9B is in the same family and gets the same default unmeasured.
  • Only gfx1151 was measured. Other gfx11 cards (gfx1100, gfx1101, gfx1102) get the default on the strength of the same kernels, plus the per-device check.
  • If flash_attn is installed but built for CK without the Triton flag, the import fails. The existing one-time "flash_attn is not importable" warning then appears on a klein auto load.
  • Windows ROCm was not run.

danielhanchen and others added 5 commits October 4, 2026 02:09
FLUX.1 / FLUX.2 denoisers on ROCm now run diffusers' apply_rotary_emb as one
Triton kernel (the existing FLUX.2 fp16 kernel, bit-identical to stock) and
AdaLayerNormZero / AdaLayerNormZeroSingle / AdaLayerNormContinuous as one
LayerNorm plus modulation kernel that keeps the stock rounding chain.

Switches UNSLOTH_DIFFUSION_FUSED_ROPE and UNSLOTH_DIFFUSION_FUSED_ADALN
(auto|0|1, auto = ROCm only, so CUDA loads are unchanged). Inference only,
skipped while compiling, per-call fallback to the forward live at install.
…gfx11

On gfx1151 FLUX.2-klein-4B with the fused RoPE renders in 7.37 to 7.40 s with flash_attn against
8.04 to 8.09 s on SDPA (LPIPS 0.008). auto now selects flash for the flux.2-klein family on a gfx11
card when flash_attn is installed and passes the on-card check, under a speed profile or with the
speed left unset (ROCm resolves unset to off, so that is the default path). An explicit speed_mode
off, other families, other arches and every NVIDIA path keep their current pick.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-05T11:43:54.981149Z d09677d New commits
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-04T23:01:17.149233Z ceac9f7 PR opened
ℹ️ 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.

…eline

install_rope() only calls importlib.import_module on a module name that is
already a key of _ROPE_FNS, which is built from the fixed _MODULES tuple of
the two diffusers Flux transformer modules. No model, request or download
can choose the imported name, so the call site is safe to record.
test_diffusion_backend.py can leave Studio's compile-safe eager patch live on
diffusers' AdaLN classes. When it runs first in the same session, every
teardown here saw that forward instead of stock and failed (1 failed, 14
errors off-GPU). Remove the eager patch before capturing the stock forwards
and before each test, so the suite passes in either file order on CPU and GPU.
@danielhanchen danielhanchen self-assigned this Oct 5, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 5, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 04f6232b67

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 1106fa1071

ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 5, 2026
Base automatically changed from studio/amd-diffusion-fused-rope to main October 5, 2026 11:42
@danielhanchen
danielhanchen merged commit 9d8453e into main Oct 5, 2026
@danielhanchen
danielhanchen deleted the studio/amd-klein-flash-default branch October 5, 2026 11:44
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