cached_attention
cached_attention
¶
Attention with generation and conditioning cache support.
CachedAttention
¶
CachedAttention(embedding_dimension, number_of_heads, number_of_key_value_heads=None, head_dimension=None, dropout=0.0, bias=True, attention_type=value)
Bases: Module
Base attention module with KV cache support.
Supports both Multi-Head Attention (MHA) and Grouped Query Attention (GQA). Can be used for self-attention or cross-attention.
Initialize cached attention module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Model embedding dimension |
required |
number_of_heads
|
int
|
Number of query heads |
required |
number_of_key_value_heads
|
int | None
|
Number of key/value heads (for GQA) |
None
|
head_dimension
|
int | None
|
Per-head dimension. Defaults to embedding_dimension // number_of_heads. Override for architectures where hidden_size != number_of_heads * head_dim (e.g. Gemma2). |
None
|
dropout
|
float
|
Dropout probability for attention weights |
0.0
|
bias
|
bool
|
Whether to include bias in projections |
True
|
attention_type
|
str
|
Type of attention (use AttentionType enum values) |
value
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If dimensions don't match or invalid attention type |
Source code in src/versatil/models/layers/transformer/attention/cached_attention.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
compute_query
¶
Project and reshape query input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_input
|
Tensor
|
(B, query_len, D) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Projected queries (B, number_of_heads, query_len, head_dim). |
Source code in src/versatil/models/layers/transformer/attention/cached_attention.py
compute_key
¶
Project and reshape key input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_input
|
Tensor
|
(B, key_len, D) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Projected keys (B, kv_heads, key_len, head_dim). |
Source code in src/versatil/models/layers/transformer/attention/cached_attention.py
compute_value
¶
Project and reshape value input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_input
|
Tensor
|
(B, value_len, D) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Projected values (B, kv_heads, value_len, head_dim). |
Source code in src/versatil/models/layers/transformer/attention/cached_attention.py
compute_query_key_value
¶
Project inputs to query, key, value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_input
|
Tensor
|
Query input (B, query_len, D) |
required |
key_input
|
Tensor
|
Key input (B, key_len, D) |
required |
value_input
|
Tensor
|
Value input (B, value_len, D) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of (queries, keys, values). Queries: (B, number_of_heads, query_len, head_dim). |
Tensor
|
Keys/values: (B, kv_heads, key_len, head_dim). |
Source code in src/versatil/models/layers/transformer/attention/cached_attention.py
compute_attention
¶
Compute scaled dot-product attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
Tensor
|
Query tensor (B, number_of_heads, query_len, head_dim) |
required |
keys
|
Tensor
|
Key tensor (B, kv_heads, key_len, head_dim) - compact for GQA |
required |
values
|
Tensor
|
Value tensor (B, kv_heads, value_len, head_dim) - compact for GQA |
required |
attention_mask
|
Tensor | None
|
Optional bool mask (B, 1, query_len, key_len) where True means masked. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention output (B, query_len, embedding_dimension) |
Source code in src/versatil/models/layers/transformer/attention/cached_attention.py
forward
¶
forward(query_input, key_input=None, value_input=None, attention_mask=None, generation_cache=None, positional_encoding=None, conditioning_cache=None)
Forward pass with optional generation and conditioning caches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_input
|
Tensor
|
Query input (B, query_len, D). |
required |
key_input
|
Tensor | None
|
Key input (B, key_len, D). None when using conditioning_cache. |
None
|
value_input
|
Tensor | None
|
Value input (B, value_len, D). None when using conditioning_cache. |
None
|
attention_mask
|
Tensor | None
|
Bool mask (B, 1, query_len, key_len), True = masked. |
None
|
generation_cache
|
GenerationLayerCache | None
|
Cached K/V from the main sequence. When provided, an updated cache is returned. |
None
|
positional_encoding
|
Module | None
|
Optional RoPE module. |
None
|
conditioning_cache
|
ConditioningLayerCache | None
|
Precomputed K/V for static conditioning. When present, key_input/value_input are ignored and cached K/V is used directly. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, GenerationLayerCache | None]
|
Tuple of (output (B, query_len, D), updated GenerationLayerCache or None). |