Skip to content

vllm.v1.attention.ops.turboquant_soa.triton_turboquant_unified_attention

Unified (prefill + decode) Triton attention kernel for TurboQuant.

Structure is ported from vllm/v1/attention/ops/triton_unified_attention.py (the AITER unified attention kernel already upstreamed into vLLM). Only the K and V load sites are replaced: instead of reading raw fp16 keys/values from two contiguous caches, this kernel reads TurboQuant-packed bytes from a single combined cache and dequantizes on the fly inside the tile loop.

Benefits over the v1/v2 decode-only kernels:

  1. GQA heads are stacked into BLOCK_M and the Q·K and P·V ops are proper tl.dot tensor-core operations (MFMA on MI300X).
  2. The same kernel handles decode (BLOCK_Q=1) and prefill (BLOCK_Q>1) -- no more Python per-request for-loop for continuation chunks.
  3. Only the subset of features exercised by the current TQ paths is kept (causal, GQA). Sinks / softcap / ALiBi / sliding-window / qq-bias / mm-prefix are deferred to follow-ups.

This is an opt-in v3 path behind VLLM_TQ_DECODE_V3.

Functions:

_get_pair_lut(centroids)

Return a fresh pair-LUT for centroids on each call.

Previously cached by (data_ptr, device) — that key is unsafe because the CUDA allocator can reuse freed addresses for different centroid tensors, yielding a stale (wrong-values) LUT. The LUT is tiny (NN2 fp32, ~2KB at N=16) so unconditional rebuild is essentially free compared to attention work. If this shows up on a profile, replace with a hash-of-values fingerprint, not data_ptr.

_tq_fuse_q_rotation(Q, PiT_ptr, PiT_stride_0, PiT_stride_1, dim_mask, HEAD_SIZE_PADDED)

Fused Q @ PiT prologue. Called once per program for the MSE-key path when the launcher has passed the raw (un-rotated) query. Returns the rotated Q in the original dtype.

_tq_load_k_tile(KV_cache_ptr, KV_cache_u16_ptr, data_bases, knorm_u16_addrs, d_offs, d_mask, tile_mask, Centroids_ptr, Pair_lut_ptr, OUT_DTYPE, HEAD_DIM, BLOCK_D, MSE_BITS, N_CENTROIDS, KEY_FP8, USE_PAIR_LUT, NORM_CORRECTION, FP8_E4B15, TILE_SIZE)

Load + dequantize a TILE_SIZE × HEAD_SIZE block of keys and return the transposed tile K_T : [HEAD_SIZE_PADDED, TILE_SIZE].

Opt#3 SoA layout: packed K data is at data_bases[t] + [0, MSE_BYTES) for MSE keys (or [0, D) for FP8). The per-token K-norm lives in the per-block SoA metadata region; knorm_u16_addrs already encodes its u16 element index. For decode tiles aligned with blocks, these addresses are contiguous → one coalesced wide load replaces TILE_SIZE scattered loads (the whole point of Opt#3).

_tq_load_v_tile(KV_cache_ptr, KV_cache_u16_ptr, val_bases, vscale_u16_addrs, vzero_u16_addrs, d_offs, d_mask, tile_mask, OUT_DTYPE, HEAD_DIM, VQB)

Load + dequantize a TILE_SIZE × HEAD_SIZE block of values.

Opt#3 SoA layout: packed V data is at val_bases[t] + [0, VAL_DATA_BYTES) (V-data immediately follows K-data within the slot's data region, and val_bases is precomputed by the caller as data_base + KEY_DATA_BYTES). V-scale / V-zero live in the per-block SoA metadata region at indices vscale_u16_addrs / vzero_u16_addrs. For tiles aligned to block boundaries, those addresses are contiguous → one coalesced wide load per field instead of TILE_SIZE scattered 2-byte loads.

triton_turboquant_decode_attention_soa(query, kv_cache, block_table, seq_lens, Pi, centroids, scale, mse_bits, key_packed_size, value_quant_bits, value_packed_size, key_fp8=False, norm_correction=False, PiT=None, max_seq_len=0, mid_o_buf=None, output_buf=None, lse_buf=None, buf_holder=None, max_num_kv_splits=32, sinks=None)

Decode-only convenience wrapper around triton_turboquant_unified_attention.

Treats a rank-3 [B, Hq, D] query as one token per request (query_len = 1) and synthesizes a query_start_loc of [0, 1, 2, ..., B]. max_num_kv_splits is forwarded to the unified launcher as the 3D split-KV segment count (capped per-call against max_seq_len). sinks (optional [Hq] fp32) are forwarded to the kernel which folds them into the softmax denominator via the init-time trick.

triton_turboquant_unified_attention(query, kv_cache, block_table, seq_lens, query_start_loc, Pi, centroids, scale, mse_bits, key_packed_size, value_quant_bits, value_packed_size, key_fp8=False, norm_correction=False, PiT=None, output=None, tile_size=None, max_query_len=None, max_seq_len=None, num_kv_splits=None, force_2d=False, fuse_q_rot=True, sinks=None)

Launch unified TQ attention (v3).

query carries raw query vectors. For the MSE-key path the query has to be rotated by PiT before it can be multiplied against the (already-rotated) stored K. By default (fuse_q_rot=True) that rotation is done inside the attention kernel prologue as a single small MFMA — no extra dispatch, no HBM round-trip. Setting fuse_q_rot=False restores the original launcher path (fp32 rocBLAS GEMM + casts + .contiguous()), which is kept as an A/B toggle for bench harnesses; numerical results match the fused path to within a few ulp of fp32 round-off. The FP8-key path never rotates Q regardless of this flag.

tile_size defaults to 32 for prefill (max_query_len > 1) and 16 for pure decode (max_query_len == 1). Callers may override.

Dispatch rule
  • Prefill / chunked-prefill (any query block with BLOCK_Q > 1) and num_tokens > num_seqs: 2D kernel (plenty of CTAs already).
  • Pure decode with long KV: 3D split-KV kernel + reduce_segments for extra parallelism; this is the same pattern v2 uses.
  • force_2d=True: force the 2D path regardless (used by the apples-to-apples bench to isolate pure TQ dequant overhead).

num_kv_splits controls the 3D-split segment count (default 16).

Returns output of shape [num_tokens, Hq, D] in query.dtype.