Attention API¶
Hybrid attention over compressed history and exact recent buffer.
Provides efficient attention computation combining: - Compressed historical KV (TurboQuant quantized) - Exact recent buffer (fp16/bf16)
Design: compressed path is only invoked when history is large enough to justify quantization overhead (>= MIN_HISTORY_FOR_TQ tokens).
FlatCache ¶
Bases: NamedTuple
Flattened view of compressed KV for fast read access.
HybridAttentionResult ¶
Bases: NamedTuple
Result of hybrid attention computation.
compute_hybrid_attention ¶
compute_hybrid_attention(query: Tensor, compressed_keys: Optional['ProdData'], compressed_values: Optional['ValueData'], recent_k: Optional[Tensor], recent_v: Optional[Tensor], quantizer: Optional['TorbuquantProd'], num_query_heads: int, num_kv_heads: int, head_dim: int, scale: Optional[float] = None, return_scores: bool = False) -> HybridAttentionResult
Compute attention output combining compressed history and exact recent buffer.
Args: query: (num_tokens, num_query_heads, head_dim) — typically num_tokens=1 for decode compressed_keys: ProdData from TurboQuantProd quantization, or None compressed_values: ValueData from value quantization, or None recent_k: (recent_len, num_kv_heads, head_dim) or None recent_v: (recent_len, num_kv_heads, head_dim) or None quantizer: TurboQuantProd instance for dequantization num_query_heads: total query heads (for GQA expansion) num_kv_heads: number of KV heads head_dim: dimension per head scale: attention scale factor (default: 1/sqrt(head_dim)) return_scores: whether to return attention scores
Returns: HybridAttentionResult with output: (num_tokens, num_query_heads, head_dim)
Source code in turboquant/attention/hybrid.py
compute_hybrid_attention_store ¶
compute_hybrid_attention_store(query: Tensor, flat_cache, key_compressor, value_compressor, recent_k: Optional[Tensor], recent_v: Optional[Tensor], num_query_heads: int, num_kv_heads: int, scale: Optional[float] = None) -> torch.Tensor
Compute hybrid attention using store-based compressed cache.
This variant works with the CompressedKVStore from kv/store.py.
Args: query: (num_tokens, num_query_heads, head_dim) query vectors flat_cache: FlatCache from CompressedKVStore.get_flat_cache() key_compressor: TurboQuantCompressorV2 instance value_compressor: TurboQuantCompressorMSE instance recent_k: (recent_len, num_kv_heads, head_dim) or None recent_v: (recent_len, num_kv_heads, head_dim) or None num_query_heads: total query heads num_kv_heads: number of KV heads scale: attention scale (default: 1/sqrt(head_dim))
Returns: output: (num_tokens, num_query_heads, head_dim)
Source code in turboquant/attention/hybrid.py
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 308 309 310 311 | |
online_softmax_attention ¶
online_softmax_attention(query: Tensor, keys: Tensor, values: Tensor, *, scale: float, block_size: int = 64) -> torch.Tensor
Online softmax attention over KV blocks.
Flash-attention style memory-efficient implementation.
Args: query: (Q, D) single query vector keys: (N, D) key vectors values: (N, D) value vectors scale: attention scale block_size: block size for online processing
Returns: output: (Q, D) attention output