vllm.v1.attention.backends.turboquant_attn
¶
TurboQuant attention backend for vLLM.
Standard scaled dot-product attention on uncompressed K/V,
then quantize K and store K+V into combined cache slot.
Decode: Compute TQ attention scores from compressed cache, unpack FP16 values, softmax + weighted sum.
Cache layout (no leading 2 dimension): (num_blocks, block_size, num_kv_heads, slot_size) where slot_size = key_packed_size + value_fp16_size
Per-head per-position slot layout
[key_packed (kps bytes) | value_fp16 (D*2 bytes)] For turboquant_k3v4_nc head_dim=256: [100 bytes key | 512 bytes value] = 612
Classes:
-
TurboQuantAttentionBackend–Attention backend using TurboQuant KV-cache compression.
-
TurboQuantAttentionImpl–TurboQuant attention implementation.
-
TurboQuantMetadata–Metadata for TurboQuant attention.
-
TurboQuantMetadataBuilder–Builds TurboQuantMetadata from scheduler output.
TurboQuantAttentionBackend
¶
Bases: AttentionBackend
Attention backend using TurboQuant KV-cache compression.
Methods:
-
customize_spec–TurboQuant packs K+V into one slot per head.
customize_spec(spec)
classmethod
¶
TurboQuant packs K+V into one slot per head.
TurboQuantAttentionImpl
¶
Bases: AttentionImpl['TurboQuantMetadata']
TurboQuant attention implementation.
Vectorized PyTorch: batch quantize/store, vectorized bit-unpack decode with einsum scores and value gather.
Methods:
-
do_kv_cache_update–Store compressed K/V into the combined TQ cache.
_continuation_prefill(layer, query, key_chunk, val_chunk, kv_cache, block_table, cached_len, seq_len, Pi, centroids)
¶
Handle continuation chunk by dequanting cached K/V from TQ cache.
Dequants previously cached K/V, concatenates with the current chunk's raw K/V, then runs flash_attn with causal masking.
_dispatch_decode_soa(**kwargs)
¶
SoA-aware Triton decode — fallback for FlyDSL-ineligible layers.
The SoA decode launcher accepts a subset of the FlyDSL kwargs; filter to the params it accepts and raise if a meaningful (non-None) kwarg would be silently dropped (so we never mask a real feature gap).
_ensure_on_device(layer, device)
¶
One-time derivation of TQ buffers (rotation matrix, midpoints).
The Hadamard rotation is shared across all layers: random sign flips do not improve Lloyd-Max quantization quality because the quantizer is symmetric around zero (sign-flipping a coordinate maps it to the mirror centroid with identical distortion).
_max_capture_batch_size()
¶
Largest decode batch we might see at runtime (for workspace pre-warm).
Take max(cudagraph_capture_sizes, scheduler.max_num_seqs): a forward pass exceeding the largest captured graph size falls back to eager, but the workspace is locked and that eager path can still hit batch sizes up to max_num_seqs. Falls back to 1024 if config is unavailable.
_store_kv(key, value, kv_cache, slot_mapping, layer)
¶
Quantize + store via fused Triton kernel.
do_kv_cache_update(layer, key, value, kv_cache, slot_mapping)
¶
Store compressed K/V into the combined TQ cache.
Called as a separate custom op (unified_kv_cache_update) BEFORE the attention forward, matching FlashAttention's split pattern. slot_mapping is already sliced to num_actual_tokens by the caller.
TurboQuantMetadata
dataclass
¶
Bases: AttentionMetadata
Metadata for TurboQuant attention.
TurboQuantMetadataBuilder
¶
Bases: AttentionMetadataBuilder[TurboQuantMetadata]
Builds TurboQuantMetadata from scheduler output.
Methods:
-
build–Build TurboQuantMetadata from common attention metadata.
build(common_prefix_len, common_attn_metadata, fast_build=False)
¶
Build TurboQuantMetadata from common attention metadata.
_build_hadamard(d, device_str)
¶
Orthonormal Hadamard matrix (Sylvester construction), cached per (d, device).
Precomputed D×D matrix enables matmul-based WHT — single cuBLAS GEMM instead of log2(D) butterfly kernel launches. 64KB for D=128.
_soa_imports()
¶
Lazy import of the HIP-free SoA Triton subset (store / dequant / decode).
Kept lazy so the default (FlyDSL-off) path never imports these modules.