flash_attention_tq4
turboquant_vllm.triton.flash_attention_tq4 ¶
Fused TQ4 Flash Attention -- K decompression inside the FA inner loop.
Phase 2 of the P5 roadmap. Replaces the standard K tile load with:
nibble unpack -> centroid gather -> interleave -> norm scale. The query
is pre-rotated by Pi^T outside the kernel. Values remain standard
fp16/bf16. Non-power-of-two HEAD_DIM (e.g., 96) is supported via padded
tl.arange + masking.
The fp32 online softmax (m_i, l_i, acc) state machine prevents the
0.023/layer cosine drift that killed the Q@K^T-only kernel (Key Lesson #7).
Attributes:
| Name | Type | Description |
|---|---|---|
triton_flash_attention_tq4 |
Tensor
|
Python wrapper that pre-rotates Q and launches the fused TQ4 kernel. |
Examples:
from turboquant_vllm.triton.flash_attention_tq4 import (
triton_flash_attention_tq4,
)
out = triton_flash_attention_tq4(
q,
k_packed,
k_norms,
centroids,
rotation_matrix,
v,
)
See Also
:mod:turboquant_vllm.triton.flash_attention: Phase 1 vanilla kernel.
:mod:turboquant_vllm.quantizer: TurboQuantMSE rotation + quantization.
Functions¶
triton_flash_attention_tq4 ¶
triton_flash_attention_tq4(
q: Tensor,
k_packed: Tensor,
k_norms: Tensor,
centroids: Tensor,
rotation: Tensor,
v: Tensor,
sm_scale: float | None = None,
is_causal: bool = False,
) -> Tensor
Fused TQ4 Flash Attention with compressed K and standard V.
Pre-rotates Q by rotation^T, then launches the fused kernel that
decompresses nibble-packed K indices inline via centroid gather.
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 |
centroids
|
Tensor
|
Lloyd-Max codebook |
required |
rotation
|
Tensor
|
Orthogonal rotation matrix |
required |
v
|
Tensor
|
Values |
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.py
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 | |