vllm.model_executor.layers.indexer_topk
¶
Top-k kernels for the DSA sparse attention indexer.
Classes:
-
SparseIndexerTopk–The sparse indexer's decode top-k stage.
Functions:
-
deep_select_topk–Select the top-k indices per row of
inputwith DeepSelect. -
get_deep_select_stride_requirement–Stride alignment requirement (input, output) in bytes.
-
is_deep_select_supported–Whether the kernel accepts this input (dtype/stride/topk constraints).
SparseIndexerTopk
¶
Bases: Module
The sparse indexer's decode top-k stage.
Selects among the available top-k kernels (see kernel_config.sparse_indexer_topk_backend) and runs the chosen one.
Methods:
-
forward–Run the resolved decode top-k implementation, writing into
-
forward_prefill–Prefill top-k: row
rselects over -
resolve_backend–Resolve the decode top-k implementation from the configured
-
resolve_prefill_backend–flashinfer_gvr2 for chunks of at least GVR2_PREFILL_MIN_ROWS
-
takes_bf16_logits–Whether this decode call can consume BF16 without a full FP32 copy.
Source code in vllm/model_executor/layers/indexer_topk.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |
_cooperative_constraints(logits, topk_tokens, num_rows)
¶
Unmet constraints of cooperative_topk (empty when applicable).
Source code in vllm/model_executor/layers/indexer_topk.py
_gvr2_constraints(logits, topk_tokens)
staticmethod
¶
Unmet input constraints of gvr_2 (empty when applicable).
Source code in vllm/model_executor/layers/indexer_topk.py
_gvr2_warmup(logits, topk_tokens)
¶
Compile gvr_2's kernel variants for every decode row count once.
The kernel family and template depend on the row count, and the indexer runs eagerly (outside CUDA graphs) in some models, so without this every new batch size would JIT-compile (~1 s) in the middle of serving. Must run eagerly; the launchers are cached per row count.
Source code in vllm/model_executor/layers/indexer_topk.py
_resolve_auto(logits, topk_tokens, num_rows)
¶
The priority chain: cooperative -> persistent -> per_row. aiter/deep_select/flashinfer/flashinfer_gvr2/torch are opt-in only.
cooperative_topk is preferred whenever it is applicable, i.e. within its AUTO_COOPERATIVE_MAX_ROWS row limit; larger batches go to persistent_topk.
aiter not auto-resolved as it does not outperform the per-row on every shape, hence we delegate to the caller to decide when to use it.
Source code in vllm/model_executor/layers/indexer_topk.py
_row_ends(seq_lens, next_n, num_rows)
staticmethod
¶
Per-row exclusive end offsets (int32, (num_rows,)) for top-k kernels that take ragged lengths.
seq_lens is (B, next_n) per-row effective lens for native spec decode and (B, 1) otherwise, in which case per-row lens are derived the same way as the other decode top-k kernels.
Source code in vllm/model_executor/layers/indexer_topk.py
forward(logits, seq_lens, next_n, topk_indices, topk_tokens, max_seq_len)
¶
Run the resolved decode top-k implementation, writing into topk_indices (int32, -1 fill for rows shorter than topk_tokens).
Source code in vllm/model_executor/layers/indexer_topk.py
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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
forward_prefill(logits, row_starts, row_ends, topk_indices, topk_tokens)
¶
Prefill top-k: row r selects over
logits[r, row_starts[r]:row_ends[r]] (row_starts None means 0) and
writes indices relative to the row start (int32, -1 fill for rows
shorter than topk_tokens).
Source code in vllm/model_executor/layers/indexer_topk.py
resolve_backend(logits, topk_tokens, num_rows)
¶
Resolve the decode top-k implementation from the configured backend ("auto" = the pre-existing chain, or a validated explicit value).
Source code in vllm/model_executor/layers/indexer_topk.py
resolve_prefill_backend(logits, topk_tokens, row_starts)
¶
flashinfer_gvr2 for chunks of at least GVR2_PREFILL_MIN_ROWS row-relative rows (row_starts is None) when it was requested explicitly, otherwise per_row (top_k_per_row_prefill).
gvr_2 reads every row from column 0. Unmet gvr_2 constraints fall back instead of raising, since the same backend setting also drives decode.
Source code in vllm/model_executor/layers/indexer_topk.py
takes_bf16_logits(logits, topk_tokens, num_rows)
¶
Whether this decode call can consume BF16 without a full FP32 copy.
Source code in vllm/model_executor/layers/indexer_topk.py
_get_empty_and_aligned_tensor(dim0, dim1, device, dtype)
¶
Tensor with shape (dim0, dim1) whose stride(0) is 32B-aligned.
Source code in vllm/model_executor/layers/indexer_topk.py
deep_select_topk(input, topk, end=None, output_idx=None, indices_dtype=torch.int32)
¶
Select the top-k indices per row of input with DeepSelect.
Parameters:
-
(input¶Tensor) –(num_rows, vocab_size), bf16 or fp32. stride(1) must be 1 and stride(0) must be 1024B-aligned.
-
(topk¶int) –Number of elements to select per row; must be <= 4096.
-
(end¶Tensor | None, default:None) –Optional (num_rows,) int32 tensor with the exclusive right boundary of each row. Rows with
end[i] < topkget their remaining indices filled with -1. -
(output_idx¶Tensor | None, default:None) –Optional preallocated (num_rows, topk) output tensor whose stride(0) is 32B-aligned (e.g. a slice of a wider buffer).
-
(indices_dtype¶dtype, default:int32) –Output dtype when
output_idxis not provided.
Returns:
-
Tensor–The (num_rows, topk) indices tensor.
Source code in vllm/model_executor/layers/indexer_topk.py
get_deep_select_stride_requirement()
cached
¶
Stride alignment requirement (input, output) in bytes.
is_deep_select_supported(input, topk)
¶
Whether the kernel accepts this input (dtype/stride/topk constraints).