vllm.models.deepseek_v4.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.
-
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.
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) ¶
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
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 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 | |
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
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.
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_v4/common/ops/cache_utils.py
quantize_and_insert_k_cache(k, k_cache, slot_mapping, block_size=64, is_ue8m0=True, use_fnuz=False) ¶
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
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_v4/common/ops/cache_utils.py
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_v4/common/ops/cache_utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 | |