Skip to content
class

StaticCache

extendsCache
StaticCache(max_cache_len: int)

A bounded cache backed by fixed pre-allocated buffers written in place.

Parameters

max_cache_lenint
Maximum sequence length the buffers can hold. Must not exceed the model's positional range.

Attributes

max_cache_lenint
Fixed sequence capacity of every layer's buffer.
key_cachelist[Tensor]
Per-layer key buffers, each (B, num_heads, max_cache_len, head_dim) (lazily allocated on the first update).
value_cachelist[Tensor]
Per-layer value buffers, same shapes.

Notes

get_seq_length is tracked by a per-layer counter (not inferred from the buffer shape, which is always max_cache_len). update stores into the full buffer; when the consumer opts in (passes a read_len in cache_kwargs), it returns a filled-prefix view so attention attends only over real keys — O(filled), not O(max_cache_len). The stored buffer stays full, so the fixed buffer shape is preserved.

Used by 2

Constructors

1
dunder

__init__

→None
__init__(max_cache_len: int)

Create an empty static cache with capacity max_cache_len.

Properties

1
prop

seen_tokens

→int
seen_tokens: int

Total number of tokens written (tracked from layer_idx == 0).

Instance methods

8
fn

batch_repeat_interleave

→None
batch_repeat_interleave(repeats: int)

Repeat every batch row repeats times along the batch dimension.

fn

batch_select_indices

→None
batch_select_indices(indices: Tensor)

Keep only the batch rows named by indices along the batch dim.

fn

crop

→None
crop(max_length: int)

Logically truncate the cache to max_length tokens.

The buffer is fixed-size, so cropping only rewinds the length counters (cropped positions become 'future' and are masked by the causal mask). A negative max_length drops that many tokens off the end.

fn

get_max_cache_shape

→int | None
get_max_cache_shape()

Return the fixed buffer capacity max_cache_len (always bounded).

fn

get_seq_length

→int
get_seq_length(layer_idx: int = 0)

Number of tokens written so far for layer_idx, read from the per-layer counter (the buffer shape is always max_cache_len, so the count cannot be inferred from it).

fn

reorder_cache

→None
reorder_cache(beam_idx: Tensor)

Reindex every layer's buffers along the batch dimension with beam_idx (in place) to keep beams aligned after a beam permutation.

fn

reset

→None
reset()

Zero every buffer and reset the length counters in place.

fn

update

→tuple of (Tensor, Tensor)
update(key_states: Tensor, value_states: Tensor, layer_idx: int, cache_kwargs: dict[str, object] | None = None)

Write key_states / value_states into the fixed buffer at cache_position and return a filled-prefix view of it.

The write goes through the traceable lucid.index_copy (not in-place __setitem__), so the decode forward stays compile-visible and the stored buffer keeps a constant max_cache_len shape every step. The returned keys/values are narrowed (dim 2) to the filled-prefix width (the read_len in cache_kwargs) so attention attends over the real keys, not the zero-padded tail — recovering O(filled) attention cost.

Parameters

key_statesTensor
New key projections of shape (B, num_heads, T_new, head_dim).
value_statesTensor
New value projections of the same shape.
layer_idxint
Index of the transformer layer this update belongs to (buffers are lazily allocated on the first call for a layer).
cache_kwargsdict or None= None
May carry "cache_position" (a fixed-shape int64 Tensor of absolute write indices; absent → positions from the running counter) and "read_len" (an int — narrow the returned view to this width; the decoder-only attention passes past_len + T here to attend over only the filled prefix). When no read_len is given the full buffer is returned (back-compatible).

Returns

tuple of (Tensor, Tensor)

The (keys, values) for layer_idx, narrowed on dim 2 to the opted-in read_len when provided, else the full (B, num_heads, max_cache_len, head_dim) buffer. A narrowed view is equivalent to the full buffer with the unwritten tail masked.

Dunder methods

3
dunder

__getitem__

→tuple[Tensor, Tensor]
__getitem__(layer_idx: int)

Return the full (keys, values) buffers for layer_idx.

dunder

__iter__

→Iterator[tuple[Tensor, Tensor]]
__iter__()

Iterate the per-layer (keys, values) buffers.

dunder

__len__

→int
__len__()

Number of layers currently allocated.