triton
turboquant_vllm.triton ¶
Fused Triton kernels for TurboQuant compressed attention.
Phase 1 (P5): Vanilla Flash Attention kernel with GQA support. Phase 2 (P5): Fused TQ4 K decompression inside the FA inner loop. Phase 3 (P5): Fused TQ4 K+V decompression with post-rotation. Phase 3c.8: Standalone TQ4 cache decompress kernel for vLLM backend. Phase 3c.9: Standalone TQ4 compress kernel for vLLM backend.
Legacy: Q@K^T-only fused kernel (superseded -- see Key Lesson #7).
Attributes:
| Name | Type | Description |
|---|---|---|
triton_flash_attention |
Tensor
|
Vanilla FA forward with online softmax. |
triton_flash_attention_tq4 |
Tensor
|
Fused TQ4 FA with compressed K tiles. |
triton_flash_attention_tq4_kv |
Tensor
|
Fused TQ4 FA with compressed K+V tiles. |
triton_fa_forward |
tuple[Tensor, None]
|
HF AttentionInterface-compatible wrapper. |
register_triton_fa |
None
|
Register the |
install_triton_fa |
None
|
Register and activate vanilla FA on a model. |
install_fused_tq4_kv |
None
|
Activate fused TQ4 K+V with cache side-channel. |
uninstall_fused_tq4_kv |
None
|
Remove fused attention and restore SDPA. |
tq4_compress |
None
|
Fused TQ4 compress (norm+rotate+quantize+pack). |
tq4_decompress |
None
|
Fused TQ4 decompress (unpack+gather+scale). |
fused_qk_scores |
Tensor
|
Legacy Q@K^T-only kernel (kept for reference). |
Examples:
Direct kernel usage:
HuggingFace integration:
from turboquant_vllm.triton import install_triton_fa
install_triton_fa(model)
output = model.generate(...)
See Also
:mod:turboquant_vllm.kv_cache: CompressedDynamicCache storage layer.
Functions¶
install_fused_tq4_kv ¶
install_fused_tq4_kv(model: Module, cache: CompressedDynamicCache) -> None
Activate fused TQ4 K+V attention on model with cache side-channel.
Registers the triton_fa_tq4_kv backend, stashes cache on each
attention layer as module._tq4_cache, sets the model's
_attn_implementation, and enables fused_mode on the cache
to skip wasted decompression (P5b optimization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
HuggingFace model with attention layers that have |
required |
cache
|
CompressedDynamicCache
|
CompressedDynamicCache instance that stores compressed K/V. |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If model has no |
Source code in src/turboquant_vllm/triton/attention_interface.py
install_triton_fa ¶
Register the backend and activate it on model.
Changes model.config._attn_implementation to "triton_fa".
The model resolves the attention function at forward time, so this
takes effect on the next forward call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
A HuggingFace model with a |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If model has no |
Source code in src/turboquant_vllm/triton/attention_interface.py
register_triton_fa ¶
Register triton_fa as a global attention backend in HuggingFace.
Safe to call multiple times -- overwrites the previous registration.
Source code in src/turboquant_vllm/triton/attention_interface.py
triton_fa_forward ¶
triton_fa_forward(
module: Module,
query: Tensor,
key: Tensor,
value: Tensor,
attention_mask: Optional[Tensor],
dropout: float = 0.0,
scaling: Optional[float] = None,
**kwargs: object,
) -> tuple[Tensor, None]
HF-compatible attention forward using Triton Flash Attention.
Signature matches transformers.integrations.sdpa_attention.sdpa_attention_forward.
Handles GQA natively (no KV repeat expansion needed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The attention layer module. Used to read |
required |
query
|
Tensor
|
|
required |
key
|
Tensor
|
|
required |
value
|
Tensor
|
|
required |
attention_mask
|
Optional[Tensor]
|
Optional additive mask |
required |
dropout
|
float
|
Dropout rate (must be 0 -- inference only). |
0.0
|
scaling
|
Optional[float]
|
Softmax scale. Defaults to |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
is_causal |
bool | None
|
Override causal mode. If |
**kwargs |
object
|
Additional model-specific arguments (ignored). |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
None
|
(transposed to match HF convention). |
Source code in src/turboquant_vllm/triton/attention_interface.py
triton_fa_tq4_kv_forward ¶
triton_fa_tq4_kv_forward(
module: Module,
query: Tensor,
key: Tensor,
value: Tensor,
attention_mask: Optional[Tensor],
dropout: float = 0.0,
scaling: Optional[float] = None,
**kwargs: object,
) -> tuple[Tensor, None]
Fused TQ4 K+V attention via cache side-channel.
Reads compressed K/V from the CompressedDynamicCache stashed on
module._tq4_cache (ignoring the decompressed key/value args).
Falls back to vanilla Triton FA if no cache reference is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
Attention layer with |
required |
query
|
Tensor
|
|
required |
key
|
Tensor
|
|
required |
value
|
Tensor
|
|
required |
attention_mask
|
Optional[Tensor]
|
Optional additive mask. |
required |
dropout
|
float
|
Must be 0 (inference only). |
0.0
|
scaling
|
Optional[float]
|
Softmax scale. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
is_causal |
bool | None
|
Override causal mode. |
**kwargs |
object
|
Additional model-specific arguments (ignored). |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, None]
|
|
Source code in src/turboquant_vllm/triton/attention_interface.py
uninstall_fused_tq4_kv ¶
Remove fused TQ4 attention and restore SDPA.
Removes _tq4_cache from attention layers, disables fused_mode
on the cache, and resets _attn_implementation to "sdpa".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Model previously configured with |
required |
Source code in src/turboquant_vllm/triton/attention_interface.py
triton_flash_attention ¶
triton_flash_attention(
q: Tensor,
k: Tensor,
v: Tensor,
sm_scale: float | None = None,
is_causal: bool = False,
attention_mask: Tensor | None = None,
) -> Tensor
Compute scaled dot-product attention using Triton Flash Attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor |
required |
k
|
Tensor
|
Key tensor |
required |
v
|
Tensor
|
Value tensor |
required |
sm_scale
|
float | None
|
Softmax scale factor. Defaults to |
None
|
is_causal
|
bool
|
Apply causal masking. Only valid when |
False
|
attention_mask
|
Tensor | None
|
Optional additive mask
|
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention output |
Source code in src/turboquant_vllm/triton/flash_attention.py
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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
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 | |
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.
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
217 218 219 220 221 222 223 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 306 307 | |
fused_qk_scores ¶
fused_qk_scores(
q_rotated: Tensor,
packed_indices: Tensor,
norms: Tensor,
centroids: Tensor,
scale: float,
*,
n_q_heads: int,
n_kv_heads: int,
) -> Tensor
Compute attention scores from pre-rotated queries and nibble-packed keys.
The query must be pre-rotated by the TurboQuant rotation matrix
(q_rot = q @ Pi_T) so the kernel avoids the expensive 128x128
rotation matmul in its inner loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q_rotated
|
Tensor
|
Pre-rotated query, shape
|
required |
packed_indices
|
Tensor
|
Nibble-packed 4-bit key indices, shape
|
required |
norms
|
Tensor
|
Key vector norms, shape
|
required |
centroids
|
Tensor
|
Lloyd-Max centroid values, shape |
required |
scale
|
float
|
Attention scale factor (typically |
required |
n_q_heads
|
int
|
Number of query attention heads. |
required |
n_kv_heads
|
int
|
Number of key-value attention heads. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention scores, shape |
Source code in src/turboquant_vllm/triton/fused_qk_attention.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 | |