Skip to content

SSM.PREFIX.* — recurrent-state companion

Hybrid Mamba / GatedDeltaNet / RWKV models carry state that is not K/V. SSM.PREFIX.* stores that state under the same namespace contract as KV.PREFIX.*, so a hybrid model's whole prefix — softmax layers via the V-store, linear layers here — is shared across processes and survives a restart.

The KV.PREFIX.* substrate above handles transformer-attention layers. For hybrid Mamba+Transformer models (LoLCATs, MOHAWK, Mamba-in-Llama recipes; or any future external small hybrid), the SSM half of the model has per-layer recurrent state, not per-token K/V. SSM.PREFIX.* is the companion substrate for that state.

SSM.PREFIX.STORE  <sid> <layer> <state_blob>     → +OK
SSM.PREFIX.FETCH  <sid> <layer>                  → bulk string | $-1
SSM.PREFIX.DROP   <sid> [<layer>]                → +OK (layer omitted = drop all layers for the session)

state_blob is opaque to the server — pure host-side byte storage. The consumer chooses serialization. The reference format (see tests/test_ssm_prefix_roundtrip.py):

uint32 version = 1
uint32 n_arrays
for each array:
    uint32 ndim
    uint32[ndim] shape
    uint32 dtype_code   (0=fp32, 1=fp16, 2=bf16 — serialized as fp32, lossless)
    raw bytes

This keeps the substrate model-family-agnostic: Mamba (size=2 [conv_state, ssm_state]), RWKV-7 (size=3), and future RetNet / Hedgehog / GLA all serialize differently but the server never parses. Adding a new family is ~1-2 days of consumer-side (de)serializer work.

Validation: - Drift check (reproduced by tests/test_ssm_prefix_roundtrip.py): bit-perfect state hydration across Mamba-130M-f32 (2048 decode tokens), Mamba-370M-f16 (1024 decode tokens), and RWKV-7 (168M parameters, 512 decode tokens). All show token agreement 100%, max-abs-diff = 0.000e+00 vs no-snapshot baseline. Deterministic-recurrence property holds. - Wire round-trip (tests/test_ssm_prefix_roundtrip.py): end-to-end through pion-server. 64/64 bit-perfect Mamba-130M, 64/64 bit-perfect RWKV-7. 1 MB random blob round-trip + overwrite + multi-layer drop all OK.

Pickup cost for new families: each new family needs its own (de)serializer in tests/test_ssm_prefix_roundtrip.py's serialize_arrays_cache / deserialize_arrays_cache. The RWKV-7 serializer is the reference implementation.

End-to-end hybrid model. mlx-community/Qwen3.5-4B-MLX-4bit (24 GatedDeltaNet + 8 Qwen3NextAttention, 3:1 ratio, Apache 2.0), a publicly released hybrid, with measured wins:

Prefix length Vanilla cold Pion warm Speedup Token agreement
2,048 tokens 4,972 ms 355 ms 14.0× 18/18
4,096 tokens 10,245 ms 407 ms 25.3× 17/18
8,192 tokens 21,328 ms 936 ms 29.0× 18/18

Measured 2026-10-02 on an M4 Mac mini against Pion 0.9.1, vanilla prefilled the way mlx-lm's generate_step does. Each cell is the mean over three queries, and the speedup is the mean of the per-query ratios (benchmarks/reproducers/sweep_qwen3_5_warm_ttft.py, raw output in stage1_qwen3_5_prefix_sweep_2026_10_02.json). The speedups are against a cold start. mlx-lm's save_prompt_cache also serializes recurrent (ArraysCache) state, which is what Qwen3.5's GatedDeltaNet layers keep, so mlx-lm's own prompt-cache file is the like baseline for this table; it was not measured on this sweep (benchmarks/reproducers/file_cache_ttft.py --workload ssm checks the file round trip on a Mamba model). Warm TTFT grows with the bytes shipped: 118.6 MB at 2K and 320 MB at 8K, with a 33.55 MB largest layer. Token agreement misses one token in 18 at 4K, greedy-argmax noise present on both paths. Earlier versions of this table timed a vanilla side that computed logits at every prompt position, and read differently; the changelog has the correction.

18/18 layers bit-perfect across the cleanly-typed split path (24 GatedDeltaNet via SSM.PREFIX.*, 8 Qwen3NextAttention via KV.PREFIX.* + V.STOREBATCH). PionPromptCache is hybrid-aware — _classify_cache walks the cache list and routes per-slot. Drop-in for mixed-cache models: pc = PionPromptCache(model, vquant="fp16", port=1974); cache = pc.get_or_prefill(prefix_ids, namespace=ns). Reproducer: benchmarks/reproducers/sweep_qwen3_5_warm_ttft.py. A layer's K/V has to fit in one request (CLIENT_BUF_SIZE, 256 MB); the largest layer at 8K is 33.55 MB, and lengths past 8K are not measured.