Skip to content

Studio sd.cpp: keep text-encoder flash attention on (per-module --flash-attn instead of --diffusion-fa) - #18787

Merged
danielhanchen merged 3 commits into
mainfrom
nov21-sdcpp-te-fa
Nov 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
nov21-sdcpp-te-fa

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Studio's sd.cpp speed and offload flags send --diffusion-fa (native_speed_flags and offload_flags in sd_cpp_args.py). The pinned build, master-982-0518b34-u27b9ddc, now treats that flag as a deprecated alias:

--diffusion-fa    deprecated; use --flash-attn off,diffusion=on

So the flag Studio adds for speed turns text-encoder flash attention off. Without it, the binary's own auto would turn it on for CPU, CUDA MMA and Metal. An LLM text encoder (Qwen3, Qwen3-VL, Mistral) then builds the full [heads, n, n] F32 score matrix for every layer. On CPU that costs time. On Vulkan, once the matrix passes the 4 GiB storage-buffer limit, the op falls back to the CPU. That is leejet/stable-diffusion.cpp #1986: Qwen3-VL-32B at about 4.2k tokens on an RX 7900 XTX, fixed upstream by #1987, which only helps when flash attention is actually on for the text encoder.

Change

  • On builds whose --help documents per-module --flash-attn assignments (te=on|off), the managed flags now carry --flash-attn off,diffusion=on,te=auto in place of --diffusion-fa.
  • The diffusion model keeps flash attention forced on, as before. The text encoder goes back to the binary's own per-backend auto: on for CPU, CUDA and HIP with MMA, Metal and Vulkan on NVIDIA RTX, and decided by its timed probe elsewhere, so no backend runs a text-encoder kernel its own probe rejected. The VAE keeps it off, as --diffusion-fa had it, so VAE output is unchanged.
  • The rewrite runs at command assembly in the sd-cli image, sd-cli video and sd-server builders, and on the speed flags in SdCppEngine.generate. One --flash-attn is emitted even when both the offload and speed lists asked for --diffusion-fa.
  • The memory planners and tests that read the offload lists still see --diffusion-fa.
  • A user's own extra args come last and win, as before (sd.cpp's parser is last-wins).
  • Older builds without the module syntax, or a binary whose help cannot be read, keep --diffusion-fa. The help probe is cached per binary path, size and mtime, the same way as the existing --conv-direct probe.

Tests

New tests/test_sd_cpp_flash_attn_modules.py (CPU), using the pinned build's own help text. It covers:

  • the server, sd-cli image and sd-cli video commands
  • offload plus speed producing a single --flash-attn
  • the engine path with a user override
  • builds without the module syntax
  • unknown binaries

test_differential.py: DISCRIMINATING. 4 tests fail on main and pass here, 1 is new API, 1 is unchanged, head_fail=0. The sd.cpp and H3 native suites pass: 2123 passed, 6 skipped. Detection was checked against the real pinned Linux sd-cli binary.

Results

The timings below were taken with te=on. On the CPU backend the binary's auto resolves to on, so te=auto runs the same kernel there.

The CPU of a Colab L4 VM: Xeon @ 2.20 GHz (Cascade Lake), 6 cores. The pinned release's Linux CPU sd-cli was used for both arms. Z-Image Turbo Q4_K_M with the Qwen3-4B Q8_0 encoder, 512 px, 1 step, seed 42. The only difference between the arms is the flag. Arms were interleaved over 2 rounds, and both rounds of each arm matched to within 0.3 s.

Prompt Encode, --diffusion-fa Encode, --flash-attn off,diffusion=on,te=on Total
~900 tokens 14.74 / 14.45 s 9.99 / 10.25 s (1.45x) 74.2 -> 69.3 s
~3000 tokens 78.39 / 78.60 s 37.31 / 37.11 s (2.11x) 189.8 -> 148.7 s (1.28x)
  • Peak RSS was the same: 8.6 to 8.7 GB, dominated by the weights.
  • Each flag's repeat render was pixel-identical.
  • Renders with the two flags differ only by flash-attention rounding in the encoder: PSNR 32.2 dB at ~900 tokens and 36.5 dB at ~3000 tokens, visually the same image.

ARM CPUs (GitHub runners, relative timing; source builds of the same pin; FLUX.2 klein 4B Q4_0 + Qwen3-4B Q4_0, 256 px, 1 step; medians of 3 rotated rounds). Text-encode seconds:

Runner --diffusion-fa module flag
ubuntu-24.04-arm (Neoverse N2, 4 cores) 7.86 5.85 (1.34x)
windows-11-arm 37.35 35.66 (1.05x)
macos-15 (M1 virtual, 3 vCPU) 17.62 16.70 (a tie: run-to-run spread +-3-4 s)

Not proven here

  • Vulkan was not measured: the Colab L4 container has no working NVIDIA Vulkan ICD (vkCreateInstance: ERROR_INCOMPATIBLE_DRIVER). The 4 GiB fallback is upstream's report, not measured here.
  • Metal was not timed. The macOS runners' paravirtual GPU has no Metal flash attention and Studio places the graph on the CPU there. On real Apple GPUs the binary's own auto already picks flash attention for the encoder, which this restores.
  • No LPIPS against a non-FA reference. The encoder change is FA rounding only.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T20:54:33.552192Z 7694e86 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: f1289a95cb

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

…n auto (te=auto) instead of forcing it on

[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. Hooray!

Reviewed commit: 7694e86ef4

ℹ️ 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 c15f8e1 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the nov21-sdcpp-te-fa branch November 22, 2026 01:58
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