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.
-
init_rht_signs–Pre-build the
single_rhtsigns so no forward takes the H2D miss. -
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.
_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).
_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.
_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.
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).
init_rht_signs(head_size, device)
¶
Pre-build the single_rht signs so no forward takes the H2D miss.
pack_int4_nibbles(lo, hi)
¶
Pack two uint8 values (each in [0, 15]) into one byte.
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.
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.
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(b) for b the largest power-of-two
divisor of head_size (b == head_size for power-of-two sizes), so
softmax_scale is divided by b and the inverse RHT divides the output
by b as well.
unpack_int4_nibbles(packed)
¶
Split one packed byte into the (low, high) nibble pair as uint8.