kempnerforge.model.attention¶
Multi-head attention with Grouped-Query Attention (GQA) support.
- GQA is the general case:
n_kv_heads == n_heads → standard Multi-Head Attention (MHA)
n_kv_heads == 1 → Multi-Query Attention (MQA)
1 < n_kv_heads < n_heads → Grouped-Query Attention (GQA)
Classes
Grouped-Query Attention with RoPE and SDPA. |
|
Pre-allocated KV cache for autoregressive generation. |
- class kempnerforge.model.attention.KVCache[source]¶
Bases:
objectPre-allocated KV cache for autoregressive generation.
Stores key and value tensors for all previous positions, enabling incremental decoding without recomputing attention over the full sequence. Keys are stored after RoPE application but before GQA expansion.
- __init__(batch_size, max_seq_len, n_kv_heads, head_dim, dtype, device)[source]¶
- Parameters:
batch_size (int)
max_seq_len (int)
n_kv_heads (int)
head_dim (int)
dtype (torch.dtype)
device (torch.device)
- Return type:
None
- update(k_new, v_new)[source]¶
Append new key/value entries and return full cached tensors.
- Parameters:
k_new (torch.Tensor) – New keys, shape (batch, n_kv_heads, new_seq_len, head_dim).
v_new (torch.Tensor) – New values, shape (batch, n_kv_heads, new_seq_len, head_dim).
- Returns:
Tuple of (all_keys, all_values), each (batch, n_kv_heads, total_seq_len, head_dim).
- Return type:
- keep_rows(rows)[source]¶
Keep only the batch rows
rows, in that order, e.g. to drop finished sequences.- Parameters:
rows (torch.Tensor)
- Return type:
None
- class kempnerforge.model.attention.Attention[source]¶
Bases:
ModuleGrouped-Query Attention with RoPE and SDPA.
- __init__(dim, n_heads, n_kv_heads, head_dim=None, qk_norm=False, sdpa_backend='auto', norm_eps=1e-05)[source]¶
- forward(x, rope_cos, rope_sin, *, kv_cache=None, attn_mask=None)[source]¶
Forward pass.
Attention consumes the visibility mask; it never derives one. Every visibility rule (document isolation, bidirectional groups, key validity, and their combination) is expressed by
kempnerforge.model.masking, which builds this one object once per forward and shares it across layers. Without a mask, attention is causal, aligned to the end of the key axis when tokens are appended to a filledkv_cache.- Parameters:
x (torch.Tensor) – Input tensor of shape (batch, seq_len, dim).
rope_cos (torch.Tensor) – RoPE cosine frequencies, shape (seq_len, head_dim // 2) shared across the batch or (batch, seq_len, head_dim // 2) per row.
rope_sin (torch.Tensor) – RoPE sine frequencies, same shape as
rope_cos.kv_cache (KVCache | None) – Optional KV cache for incremental generation. The tokens of this forward are appended to it, and without
attn_maskeach attends to every cached key and to the appended keys up to its own position.attn_mask (torch.Tensor | BlockMask | None) – The mask from
kempnerforge.model.masking.build_attention_mask: a FlexAttentionBlockMask, a dense bool tensor broadcastable to (batch, n_heads, seq_len, seq_len) whereTrue= attend, orNonefor plain causal attention. Withkv_cacheits key axis covers the cached keys and the appended ones, so aBlockMask, which is square, needs an empty cache.
- Returns:
Output tensor of shape (batch, seq_len, dim).
- Raises:
ValueError – If
attn_mask’s key axis does not cover the keyskv_cacheholds after this forward, or a denseattn_maskcomes withsdpa_backend="flash"on CUDA.NotImplementedError – If
capture_attention_weightsis set andattn_maskis aBlockMask.
- Return type: