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
-
resolve_backend–Resolve the decode top-k implementation from the configured
Source code in vllm/model_executor/layers/indexer_topk.py
143 144 145 146 147 148 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 | |
_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
_resolve_auto(logits, topk_tokens, num_rows)
¶
The priority chain: cooperative -> persistent -> per_row. aiter/deep_select/flashinfer/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 (DeepSelect, FlashInfer, torch reference).
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
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 | |
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
_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).