Skip to content

sycl: route BMG D=256 single-query decode to the FA vector kernel - #30606

Merged
Titaniumtown merged 2 commits into
ggml-org:masterfrom
anantshri:sycl-bmg-vec-decode
Nov 26, 2026
Merged

Titaniumtown merged 2 commits into
ggml-org:masterfrom
anantshri:sycl-bmg-vec-decode

Conversation

@anantshri

Copy link
Copy Markdown
Contributor

Summary

  • on Battlemage (bmg_g21/g31) with head dim 256, the FA vector kernel beats the dispatcher's default choice — the GQA-optimized tile config — for masked single-query decode: 832 vs 1150 us per call at kv=65536, GQA4 (op-level twins, Arc Pro B70)
  • route Q->ne[1] == 1, non-quantized-KV, max_bias == 0 decode to BEST_FATTN_KERNEL_VEC on those parts/head sizes only; every other arch, head size, and query count keeps the existing preference
  • drop the now redundant K->ne[1] % FATTN_KQ_STRIDE gate from gqa_opt_applies: the GQA-optimized TILE variant iterates its trailing chunk out-of-bounds-checked, so n_kv no longer needs to be a multiple of FATTN_KQ_STRIDE — hygiene, not perf (see marker 3)
  • extends test_flash_attn_ext coverage for D=256 at aligned and unaligned kv (1024/1025/5003/16383/16384/32767/32768/65535/65536, GQA4 and GQA6, batch 1/2/5) so both the gate drop and the decode routing are regression-covered

Performance

Arc Pro B70, llama-bench -fa 1, 2 rounds in reversed arm order:

cell before after Δ
Spark-X2.5-4B BF16 tg @0 / @8k / @64k 52.05 / 47.93 / 33.68 52.91 / 49.84 / 37.68 +1.7 / +4.0 / +11.9%
Spark-4B-Uncensored Q8_0 tg @0 / @8k / @64k 98.11 / 84.65 / 48.49 101.73 / 90.68 / 57.14 +3.7 / +7.1 / +17.8%
Qwen3.8-27B Q4_K_XL tg128 @32k 18.59 20.58 +10.7%

Prefill unchanged by construction (the dispatch edit is Q->ne[1] == 1 only); tail-arm pp cells measured flat (7603/7326/4859 vs 7603/7323/4853).

How this was found (markers)

  1. Op-level twins: max_bias=0 vs max_bias=8 at hsk=256,nh=4,nr23=[4,1],kv=65536,nb=1 on the same binary → 1150 µs (tile) vs 832 µs (vector) — the marker that located the win.
  2. Control: forcing use_gqa_opt = false everywhere collapses deep decode 3× (Spark BF16 d64k: 33.7 → 11.7 tok/s) — the tile config still wins at its shapes; this change is shape-scoped, not a general preference flip.
  3. Refuted premise: the n_kv % FATTN_KQ_STRIDE alignment gate essentially never blocks in practice — llama.cpp pads FA-visible KV to 256-token granularity (live dispatch instrumentation shows only n_kv ∈ {256, 1024, 65536, 65792}). An end-to-end A/B of just the tail/gate change was flat (±0.9%); the gate drop rides along as dead-condition cleanup.

Verification

  • FA op suite (SYCL0): 4073/4085 — the 12 non-green are pre-existing on the production binary (2 failures) plus 10 known ≥32k-KV tolerance overages, identical across both kernels
  • perplexity (wikitext, 4×8192): 14.7975 ± 0.37670 — byte-identical between arms
  • greedy parity (5 prompts × 96 tok, temp 0): 4/5 byte-identical; the 5th differs only in whitespace
  • Qwen3.8-27B Q4_K_XL (D=256, GQA4) is the third end-to-end cell — the change is not Spark-specific

Requirements

  • I have read the contributing guidelines: yes
  • AI usage disclosure: yes — the op-level probe, dispatch instrumentation, and measurements were developed with AI assistance (GLM 5.3, running in opencode); all numbers are from real hardware runs and raw JSONL is available on request.

