Skip to content

sycl : coalesce MKL-FA softmax loads instead of one work-item per row - #28918

Merged
ggerganov merged 2 commits into
ggml-org:masterfrom
anantshri:sycl-mkl-fa-coalesced-softmax
Sep 21, 2026
Merged

ggerganov merged 2 commits into
ggml-org:masterfrom
anantshri:sycl-mkl-fa-coalesced-softmax

Conversation

@anantshri

@anantshri anantshri commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This pull request is part of multiple pull requests (subsequent pull requests will refer to this one once this is accepted) focused on performance improvement of llama.cpp for the SYCL. This specific PR focuses on the MKL flash-attention path (fattn-mkl.cpp, added in #25025). We identified a scenario where duringdeep prefull (64K to 128K tokens) the kernel was spending about 70-75% time in flash attention. so we focused on optimizing them.

It appears that the kernel assigns one work-item per query row, and each item walks the whole KV chunk (8192 elements) serially, twice: once for the max, once for the exp pass. With a 256-item work-group, adjacent items read addresses 32 KB apart, so their loads never merge into shared memory transactions. Reducing the bandwidth to 32 GB/s effective on a 608 GB/s Arc Pro B70.

The rewrite gives each query row a whole work-group:

  • items stride the chunk (for i = lid; i < chunk_size; i += ls), so adjacent items touch adjacent elements on both passes
  • row max and sum come from sycl::reduce_over_group
  • the DV-long VKQ rescale is spread across the items
  • item 0 remains the only writer of KQ_max and KQ_sum; the second group reduction orders those writes after every other item's reads

Measurements

Device and software specification

  • Intel Arc Pro B70 (BMG, 32 GB),
  • oneAPI DPC++/C++ 2026.1.1,
  • Level-Zero 1.32.0,
  • kernel 7.1.8.
  • Qwen3.8-27B UD-Q4_K_XL,

llama-bench -fa on -b 4096 -ub 1024 -ngl 999, single rep, kernel-level split via GGML_SYCL_MKL_FA_DEBUG=1.

Prompt processing, tok/s:

prefill master F16 patch F16 master q8_0 patch q8_0
8 960 1010.67 1081.19 1003.78 1075.20
64 000 586.10 781.59 586.78 783.72
126 976 402.99 601.70 403.75 602.20

That is +7 %, +33 %, +49 % top to bottom, same shape for both KV dtypes.

Redoing the profiling confirmed change only affected the intended section

component master patch
dequant 72 ms 73 ms
GEMM_KQ 288 ms 287 ms
softmax (this kernel) 2407 ms 440 ms (5.5×)
GEMM_VKQ 212 ms 201 ms

Every untouched component is flat; only the rewritten kernel moves.

Notes

  • clang-format 23 violations on fattn-mkl.cpp go down with the patch: 537 to 503.

Requirements

  • I have read the contribution guidelines (CONTRIBUTING.md)
  • AI usage disclosure:: Yes

Claude Code with GLM 5.3 was used to profile the kernel, identify the uncoalesced access pattern, and draft the implementation.

@anantshri
anantshri requested a review from a team as a code owner September 14, 2026 22:25
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language labels Sep 14, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

Hi @anantshri, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@anantshri

Copy link
Copy Markdown
Contributor Author

Details updated, it seems there are some checks failing but those dont seem to be coz of my changes as my changes dont touch that path so i will leave them be for now.

@arthw

arthw commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

@anantshri
Don't worry about them!

Comment thread ggml/src/ggml-sycl/fattn-mkl.cpp Outdated
Comment on lines +129 to +141
const int64_t wg = ((q_rows + wg_size - 1) / wg_size) * wg_size;

const int ls = (int)wg_size;
// Exactly q_rows groups; every group id < q_rows, no tail guard needed.
stream->submit([&](sycl::handler & cgh) {
cgh.parallel_for(sycl::nd_range<1>(wg, wg_size),
cgh.parallel_for(
sycl::nd_range<1>(sycl::range<1>((size_t)q_rows * ls),
sycl::range<1>(ls)),

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.

This part of code update has no impact to the performance.
The new var name is too short, make it hard to understand.
const int64_t wg = ((q_rows + wg_size - 1) / wg_size) * wg_size; is better than using q_rows directly.

Suggest rollbacking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please see the comment on the pull request. Rollback seems to be giving negative result at my end.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have addressed the variable name concerns.

@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 have tested this PR.
It has perf increase.
The code logic is OK.

The only comments are about var naming.
Because the code is still maintained by human being, we hope the var naming will help developer to understand the code.
Too short name is not recommanded.

Thank you!

Comment thread ggml/src/ggml-sycl/fattn-mkl.cpp Resolved
Comment thread ggml/src/ggml-sycl/fattn-mkl.cpp Resolved
Comment thread ggml/src/ggml-sycl/fattn-mkl.cpp Resolved
@anantshri

Copy link
Copy Markdown
Contributor Author

@arthw Naming accepted everywhere.

The formula rollback is giving negative result. I have shaped it as per other similar entries.

I think the important bit is that the old rounded-up form was counting global items for the previous one-item-per-row mapping. This kernel works differently. It maps one work-group per row using row = item.get_group(0), so what we actually need is exactly q_rows work-groups. That makes the correct global work size q_rows * wg_size.

Took me some time coz i didnt wanted to discount it as a theoretical argument. I tested the rollback. With the old formula the dispatch count stays the same, but pp64000 drops from around 650 to 315 tok/s. More importantly, greedy generation after a 2.4k-token prompt starts degrading into NaN soup because only 2 of the 512 rows are getting processed on each call.

The current launch line also matches the shape used by the other kernels in this file. Does that works better now.

@NeoZhangJianyu

Copy link
Copy Markdown
Contributor

@arthw Naming accepted everywhere.

The formula rollback is giving negative result. I have shaped it as per other similar entries.

I think the important bit is that the old rounded-up form was counting global items for the previous one-item-per-row mapping. This kernel works differently. It maps one work-group per row using row = item.get_group(0), so what we actually need is exactly q_rows work-groups. That makes the correct global work size q_rows * wg_size.

Took me some time coz i didnt wanted to discount it as a theoretical argument. I tested the rollback. With the old formula the dispatch count stays the same, but pp64000 drops from around 650 to 315 tok/s. More importantly, greedy generation after a 2.4k-token prompt starts degrading into NaN soup because only 2 of the 512 rows are getting processed on each call.

The current launch line also matches the shape used by the other kernels in this file. Does that works better now.

Yes, I agree!
Please keep your original code.

Thank you!

@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.

OK, it's good job!

Thank you!

@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 Sep 19, 2026
@KellenRenshaw

Copy link
Copy Markdown

Did some testing of this PR on an Arc B50. Results are below:

With GGML_SYCL_ENABLE_VMM: 0

master at 1af554f

model size params backend ngl sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 3531.25 ± 15.92
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 103.23 ± 0.32
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 4875.97 ± 51.21
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 35.50 ± 0.04

build: 1af554f (11053)

test at 2b04f4706

model size params backend ngl sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 3507.91 ± 7.01
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 103.15 ± 0.28
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 4831.85 ± 64.62
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 35.43 ± 0.04

build: 2b04f4706 (11055)

With GGML_SYCL_ENABLE_VMM: 1

master at 1af554f

model size params backend ngl sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 3637.58 ± 22.81
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 103.04 ± 0.17
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 4823.43 ± 41.32
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 35.41 ± 0.02

build: 1af554f (11053)

test at 2b04f4706

model size params backend ngl sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 3639.44 ± 19.75
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 102.87 ± 0.20
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 pp512 4817.62 ± 28.91
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 none SYCL0 tg128 35.39 ± 0.03

build: 2b04f4706 (11055)

@KellenRenshaw

Copy link
Copy Markdown

Saw that my testing wasn't at the same prefill length, so I tested the first 2 options with the prior settings.

master at 1af554f

model size params backend ngl n_batch n_ubatch sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp8960 3291.81 ± 6.01
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 103.09 ± 0.20
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp8960 3818.97 ± 2.75
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 35.33 ± 0.03

build: 1af554f (11053)

test at 2b04f4706

model size params backend ngl n_batch n_ubatch sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp8960 3260.10 ± 2.43
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 102.67 ± 0.21
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp8960 3791.80 ± 2.05
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 35.31 ± 0.01

build: 2b04f4706 (11055)

master at 1af554f

model size params backend ngl n_batch n_ubatch sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp64000 1293.84 ± 3.44
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 102.67 ± 0.23
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp64000 1364.50 ± 0.83
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 35.34 ± 0.01

build: 1af554f (11053)

test at 2b04f4706

model size params backend ngl n_batch n_ubatch sm dev test t/s
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp64000 1290.25 ± 0.60
llama ?B Q4_K - Medium 1.45 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 102.55 ± 0.22
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 pp64000 1364.98 ± 0.38
llama ?B F16 4.69 GiB 2.52 B SYCL,Vulkan -1 4096 1024 none SYCL0 tg128 35.32 ± 0.02

build: 2b04f4706 (11055)

@ggerganov
ggerganov merged commit 1aa2954 into ggml-org:master Sep 21, 2026
29 of 31 checks passed
@BrewTestBot BrewTestBot mentioned this pull request Sep 23, 2026
1 task done
LadislavSopko pushed a commit to 0ics-srls/llama.cpp that referenced this pull request Oct 5, 2026
…ggml-org#28918)

* sycl : coalesce MKL-FA softmax loads instead of one work-item per row

* better human readable variable name
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 7, 2026
…ggml-org#28918)

* sycl : coalesce MKL-FA softmax loads instead of one work-item per row

* better human readable variable name

(cherry picked from commit 1aa2954)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants