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.
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.
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
224 225 226 227 228 229 230 231 232 233 234 235 236 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 | |