Skip to content

ggml-hrx: run prompt flash attention above 2048 query rows as query chunks (engine #390) - #120

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/fa-prompt-query-chunks-v2
Oct 21, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/fa-prompt-query-chunks-v2

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

The Loom prompt kernel ggml_flash_attention_f32_f16_wmma launches at most 2048
query rows, so the matcher refused larger batches and FLASH_ATTN_EXT went to the
CPU: Qwen3.8-27B UD-Q4_K_XL llama-bench pp4096 at -ub 4096 ran at 6.5 tok/s.
The matcher now takes up to kUbatchMaxTokens and emits ceil(T / 2048) equal
chunks of query rows; each chunk binds its rows of Q, mask, gate and output and
all of K and V. New files dispatch-query-chunks.{h,cpp} (ours); two call sites
in dispatch-flash-attention.cpp. GGML_HRX_QUERY_CHUNK_TOKENS= forces smaller
chunks (test switch).

Measured on strixhalo gfx1151, performance mode, with gpu-kernels
1bit/prefill-large-m: 27B pp4096 -ub 4096 6.5 -> 588.5 tok/s (pp2048 -ub 4096 590.1).
27B llama-perplexity -c 4096 --chunks 2 against the base build at -ub 2048:
new -ub 2048 mean KLD 0.000000, same top 99.976%; with GGML_HRX_QUERY_CHUNK_TOKENS=1024
identical; =700 (chunks not 16-row aligned) mean KLD 0.000071, max 0.0023.
New -ub 4096 vs that base: mean KLD 0.00194, max 0.232, same top 97.9%,
PPL 6.7089 vs 6.7183. Explained (2026-10-21): the difference is not in attention.
At up to 2048 tokens the gate/up SwiGLU of the Q5_K/IQ4_XS layers runs
common.mul_mat_swiglu_q5_projection.symmetric_i4_q8_plane, which quantizes the activations
to symmetric int4; its matcher stops at 2048 tokens, so -ub 4096 takes the exact f16 HIP
dense SwiGLU. With that matcher disabled at -ub 2048 (GGML_HRX_DISABLE_DISPATCH=symmetric_i4)
the KLD against -ub 4096 is 0.000003 (max 0.00086, PPL 6.7097 vs 6.7089); disabling only
the q8_1 SwiGLU leaves it at 0.001946. So -ub 4096 is the more accurate of the two.

Rebased on 5dcf662 (the earlier branch 1bit/fa-prompt-query-chunks carried three commits that have since landed). Our code is in new files (dispatch-query-chunks.{h,cpp}); the hooks are two call sites in dispatch-flash-attention.cpp and the CMake list.

🤖 Generated with Claude Code

…hunks (engine #390)

The Loom prompt kernel ggml_flash_attention_f32_f16_wmma launches at most 2048
query rows, so the matcher refused larger batches and FLASH_ATTN_EXT went to the
CPU: Qwen3.8-27B UD-Q4_K_XL llama-bench pp4096 at -ub 4096 ran at 6.5 tok/s.
The matcher now takes up to kUbatchMaxTokens and emits ceil(T / 2048) equal
chunks of query rows; each chunk binds its rows of Q, mask, gate and output and
all of K and V. New files dispatch-query-chunks.{h,cpp} (ours); two call sites
in dispatch-flash-attention.cpp. GGML_HRX_QUERY_CHUNK_TOKENS=<n> forces smaller
chunks (test switch).

Measured on strixhalo gfx1151, performance mode, with gpu-kernels
1bit/prefill-large-m: 27B pp4096 -ub 4096 6.5 -> 588.5 tok/s (pp2048 -ub 4096 590.1).
27B llama-perplexity -c 4096 --chunks 2 against the base build at -ub 2048:
new -ub 2048 mean KLD 0.000000, same top 99.976%; with GGML_HRX_QUERY_CHUNK_TOKENS=1024
identical; =700 (chunks not 16-row aligned) mean KLD 0.000071, max 0.0023.
New -ub 4096 vs that base: mean KLD 0.00194, max 0.232, same top 97.9%,
PPL 6.7089 vs 6.7183. Explained (2026-10-21): the difference is not in attention.
At up to 2048 tokens the gate/up SwiGLU of the Q5_K/IQ4_XS layers runs
common.mul_mat_swiglu_q5_projection.symmetric_i4_q8_plane, which quantizes the activations
to symmetric int4; its matcher stops at 2048 tokens, so -ub 4096 takes the exact f16 HIP
dense SwiGLU. With that matcher disabled at -ub 2048 (GGML_HRX_DISABLE_DISPATCH=symmetric_i4)
the KLD against -ub 4096 is 0.000003 (max 0.00086, PPL 6.7097 vs 6.7089); disabling only
the q8_1 SwiGLU leaves it at 0.001946. So -ub 4096 is the more accurate of the two.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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