CUDA: enable sparse FlashAttention on Volta - #28887
Closed
CoREse wants to merge 1 commit into
Closed
Conversation
The n_kv_max sparse path was gated behind turing_mma_available(), so on Volta DeepSeek-V4's CSA layers attended over the whole compressed KV cache even though the mask keeps only min(n_swa, 128) + top_k(512) = 640 entries per query row. Sparse gathers one index row per launch (ncols1 == 1) and the Volta build has no device code below 32 columns, so the only viable instance is <512, 512, 1, 32>: all GQA heads of one query row in a single launch. Add that instance, allow it in may_use_sparse(), route Volta shapes with gqa_ratio % 32 == 0 to it, and prefer it over the dense tile kernel for single-token decode. test-backend-ops on V100: 3 new DeepSeek-V4 CSA cases pass, rest unchanged. kv=32768: 14.6x for a 256-token batch, 6.1x for one token. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FBqdxhtLYCXWCqbGx6LJuh
|
Hi @CoREse, 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sparse flash attention (the
n_kv_maxpath added in #27970) is currently gated behindturing_mma_available(), so on Volta it never runs: DeepSeek-V4's CSA layers attend over the whole compressed KV cache even though the mask only keepsmin(n_swa, 128) + top_k(512) = 640entries per query row. This PR enables the sparse path on Volta, where it is worth up to 14x for prompt processing and 6x for token generation at long context.Two constraints have to be satisfied at once:
ncols1 == 1;fattn-mma-f16.cuh:#ifdef VOLTA_MMA_AVAILABLE if (ncols1*ncols2 < 32) NO_DEVICE_CODE), so the existing sparse instances (<512, 512, 1, 8>,<576, 512, 1, 16>) abort there.The only viable combination is
<512, 512, 1, 32>: all GQA heads of one query row in a single 32-column launch. DeepSeek-V4 has 64 query heads on 1 KV head, so its CSA layers split into two such launches per query row. The instance is added to the existingncols1_1-ncols2_32file (which already carries 320/256 and 576/512).Changes:
ggml_cuda_fattn_volta_sparse_ok(): allow sparse on Volta for DKQ == DV == 512 withgqa_ratio % 32 == 0; the rest of the sparse preconditions are unchanged.ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(): route such shapes to<DKQ, DV, 1, 32>on Volta.ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1(): keep Volta out of the narrower sparse instances.ggml_cuda_get_best_fattn_kernel(): for a single token, prefer the sparse MMA launch over the dense tile kernel (which reads all of K for every token).shall_use_sparse()into acc-based helper so the kernel selection can use it too.may_use_sparse(), extern declaration, instance file andgenerate_cu_files.pyupdated for the new instance; onlyncols1 == 1is generated for 512/32.Test
test-backend-ops, V100 (sm_70), CUDA 12.9:n_kv_max = 640, with V both separate and a view of K, batch 32 and 1) — pass.-o FLASH_ATTN_EXTotherwise unchanged: 2874/2874 pass. (Two pre-existing Volta failures are excluded and are unrelated to this PR —hsk=320aborts incudaFuncSetAttributewith "invalid argument", andmax_bias=8hits "no device code for arch 700" because ALiBi disables the GQA optimization and lands on a 16-column instance. Both reproduce on master.)test-backend-ops perf, V100, DeepSeek-V4 CSA shape (hsk=hsv=512, 64 query heads on 1 KV head,n_kv_max=640):End to end, DeepSeek-V4-Flash (UD-Q8_K_XL, 4x V100-16GB, experts on CPU), 54,564-token prompt:
i.e. the attention cost stops growing with context length; what is left is the (CPU-side) expert matmul.
Quality,
llama-perplexity --kl-divergence, wikitext-2,-c 32768 --chunks 2(the scored half of each chunk is past the point where the sparse path engages):<512, 512, 1, 32>instance without the sparse gather gives KLD 0.000000 / 100% agreement, and an instrumented compaction kernel confirms no row ever has more unmasked entries thann_kv_max, so nothing is dropped. The deviation is the model's own sensitivity to summation order in its top-k routing: an exact-f32, mathematically equivalent reordering elsewhere in the graph produces the same 0.0043. For reference, Q8_0 -> Q4_K on this model is KLD 0.0102.Notes
cc == GGML_CUDA_CC_VOLTA.🤖 Generated with Claude Code
https://claude.ai/code/session_01FBqdxhtLYCXWCqbGx6LJuh