@anantshri
anantshri marked this pull request as ready for review October 15, 2026 14:49
@anantshri
anantshri requested review from a team and ggerganov as code owners October 15, 2026 14:49
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language labels Oct 15, 2026

@arthw arthw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I test on B60.
There is not perf increasing:

GGUF Test fa Base t/s Primary t/s Increase Rate (Primary vs Base)
Qwen3.8-27B-UD-Q4_K_XL.gguf pp8192 0 188.77 188.76 -0.01%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp8192 1 190.99 190.99 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp2048 0 192.29 192.28 -0.01%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp2048 1 192.67 192.66 -0.01%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp1024 0 193.09 193.09 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp1024 1 192.82 192.82 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp32 0 45.66 45.68 0.04%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp32 1 44.29 44.30 0.02%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp16 0 23.91 23.91 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf pp16 1 23.17 23.17 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf tg128 0 15.90 15.90 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf tg128 1 15.91 15.98 0.44%

Could you share the whole test cmd?

Thank you!

Comment thread ggml/src/ggml-sycl/fattn-tile.hpp Resolved
@anantshri

Copy link
Copy Markdown
Contributor Author

Hey @arthw : Thanks for testing on B60 — those numbers are exactly what this change predicts, because none of the cells in that matrix hit the case this PR touches.

The condition, in plain terms. This PR changes exactly one dispatch decision: generating tokens one at a time (decode, Q->ne[1] == 1) with flash attention on, on a Battlemage GPU (bmg_g21/g31 - B60, B70, B580), with head dim 256 and unquantized KV. In that case decode runs on the FA vector kernel instead of the GQA-optimized tile kernel. The gain scales with how full the KV cache is: nothing at a few hundred tokens, +10..18% at 32k–64k. Everything else - all prefill, shallow contexts, other head sizes, quantized KV, non-Battlemage GPUs — takes the same code path as before, so flat numbers there are the expected result.

Why each of your cells is flat:

  • pp (all sizes, fa 0/1): prefill processes many query rows at once; the change is gated on exactly one query row. Untouched by construction — your ±0.01% matches the flat pp cells we measured on B70 too.
  • tg128 without -d: llama-bench starts generating from an empty KV cache, and 128 generated tokens never fill it — so the timed tokens attend over a few hundred entries at most, where the two kernels are equivalent. Your fa=1 +0.44% is the small leading edge of the effect. The routing itself is active on your card (B60 is bmg_g21, same as the B70).

What we measured, model by model (Arc Pro B70, single GPU, -fa 1, f16 KV cache, llama-bench, 2 rounds with arm order reversed between rounds; @N = KV depth at the start of the timed generation, set by -d N):

Model Quant Test base t/s branch t/s Δ
Spark-X2.5-4B BF16 tg256 @0 52.05 52.91 +1.7%
Spark-X2.5-4B BF16 tg256 @8k 47.93 49.84 +4.0%
Spark-X2.5-4B BF16 tg256 @64k 33.68 37.68 +11.9%
Spark-X2.5-4B-Uncensored Q8_0 tg256 @0 98.11 101.73 +3.7%
Spark-X2.5-4B-Uncensored Q8_0 tg256 @8k 84.65 90.68 +7.1%
Spark-X2.5-4B-Uncensored Q8_0 tg256 @64k 48.49 57.14 +17.8%
Qwen3.8-27B UD-Q4_K_XL tg128 @32k 18.59 20.58 +10.7%
your B60 run — Qwen3.8-27B UD-Q4_K_XL pp16–8192, fa 0/1 — — ±0.01% (expected)
your B60 run — Qwen3.8-27B UD-Q4_K_XL tg128 @0 (no -d), fa 1 — — +0.44% (your number; expected — shallow KV)

