vllm.v1.attention.ops.triton_mla_sparse_kernel ¶
Triton sparse MLA attention with split-KV for low-batch decode.
_choose_num_kv_splits cached ¶
_choose_num_kv_splits(
num_tokens: int,
num_head_groups: int,
index_topk: int,
sm_count: int,
) -> int
Pick a power-of-2 split count that fills the device without dropping per-split work below _MIN_TOPK_PER_SPLIT. Returns 1 when the single-pass grid already reaches ~1/_SPLIT_MAX_OCCUPANCY utilization.
Source code in vllm/v1/attention/ops/triton_mla_sparse_kernel.py
_sparse_mla_compute_tile ¶
_sparse_mla_compute_tile(
q_buffer,
k_buffer,
indices_ptr,
cur_q,
cur_head,
cur_kv_head_id,
mask_h,
split_start,
split_end,
seq_kv,
stride_q_token,
stride_q_head,
stride_kv_token,
stride_kv_head,
stride_indices_token,
stride_indices_head,
sm_scale,
BLOCK_H: constexpr,
BLOCK_N: constexpr,
BLOCK_DV: constexpr,
BLOCK_DMODEL: constexpr,
BLOCK_DPE: constexpr,
)
Shared stage-1 body: load Q, run the sparse online-softmax loop over [split_start, split_end) of the topk axis, return accumulators.
Source code in vllm/v1/attention/ops/triton_mla_sparse_kernel.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
_sparse_mla_kernel_final ¶
_sparse_mla_kernel_final(
q_buffer,
k_buffer,
indices_ptr,
out_ptr,
seq_kv,
h_q,
stride_q_token,
stride_q_head,
stride_kv_token,
stride_kv_head,
stride_out_token,
stride_out_head,
stride_indices_token,
stride_indices_head,
sm_scale,
index_topk: constexpr,
kv_group_num: constexpr,
BLOCK_H: constexpr,
BLOCK_N: constexpr,
BLOCK_DV: constexpr,
BLOCK_DMODEL: constexpr,
BLOCK_DPE: constexpr,
)
Single-pass fast path: full topk, write final bf16 output directly.
Source code in vllm/v1/attention/ops/triton_mla_sparse_kernel.py
_sparse_mla_kernel_split ¶
_sparse_mla_kernel_split(
q_buffer,
k_buffer,
indices_ptr,
mid_out_ptr,
seq_kv,
h_q,
stride_q_token,
stride_q_head,
stride_kv_token,
stride_kv_head,
stride_mid_token,
stride_mid_head,
stride_mid_split,
stride_indices_token,
stride_indices_head,
sm_scale,
index_topk: constexpr,
NUM_KV_SPLITS: constexpr,
kv_group_num: constexpr,
BLOCK_H: constexpr,
BLOCK_N: constexpr,
BLOCK_DV: constexpr,
BLOCK_DMODEL: constexpr,
BLOCK_DPE: constexpr,
LOGE2: constexpr,
)
Stage 1 of split-KV: process one slice of the topk axis and write its (out_partial, lse_partial) into the mid buffer.
Source code in vllm/v1/attention/ops/triton_mla_sparse_kernel.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
_sparse_mla_merge_kernel ¶
_sparse_mla_merge_kernel(
mid_out_ptr,
out_ptr,
h_q,
stride_mid_token,
stride_mid_head,
stride_mid_split,
stride_out_token,
stride_out_head,
NUM_KV_SPLITS: constexpr,
kv_group_num: constexpr,
BLOCK_H: constexpr,
BLOCK_DV: constexpr,
BLOCK_DV_TILE: constexpr,
)
Stage 2: N-way online-softmax merge of per-split (out, lse) tiles.
Grid is (num_tokens, num_head_groups, num_dv_tiles). Each program handles BLOCK_H heads × BLOCK_DV_TILE output-dim lanes. The LSE reduction is identical across DV tiles for the same (token, head) — each program recomputes it locally, which is cheap (O(NUM_KV_SPLITS) scalars) and avoids inter-CTA synchronization.
Source code in vllm/v1/attention/ops/triton_mla_sparse_kernel.py
triton_mla_sparse_attention ¶
triton_mla_sparse_attention(
q: Tensor,
kv: Tensor,
indices: Tensor,
sm_scale: float,
num_kv_splits: int | None = None,
sm_count: int | None = None,
) -> Tensor
Sparse MLA attention over topk indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q | Tensor | [num_tokens, num_heads_q, dim_qk] bf16 | required |
kv | Tensor | [seq_kv, num_heads_kv=1, dim_qk] bf16 | required |
indices | Tensor | [num_tokens, num_heads_kv=1, topk] int32 | required |
sm_scale | float | softmax scale | required |
num_kv_splits | int | None | override auto-heuristic; None/0 = auto, 1 = force single-pass. | None |
sm_count | int | None | cached device SM count for the split heuristic. | None |
Returns:
| Name | Type | Description |
|---|---|---|
out | Tensor | [num_tokens, num_heads_q, _BLOCK_DV] bf16 |
Source code in vllm/v1/attention/ops/triton_mla_sparse_kernel.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |