KV Cache
KeyValueCache and KeyValueCacheEntry provide small containers for
autoregressive key/value activations. They are intentionally simple so model
adapters can expose cache state without binding SafeLens to a specific
transformers implementation.
Supported operations:
- Lazy per-layer entry creation through
cache[layer]. - Sequence-axis append through
append(layer, keys, values). - Best-effort
sequence_lengthinference for tensor-like and nested-list data. - Serialization-friendly
to_dict().
Example:
from SafeLens.core.kv_cache import KeyValueCache
cache = KeyValueCache()
cache.append(0, keys=[[[1]]], values=[[[2]]])
cache.append(0, keys=[[[3]]], values=[[[4]]])
assert cache[0].keys == [[[1], [3]]]
assert cache[0].sequence_length == 2
TransformerLens-compatible key/value cache containers.
KeyValueCache
dataclass
Dictionary-like key/value cache keyed by layer index.
Source code in src/SafeLens/core/kv_cache.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 | |
append(layer, keys, values, *, dim=1)
Append key/value activations for one layer.
Source code in src/SafeLens/core/kv_cache.py
170 171 172 | |
append_attention_mask(attention_mask)
Append a batch attention mask and return the full mask.
Source code in src/SafeLens/core/kv_cache.py
186 187 188 189 190 191 192 193 194 195 196 197 198 | |
freeze()
Prevent future appends from mutating this cache.
Source code in src/SafeLens/core/kv_cache.py
174 175 176 177 178 | |
init_cache(cfg, device, batch_size=1)
classmethod
Create an empty cache following TransformerLens' init_cache API.
Source code in src/SafeLens/core/kv_cache.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
to_dict()
Return a serializable view.
Source code in src/SafeLens/core/kv_cache.py
200 201 202 203 204 | |
unfreeze()
Allow future appends to mutate this cache.
Source code in src/SafeLens/core/kv_cache.py
180 181 182 183 184 | |
KeyValueCacheEntry
dataclass
Cache entry for one layer's key and value activations.
Source code in src/SafeLens/core/kv_cache.py
12 13 14 15 16 17 18 19 20 21 22 23 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 | |
past_keys
property
writable
TransformerLens name for cached keys.
past_values
property
writable
TransformerLens name for cached values.
sequence_length
property
Return cached sequence length when shape is available.
append(keys, values, *, dim=1)
Append new key/value tensors along the sequence dimension.
Source code in src/SafeLens/core/kv_cache.py
98 99 100 101 102 103 104 105 | |
init_cache_entry(cfg, device, batch_size=1)
classmethod
Create an empty TL-layout entry shaped [batch, 0, heads, d_head].
Source code in src/SafeLens/core/kv_cache.py
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 | |
to_dict()
Return a serializable view.
Source code in src/SafeLens/core/kv_cache.py
113 114 115 | |
concat_values(old, new, *, dim=1)
Concatenate tensor-like or nested-list values.
Source code in src/SafeLens/core/kv_cache.py
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 | |
shape_of(value)
Return best-effort shape.
Source code in src/SafeLens/core/kv_cache.py
249 250 251 252 253 254 255 256 257 258 | |