pp cells on B70 were also flat by construction (7603/7326/4859 vs 7603/7323/4853 t/s at the tail-arm pp sizes). All three models have head dim 256 with 4 KV heads (Spark: 16 query heads → GQA4; Qwen3.8: 24 query heads → GQA6 — verified from the GGUF metadata) — the shapes the routing targets.

The exact commands behind the table:

# Spark BF16 — tg @0 / @8k / @64k
llama-bench -m Spark-X2.5-4B.gguf -t 6 -ngl 999 -fa 1 -b 2048 -ub 2048 -p 0 -n 256 -d 0,8192,64000

# Spark Uncensored Q8_0 — same three depths
llama-bench -m Spark-X2.5-4B-Uncensored-Q8_0.gguf -t 6 -ngl 999 -fa 1 -b 2048 -ub 2048 -p 0 -n 256 -d 0,8192,64000

# Qwen3.8-27B — the tg128 @32k cell
llama-bench -m Qwen3.8-27B-UD-Q4_K_XL.gguf -t 6 -ngl 999 -fa 1 -b 4096 -ub 1024 -p 0 -n 128 -d 32768 -r 3

-d/--n-depth is the flag that matters: llama-bench prefills n_depth tokens into the KV cache before the timed generation (the prefilled state is cached and restored between reps), so -d 32768 means every timed token attends over ≥32k KV entries.

To reproduce on B60, the single most informative cell is:

llama-bench -m Qwen3.8-27B-UD-Q4_K_XL.gguf -ngl 99 -fa 1 -p 0 -n 128 -d 32768

base vs this branch. On B70 this went 18.59 → 20.58 tok/s; on B60 I'd expect the same direction, with the magnitude depending on how the two kernels compare on your memory subsystem.

Model sources (all public Hugging Face repos, so the exact cells are reproducible):

GGUF used Repo File
Spark-X2.5-4B (BF16) https://huggingface.co/XHToken/Spark-X2.5-4B Spark-X2.5-4B.gguf
Spark-X2.5-4B-Uncensored (Q8_0) https://huggingface.co/toandev/Spark-X2.5-4B-Uncensored-GGUF (pinned revision 8d13862e2cfa9e1e67b1f19bf17759f41bd583f2) Spark-X2.5-4B-Uncensored-Q8_0.gguf
Qwen3.8-27B (UD-Q4_K_XL) https://huggingface.co/unsloth/Qwen3.8-27B-GGUF Qwen3.8-27B-UD-Q4_K_XL.gguf

If you want to watch the routing decide live: GGML_SYCL_FA_TILE_DEBUG=1 (rewired per your inline comment) prints [FA-TILE] DKQ=.. DV=.. gqa=.. q=.. n_kv=.. gqa_opt=.. for every TILE launch. On this branch the deep-KV decode calls no longer appear in that log — they are routed to the VEC kernel. The TILE lines that remain are prefill/small batches: gqa_opt=1 only when the batch is small (Q->ne[1] <= 16 at this GQA ratio/head size), larger prefill batches print gqa_opt=0 since the GQA-optimized variant is capped at 16 query rows.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Oct 20, 2026
@anantshri
anantshri requested a review from arthw October 21, 2026 19:23

@arthw arthw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good job!

On B60, I get perf increasing with "-fa 1 -p 0 -n 128 -d 32768":

GGUF Test fa Base t/s Primary t/s Increase Rate (Primary vs Base)
Qwen3.8-27B-UD-Q4_K_XL.gguf tg128 0 10.44 10.44 0.00%
Qwen3.8-27B-UD-Q4_K_XL.gguf tg128 1 11.19 12.21 9.12%

Thank you!

Comment thread tests/test-backend-ops.cpp Resolved
@anantshri
anantshri force-pushed the sycl-bmg-vec-decode branch from 1650840 to c514a38 Compare November 3, 2026 03:58
@arthw

arthw commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

@anantshri
Could you resolve the conficts?

Thank you!

@anantshri

Copy link
Copy Markdown
Contributor Author
  1. rebased to master
  2. resolved conflict

over to you @arthw @Titaniumtown

