Expand description
Persistent inference state for the pipelined executor (Phase 7.6 step 9c, sub-phase E.3.0).
§What this module owns
-
InferenceContext: a host-side per-session struct holding long-livedArc<RwLock<Storage>>references that survive across realize calls. Replaces the legacy executor’sconst_poolfor the pipelined dispatch world. Today’s backing is a simple in-memoryHashMap<NodeId, Arc<...>>; future work may swap this for an mmap-backed coherent store perproject_unified_durable_tensor_store.md. -
KvCache,KvLayer,KvLayerId,KvSlot,AuthorityState: the backend-erased KV cache primitive that replacedlazy::KVCache<B>andlazy::LlamaKVCache(both retired — E.3.3.D and Unification Session 4 E.3.4). Single concrete type with no generic-over-B parameter; each layer holdsArc<RwLock<Storage>>for K and V plus side-table metadata (layout, monotonic version, coherence authority).
§What this module deliberately does NOT do (yet)
- Pre-allocated buffers (added Phase E.3.3.A, 2026-05-20):
KvCache::with_capacityeagerly allocates[1, n_kv_heads, max_seq_len, head_dim]zero-initialized K + V buffers on a target device. Subsequent forward steps write into these viaOp::WriteSlicerather than growing-by-replacement. The legacyKvCache::with_dimsconstructor stays available for callers not yet on the pre-allocated path (grow-by-replace works without changes). - Multi-device coherence protocol: the
authorityandversionfields exist as placeholders. No protocol consults them yet — every layer starts and staysAuthorityState::Hostfor the lifetime of the session in single-device usage. Phase J (multi-GPU) activates the protocol. - Weight persistence: weights stay in the graph’s storage_map per the design discussion; the persistent map handles KV layers and transient cross-step state only.
Structs§
- Decode
Session - Plan-once persistent decode state for one
LlamaModel+ oneKvCachecapacity/dtype. Built on the firstseq == 1decode token; reused (graph + optimize view HELD, only the per-token data Consts +SymEnvre-bound) for every subsequent token. - Decode
Token Data - Per-token re-bound data-Const Arcs for a persistent decode step
(Phase D · D2b). Each is a device-resident
fuel_memory::Storagebuilt from freshly recomputed host bytes (token-ids = the new token, RoPE cos+sin atposition = cached_len, mask with the shifted-infboundary).DecodeSession::realize_tokenoverwrites the heldbase_cache’s stable data-Const entries with these for the token. - Inference
Context - Per-session host-side context holding long-lived storage Arcs that survive across realize calls.
- KvCache
- Backend-erased KV cache. Replaces
lazy::KVCache<B>andlazy::LlamaKVCache. Indexed by global layer index;layers[i]isNoneif layerihasn’t been populated yet. - KvLayer
- One layer’s K + V slots in a backend-erased KV cache.
- KvLayer
Id - Identifies a single key-or-value tensor within a
KvCache. Stable across realize calls — the graph-level NodeId may churn per step (eachOp::WriteSliceproduces a fresh NodeId pointing at the same Storage Arc) but the logical KV slot identity stays. - Latent
KvCache - Cross-graph, cross-forward-pass persistent N-slot decode cache —
the
KvCachegeneralization for latent-caching attention architectures. - Latent
Slot - One slot’s persistent latent buffer within a
LatentKvCachelayer. The per-slot analogue ofKvLayer’sk/vpair, generalized to an arbitrary slot count and independent per-slot trailing shape. - Paged
Decode Session - Plan-once persistent decode state for one PAGED (block-pool) decode
stream — the paged twin of
DecodeSession. Built on the firstseq == 1paged decode token; reused (graph + optimize view HELD, only the per-token data Consts +SymEnvre-bound) for every subsequent token. - Paged
Decode Token Data - Per-token re-bound data Consts for one PAGED decode step — the paged twin
of
DecodeTokenData. The shapes are stable across tokens (that is the whole point of the plan-once path); only the bytes change, so each is anArc<RwLock<Storage>>overwriting the heldPagedDecodeSession’s base-cache entry for its stable NodeId.
Enums§
- Authority
State - Coherence authority for one KV slot’s storage. Placeholder for the
multi-device protocol; today every slot starts and stays
AuthorityState::Hostfor the lifetime of the session unless future code explicitly transitions it. - KvSlot
- Which half of a KV pair a
KvLayerIdaddresses. - Paged
Decode Plan - Runtime policy for the paged persistent decode path
(
LlamaModel::forward_paged_step_persistent): whether to build the plan ONCE and reuse it across tokens, or re-plan every token (the pre-plan-once behavior). The paged driver holds this per config and passes it each step; the driver default isPlanOnce(set byPagedSessionScheduler::new), withReplanavailable as an explicit opt-out. - Plan
Validity - Whether a held plan may serve a step, and if not, why not — one set of
comparisons, so the “is it valid” and “is only the allocation different”
questions cannot drift apart.
DecodeSession::is_valid_foris a thin view over this. - Rebind
Refusal - Why a held plan could not be re-bound onto a replacement KV allocation.
Traits§
- KvRebind
Source - A KV allocation a held
DecodeSessioncan be re-bound onto — the one abstraction overKvCache(K/V pairs) andLatentKvCache(MLA’s(latent, kpe)pairs), which already shareDecodeSessionverbatim and therefore share its welding.
Functions§
- alloc_
batched_ kv - Allocate
n_layerspairs of zero-initialized[k, n_kv_heads, max_seq_len, head_dim]K/V buffers ondevicewithdtype— the shared batch-slot KV buffer for the multi-session batched decode arm (serving Increment 1, C3). SameOp::Alloc→Op::ZeroFillemission +PipelinedExecutor::realize_manypattern asKvCache::with_capacity, with the leading dimk(the batch slot) instead of1. Returns the per-layer(K_arc, V_arc)pairs; the caller binds them into a batched decode graph.