Caches

A Cache is persistent tensor storage owned by a runtime session. Declare it in the outer graph. Its shape is a fixed capacity, and its dtype specifies storage.

Cache(*, name, shape, dtype, mode="overwrite", axis=None, initial=0)

Names must be unique. axis is required for append/sliding caches and omitted for overwrite caches.

Modes

ModeInitial stateUpdate behavior
overwriteFull declared shape, filled with initialassign replaces the full tensor
appendLogically emptyappend extends it; overflow is an error
slidingLogically emptyappend evicts old entries when capacity is reached

Sliding reads expose retained entries in chronological order.

Read and write

from redwood.rdl import *

with Graph() as graph:
    x = Input(name="x", shape=(4,), dtype="fp32")
    state = Cache(name="state", shape=(4,), dtype="fp32")
    updated = Add(state.read(), x)
    state.assign(updated)
    Output(updated, name="y")

This accumulates an input vector across successful calls. read() produces a logical snapshot; a later write cannot change that tensor’s earlier value.

assign(value) replaces logical contents. Overwrite caches require the full shape. Append/sliding caches allow a shorter length along their append axis and set their logical length to it.

append(value) is available for append and sliding modes. Other dimensions must agree with the cache. The compiler preserves read/write ordering and may avoid physical copies when doing so is safe.

KV cache layout

Use [tokens, heads, head_dim] with the token axis outermost:

key = Cache(
    name="key", shape=(4096, 8, 128), dtype="bf16",
    mode="append", axis=0,
)

This is a declaration inside an outer graph. Append the new key projection, then read the retained keys for attention. GQA uses the KV head count here, which can differ from the query head count.

Values written to a cache may have a different dtype. The compiler inserts the required storage conversion. Choose activation precision separately from cache storage precision.

Prefix reuse

ReusePrefix(tokens, history, *, caches, reset=None)

Use this composition when the caller supplies the complete prompt prefix each time. history is a rank-one, axis-zero append cache of processed tokens. caches contains the corresponding attention/recurrent caches.

It finds the reusable prefix, trims or resets state as needed, and returns the position from which prefill should continue. It can replay the last prompt token when its logits were not retained.

A recurrent state cannot generally be rolled back by shortening a tensor. When necessary, ReusePrefix clears state and starts replay from the beginning. Sliding caches cannot provide the full history this helper requires.

The optional reset input forces a fresh start. Reset when other conditioning changes, even if token IDs happen to match.

Bespoke updates

For an unusual pattern, read the cache, compute updated values with ordinary tensor operations, and call assign. For example, use functional Scatter to replace selected state entries.

The compiler can use available kernels to optimize the resulting tensor operations.

Caches persist while the instance is open. Your application stores conversation history. See sessions for the application contract.