@arthw arthw added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 9, 2026
@Titaniumtown

Copy link
Copy Markdown
Contributor

@arthw we should wait on @ggerganov to review the test changes

@Titaniumtown Titaniumtown removed the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 9, 2026
@arthw

arthw commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@Titaniumtown
Yes, this PR need @ggerganov to review and approve before merge.
I add "merge ready" label to notify him. :)

@Titaniumtown Titaniumtown added merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. and removed merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. labels Nov 17, 2026
@Titaniumtown

Copy link
Copy Markdown
Contributor

@anantshri can you please rebase? Thank you!

@anantshri

Copy link
Copy Markdown
Contributor Author

@Titaniumtown @arthw Rebased.

@arthw arthw added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 18, 2026
Comment thread tests/test-backend-ops.cpp Resolved
@ggerganov ggerganov removed the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 21, 2026
The GQA-optimized tile config has been the dispatcher's preference for
masked single-query decode since it landed. On Battlemage (bmg_g21/g31)
with head dim 256 the vector kernel is materially faster at that shape:
832 vs 1150 us per call at kv=65536, GQA4 (B70, op-level twins).

Route single-query (Q->ne[1] == 1), non-quantized-KV, max_bias == 0 decode
to BEST_FATTN_KERNEL_VEC on those parts/head sizes; every other arch, head
size, and query count keeps the existing preference. Also drop the now
redundant K->ne[1] % FATTN_KQ_STRIDE gate: the GQA-optimized TILE variant
iterates its trailing chunk out-of-bounds-checked, so n_kv no longer needs
to be a multiple of FATTN_KQ_STRIDE.

End-to-end (Arc Pro B70, llama-bench -fa 1, 2 rounds reversed order):

| cell | before | after | delta |
|---|---:|---:|---:|
| Spark-X2.5-4B BF16 tg @0 / @8k / @64k | 52.05 / 47.93 / 33.68 | 52.91 / 49.84 / 37.68 | +1.7 / +4.0 / +11.9% |
| Spark-4B-Uncensored Q8_0 tg @0 / @8k / @64k | 98.11 / 84.65 / 48.49 | 101.73 / 90.68 / 57.14 | +3.7 / +7.1 / +17.8% |
| Qwen3.8-27B Q4_K_XL tg128 @32k | 18.59 | 20.58 | +10.7% |

Prefill unchanged by construction (dispatch edit is Q->ne[1] == 1 only);
tail-arm pp cells measured flat (7603/7326/4859 vs 7603/7323/4853).

Correctness gates: FA op suite 4073/4085 — the 12 non-green are
pre-existing on the production binary (2 failures) plus 10 known >=32k-KV
tolerance overages identical across both kernels; perplexity byte-identical
between arms (14.7975 +/- 0.37670); greedy parity 4/5 byte-identical, the
5th differs only in whitespace.
Address review feedback on the FA TILE dispatch instrumentation added in
this PR: it read the env var through a static-local getenv at the call
site. Follow the GGML_SYCL_DEV_DEBUG pattern instead:

- g_ggml_sycl_fa_tile_debug is a global (extern in base.hpp, defined in
  ggml-sycl.cpp), read once in ggml_check_sycl() during backend init,
  so the per-call cost on the dispatch path is one unlikely branch: no
  lazy first-call getenv and no per-template-instantiation static guard
  (fattn-tile.hpp is instantiated once per DKQ/DV combination)
- the call site in fattn-tile.hpp uses a GGML_SYCL_FA_TILE_DEBUG()
  macro shaped like GGML_SYCL_DEV_DEBUG()
- documented in docs/backend/SYCL.md (runtime env table) and echoed in
  the init "Running with Environment Variables" log alongside the other
  debug vars
@anantshri

Copy link
Copy Markdown
Contributor Author

Tests dropped, rebased on master @Titaniumtown over to you.

@Titaniumtown
Titaniumtown merged commit a07831a into ggml-org:master Nov 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants