Repository navigation
[2/N][Attention] Enable masked MHA for sparse MLA prefills #48770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vllm-bot
merged 28 commits into
vllm-project:main
from
MatthewBonanni:fa4_masked_mha_remainder
Jul 31, 2026
Merged
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
2721e26
[Attention] Add masked MHA path for sparse MLA
MatthewBonanni e2b53b6
Fix IMA
MatthewBonanni 3008fcc
Clean up padding
MatthewBonanni b47873f
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni 15d6999
Chunked context support
MatthewBonanni a3ca0e2
Reuse global topk mask
MatthewBonanni 1a37b3c
Remove block sparse stuff
MatthewBonanni c19aa02
Add heuristic
MatthewBonanni 81ec3e4
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni 08524a0
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni 6562310
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni 512b29b
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni cfc2957
Review comment
MatthewBonanni 25e0528
Cleanup
MatthewBonanni b09ba1d
Cleanup
MatthewBonanni f17b1a7
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni 7a0c45a
Cleanup
MatthewBonanni c68f1a1
Cleanup
MatthewBonanni 8e1241a
Cleanup, break out helper
MatthewBonanni 002ed43
Preallocate topk mask buffer
MatthewBonanni 247048e
Gate workspace allocation on masked mha availability
MatthewBonanni c266472
Clean up gate and heuristic
MatthewBonanni e8f3507
Fix tests
MatthewBonanni 34a709f
update FA
MatthewBonanni 5722c83
Zero in kernel
MatthewBonanni 8d44a98
update FA
MatthewBonanni 469862e
update FA after landing
MatthewBonanni 6338c21
Merge branch 'main' into fa4_masked_mha_remainder
MatthewBonanni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
123 changes: 123 additions & 0 deletions
123
benchmarks/attention_benchmarks/configs/mla_sparse_masked_mha_vs_mqa.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Sparse MLA benchmark: forward_mha vs forward_mqa crossover plot | ||
| # | ||
| # Dense pure-prefill sweep to generate a smooth speedup curve. The 100 points | ||
| # run from q32 through q262144, approximately even on the log2 scale, and include | ||
| # routing boundaries plus extra samples around the 6k-8k crossover region. | ||
| # Dense MHA is only benchmarked through q32768 because it dominates runtime above | ||
| # that range. | ||
| # | ||
| # Usage: | ||
| # python benchmark.py --config configs/mla_sparse_mha_vs_mqa.yaml | ||
|
|
||
| mode: mha_vs_mqa | ||
|
|
||
| model: | ||
| name: "deepseek-v3" | ||
| num_layers: 60 | ||
| num_q_heads: 16 | ||
| num_kv_heads: 1 | ||
| head_dim: 576 | ||
| kv_lora_rank: 512 | ||
| qk_nope_head_dim: 128 | ||
| qk_rope_head_dim: 64 | ||
| v_head_dim: 128 | ||
| block_size: 128 | ||
| max_model_len: 262144 | ||
|
|
||
| batch_specs: | ||
| - "q32" | ||
| - "q36" | ||
| - "q40" | ||
| - "q45" | ||
| - "q50" | ||
| - "q56" | ||
| - "q62" | ||
| - "q70" | ||
| - "q78" | ||
| - "q87" | ||
| - "q97" | ||
| - "q109" | ||
| - "q122" | ||
| - "q136" | ||
| - "q152" | ||
| - "q170" | ||
| - "q190" | ||
| - "q212" | ||
| - "q237" | ||
| - "q255" | ||
| - "q256" | ||
| - "q257" | ||
| - "q265" | ||
| - "q296" | ||
| - "q331" | ||
| - "q370" | ||
| - "q413" | ||
| - "q462" | ||
| - "q516" | ||
| - "q577" | ||
| - "q645" | ||
| - "q721" | ||
| - "q806" | ||
| - "q901" | ||
| - "q1007" | ||
| - "q1125" | ||
| - "q1257" | ||
| - "q1405" | ||
| - "q1571" | ||
| - "q1756" | ||
| - "q1962" | ||
| - "q2048" | ||
| - "q2049" | ||
| - "q2193" | ||
| - "q2451" | ||
| - "q2740" | ||
| - "q3062" | ||
| - "q3422" | ||
| - "q3825" | ||
| - "q4275" | ||
| - "q4778" | ||
| - "q5340" | ||
| - "q5854" | ||
| - "q5969" | ||
| - "q6000" | ||
| - "q6144" | ||
| - "q6367" | ||
| - "q6671" | ||
| - "q6925" | ||
| - "q7168" | ||
| - "q7456" | ||
| - "q7532" | ||
| - "q8192" | ||
| - "q8193" | ||
| - "q8333" | ||
| - "q9314" | ||
| - "q10410" | ||
| - "q11635" | ||
| - "q13004" | ||
| - "q14534" | ||
| - "q16244" | ||
| - "q18156" | ||
| - "q20292" | ||
| - "q22680" | ||
| - "q25349" | ||
| - "q28332" | ||
| - "q31665" | ||
| - "q32768" | ||
| - "q32769" | ||
| - "q35391" | ||
| - "q39556" | ||
| - "q44210" | ||
| - "q49413" | ||
| - "q55227" | ||
| - "q61726" | ||
| - "q65536" | ||
|
|
||
| backends: | ||
| - FLASHMLA_SPARSE | ||
|
|
||
| device: "cuda:0" | ||
| profile_memory: true | ||
| sparse_mla_dense_mha_max_seq_len: 25349 | ||
| sparse_mla_masked_mha_max_seq_len: 8192 | ||
| sparse_mla_mha_variants: ["dense_mha", "masked_mha", "mqa"] | ||
| sparse_mla_topk_pattern: "random" |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| pytest.importorskip("cutlass") | ||
|
|
||
| from vllm.model_executor.layers.attention.sparse_mla_attention import ( | ||
| _build_topk_mask, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA") | ||
| def test_build_topk_mask_single_request_matches_generic_path() -> None: | ||
| topk = torch.tensor( | ||
| [[0, 31, 32, 63, -1], [1, 64, 127, -1, -1]], | ||
| dtype=torch.int32, | ||
| device="cuda", | ||
| ) | ||
|
|
||
| single_req = _build_topk_mask([topk], [2], 2, 128, topk.device) | ||
| generic = _build_topk_mask([topk[:1], topk[1:]], [1, 1], 1, 128, topk.device) | ||
|
|
||
| torch.testing.assert_close(single_req[0, 0], generic[0, 0]) | ||
| torch.testing.assert_close(single_req[0, 1], generic[1, 0]) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.