mstar.engine.resources.kv.ring#
Ring KV storage: a fixed horizon of frame slots, overwritten in place.
The storage strategy Waypoint declares. KVSpec.resource_class dispatches
here on seeing a RingKVConfig, so a paged model never imports it and this
package never imports FlashInfer.
- class mstar.engine.resources.kv.ring.LayerRingCache(num_sessions, n_kv_heads, ring_frames, ring_buckets, d_head, tokens_per_frame, pinned_dilation, dtype, device)[source]#
Bases:
objectOne attention layer’s ring:
ring_framesframe slots of history plus one scratch frame at the tail, timesnum_sessionsresident sessions.Storage is a single
[2, 1, H_kv, num_sessions * capacity, D]tensor so that a commit is oneindex_copy_for K and V together and the read is oneunbind(0)into two views – no copy on the read path.capacityis ONE session’s token slots;total_slotsis the token-dim length of the buffer.- Parameters:
- property memory_bytes: int#
Resident bytes of KV storage, all sessions (the bool/index buffers are noise).
- reset(session_idx)[source]#
Drop ONE session’s state and re-arm its scratch tail.
- Parameters:
session_idx (int)
- Return type:
None
- session_span(session_idx)[source]#
[lo, hi)token slots owned bysession_idx.A host
inthere, unlike everywhere on the forward path: the two callers below are host-side lifecycle (a rollout ending, capture tearing down), never inside a captured region.
- upsert(kv, frame_pos, commit, session_idx, *, build_visibility=True)[source]#
kvis[2, 1, H_kv, B*tokens_per_frame, D], one frame for each ofBsessions;commitwrites the frame into its ring slot; without it the frame lands in that session’s scratch tail only, visible to itself and to nothing later.Returns
(k, v, visible)
- class mstar.engine.resources.kv.ring.RingKVManager(config, name, device, dtype=torch.bfloat16)[source]#
Bases:
AttentionResourcePer-layer ring caches holding
num_sessionssessions, one per request.- Parameters:
config (RingKVConfig)
name (str)
device (device)
dtype (dtype)
- admit(step, ctx)[source]#
Claim a session for this step’s request. A full pool is retried; a malformed step is refused terminally. Does not support eviction for now
- Parameters:
step (ResourceStep)
ctx (StepContext)
- Return type:
- classmethod build(spec, info)[source]#
- Parameters:
spec (KVSpec)
info (EngineResourceInfo)
- Return type:
- build_accelerator_graph_buffers(slots, max_bs, max_seq_len)[source]#
No-op: every buffer a replay touches was allocated at
build,_static_session_idxincluded.- Parameters:
slots (list[CGSlotSpec])
max_bs (int)
max_seq_len (int)
- Return type:
None
- capacity(layer_idx)[source]#
Token slots ONE session owns in
layer_idx’s ring, scratch frame included.total_slotsis the whole buffer.
- commit(step, ctx)[source]#
Record the frame each request just committed. Metadata only.
- Parameters:
step (ResourceStep)
ctx (StepContext)
- Return type:
None
- get_state(rid)[source]#
Snapshot one request’s session. Cloned, so the caller can hold it across further rollout steps that mutate the rings in place.
- ingest_request(rid, overrides=None)[source]#
Register
rid. Deliberately does not claim a session.- Parameters:
rid (str)
overrides (ResourceReqConfig | None)
- Return type:
None
- plan(step, ctx)[source]#
Stage the padded batch’s session indices, one per row. Ring addresses stay in the graph.
A replay pads the batch to its capture bucket with dummy rids that hold no session. Those rows still write and attend (their output is dropped downstream), so every one is parked on
_padding_session– the shared scratch session outside the resident pool. The flex mask scopes each row to its own session, so a padding row’s read and its commit both stay on that session and never reach a real request’s history; no request is ever admitted to it, so the tail it leaves behind is never read.- Parameters:
step (ResourceStep)
ctx (StepContext)
- Return type:
- post_warmup_validate()[source]#
Capture must leave every session exactly as it found it.
- Return type:
None
- remove_request(rid)[source]#
The request is gone: drop its session, its claim, and its registration.
- Parameters:
rid (str)
- Return type:
None
- reset_request(rid, free=False)[source]#
Zero
rid’s session and release its claim.freeis ignored, there is no physical allocation to hand back.
- session_of(rid)[source]#
rid’s session index, or None if it holds none. Host-side introspection; the forward reads the staged tensor, never this.
- total_slots(layer_idx)[source]#
Token slots in
layer_idx’s buffer across every session – the length of the KV viewupsertreturns and of its visibility row.
- upsert(k, v, layer_idx, frame_pos, *, commit, build_visibility=True)[source]#
Write one step’s K/V for
layer_idxand return what to attend to.k/vare[B, H_kv, tokens_per_frame, D], one frame per resident session in the step batch.kis already RoPE’d and RMS-normed andvis post value-residual lerp: the cache stores post-RoPE keys, so replayed history is never re-rotated. Returns(k_all, v_all, visible), the first two spanning the whole buffer, every resident session and the third a[total_slots]bool row that is False everywhere outside the calling request’s own session.frame_posandcommitare both arguments rather than resource state, for the same reason.frame_posis the[B]int64 ring clock – not a slot id – and alone determines the slot written and the visibility row;
Modules