Triton API¶
Triton kernels for contiguous compressed decode blocks.
UnsupportedFormatError ¶
Bases: RuntimeError
Raised when a requested compressed attention kernel is unavailable.
Fused TQ4 Flash Attention -- both K and V decompressed in the inner loop.
Both K and V tiles are decompressed inline from nibble-packed uint8 indices.
The query is pre-rotated by Pi^T and the output is post-rotated by Pi
outside the kernel (since K and V share the same rotation matrix).
Non-power-of-two HEAD_DIM (e.g., 96) is supported via padded tl.arange + masking.
Autotune configs include BLOCK_M values 16, 32, 64, 128 to cover head_dim up to 256.
Examples:
from turboquant.triton.flash_attention_tq4_kv import (
triton_flash_attention_tq4_kv,
)
out = triton_flash_attention_tq4_kv(
q,
k_packed,
k_norms,
v_packed,
v_norms,
centroids,
rotation,
sm_scale=None,
)
triton_flash_attention_tq4_kv ¶
triton_flash_attention_tq4_kv(q: Tensor, k_packed: Tensor, k_norms: Tensor, v_packed: Tensor, v_norms: Tensor, centroids: Tensor, rotation: Tensor, sm_scale: float | None = None, is_causal: bool = False) -> torch.Tensor
Fused TQ4 Flash Attention with both K and V compressed.
Pre-rotates Q by rotation^T, launches the kernel that decompresses
both K and V inline, then post-rotates the output by rotation to
return to the original coordinate space. Non-power-of-two head
dimensions (e.g., 96) are handled via padded tile loads and boundary
masking inside the kernel.
Args:
q: Query [batch, H_Q, seq_q, head_dim] fp16/bf16.
k_packed: Nibble-packed key indices [batch, H_KV, seq_kv, D//2] uint8.
k_norms: Key norms [batch, H_KV, seq_kv] or [..., 1] fp32.
v_packed: Nibble-packed value indices [batch, H_KV, seq_kv, D//2] uint8.
v_norms: Value norms [batch, H_KV, seq_kv] or [..., 1] fp32.
centroids: Shared Lloyd-Max codebook [16] fp32.
rotation: Shared orthogonal rotation [head_dim, head_dim] fp32.
sm_scale: Softmax scale. Defaults to 1 / sqrt(head_dim).
is_causal: Apply causal masking.
Returns:
Attention output [batch, H_Q, seq_q, head_dim] in original space.
Source code in turboquant/triton/flash_attention_tq4_kv.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
Fused paged TQ4 decode attention -- decompresses directly from page table.
This kernel reads TQ4-compressed blocks directly from vLLM's paged block table, decompresses in SRAM (nibble unpack -> centroid gather -> norm scale), and computes FP16 Q@K^T with online softmax in a single fused pass.
No HBM writes of decompressed cache -- HBM traffic drops from 1,160 to 136 bytes/token (8.5x reduction).
The kernel operates entirely in rotated space. The caller pre-rotates Q
by Pi^T and post-rotates the output by Pi. Decompression does NOT
apply rotation (matching tq4_decompress.py).
Scope: FP16/BF16 Q decode path only (USE_INT8_QK=False).
Autotune: 8 configs (BLOCK_N in {32, 64} x stages {2,3} x warps {4,8}).
Examples:
from turboquant.triton.fused_paged_tq4_attention import (
fused_paged_tq4_decode,
)
out = fused_paged_tq4_decode(
q,
kv_cache,
block_table,
seq_lens,
centroids,
rotation,
num_kv_heads=4,
head_dim=128,
block_size=16,
)
fused_paged_tq4_decode ¶
fused_paged_tq4_decode(q: Tensor, kv_cache: Tensor, block_table: Tensor, seq_lens: Tensor, centroids: Tensor, rotation: Tensor, num_kv_heads: int, head_dim: int, block_size: int, sm_scale: float | None = None, out: Tensor | None = None) -> torch.Tensor
Fused paged TQ4 decode attention.
Pre-rotates Q by rotation^T, launches the fused paged kernel
that decompresses TQ4 blocks in-tile from the page table, then
post-rotates the output by rotation to return to original space.
Args:
q: Query [num_seqs, H_Q, head_dim] fp16/bf16 (one token per seq).
kv_cache: Packed paged cache [num_blocks, block_size, total_bytes]
uint8.
block_table: Page table [num_seqs, max_num_blocks_per_seq] int32.
seq_lens: Sequence lengths [num_seqs] int32.
centroids: TQ4 codebook [16] fp32.
rotation: Orthogonal rotation [head_dim, head_dim] fp32.
num_kv_heads: Number of KV heads.
head_dim: Head dimension (e.g. 128).
block_size: vLLM page size (tokens per block).
sm_scale: Softmax scale. Defaults to 1 / sqrt(head_dim).
out: Optional pre-allocated output [num_seqs, H_Q, head_dim].
Returns:
Attention output [num_seqs, H_Q, head_dim] in original space.
Source code in turboquant/triton/fused_paged_tq4_attention.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |