Skip to content
abstract

Cache

extendsABC
Cache()

Abstract base class for all key/value caches used in attention.

A cache stores, per transformer layer, the key and value projections of every token seen so far so that an incremental (one-token-at-a-time) forward pass only has to project the new token's key/value and attend against the accumulated history — turning an O(T2)O(T^2) re-encode of the whole prefix at every step into an O(T)O(T) update.

Subclasses implement update (the per-layer write/read), plus the length-introspection and beam-reordering hooks. Concrete subclasses are lucid.utils.cache.DynamicCache (the default growing cache), lucid.utils.cache.StaticCache (a fixed pre-allocated buffer for compiled decoding), and lucid.utils.cache.EncoderDecoderCache (paired self- + cross-attention).

Used by 4

Instance methods

5
abstract

get_max_cache_shape

→int | None
get_max_cache_shape()

Return the maximum sequence length the cache can hold.

None for an unbounded (dynamically growing) cache.

abstract

get_seq_length

→int
get_seq_length(layer_idx: int = 0)

Return the number of cached tokens for layer_idx (0 if empty).

fn

get_usable_length

→int
get_usable_length(new_seq_length: int, layer_idx: int = 0)

Return how many cached tokens can be used given new_seq_length.

For a bounded cache this clamps so that previous + new never exceeds get_max_cache_shape; for an unbounded cache it is simply the current sequence length.

Parameters

new_seq_lengthint
Number of new tokens about to be appended this step.
layer_idxint= 0
Layer whose current length is queried.

Returns

int

The count of already-cached tokens that remain usable once the new_seq_length tokens are accounted for against the cap.

abstract

reorder_cache

→None
reorder_cache(beam_idx: Tensor)

Reindex the cache along the batch dimension with beam_idx.

Used by beam search to keep each beam's cache aligned after the per-step beam permutation. Mutates the cache in place.

abstract

update

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

Append key_states / value_states for layer_idx and return the full cached key/value tensors.

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.
cache_kwargsdict or None= None
Extra per-cache arguments (e.g. rotary cache_position). Ignored by the dynamic cache; reserved for parity with other cache implementations.

Returns

tuple of (Tensor, Tensor)

The accumulated (keys, values) for layer_idx, each of shape (B, num_heads, T_total, head_dim).