vllm.v1.attention.ops.int4_per_token_head ¶
Sub-byte packed (INT4) per-token-head KV cache mode.
INT4 packs two 4-bit values per cache byte, pre-rotates with a single RHT, and hides a 4-bit zero-point in the scale's low mantissa bits — too different from the core kernel to share it. Owns the whole mode: nibble pack/unpack, the reshape (write) kernel, the split-dot attention (read) kernel, the RHT transform, and the public reshape_and_cache_int4 / unified_attention_int4 entry points.
Functions:
-
fast_hadamard_transform–Unnormalized Walsh-Hadamard Transform along the last dimension.
-
pack_int4_nibbles–Pack two uint8 values (each in [0, 15]) into one byte.
-
reshape_and_cache_int4–Pre-rotate (RHT), pack to INT4 and write into the paged cache.
-
single_rht–Single Randomized Hadamard Transform: H × D₁ × x.
-
unified_attention_int4–Paged attention over the INT4 packed cache, writing into out.
-
unpack_int4_nibbles–Split one packed byte into the (low, high) nibble pair as uint8.
_get_rht_signs(d, round_idx, device) ¶
Return a cached deterministic ±1 sign vector of length d.
Source code in vllm/v1/attention/ops/int4_per_token_head.py
_launch_packed_attn(*, q, k_cache, v_cache, out, cu_seqlens_q, max_seqlen_q, seqused_k, softmax_scale, window_size, block_table, softcap, sinks, alibi_slopes, use_alibi_sqrt, qq_bias, output_scale, mm_prefix_range, k_scale_cache, v_scale_cache, seq_threshold_3D, num_par_softmax_segments, softmax_segm_output, softmax_segm_max, softmax_segm_expsum, packing_factor) ¶
Launch _attn_packed for one of the sub-byte modes.
Handles 2D-vs-3D dispatch, placeholder pointers for the unused side of that split, and the trailing reduce_segments pass. Writes into out (directly for 2D; via the segm buffers for 3D).
Source code in vllm/v1/attention/ops/int4_per_token_head.py
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 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 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 | |
_reshape_cache_int4_kernel(key_ptr, value_ptr, key_cache_ptr, value_cache_ptr, k_scale_cache_ptr, v_scale_cache_ptr, slot_mapping_ptr, stride_key_tok, stride_key_head, stride_val_tok, stride_val_head, stride_kc_blk, stride_kc_slot, stride_kc_head, stride_vc_blk, stride_vc_slot, stride_vc_head, stride_ks_blk, stride_ks_slot, stride_ks_head, stride_vs_blk, stride_vs_slot, stride_vs_head, block_size, head_size, head_size_v, PACKED_HEAD_PADDED) ¶
INT4 asymmetric quantization with zero-point steganography.
Source code in vllm/v1/attention/ops/int4_per_token_head.py
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 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 | |
_run_reshape_kernel(kernel, *, key, value, key_cache, value_cache, k_scale_cache, v_scale_cache, slot_mapping, packing_factor) ¶
Launch the packed INT4 reshape kernel.
Source code in vllm/v1/attention/ops/int4_per_token_head.py
fast_hadamard_transform(x) ¶
Unnormalized Walsh-Hadamard Transform along the last dimension.
H_d × x where H_d × H_d = d × I. Last dim must be a power of 2.
Three-tier dispatch
- Hadacore CUDA Tensor Core kernel (sm_80+).
- Triton MMA matmul kernel (CUDA fallback + ROCm MFMA/WMMA path).
- PyTorch butterfly (CPU and any GPU/dtype combo Triton can't take).
Source code in vllm/v1/attention/ops/int4_per_token_head.py
pack_int4_nibbles(lo, hi) ¶
reshape_and_cache_int4(key, value, key_cache, value_cache, slot_mapping, *, k_scale_cache, v_scale_cache) ¶
Pre-rotate (RHT), pack to INT4 and write into the paged cache.
Source code in vllm/v1/attention/ops/int4_per_token_head.py
single_rht(x, inverse=False) ¶
Single Randomized Hadamard Transform: H × D₁ × x.
Used by INT4 per-token-head quantization to gaussianize data before asymmetric quantization.
Source code in vllm/v1/attention/ops/int4_per_token_head.py
unified_attention_int4(q, k_cache, v_cache, out, *, cu_seqlens_q, max_seqlen_q, seqused_k, max_seqlen_k, softmax_scale, window_size, block_table, softcap, sinks, alibi_slopes, use_alibi_sqrt, qq_bias, output_scale, mm_prefix_range, k_scale_cache, v_scale_cache, seq_threshold_3D=None, num_par_softmax_segments=None, softmax_segm_output=None, softmax_segm_max=None, softmax_segm_expsum=None) ¶
Paged attention over the INT4 packed cache, writing into out.
The forward RHT has norm sqrt(head_size), so softmax_scale is divided by head_size and the inverse RHT divides the output by head_size as well.