vllm.models.deepseek_v41.common.ops
¶
DeepSeek V4.1 fused ops.
Single bridging point for the kernels V4.1 shares verbatim with V4: the
re-exports below are the only place this package reaches into
vllm.models.deepseek_v4, so submodules import them from . instead of
naming the V4 path themselves. Keep these imports above the .-relative
ones — indexer_k_store reads them off this partially-initialized module.
Modules:
-
cache_utils–Triton kernels for DeepseekV4 paged K-cache management and sparse-attention index
-
fused_compress_quant_cache–V4.1 state saving/compression and independently schedulable cache insertion.
-
fused_layout–Weight permutations for FlashMLA's mega-attention kernel.
-
indexer_k_store–Indexer K production for DeepSeek V4.1 kv-source layers.
-
query_quant–
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.
-
fused_indexer_q_rope_quant–Fused RoPE + quantize Q for the sparse indexer.
-
fused_inv_rope_fp8_quant–Fused inverse RoPE + block-scaled FP8 quantization.
-
indexer_k_norm_rope_store–k_norm → RoPE → quant → paged store for indexer keys.
-
quantize_and_insert_k_cache–Quantize K tensor and insert into paged K cache.
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
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 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 | |
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)
¶
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
fused_indexer_q_rope_quant(positions, index_q, index_q_cos_sin_cache, index_weights, index_weights_softmax_scale, index_weights_head_scale, use_fp4=False, weights_out_dtype=torch.float32)
¶
Fused RoPE + quantize Q for the sparse indexer.
weights_out_dtype is the dtype the downstream scoring kernel takes:
fp32 for the dense MQA-logits kernels, bf16 for DeepGEMM's sparse
MQA-logits kernels (CUDA MXFP4 path only).
Weight-fold semantics (important — the two paths differ):
FP8 path (use_fp4=False, default):
q_fp8 : (T, H, HEAD_DIM) platform fp8 (e4m3fnuz on gfx942,
e4m3fn elsewhere); per-token-per-head scalar scale
(NOT stored — folded into weights below)
weights_out = weights * q_scale * softmax_scale * head_scale
Rationale: a single per-token q_scale is a scalar the downstream FP8
logits kernel would otherwise multiply in. Folding it into weights
avoids emitting a separate tensor and is free for the logits kernel.
MXFP4 path (use_fp4=True):
q_packed : (T, H, HEAD_DIM // 2) uint8 (2 E2M1 nibbles per byte)
q_scale : (T, H, HEAD_DIM // MXFP4_BLOCK_SIZE) uint8 ue8m0 bytes
weights_out = weights * softmax_scale * head_scale
Rationale: MXFP4 has PER-BLOCK (32-element) scales that live with
the Q values — they cannot be folded into a per-token weight
scalar, so weights carries only the softmax and head scales.
Returns (q_quant, weights_out) where q_quant is either a Tensor (FP8) or
a (values, scales) tuple (MXFP4). This matches the union type accepted
by SparseAttnIndexer.forward_*.
Source code in vllm/models/deepseek_v4/common/ops/fused_indexer_q.py
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 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 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 | |
fused_inv_rope_fp8_quant(o, positions, cos_sin_cache, n_groups, heads_per_group, nope_dim=448, rope_dim=64, quant_group_size=128, tma_aligned_scales=False, quantize=True)
¶
Fused inverse RoPE + block-scaled FP8 quantization.
Parameters:
-
(o¶Tensor) –Attention output [num_tokens, num_heads, head_dim] bf16.
-
(positions¶Tensor) –Token positions [num_tokens] int64.
-
(cos_sin_cache¶Tensor) –Precomputed [max_pos, rope_dim] with cos||sin.
-
(n_groups¶int) –Number of output groups.
-
(heads_per_group¶int) –Heads per group.
-
(nope_dim¶int, default:448) –Non-RoPE dimensions per head (default 448).
-
(rope_dim¶int, default:64) –RoPE dimensions per head (default 64).
-
(quant_group_size¶int, default:128) –FP8 quantization block size (default 128).
-
(tma_aligned_scales¶bool, default:False) –Output INT32 packed UE8M0 for SM100 (True) or FP32 for SM90 (False).
-
(quantize¶bool, default:True) –Quantize the rotated output to FP8 and return its scales.
Returns:
-
Tensor–Rotated output in [T, G, D] and its FP8 scales. The scale tensor is
-
Tensor–empty when quantization is disabled.
Source code in vllm/models/deepseek_v4/common/ops/fused_inv_rope_fp8_quant.py
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 | |
indexer_k_norm_rope_store(k_pre, positions, cos_sin_cache, rms_norm_weight, rms_norm_eps, k_cache, kv_slot_mapping, compress_ratio, use_fp4_cache)
¶
k_norm → RoPE → quant → paged store for indexer keys.
Parameters:
-
(k_pre¶Tensor) –[num_tokens, 128] bf16, the
wk(latent)projection. Only group-boundary rows are read. -
(positions¶Tensor) –[num_tokens] int64 token positions.
-
(cos_sin_cache¶Tensor) –[max_pos, rope_head_dim] GPT-J layout (cos half, then sin half), from the layer's compress-RoPE instance.
-
(rms_norm_weight¶Tensor) –[128] k_norm weight.
-
(rms_norm_eps¶float) –Epsilon of the k_norm RMSNorm.
-
(k_cache¶Tensor) –uint8 paged indexer cache [num_blocks, block_size, row_bytes].
-
(kv_slot_mapping¶Tensor) –[num_tokens] slots in the indexer cache (-1 = skip).
-
(compress_ratio¶int) –group size; keys are emitted at group boundaries.
-
(use_fp4_cache¶bool) –MXFP4 (2 nibbles/byte + ue8m0 per 32) when True, else per-token FP8 with a single fp32 scale.
Source code in vllm/models/deepseek_v41/common/ops/indexer_k_store.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 | |