vllm.model_executor.layers.sparse_attn_indexer_kpool ¶
Custom Sparse Attention Indexer layers.
Classes:
-
SparseAttnIndexerKpool–Sparse Attention Indexer Custom Op Layer. This layer is extracted as a
Functions:
-
kv_cache_as_quant_view–4D
[num_blocks, block_size, 1, head_width]view expected by
SparseAttnIndexerKpool ¶
Bases: CustomOp
Sparse Attention Indexer Custom Op Layer. This layer is extracted as a separate custom op since it involves heavy custom kernels like mqa_logits, paged_mqa_logits and top_k_per_row, etc. Those kernels maybe requires specific memory layout or implementation for different hardware backends to achieve optimal performance.
For now, the default native path will use CUDA backend path. Other platform may requires add the corresponding Custom Op name sparse_attn_indexer to custom_ops in CompilationConfig to enable the platform specific path.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
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 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 | |
_build_decode_scatter_indices(decode_lens, num_requests, n) ¶
Per-token (request id, intra-request index) for a non-uniform decode batch, with n == decode_lens.sum() as a host int (avoids a device sync and keeps both repeat_interleaves sync-free).
Shared by every _scatter_decode_tokens_by_request call in a step: building it per call would repeat the same repeat_interleave/cumsum chain up to 5x per layer on the eager decode break.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
_decode_topk_seq_lens(positions, decode_lens, num_decode_tokens, batch_size, next_n, requires_padding) ¶
Token-granular seq_len (pos + 1) per pool-topk row, layout-aware.
pool_topk (and the logits it comes from) follow the padded [batch_size, next_n] grid whenever requires_padding is set, so row (b, t) corresponds to flat decode token offset_b + t -- NOT b * next_n + t. Slicing flat positions[: batch_size * next_n] (the uniform-layout shortcut) misaligns every row after the first non-uniform request and, past the decode region, reads prefill tokens' positions; expand_pools_and_append_tail then anchors the tail at another request's length, dropping the row's real tail tokens or emitting indices past its sequence (out-of-bounds block-table reads). Padded rows get 0 (empty tail); they are dropped by unpack_seq_triton anyway.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
_fill_short_decode_causal_indices(topk_indices_buffer, positions, num_decode_tokens, max_seq_len, topk_tokens) ¶
Fill exact causal rows when sparse decode would select every token.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
_gather_workspace_shapes(total_seq_lens, head_dim, fp8_dtype, use_fp4_cache) ¶
Return ((values_shape, values_dtype), (scales_shape, scales_dtype)) for the K-gather workspace. FP8 path: (T, head_dim) fp8 + (T, 4) uint8 fp32 scales. MXFP4 path: (T, head_dim // 2) uint8 packed mxfp4 + (T, head_dim // MXFP4_BLOCK_SIZE) uint8 ue8m0 scales.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
_kpool_compress_insert(k, gate_score, ape, kv_cache, slot_mapping, kpool, head_dim, round_scale) ¶
Pool kpool consecutive tokens into one fp8 K and write at pool slots.
slot_mapping is pool-granular (compress_ratio == kpool on the spec): only the last token of each complete pool carries a valid (>=0) slot; intra-pool tokens are -1. Every position is treated as a pool-completion candidate and non-completions are masked off inside the kernel. Compacting the valid rows first costs two device syncs on the eager prefill path and buys nothing numerically. Assumes pool-aligned chunk starts.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
_scatter_decode_tokens_by_request(tokens, pad_value, num_requests, lmax, scatter_indices) ¶
Group [N, ...] decode tokens into a padded [num_requests, lmax, ...] layout: request r's tokens at row r in order; short requests padded.
Unlike pack_seq_triton this is dtype-agnostic (needed for the int32 slot/pos tensors) — it scatters with the shared per-step indices from _build_decode_scatter_indices. Used only for the non-uniform (requires_padding) decode batch; uniform batches use a zero-copy reshape.
Source code in vllm/model_executor/layers/sparse_attn_indexer_kpool.py
kv_cache_as_quant_view(kv_cache, head_dim, use_fp4_cache) ¶
4D [num_blocks, block_size, 1, head_width] view expected by DeepGEMM, from the 3D indexer kv-cache allocation.