Expand description
The dual cache lives in the shared memra-kv crate (Phase D extraction); this
re-export keeps every crate::cache:: / memra_engine::cache:: path unchanged.
Structs§
- Cache
- Cache
Snapshot - Snapshot of the dual cache taken BEFORE a spec-decode draft+verify round (MTP-PLAN §C/§D.4).
- Dflash
TapSink - See
Cache::dflash_taps. Armed per forward by the dflash round (t = that forward’s row count); the trunk writes tap slot s of row r at buf[rn_tapshidden + s*hidden ..]. - HcTap
Sink - See
Cache::hc_taps. Armed per walk by the glm5 DFlash2 draft source; the hc trunk writes the CONTRACTED (stream-mean) completed output of tapped layerlayer_ids[s]for walk row r atrows[(base + r) * n_taps * hidden + s * hidden ..][..hidden]— the drafter fc’s input layout, measured by the dflash2 probe’s capture seam (research/glm53-flash-bringup-20260827/dflash2-probe-20260829/: stream-mean of the completed layer output == the SGLang glm5_next hc_contract aux-hidden definition). - KvLayer
- Per-full-attn-layer growing KV cache, resident on GPU. QUANTIZED (KVQUANT-PLAN §B): K stored q8_0 (34 B/32 elem), V stored q5_1 (24 B/32 elem). Per-token byte layout keeps the [token, kv_head, dim] element order so a 32-block never straddles a head (assert head_dim%32==0). Element-within-token index = kv_head*head_dim + d; block = idx/32; lane = idx%32.
- KvRing
- Latent
KvLayer - Per-MLA-layer latent KV plane (DESIGN.md §3.2). ONE row per token,
widthelements wide, wherewidth==StatePlan::LatentKvCache { width }== kv_lora_rank + rope_head_dim: row = [ rmsnorm(c_kv) : kv_rank | rope(k_pe) : d_rope ] There is NO V plane — V is the FIRSTkv_rankelements of the SAME row, and every query head streams that one row (MQA). NoPE models (glm5_next, rope_head_dim 0) have width == kv_rank and no k_pe tail. - Latent
Plane Snapshot - One MLA/DSA layer’s captured latent-plane state: everything
mla_attn_cached+mla_kpool_indicesneed to continue as if the destination session had primed the prefix itself (lane/glm5-prefix-latent, 2026-08-30; design in research/glm5-prefix-latent-20260830/DESIGN.md). - Latent
Tail Capture - The generation-destroyed slice of one latent layer’s BOUNDARY state, captured EAGERLY at a spec session’s prompt boundary (lane/glm5-prefix-latent2, 2026-09-01) so a DEFERRED prefix publication can be completed later against the live plane:
- Recur
Layer - Per-linear-attn-layer fixed recurrent state. conv_state and ssm_state are BOTH kept RESIDENT on GPU — the conv ring assemble + roll runs on-device (conv_assemble_and_roll), so there is no per-step dtoh/htod for either.
- Resident
TpKv Cache - Resident
TpKv Cache Rank - TpKv
Append Plan - TpKv
Grow Plan - TpKv
Rank Allocation Shape - Exact allocation geometry for one rank of a tensor-parallel KV sidecar.
- TpKv
Transaction
Enums§
Constants§
- INDEX_
RING_ WORKING_ ROWS - WORKING-SET rows the DSA indexer TAIL RING books by default.
- PRIME_
CHUNK_ MAX_ TOKENS - With the SWA-ring door open,
prime_chunk_tokenscaps every legal chunk at this bound. The ring carries one whole maximum-size prime chunk in addition to the reader’s window. - SWA_
REWIND_ SLACK_ ROWS - Rows a ring-backed plane keeps BELOW the aligned window start so a backward rewind stays legal.
Statics§
- KV_
FP8_ FORCE - Per-model FP8-KV door (-1 = unset → env/default off; 0 = off; 1 = on). Set at qwen model load: the 2026-07-12 arc closed per-model — 9B +0.7-4% scaling with depth, 27B flat (weight-bound), 35B −2% (fp8 format-gates its v3 dp4a lane off). Explicit MEMRA_KV_FP8 wins. 9B adoption attempt REVERTED by measurement 2026-07-29 (−1% at 12k on the then-current build) — loaders currently store 0.
Traits§
- KvDev
- The 7 device ops the cache needs — nothing more. Implemented by the engine (and by any future backend); all ops are stream-ordered on the implementor’s worker stream.
Functions§
- cache_
bytes_ per_ token - Context-linear bytes allocated by one trunk cache token.
- cache_
bytes_ per_ token_ for_ layers - Context-linear cache bytes per token owned by layers in
[lo, hi). PP admission uses the same layer ranges asCache::new_ppn, so each device is charged for exactly the cache planes it allocates rather than for the aggregate model geometry. - cache_
bytes_ per_ token_ for_ plan - cache_
ring_ bytes_ per_ token - Portion of
cache_bytes_per_tokenwhose physical row count is capped by the Step35 SWA ring. Zero with the flag off and for every non-Step35 architecture. - cache_
ring_ bytes_ per_ token_ for_ layers - Ring-capped portion of
cache_bytes_per_token_for_layersfor[lo, hi). - cache_
ring_ bytes_ per_ token_ for_ plan - cache_
ring_ row_ cap - Physical row cap shared by the Step35 SWA trunk and MTP scratch; zero when no ring is active.
- cache_
ring_ row_ cap_ for_ plan - gkv_on
- FP8-GLOBALS switch (MEMRA_GEMMA_GKV, default ON): gemma global (hd512) layers keep their KV in e4m3 — the dequant-latency arc (HANDOVER). Windowed layers stay q8_0/q5_1.
- index_
plane_ physical_ row - Physical row of absolute row
absin an indexer state plane.ring_rows == 0is the flat plane (absolute addressing); otherwise the EFFECTIVE ring isring_rowsrounded down to a whole number of pools, exactly the engine’s own rounding (mla_kpool_indices), because a ring that is not a multiple ofpoolwould split a pool across the wrap. - index_
ring_ default_ rows - PHYSICAL rows the SHIPPED DEFAULT derivation books for
max_ctx: noMEMRA_DSA_INDEX_RINGoverride. Pure, so the sizing gate can assert on it without racing another test’s environment. This is the one function the sizing gate calls. - index_
ring_ rows - DSA k-pool indexer TAIL RING sizing (
MEMRA_DSA_INDEX_RING, default ON, see docs/FLAGS.md). Unparseable values are treated as unset.MEMRA_PRIME_CHUNKis NO LONGER READ HERE: the ring is drained inside the call, so no prefill chunk discipline can size it or break it. - index_
ring_ rows_ for - Physical rows of the DSA k-pool indexer state plane when it is a TAIL RING, or
Noneto keep the flatmax_ctx-row plane. PURE: the env read isindex_ring_rows. - index_
ring_ take - Rows of
remainingthe indexer may append to the tail ring BEFORE the pool-key build has to drain it, orNonewhen the rows this call still owes an unbuilt pool are already lapped. - kv_
blk_ bytes - Per-32-element block bytes for the selected (K, V) formats.
- kv_
cache_ formats - Env-selected KV cache formats (MEMRA_KV_K / MEMRA_KV_V). The engine’s flash-fatbin router and the cache sizing below MUST agree — both read this one function.
- kv_
fp8_ on - QWEN FP8-KV switch (MEMRA_KV_FP8 explicit; else the per-model KV_FP8_FORCE door set at model load; else OFF). Non-gemma full-attn layers hold e4m3 K/V via the kf8vf8 module.
- latent_
kv_ bytes_ per_ token_ for_ plan - Context-linear bytes per token owned by
StatePlan::LatentKvCachelayers in[lo, hi), mirroringCache::new_inner’s latent arm plus the engine’s lazy resident pool-key plane (lane/glm5-gpf-workspace, 2026-08-30). - set_
swa_ ring_ default - swa_
retain_ from - The retain a ring-backed append must request: the aligned window start for
first_row, minus the rewind slack, but NEVER below what the ring still holds. - swa_
ring_ on - swa_
ring_ rows - Physical rows required by the Step35 SWA reader contract. Prime starts at
(base_len - (window - 1)) & !31, so at most 31 masked rows precede the live window. - tp_
kv_ rank_ allocation_ shape - wkv_on
- FP8-WINDOWED switch (MEMRA_GEMMA_WKV; serving-mode default): SPEC serving (MEMRA_DRAFT
set) -> OFF, plain -> ON — the acceptance-vs-depth record lives on the engine-side
history of
Engine::wkv_on(git). Explicit env always wins.