Repository navigation
sycl : coalesce MKL-FA softmax loads instead of one work-item per row - #28918
Conversation
|
Hi @anantshri, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
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. |
|
@anantshri |
| 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)), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Please see the comment on the pull request. Rollback seems to be giving negative result at my end.
There was a problem hiding this comment.
I have addressed the variable name concerns.
arthw
left a comment
There was a problem hiding this comment.
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!
|
@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! Thank you! |
arthw
left a comment
There was a problem hiding this comment.
OK, it's good job!
Thank you!
|
Did some testing of this PR on an Arc B50. Results are below: With GGML_SYCL_ENABLE_VMM: 0 master at 1af554f
build: 1af554f (11053) test at 2b04f4706
build: 2b04f4706 (11055) With GGML_SYCL_ENABLE_VMM: 1 master at 1af554f
build: 1af554f (11053) test at 2b04f4706
build: 2b04f4706 (11055) |
|
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
build: 1af554f (11053) test at 2b04f4706
build: 2b04f4706 (11055) master at 1af554f
build: 1af554f (11053) test at 2b04f4706
build: 2b04f4706 (11055) |
…ggml-org#28918) * sycl : coalesce MKL-FA softmax loads instead of one work-item per row * better human readable variable name
…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)
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:
for i = lid; i < chunk_size; i += ls), so adjacent items touch adjacent elements on both passessycl::reduce_over_groupMeasurements
Device and software specification
llama-bench -fa on -b 4096 -ub 1024 -ngl 999, single rep, kernel-level split viaGGML_SYCL_MKL_FA_DEBUG=1.Prompt processing, tok/s:
That is +7 %, +33 %, +49 % top to bottom, same shape for both KV dtypes.
Redoing the profiling confirmed change only affected the intended section
Every untouched component is flat; only the rewritten kernel moves.
Notes
fattn-mkl.cppgo down with the patch: 537 to 503.Requirements
Claude Code with GLM 5.3 was used to profile the kernel, identify the uncoalesced access pattern, and draft the implementation.