Skip to content
class

EncoderDecoderCache

extendsCache
EncoderDecoderCache(self_attention_cache: _KVCache, cross_attention_cache: _KVCache, is_updated: dict[int, bool] | None = None)

Wraps a decoder self-attention cache and a cross-attention cache.

Parameters

self_attention_cacheDynamicCache or StaticCache
Grows by one position per decode step (decoder self-attention).
cross_attention_cacheDynamicCache or StaticCache
Filled once from the encoder memory then reused unchanged.
is_updated(dict[int, bool] or None, optional, keyword - only)= None
Per-layer cross-attention "already filled" flags. When None (default) they are inferred from cross_attention_cache's sequence length — correct for eager use. The compiled decode path passes them explicitly (all True) because a re-wrapped StaticCache resets its length counter, so length can no longer signal filled-ness.

Attributes

self_attention_cacheDynamicCache or StaticCache
cross_attention_cacheDynamicCache or StaticCache
is_updateddict[int, bool]
Per-layer flag marking whether the cross-attention key/value for that layer has already been computed; lets cross-attention skip the redundant memory projection on every step after the first.

Used by 1

Constructors

1
dunder

__init__

→None
__init__(self_attention_cache: _KVCache, cross_attention_cache: _KVCache, is_updated: dict[int, bool] | None = None)

Pair an existing self-attention and cross-attention cache.

Class methods

1
cls

from_legacy_cache

→Self
from_legacy_cache(past_key_values: tuple[tuple[Tensor, Tensor, Tensor, Tensor], ...] | None = None)

Build an EncoderDecoderCache from the legacy 4-tuple format.

Instance methods

9
fn

batch_repeat_interleave

→None
batch_repeat_interleave(repeats: int)

Repeat every batch row repeats times in both sub-caches.

fn

batch_select_indices

→None
batch_select_indices(indices: Tensor)

Keep only the batch rows named by indices in both sub-caches.

fn

crop

→None
crop(max_length: int)

Truncate the decoder self-attention cache to max_length tokens.

The cross-attention cache is the fixed-length encoder memory and is left untouched.

fn

get_max_cache_shape

→int | None
get_max_cache_shape()

Maximum decoder length, delegated to the self-attention sub-cache (None if it is a growing DynamicCache).

fn

get_seq_length

→int
get_seq_length(layer_idx: int = 0)

Decoder sequence length for layer_idx (from the self-attention sub-cache; the cross-attention length is fixed and not counted).

fn

reorder_cache

→None
reorder_cache(beam_idx: Tensor)

Reindex both sub-caches along the batch dimension with beam_idx (in place) so self- and cross-attention stay beam-aligned.

fn

reset

→None
reset()

Empty both sub-caches and clear the cross-attention 'updated' flags.

fn

to_legacy_cache

→tuple[tuple[Tensor, Tensor, Tensor, Tensor], ...]
to_legacy_cache()

Convert to the legacy per-layer (self_k, self_v, cross_k, cross_v) tuple format.

fn

update

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

Update the decoder self-attention cache for layer_idx.

Cross-attention key/value are written directly through the cross_attention_cache by the attention module (guarded by is_updated), not through this method — so this delegates only to the self-attention sub-cache.

Parameters

key_statesTensor
New decoder self-attention keys, (B, num_heads, T_new, head_dim).
value_statesTensor
New decoder self-attention values, same shape.
layer_idxint
Index of the decoder layer this update belongs to.
cache_kwargsdict or None= None
Forwarded to the self-attention sub-cache (e.g. a StaticCache reads "cache_position" from it).

Returns

tuple of (Tensor, Tensor)

The accumulated self-attention (keys, values) for layer_idx.

Dunder methods

3
dunder

__len__

→int
__len__()

Number of decoder layers (from the self-attention cache).