vllm.models.deepseek_v41.common.ops.cache_utils
¶
Triton kernels for DeepseekV4 paged K-cache management and sparse-attention index preparation.
- quantize_and_insert_k_cache: quantize bf16 K to UE8M0 FP8 and insert into the paged cache.
- dequantize_and_gather_k_cache: gather and dequantize FP8 K from the paged cache for sparse/SWA prefill.
- compute_global_topk_indices_and_lens: map local topk indices to global KV cache slots and count valid entries.
- combine_topk_swa_indices: concatenate topk compressed indices with SWA window indices for sparse prefill.
Functions:
-
build_flashinfer_mixed_sparse_indices–Build the FlashInfer DSV4 sparse-index matrix for decode-first batches.
-
compute_global_topk_indices_and_lens–Map local topk indices to global KV cache slots and count valid entries.
-
dequantize_and_gather_k_cache–Dequantize and gather a paged DSv4 K cache.
-
gather_num_workers–Grid width for a gather of at most
max_gather_lentokens. -
quantize_and_insert_k_cache–Quantize K tensor and insert into paged K cache.
-
quantize_and_insert_k_kernel–Quantize K tensor and insert into paged K cache.
_combine_topk_swa_warmup_inputs(vllm_config)
¶
One warmup row per v4.1 layer type (compress ratio) in the model.
Every layer slices the shared index_topk-wide indices buffer, so the
padded width is the same for all rows; SWA-only layers (ratio 0) pass
TOP_K=0.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
_dequantize_and_gather_k_mxfp8_kernel(out_ptr, out_stride0, out_stride1, k_cache_ptr, seq_lens_ptr, block_table_ptr, offset, gather_lens_ptr, max_blocks_per_seq, head_dim, scale_dim, quant_block, cache_block_size, block_stride, use_fnuz=False)
¶
Gather V4.1 MXFP8 rows into a bf16 workspace, dequantizing in place.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
_dequantize_and_gather_k_nvfp4_kernel(out_ptr, out_stride0, out_stride1, k_cache_ptr, seq_lens_ptr, block_table_ptr, offset, gather_lens_ptr, max_blocks_per_seq, head_dim, scale_dim, quant_block, cache_block_size, block_stride)
¶
Gather and dequantize V4.1 NVFP4 rows into a bf16 workspace.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
_dequantize_and_gather_k_one_pass_kernel(out_ptr, out_stride0, out_stride1, k_cache_ptr, seq_lens_ptr, block_table_ptr, offset, gather_lens_ptr, max_blocks_per_seq, fp8_dim, bf16_dim, scale_dim, quant_block, cache_block_size, token_data_size, block_stride, output_dim, fp8_max, n_quant_blocks, use_fnuz=False)
¶
_dequantize_and_gather_k_kernel with the token read in one tile.
The seven quantization blocks tile the fp8 region exactly, so the per-block form's bounds never bite and its twenty-two memory operations per token reduce to four. Selected on ROCm; CUDA keeps the per-block kernel.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.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 | |
_quantize_and_insert_k_mxfp8_kernel(k_ptr, slot_mapping_ptr, k_cache_ptr, num_tokens, head_dim, scale_dim, quant_block, cache_block_size, block_stride, fp8_max, use_fnuz=False)
¶
Quantize a bf16 K row to MXFP8 and insert it into a V4.1 paged cache.
One program per token. Unlike the V4 record there is no bf16 tail, so every dim is scaled.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
build_flashinfer_mixed_sparse_indices(decode_swa_indices, decode_compressed_indices, decode_compressed_topk_lens, prefill_topk_indices, query_start_loc, seq_lens, token_to_req_indices, swa_block_table, swa_block_size, compressed_block_table, compressed_block_size, window_size, compress_ratio, topk, decode_compressed_indices_are_local=False, decode_is_valid_token=None, swa_block_span=None, compressed_block_span=None, *, replay_start)
¶
Build the FlashInfer DSV4 sparse-index matrix for decode-first batches.
Produces sparse_indices of shape [num_tokens, swa_total_width +
padded_topk] (the first swa_total_width columns are SWA slot ids, the
rest are compressed/top-k slot ids) and sparse_topk_lens (active length
per token). Decode tokens read precomputed SWA/compressed indices; prefill
tokens derive their SWA window from the position and translate local
compressed indices to global slots via the block tables.
replay_start ([num_reqs], SWA bounded replay) lower-bounds every
prefill token's window: positions below it hold no window KV.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 | |
compute_global_topk_indices_and_lens(topk_indices, token_to_req_indices, block_table, block_size, is_valid_token)
¶
Map local topk indices to global KV cache slots and count valid entries.
Fuses three operations into a single kernel: 1. Block-table lookup (local index → global slot id) 2. Valid-entry counting (topk_lens per token) 3. Masking padding tokens to length 0
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
dequantize_and_gather_k_cache(out, k_cache, seq_lens, gather_lens, block_table, block_size, offset, use_fnuz=False, max_gather_len=None)
¶
Dequantize and gather a paged DSv4 K cache.
The record is read off k_cache.shape[-1]; see the module header. Only
the fp8 records have a CuteDSL gather, so NVFP4 always takes the Triton
path.
use_fnuz MUST match the encoder of the specific cache being read:
False for compressed_k_cache (Triton encoder is OCP everywhere),
current_platform.is_fp8_fnuz() for swa_k_cache (C++ encoder
writes FNUZ on gfx942 and OCP on gfx950).
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
gather_num_workers(max_gather_len)
¶
Grid width for a gather of at most max_gather_len tokens.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
quantize_and_insert_k_cache(k, k_cache, slot_mapping, block_size=64, is_ue8m0=True, use_fnuz=False, bytes_per_token=V4_BYTES_PER_TOKEN)
¶
Quantize K tensor and insert into paged K cache.
bytes_per_token picks the record (see the module header): the V4 one,
or V4.1's all-dims MXFP8 one.
use_fnuz=True selects FNUZ E4M3 cache encoding and is only valid on
platforms whose FP8 format is FNUZ. use_fnuz=False selects OCP E4M3,
which is used by OCP-encoded caches even on gfx942.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
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 | |
quantize_and_insert_k_kernel(k_ptr, slot_mapping_ptr, k_cache_ptr, num_tokens, input_dim, fp8_dim, bf16_dim, scale_dim, quant_block, cache_block_size, token_data_size, block_stride, fp8_max, n_quant_blocks, use_fnuz=False)
¶
Quantize K tensor and insert into paged K cache.
K Cache block layout (block_size=64 tokens): - [0, 64576): Token data, each token has 448 fp8 + 128 bf16 - [64576, 64576 + 648): Scales, each token has 8 uint8 scales - [64576 + 648, block_stride): Padding
One program per token.
use_fnuz=True selects FNUZ (tl.float8e4b8); default OCP
(tl.float8e4nv) matches every production caller.
Source code in vllm/models/deepseek_v41/common/ops/cache_utils.py
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |