Skip to content

CUDA: sparse FlashAttention on Volta - #33848

Open
PabloSoage wants to merge 1 commit into
ggml-org:masterfrom
PabloSoage:volta-sparse-fa
Open

PabloSoage wants to merge 1 commit into
ggml-org:masterfrom
PabloSoage:volta-sparse-fa

Conversation

@PabloSoage

Copy link
Copy Markdown

Overview

This PR enables the sparse FlashAttention path (n_kv_max, used by the DSA layers of GLM-5.x and DeepSeek-V4) on Volta, both for prompt processing and for token generation. On master the path is gated to Turing and newer, so on V100 these models attend densely over the whole KV cache and slow down with context length.

return ((GGML_CUDA_CC_IS_NVIDIA(cc) && turing_mma_available(cc)) || amd_mfma_available(cc)) && dst->src[5] == nullptr &&

The Volta MMA kernel has no device code below 32 columns per CTA, so the existing ncols1 = 1, ncols2 = 8 sparse variant cannot run there:

if (ncols1*ncols2 < 32) {
NO_DEVICE_CODE;
return;
}
#endif // VOLTA_MMA_AVAILABLE

Instead of a new ncols2 = 32 instance (the approach of #28887), this keeps ncols2 = 8, which switch_ncols2 already picks on Volta for 512-wide heads, and builds the sparse gather at ncols1 = 4 and 8 (32 and 64 columns). Batches of up to 4 queries use the 4x8 variant, so token generation also gathers only the selected rows instead of running a dense tile pass. The mask-to-indices kernel gets a group of 4 for this.

Additional information

Kernel time on a V100-SXM2-16GB (CUDA 12.9), GLM-5.3-Flash shape: 512-wide heads, 64 Q heads per K head, V a view of K, 2048 selected keys. Measured with test-backend-ops perf and temporary perf cases (not part of this PR).

KV batch master PR speedup
32768 1 520 us 497 us 1.04x
32768 512 112.0 ms 60.0 ms 1.9x
131072 1 2.03 ms 0.67 ms 3.0x
131072 512 491.4 ms 74.2 ms 6.6x
262144 1 4.06 ms 0.89 ms 4.6x
262144 512 1014.8 ms 78.2 ms 13.0x
End-to-end numbers and tests

End to end, GLM-5.3-Flash UD-IQ3_XXS on 4x V100 (layer split, experts partly in host RAM), llama-bench -p 2048 -n 64 with the context already filled. Measured on an older base (59b7165) with the same change:

depth pp master pp PR tg master tg PR
128K 60 156 7.7 9.7
254K 36 135 6.7 9.0

Tests:

  • test-backend-ops -o FLASH_ATTN_EXT on V100: 4502/4502 passed. With a temporary debug print, the 4 new cases take the sparse path (4x8 for batch 1 and 3, 8x8 for batch 64 and 512).
  • test-backend-ops -o FLASH_ATTN_EXT on RTX 5070 Ti (sm_120) and RTX 2060 SUPER (sm_75): 4647/4647 passed on both after rebasing on 7258196, no change on non-Volta paths.
  • The new cases use KV 8192 with n_kv_max = 512, the smallest size where shall_use_sparse accepts both variants, to keep the CPU reference cheap.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude (Anthropic) helped write and debug the kernel routing and the tests, and ran the benchmarks. I reviewed the change and the measurements and I am responsible for it.

Written with Claude Opus 5.5

Assisted-by: Claude Opus 5.5
@PabloSoage
PabloSoage requested review from a team and ggerganov as code owners December 6, 2026 00:50
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant