vllm.models.nemotron_shortcut.nvidia.sdsa.prefill
¶
SDSA selection: each query row's exact Top-K keys, by one of two selectors.
Dense: score every key of the row's request, then Top-K. Fused: sample a threshold, select candidates in one sweep, exact Top-K among them. SAMPLES keys of every PAGES 64-key pages are scored, each at its own random offset into them, the offsets drawn once per step. A row's RANK-th largest sampled score is its threshold; every key is equally likely to be sampled, wherever it sits in the row. One sweep keeps the keys above it (candidates) and up to K keys equal to it (ties), counting both exactly; a row left short of K, or with more than CAPACITY candidates, is swept again inside the same launch at a radix-selected threshold at most its K-th largest score. No host synchronization.
K is the config's index_topk, 512 or 2048.
Classes:
-
GroupUnionMasksKernel–Builds every FlashInfer group_union_masks kernel a prefill step can run.
-
SdsaConfig–Each phase's core attention, and which of its requests take the fused
-
SdsaPrefillPlan–Tile layout for one scheduler step;
runis called once per layer. -
SdsaTiles–Query rows in 128-row tiles of one request each, for the fused selector:
Functions:
-
get_sdsa_config–The one place that builds an SDSA layer's config, from the model's text
-
indexer_pages–The keys and scales of an indexer cache, as the SDSA kernels take them:
-
reserve–Request, in the profiling run, the arena of the largest step: at most
-
union_columns–The columns of a union row, and of a mask row at 32 per int32 word: a
-
union_group–The union core's tokens per group: the most whose heads fit FA4's tile.
-
write_index_qk–BF16 indexer: copy key i to cache row slots[i] of
keys(none for -1)
GroupUnionMasksKernel
¶
Bases: VllmJitKernel['GroupUnionMasksKernel.CompileKey']
Builds every FlashInfer group_union_masks kernel a prefill step can run.
FlashInfer compiles a kernel per group size and per key bitmap size, on
first use: a power of two of 16K-key rounds up to 2**20 keys, and a
multiple of 8 rounds (128K keys) past it. The JIT warm-up builds them at
startup, so that no step stalls to compile one. With the warm-up off
(--enforce-eager turns it off), each one compiles in the first step that
needs it.
Methods:
-
get_warmup_keys–One key per bitmap size that a step can take, for groups of
group
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
get_warmup_keys(*, group, top_k, max_model_len, table_pages, columns)
¶
One key per bitmap size that a step can take, for groups of group
tokens. A launch takes a block table of table_pages pages, and unions
and mask rows of columns columns.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
SdsaConfig
¶
Each phase's core attention, and which of its requests take the fused selector. The cores: token: trtllm-gen per token; fa4: FlashAttention 4 per token; union: FA4 per group of tokens over their union of keys. get_sdsa_config builds the config from the model's config and resolves "auto" values; the config is frozen.
Methods:
-
prefill–The prefill core for a step.
Attributes:
-
decode_core(DecodeCore | Literal['auto']) –The decode core; "auto" picks it per step.
-
decode_fused(bool | tuple[int, int] | Literal['auto']) –Which decode requests take the fused selector: all, none, or those with
-
indexer_dtype(Literal['auto', 'nvfp4', 'bf16']) –The dtype of the k cache and of the indexer dot product.
-
prefill_core(PrefillCore | Literal['auto']) –The prefill core; "auto" picks it per step.
-
prefill_fused(bool | tuple[int, int] | Literal['auto']) –Which prefill requests take the fused selector: all, none, or those with
-
top_k(Literal[512, 2048]) –K, the number of keys the indexer selects for each query.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
decode_core = 'auto'
class-attribute
instance-attribute
¶
The decode core; "auto" picks it per step.
decode_fused = 'auto'
class-attribute
instance-attribute
¶
Which decode requests take the fused selector: all, none, or those with context in [lo, hi). "auto" fuses none.
indexer_dtype = 'auto'
class-attribute
instance-attribute
¶
The dtype of the k cache and of the indexer dot product.
prefill_core = 'auto'
class-attribute
instance-attribute
¶
The prefill core; "auto" picks it per step.
prefill_fused = 'auto'
class-attribute
instance-attribute
¶
Which prefill requests take the fused selector: all, none, or those with context in [lo, hi). "auto" fuses all.
top_k = 2048
class-attribute
instance-attribute
¶
K, the number of keys the indexer selects for each query.
prefill(*, heads, context, rows, kv_cache_dtype='auto')
¶
The prefill core for a step.
config = SdsaConfig() config.prefill(heads=128, context=4096, rows=4096) 'fa4' config.prefill(heads=64, context=4096, rows=4096) 'token' config.prefill(heads=32, context=4096, rows=4096) 'union' config.prefill(heads=32, context=4096, rows=32) 'token' config.prefill(heads=32, context=UNION_KEY_LIMIT, rows=UNION_KEY_LIMIT) 'union' config.prefill(heads=32, context=UNION_KEY_LIMIT + 1, rows=UNION_KEY_LIMIT) 'token'
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
SdsaPrefillPlan
¶
Tile layout for one scheduler step; run is called once per layer.
block_table holds the planned requests' pages of PAGE tokens in order,
which locate both the indexer's keys and the core's K/V. core is the
step's core attention for layers of heads heads. The step's query rows
are its first_row decode rows, then the tiles' rows; query_slots maps
the step's query tokens to them.
Methods:
-
run–Select each row's Top-K keys, given the query in tiled rows (row r
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 | |
run(query, query_scales, key_cache, key_scales, *, out, valid_counts, mask, topk_workspace)
¶
Select each row's Top-K keys, given the query in tiled rows (row r
is query query_rows[r]) and the keys, both in the config's indexer
dtype. The core attends to prefill tokens in groups of group tiled
rows. Each group
that holds queries is, in order, a batch b of FA4: its queries start at
batch_starts[b], its union of the rows' keys is unions[b] (core cache
slots, carved from out; size in valid_counts[b]), and each query's
bitmask over that union is its row of mask (group_union_masks).
Group g is batch group_batches[g], -1 if it holds no queries. The
per-token cores take no masks: a token's union is its Top-K keys'
slots, in Top-K order, -1 last.
topk_workspace is gvr_2's zeroed scratch, shared with the decode rows.
Initial and cached prefill are one pipeline: cached context is just more keys before the row's causal end. The step's threshold sample is gathered once; panels of whole 128-row query tiles of one selector then run in turn, in one panel scratch.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 | |
SdsaTiles
¶
Query rows in 128-row tiles of one request each, for the fused selector:
tile t is request tile_requests[t], its row r seeing keys
< row_ends[128 t + r] (0: no query). context_lens are the requests'
key counts; block_table holds their pages.
Methods:
-
gather_sample–Each request's sampled keys and their scales, as 64-key sample pages
-
select–Select tiles begin..end - 1 (query: their rows): their rows' Top-K
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
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 | |
gather_sample(key_cache, key_scales, sample_keys, sample_scales)
¶
Each request's sampled keys and their scales, as 64-key sample pages
(select's keys); NVFP4's scales share the keys' pages.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
select(begin, end, query, query_scales, key_cache, key_scales, samples, scratch, topk_workspace, config)
¶
Select tiles begin..end - 1 (query: their rows): their rows' Top-K key positions, -1 last.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_arena_layout(num_requests, sample_capacity, panel_rows, config, dense)
¶
Return the layout of a step's arena: every request's sampled keys and
their scales (as 8-byte words), then a panel's scratch, every buffer
panel_rows rows, and, if dense, dense prefill's scores.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_arrivals()
¶
Return the threshold select's per-tile arrival counts for a panel: zeroed once, left zeroed by every call, shared by all layers.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_dense_tiles(width)
¶
Tiles of a dense prefill panel of width-key rows: within the indexer's
logits budget, at least one.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_fused(rule, contexts)
¶
Which requests of contexts keys take the fused selector under rule.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_rows(scales, rows)
¶
_sample_capacity(context)
¶
Return the number of sampled keys per request of up to context keys,
in whole 128-key scorer tiles.
_sample_offsets(capacity, device)
¶
Each sample's random offset into its run of PAGES pages, drawn on the
device from an OS entropy seed, not the seeded random, so no input can
anticipate them.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_token_slots(positions, query_rows, requests, block_table, slots, counts)
¶
Write each tiled row's Top-K keys (key positions, -1 last) as cache
slots, -1 last, to its query's row of slots, and their count to
counts. Row r holds query query_rows[r] (-1: none) of request
requests[r], whose pages are its row of block_table.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
_top_k(scores, lengths, top, workspace, rows_per_length=1)
¶
Write each row's exact Top-K positions (K = top.shape[1]) among its
valid scores; a shorter row gets -1 in its last slots. Row r of a length's
rows_per_length rows has lengths[length] - rows_per_length + r + 1 (the
MTP rows). Uses gvr_2's host entry, since top_k_varlen takes only K from
512 to 2048 and this also takes K = RANK. One launch, with lengths read on
the device, so it can be captured in a CUDA graph. gvr_2 takes its
threshold from its own sample of the row, so it is given no hints.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
get_sdsa_config(text_config, k_proj)
¶
The one place that builds an SDSA layer's config, from the model's text
config: K is index_topk, and the sdsa_* fields set the others, each
"auto" when the checkpoint's config does not have it.
--hf-overrides sets any of them, for example
--hf-overrides '{"text_config": {"sdsa_prefill_core": "token"}}'.
sdsa_indexer_dtype "auto" gives NVFP4 if the checkpoint quantizes the
indexer's key projection k_proj, and BF16 if not. The config is frozen.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
indexer_pages(cache, indexer_dtype)
¶
The keys and scales of an indexer cache, as the SDSA kernels take them: NVFP4 [pages, PAGE, 64] uint8 and [pages, PAGE, 8] E4M3 views, BF16 [pages, PAGE, 128] and no scales.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
reserve(max_requests, max_tokens, max_context, topk_workspace, config)
¶
Request, in the profiling run, the arena of the largest step: at most
max_requests requests, max_tokens query tokens and max_context keys
per request, every buffer at its bound.
Then run gvr_2's sample Top-RANK over empty rows for every panel size at every sample width a step can take: gvr_2 compiles a kernel per launch shape on first use, which would stall a step (and every TP rank, in its next collective). Its first call per K also builds a hint table, which allocates and syncs with the host and is refused under CUDA graph capture; doing it here puts that memory in the profiled budget. Also allocate the arrival counts.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
union_columns(group, top_k, max_model_len)
¶
The columns of a union row, and of a mask row at 32 per int32 word: a group's K keys each, at most max_model_len, in whole 256-column blocks.
Source code in vllm/models/nemotron_shortcut/nvidia/sdsa/prefill.py
union_group(heads)
¶
The union core's tokens per group: the most whose heads fit FA4's tile.
write_index_qk(key, slots, keys, query, query_slots, packed_query)
¶
BF16 indexer: copy key i to cache row slots[i] of keys (none for -1)
and query token j to row query_slots[j] of packed_query, in one launch.