flash_attention_tq4_kv
turboquant_vllm.triton.flash_attention_tq4_kv ¶
Fused TQ4 Flash Attention -- both K and V decompressed in the inner loop.
Phase 3 of the P5 roadmap. 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.
Attributes:
| Name | Type | Description |
|---|---|---|
triton_flash_attention_tq4_kv |
Tensor
|
Python wrapper that pre-rotates Q, launches the fused kernel, and post-rotates the output. |
Examples:
from turboquant_vllm.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,
)
See Also
:mod:turboquant_vllm.triton.flash_attention_tq4: Phase 2 (K-only).
Functions¶
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,
) -> 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. Non-pow2 dims incur ~5-15 % throughput
penalty due to wasted lanes in padded tiles (e.g., head_dim=96 pads
to 128, wasting 25 % of memory bandwidth on K/V loads).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query |
required |
k_packed
|
Tensor
|
Nibble-packed key indices |
required |
k_norms
|
Tensor
|
Key norms |
required |
v_packed
|
Tensor
|
Nibble-packed value indices |
required |
v_norms
|
Tensor
|
Value norms |
required |
centroids
|
Tensor
|
Shared Lloyd-Max codebook |
required |
rotation
|
Tensor
|
Shared orthogonal rotation |
required |
sm_scale
|
float | None
|
Softmax scale. Defaults to |
None
|
is_causal
|
bool
|
Apply causal masking. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention output |
Source code in src/turboquant_vllm/triton/flash_attention_tq4_kv.py
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 346 347 348 349 350 351 352 353 354 355 | |