vllm.models.deepseek_v4.common.ops ¶
Modules:
-
cache_utils–Triton kernels for DeepseekV4 paged K-cache management and sparse-attention index
-
fused_compress_quant_cache–Fused compressor + FP8/MXFP4 UE8M0 quantization + KV cache insert kernels.
-
fused_indexer_q– -
fused_inv_rope_fp8_quant–Fused inverse RoPE + block-scaled FP8 quantization kernel for DeepseekV4 attention.
-
fused_mtp_input_rmsnorm–Fused MTP-input RMSNorm: enorm (with mask-zero at position 0) + hnorm.
-
save_partial_states–
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.
-
fused_indexer_q_rope_quant–Fused RoPE + quantize Q for the sparse indexer.
-
mtp_shared_head_rmsnorm–RMSNorm for MTP's SharedHead.norm, on (T, H) bf16 input.
-
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) ¶
Build the FlashInfer DSV4 sparse-index matrix for decode-first batches.
Produces sparse_indices of shape [num_tokens, window_size + padded_topk] (the first window_size 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.
Source code in vllm/models/deepseek_v4/common/ops/cache_utils.py
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 | |
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_v4/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) ¶
Fused RoPE + quantize Q for the sparse indexer.
Weight-fold semantics (important — the two paths differ):
FP8 path (use_fp4=False, default): q_fp8 : (T, H, HEAD_DIM) float8_e4m3fn, 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
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 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 434 435 436 437 438 | |
mtp_shared_head_rmsnorm(hidden_states, weight, eps) ¶
RMSNorm for MTP's SharedHead.norm, on (T, H) bf16 input.
Uses the same _rmsnorm_row body as fused_mtp_input_rmsnorm so the MTP draft path runs one consistent RMSNorm implementation end to end.
Source code in vllm/models/deepseek_v4/common/ops/fused_mtp_input_rmsnorm.py
quantize_and_insert_k_cache(k, k_cache, slot_mapping, block_size=64, is_ue8m0=True) ¶
Quantize K tensor and insert into paged K cache.
K Cache block layout (block_size=64 tokens): - First 64 * 576 = 36864 bytes: Token data - Each token: 448 bytes (fp8) + 128 bytes (bf16) - Next 64 * 8 = 512 bytes: Scales - Each token: 8 bytes (uint8 scales, 7 real + 1 padding) - Padded to multiple of 576