okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fc8dab7e1
main
DeepSeek-V4.1 suffix cache reuse
owner and scope
The DeepSeek engine owns SCR at the src/dsv41/ model/request boundary. ds4_main.cpp owns conversation history, slots, pin/reuse and the --serve request transition; ds4_model.cu owns device KV allocation and the prefill write path. SCR is process-opt-in through STRATA_DS4_SCR=1; with the variable absent, no planner, master allocation, row move, or request sequencing is enabled.
The implementation relocates only the compressed main-KV and indexer rows owned by Src. Src is constructed in ds4_model.cu:2080-2086, with layer, ratio, cap, per-device ckv (kKvFp4) and ik (kKvFp4p) arrays. Layers that reuse an owner point to that Src through Layer::src and Layer::ik_src (2088-2095). The source capacity is (max_context + ratio - 1) / ratio + 1, so the master follows the same source geometry.
fresh-write parity and rotation
finish() writes the compressed row at ds4_model.cu:7194-7203, with rd=g.rd, pos=g_first*R, pos_step=R, and quant=2; index rows are written at 7130-7141 with the same owner position and quant=3. The per-token owner uses R=1; compressed owners use the runtime l.ratio rather than a checkpoint-specific constant. SCR therefore treats an owner row's angle position as row * ratio for ratio > 1, otherwise row.
The planner is strata::core::scr_plan in include/strata/core/scr.hpp. For DS4 the request adapter supplies ScrParams{min_span=256, max_runs=64, seed=32, block=2}: compressed rows represent pairs, so relocated starts, ends, lengths, and physical shifts must be even. The adapter inserts one pad token when needed; pad carry preserves physical prompt identity across successive requests. A run is rejected or trimmed at a parity seam, never leaving compressor carry live at a relocated boundary.
The stored row is post-RoPE and quantized, so its RoPE tail cannot be rotated losslessly. At write time the opt-in path captures an F32 pre-RoPE master for every owner row, including the 64-dimensional qk_rope_head_dim tail. On relocation, non-RoPE bytes are copied byte-for-byte. The master tail is rotated directly at the target using the existing finish() kernel (float32 position times frequency, device sin/cos and the existing fused arithmetic). No old-angle subtraction or BF16 rounding is needed with the pre-RoPE master. Complete RoPE groups are then merged into the original packed row; non-RoPE groups and alignment padding remain unchanged. Main rows use kKvFp4 (kv_row_bytes(kKvFp4, g.hd)); index rows use kKvFp4p (kv_row_bytes(kKvFp4p, g.ix_hd)). Both finish call sites set fi.rd = g.rd / fc.rd = g.rd, so the index key uses the same runtime RoPE tail width; the master is stored as F32 because this engine keeps the post-normalization activation in F32 before rotation and quantization.
what is rebuilt
SCR never relocates the kKvFp8 window rings, compressor carry/snapshots, Engram ring, DSpark state or draft KV, residual/logit frontier, or selection scratch. Each relocated run is followed by a fresh segment. At the junction, the executor replays exactly a 128-row SWA band, then at least 16 extend tokens (and any parity pads); the final fresh tail is at least two tokens and produces the residual/logits frontier. No segment or seam is allowed inside an image span. All moved rows are staged before mutation so overlapping or swapped source/destination ranges are safe. This implementation refuses multiple devices or remote readers, and refuses image-bearing requests entirely.
The executor validates settled history, geometry, every run's physical ids, alignment, range, and master validity; stages every live source before any destination write; repacks/scatters the rows; repairs the crop; and walks fresh gaps and adopted runs in order, repairing each run immediately. Backing is allocated for the configured maximum context already. The final tail uses ordinary prefill/checkpoint code. Refusal falls back without cache mutation; a failure after mutation resets the conversation and reports an error. Checkpoint import is not implemented; legacy slots are disabled while SCR is enabled because their format has no masters or pad/provenance maps.
verified locations and limits
The engine source confirms the owner layout is runtime metadata, not a compile-time assumption: Geo::from reads compress_ratios, kv_source_layer_ids, and index_source_layer_ids (ds4_model.cu:423-424), and model initialization checks that each positive ratio has a preceding source of the same ratio (2088-2095). Read-only inspection of /data/models/ds41-shared/DeepSeek-V4.1-Flash-1017-UD-Q8_K_XL-00001-of-00009.gguf confirms 40 layers, main width 512, index width 128, RoPE width 64, SWA 128, ratios 0 on layers 0–1, 2 on layers 2–19, and 1 on layers 20–39. KV owners are 2/8/14/20. Index selectors also include 24/28/32/36, but l.owns_k = l.kv_src means those selectors own no key rows. The code uses the pack's source list, not hard-coded layer numbers, and refuses owner ratios outside 1/2.
The existing Qwen SCR move code is not reused for row formats or ownership: DS4 needs separate kKvFp4/kKvFp4p packing and F32 masters. The CPU reference and byte-for-byte fresh-write test are the correctness oracle; the CUDA move test now passes on the RTX 5070. See DSV41_SCR_VALIDATION.md for the measured live-splice results and remaining checks.
1# DeepSeek-V4.1 suffix cache reuse 2 3## owner and scope 4 5The DeepSeek engine owns SCR at the `src/dsv41/` model/request boundary. `ds4_main.cpp` owns conversation history, slots, pin/reuse and the `--serve` request transition; `ds4_model.cu` owns device KV allocation and the prefill write path. SCR is process-opt-in through `STRATA_DS4_SCR=1`; with the variable absent, no planner, master allocation, row move, or request sequencing is enabled. 6 7The implementation relocates only the compressed main-KV and indexer rows owned by `Src`. `Src` is constructed in `ds4_model.cu:2080-2086`, with `layer`, `ratio`, `cap`, per-device `ckv` (`kKvFp4`) and `ik` (`kKvFp4p`) arrays. Layers that reuse an owner point to that `Src` through `Layer::src` and `Layer::ik_src` (`2088-2095`). The source capacity is `(max_context + ratio - 1) / ratio + 1`, so the master follows the same source geometry. 8 9## fresh-write parity and rotation 10 11`finish()` writes the compressed row at `ds4_model.cu:7194-7203`, with `rd=g.rd`, `pos=g_first*R`, `pos_step=R`, and `quant=2`; index rows are written at `7130-7141` with the same owner position and `quant=3`. The per-token owner uses `R=1`; compressed owners use the runtime `l.ratio` rather than a checkpoint-specific constant. SCR therefore treats an owner row's angle position as `row * ratio` for `ratio > 1`, otherwise `row`. 12 13The planner is `strata::core::scr_plan` in `include/strata/core/scr.hpp`. For DS4 the request adapter supplies `ScrParams{min_span=256, max_runs=64, seed=32, block=2}`: compressed rows represent pairs, so relocated starts, ends, lengths, and physical shifts must be even. The adapter inserts one pad token when needed; pad carry preserves physical prompt identity across successive requests. A run is rejected or trimmed at a parity seam, never leaving compressor carry live at a relocated boundary. 14 15The stored row is post-RoPE and quantized, so its RoPE tail cannot be rotated losslessly. At write time the opt-in path captures an F32 pre-RoPE master for every owner row, including the 64-dimensional `qk_rope_head_dim` tail. On relocation, non-RoPE bytes are copied byte-for-byte. The master tail is rotated directly at the target using the existing `finish()` kernel (float32 position times frequency, device sin/cos and the existing fused arithmetic). No old-angle subtraction or BF16 rounding is needed with the pre-RoPE master. Complete RoPE groups are then merged into the original packed row; non-RoPE groups and alignment padding remain unchanged. Main rows use `kKvFp4` (`kv_row_bytes(kKvFp4, g.hd)`); index rows use `kKvFp4p` (`kv_row_bytes(kKvFp4p, g.ix_hd)`). Both finish call sites set `fi.rd = g.rd` / `fc.rd = g.rd`, so the index key uses the same runtime RoPE tail width; the master is stored as F32 because this engine keeps the post-normalization activation in F32 before rotation and quantization. 16 17## what is rebuilt 18 19SCR never relocates the `kKvFp8` window rings, compressor carry/snapshots, Engram ring, DSpark state or draft KV, residual/logit frontier, or selection scratch. Each relocated run is followed by a fresh segment. At the junction, the executor replays exactly a 128-row SWA band, then at least 16 extend tokens (and any parity pads); the final fresh tail is at least two tokens and produces the residual/logits frontier. No segment or seam is allowed inside an image span. All moved rows are staged before mutation so overlapping or swapped source/destination ranges are safe. This implementation refuses multiple devices or remote readers, and refuses image-bearing requests entirely. 20 21The executor validates settled history, geometry, every run's physical ids, alignment, range, and master validity; stages every live source before any destination write; repacks/scatters the rows; repairs the crop; and walks fresh gaps and adopted runs in order, repairing each run immediately. Backing is allocated for the configured maximum context already. The final tail uses ordinary prefill/checkpoint code. Refusal falls back without cache mutation; a failure after mutation resets the conversation and reports an error. Checkpoint import is not implemented; legacy slots are disabled while SCR is enabled because their format has no masters or pad/provenance maps. 22 23## verified locations and limits 24 25The engine source confirms the owner layout is runtime metadata, not a compile-time assumption: `Geo::from` reads `compress_ratios`, `kv_source_layer_ids`, and `index_source_layer_ids` (`ds4_model.cu:423-424`), and model initialization checks that each positive ratio has a preceding source of the same ratio (`2088-2095`). Read-only inspection of `/data/models/ds41-shared/DeepSeek-V4.1-Flash-1017-UD-Q8_K_XL-00001-of-00009.gguf` confirms 40 layers, main width 512, index width 128, RoPE width 64, SWA 128, ratios 0 on layers 0–1, 2 on layers 2–19, and 1 on layers 20–39. KV owners are 2/8/14/20. Index selectors also include 24/28/32/36, but `l.owns_k = l.kv_src` means those selectors own no key rows. The code uses the pack's source list, not hard-coded layer numbers, and refuses owner ratios outside 1/2. 26 27The existing Qwen SCR move code is not reused for row formats or ownership: DS4 needs separate `kKvFp4`/`kKvFp4p` packing and F32 masters. The CPU reference and byte-for-byte fresh-write test are the correctness oracle; the CUDA move test now passes on the RTX 5070. See `DSV41_SCR_VALIDATION.md` for the measured live-splice results and remaining checks.