Repository navigation
VAE attention: query-chunk the unmasked fallback so Vulkan keeps it on the GPU - #113
danielhanchen wants to merge 5 commits into
Conversation
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.
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. |
|
Codex Review: Didn't find any major issues. Another round soon, 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
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'ssupports_oprejects 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 showsvae compute buffer size: 5404.51 MB(RAM) on CPUat 1536 and16776.01 MB(RAM) on CPUat 2048.This branch has two commits on top of
0c69df2:402e3935splits 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 countsq->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.d934c558asks the backend about the chunk before using it. It builds metadata-only copies of the chunk's score matmul, softmax and output matmul and callsggml_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 =0c69df2with 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.Buffers are in MB, taken from the
vae compute buffer sizelog 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 GiBmaxBufferSizeof 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
supports_opprobe 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.