Skip to content

ggml-webgpu: improve flash_attn_vec for quantized KV at long contexts - #25956

Merged
ggerganov merged 5 commits into
ggml-org:masterfrom
yomaytk:improve-fa-webgpu
Jul 31, 2026
Merged

ggml-webgpu: improve flash_attn_vec for quantized KV at long contexts#25956
ggerganov merged 5 commits into
ggml-org:masterfrom
yomaytk:improve-fa-webgpu

Conversation

@yomaytk

@yomaytk yomaytk commented Jul 21, 2026

Copy link
Copy Markdown
Member

Overview

This PR mainly addresses two things:

  • Applies the direct path of flash_attn_vec to the quantized KV cache, which improves TG performance as shown in the following table. It significantly improves performance for the quantized KV cache, and the performance is slightly higher than with f16 KV cache.
  • Handles the direct paths for K and V separately, since they can be specified independently via -ctk and -ctv, and ggml-webgpu already handles the K and V types separately.

It also includes some refactoring of the WGSL code.

Performance

machine: M5 Max, model: Qwen3.5-35B-A3B-Q4_K_M.gguf

KV cache depth master PR speedup f16 (master)
q8_0 0 30.08 ± 0.04 30.76 ± 0.04 1.02x 30.70 ± 0.05
q8_0 16384 18.64 ± 0.01 27.66 ± 0.03 1.48x 26.91 ± 0.16
q8_0 32768 13.55 ± 0.06 24.92 ± 0.16 1.84x 24.10 ± 0.11
q4_0 0 30.60 ± 0.11 30.69 ± 0.03 1.00x 30.70 ± 0.05
q4_0 16384 20.32 ± 0.00 27.78 ± 0.02 1.37x 26.91 ± 0.16
q4_0 32768 14.82 ± 0.02 25.47 ± 0.04 1.72x 24.10 ± 0.11

Requirements

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning WebGPU labels Jul 21, 2026
@yomaytk
yomaytk marked this pull request as ready for review July 22, 2026 03:15
@yomaytk
yomaytk requested a review from a team as a code owner July 22, 2026 03:15
@yomaytk
yomaytk force-pushed the improve-fa-webgpu branch from fa637fb to 750096b Compare July 22, 2026 07:20
@@ -270,7 +269,9 @@ fn main(@builtin(workgroup_id) wg_id: vec3<u32>,
local_scores[slot] = FLOAT_MIN;
}

#ifndef KV_DIRECT
// The tile path stages K/V in shared memory so each tile can be reused across
// Q_TILE query rows. It therefore does not use the direct path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added this comment since the current implementation allows the direct path only when using subgroup matrices.

#if defined(K_DIRECT) || defined(V_DIRECT)
// Shared memory for scale factor (d) in quantized K/V. Multiple threads use the same value,
// so caching it is more efficient, even on the direct path.
var<workgroup> d_shmem: array<f32, kv_shmem_size / 32>;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I confirmed that we can improve performance by sharing only the scale factor of the quantized K/V, in the test of the PR description.

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

thanks for the improvements, sorry for the slow review. Just a couple minor questions I had.

key.common.kv_direct = decisions.use_sg_matrix && key.common.kv_direct;
key.use_sg_matrix = decisions.use_sg_matrix;
key.common.k_direct &= decisions.use_sg_matrix && key.common.k_type == GGML_TYPE_F16;
key.common.v_direct &= decisions.use_sg_matrix && key.common.k_type == GGML_TYPE_F16;

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.

should this be key.common.v_type?

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.

also this calculation for whether to use direct path seems to be in contradiction to the new ggml_webgpu_flash_attn_k/v_direct helpers?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

should this be key.common.v_type?

Thanks, yeah this should be v_type.

also this calculation for whether to use direct path seems to be in contradiction to the new ggml_webgpu_flash_attn_k/v_direct helpers?

New helpers ggml_webgpu_flash_attn_k/v_direct expands the supported type from F16 to F16/Q8_0/Q4_0, but the current subgroup matrices FA doesn’t have the direct path for quantized KV cache. So I added this additional F16 check in this condition.
But we can remove this if we implement the direct path for subgroup matrices FA with quantized KV cache, so may I follow in later?

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.

Yep that sounds good

@yomaytk

yomaytk commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

sorry for the slow review.

It's Ok, thanks for the review in busy time :)

@yomaytk
yomaytk force-pushed the improve-fa-webgpu branch from 750096b to 94aec92 Compare July 29, 2026 09:32
@yomaytk yomaytk added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 31, 2026
@ggerganov
ggerganov merged commit 8f4646a into ggml-org:master Jul 31, 2026
22 of 29 checks passed
@yomaytk
yomaytk deleted the improve-fa-webgpu branch July 31, 2026 06:12
huaxel pushed a commit to huaxel/CachyLLama that referenced this pull request Aug 2, 2026
…ggml-org#25956)

* improve fa of quantized kv cache

* Fix some bugs and some comments.

* fix v type check and some comments

* Fix build error caused by rebasing

* editorconfig checking pass
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
…ggml-org#25956)

* improve fa of quantized kv cache

* Fix some bugs and some comments.

* fix v type check and some comments

* Fix build error caused by rebasing

* editorconfig checking pass
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
…ggml-org#25956)

* improve fa of quantized kv cache

* Fix some bugs and some comments.

* fix v type check and some comments

* Fix build error caused by rebasing

* editorconfig checking pass
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. WebGPU

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants