Skip to content

VAE attention: query-chunk the unmasked fallback so Vulkan keeps it on the GPU - #113

Open
danielhanchen wants to merge 5 commits into
masterfrom
vk-vae-attn-chunk
Open

danielhanchen wants to merge 5 commits into
masterfrom
vk-vae-attn-chunk

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

With flash attention off, the unmasked attention fallback (the VAE mid block) builds one [N * n_head, L_q, L_k] F32 score matrix. For the FLUX.2 VAE that is 5.4 GB at 1536 px and 16 GiB at 2048 px. Vulkan's supports_op rejects any tensor larger than the device's max buffer size or storage buffer range, so the scheduler runs that matmul and softmax on the CPU. On a Radeon 8060S (gfx1151) the log shows vae compute buffer size: 5404.51 MB(RAM) on CPU at 1536 and 16776.01 MB(RAM) on CPU at 2048.

This branch has two commits on top of 0c69df2:

  1. 402e3935 splits the unmasked attention over query chunks of at most 2^29 score elements (2 GiB). This is a port of fix: VAE decode crash at 2048x2048 and above on CUDA/HIP leejet/stable-diffusion.cpp#2492 (MIT). The chunk size counts q->ne[2] * q->ne[3], so 4-D queries are sized correctly too. Every query row still runs the same matmul, scale, softmax and output matmul. Attention that fits in one chunk builds the same graph as before.
  2. d934c558 asks the backend about the chunk before using it. It builds metadata-only copies of the chunk's score matmul, softmax and output matmul and calls ggml_backend_supports_op, halving the chunk until all three are accepted. Backends without a per-tensor size limit (CPU, CUDA) accept the first candidate, so nothing changes for them. If no chunk size is accepted, the original chunk is kept.

Measurements

AMD CI, Radeon 8060S (gfx1151), Vulkan, one job per OS. FLUX.2 klein 4B Q4_K_M, Qwen3-4B Q4_K_M, FLUX.2 VAE, 4 steps, --flash-attn off,diffusion=on,te=auto, whole (untiled) VAE decode. VAE conv2d direct is on through the Vulkan auto rule. Base = 0c69df2 with its ggml patch series, head = base + this branch. Both are built from one tree, and they run in the order base, head, head, base for each of 2 prompts. The table gives median decode times over 4 renders per arm.

OS / driver size decode s base decode s head speedup VAE compute base VAE compute head head vs base
Linux, RADV (Mesa 25.2.8) 1024 2.11 2.12 1.00x 2560 VRAM + 2 RAM 2560 VRAM + 2 RAM identical
Linux, RADV 1536 15.02 4.96 3.03x 7929 VRAM + 5405 RAM 10161 VRAM + 4.5 RAM max 6/255, PSNR 56.2 / 56.6 dB
Linux, RADV 2048 52.85 18.55 2.85x 7680 VRAM + 16776 RAM 8848 VRAM + 11272 RAM max 7/255, PSNR 56.5 / 57.6 dB
Windows 11, AMD 26.10.1 1024 3.00 3.02 1.00x 2560 VRAM + 2 RAM 2560 VRAM + 2 RAM identical
Windows 11, AMD 1536 20.67 12.39 1.67x 4473 VRAM + 6557 RAM 6705 VRAM + 6341 RAM max 6/255, PSNR 56.5 / 57.4 dB
Windows 11, AMD 2048 58.64 23.05 2.54x 7680 VRAM + 16776 RAM 8848 VRAM + 11272 RAM max 7/255, PSNR 56.5 / 57.6 dB

Buffers are in MB, taken from the vae compute buffer size log lines. PSNR is per prompt, against the first base render. The two base renders of each size and prompt are byte-identical, and so are the two head renders. No render is black, constant or NaN.

At 1024 the attention fits in one chunk, so the graph, timing and pixels are unchanged. At 1536 on RADV the whole VAE decode now runs on the GPU: the CPU buffer drops from 5405 MB to 4.5 MB. The remaining CPU buffer at 2048 (both OSes) and at 1536 on Windows does not come from attention. Every attention chunk is at most 2 GiB, which is under both devices' limits. The sizes match the full-resolution 256-channel tensors of the last upsample block: 4 GiB at 2048, which is over RADV's 4 GiB maxBufferSize, and 2.25 GiB at 1536, which is over the 2 GiB maxBufferSize of the AMD Windows driver. That is a separate limit and this change does not touch it.

The pixel difference comes from running the attention in the Vulkan matmul and softmax kernels instead of on the CPU. The math is the same. On CPU, the query-chunked graph decodes to the same bytes as the unchunked one at 512 to 2048 px (measured earlier on the same pin).

Not covered

  • On both devices the 2^29-element cap already fits the device limits, so the supports_op probe in the second commit never had to shrink a chunk in this run. It only matters for a device with a smaller limit. That path has not been run on hardware.
  • No NVIDIA Vulkan, Intel or Apple run. The CUDA path keeps the first commit's behaviour: identical at 1024 and 1536 on an L4, and 1/255 off at 2048 (PSNR 71.9 dB).
  • I did not measure peak host memory (RSS). Buffer sizes come from the log lines only.

The unmasked full-image attention (the VAE mid block, flash attention off)
built one [N * n_head, L_q, L_k] F32 score matrix: 16 GiB at 2048x2048.
Split the queries so each chunk's score matrix stays within 2^29 elements.
Each query row's softmax and output are computed as before; attention
that fits in one chunk builds the same graph as before.

Port of leejet#2492 (MIT).
…nk op

Vulkan rejects any op whose tensor is larger than the device's max buffer
size or storage buffer range, so a score matrix over that limit made the
scheduler run the VAE mid-block matmul and softmax on the CPU (5.4 GB at
1536 px, 16.8 GB at 2048 px on Radeon 8060S). Ask the backend with
supports_op about metadata-only copies of the chunk ops and halve the
query chunk until they are accepted. Backends without a per-tensor limit
keep the existing 2^29-element chunk.
@danielhanchen danielhanchen self-assigned this Dec 3, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 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-12-03T01:44:56.104518Z 631906f 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. Another round soon, please!

Reviewed commit: 39ae553a66

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: f20fc4dd98

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 631906f9c0

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

This branch has not been deployed

No deployments
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