[GH-ISSUE #4730] [Engine] Metal 0.1.54 regression: conversation cache never parks a mid-sized conversation - the checkpoint's indexer-tail width and block_pos disagree with the Metal ring #2169

Open
opened 2026-11-04 01:57:11 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @theBigGavin on GitHub (Oct 30, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/4730

Environment

  • Strata app 0.2.8 / engine 0.1.54-metal, built from source at upstream main 870aedd9 (2026-10-30), the tree the 0.2.8 notes describe
  • Apple M4 Max (40-core GPU), 128 GiB unified memory, macOS 26.6.2 (25G83)
  • Model: Qwen3.8-Flash-Next IQ3_S (pack)
  • --serve --pack ... --native ... --ple-gguf ... --expert-profile ... --expert-cache 64 --prefill auto --spec 2 --mtp ... --max-context 131072 --mmap-experts --conversation-cache-mib 16384 --conversation-cache-slots 4 --conversation-cache-min-tokens 20000 --vision --vram-reserve-mib 3072

What happens

On 0.1.54 the conversation cache no longer parks a mid-sized conversation. Every park attempt over a ~24k-token conversation logs:

strata serve: conversation cache: skip parking (conversation snapshot: invalid checkpoint running-state payload)
strata serve: prompt 24215 tokens = 0 reused + 24215 read in 50650 ms

The same conversation on engine 0.1.44.1 parks and restores in milliseconds, so this is a regression inside 0.1.45–0.1.54, not a property of the model or the machine.

A larger conversation did park on 0.1.54 (parked 66674 tokens in 57.5 ms, restored 66077 tokens ... in 28.9 ms), so the cache is not broken outright — the checkpoint payload check refuses the smaller one.

The failing check, and what disagrees

With a temporary STRATA_KV_DIAG print in conversation_checkpoint_validate (src/core/conversation_state.cpp), the payload sizes are:

ckpt diag: ids=16384 gdn=117669888/117669888 ple=368640/368640 dead=6144/6144
           tails=24576/18432   block_pos=0/48   layers=12 z.tail=1536 z.dead=512 z.block_pos=4 z.ple=368640 ple_hist=1

Two fields disagree, both from the same source: Metal's indexer ring layout is wider than the contract the checkpoint layer validates against, and Metal has no block_pos at all.

  1. tails: the session holds 24576 bytes, the check expects 18432 (layers * z.tail = 12 * 1536).

    • z.tail = (idx_block - 1) * idx_dim * sizeof(float) = 3 * 128 * 4 = 1536 — conversation_state.cpp, conversation_state_sizes()
    • Metal allocates and copies 2048 per layer: metal/src/engine/window.cpp line 563 tail_.push_back(A(4 * 4 * 128)), line 1511 const size_t ts = 4 * 4 * 128, line 1518 c.tails.resize(tail_.size() * ts)
    • 24576 − 18432 = 6144 = 12 × 512, i.e. exactly one extra idx_dim-wide cell per layer.
  2. block_pos: the session holds 0, the check expects 48 (layers * sizeof(int32_t)).

    • metal/src/engine/window.cpp, Window::checkpoint_save ends with c.block_pos.clear();
    • It is the only mention of block_pos in the whole Metal tree.

Why: three different contracts for the same ring

The raw-tail ring is backend-owned, and the two backends legitimately lay it out differently.

CUDA (src/kernels/cuda/native_qsa_indexer.cu) keeps idx_block - 1 rows and passes the completing cell in a register:

if (slot < R - 1) tail[slot * D + d] = incoming;
if (pos == 0 || slot == R - 1) {
    float sum = pos == 0 ? incoming : tail[d];
    for (int j = 1; j < R; ++j)
        sum = __fadd_rn(sum, pos == 0 || j == R - 1 ? incoming : tail[j * D + d]);
    mean = __fmaf_rn(0.25f, sum, 0.0f);
}

Metal (metal/kernels/qsa.metal, qsa_prep) keeps all idx_block and reads them back:

if (tid < ID) tail[(cell & 3) * ID + tid] = x;
...
if (tid < ID) m = (((tail[tid] + tail[ID + tid]) + tail[2 * ID + tid]) + tail[3 * ID + tid]) * 0.25f;

The two means are identical — only the saved ring differs. conversation_state.cpp validates against the CUDA width, so Metal's image is refused.

block_pos exists for a CUDA-only reason, and the header says so: it is a device-side record of the rotation position because a CUDA graph bakes kernel arguments in at capture time (include/strata/kernels/qsa.hpp, the QsaIndexerBuffers and rope_neox notes). Metal replays no graphs: window.cpp builds a fresh QsaArgs per dispatch and passes pos_base as a value argument, so its block_pos is never needed — and was never implemented.

Blast radius

Every Metal user who serves with --mtp --conversation-cache* hits this on conversations below whatever size the 66k case was above. It costs the whole prompt read on every conversation switch — 50.6 s against 0.3 s on the same conversation and machine.

What I did not do

I tried relaxing the check to the session's own ring width and stopped: z.tail is also the serialization stride, not only the check. conversation_state.cpp uses it as the per-layer stride at line 258 (copy(s + ple + j * z.tail, st.idx_tail, z.tail, ...)), line 302 (c.tails.resize(layers * z.tail)), and line 374 in the restore direction. Widening only the comparison would pass the check and then copy 1536 of 2048 bytes per layer, dropping block_pos and cropping the ring — a silent wrong-attention, worse than the refusal. So the three places that must agree are the two kernels and the serializer, and I do not have a CUDA box to validate a change to their shared contract.

Options as I see them

  1. Bring Metal to the CUDA contract — ring of idx_block - 1, ring-sum over three rows plus the completing cell in a register, and a real block_pos buffer filled per completed block. Cleanest for the on-disk format; touches the Metal indexer's arithmetic, so it wants qsa_parity at a context above the selection threshold.
  2. Let the serialized tail be backend-width — derive the stride from the session, and put the width into geometry_key so an image is never read back by a backend with a different ring. Keeps both kernels as they are; changes the shared format and rules out cross-backend session files for this state.
  3. Metal declares no checkpoint — window_checkpoint_save returns false and the image carries no checkpoints. Cheap, but I have not measured what parking still does without them.

I have the 0.1.44.1 engine still built and can re-test any of these on this machine; what I cannot do is a CUDA/HIP build or run. Happy to follow whichever direction is correct here.

Reproduction status: reproduced locally, on the hardware and build above.

Originally created by @theBigGavin on GitHub (Oct 30, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/4730 ## Environment - Strata app 0.2.8 / engine `0.1.54-metal`, built from source at upstream `main` `870aedd9` (2026-10-30), the tree the 0.2.8 notes describe - Apple M4 Max (40-core GPU), 128 GiB unified memory, macOS 26.6.2 (25G83) - Model: Qwen3.8-Flash-Next IQ3_S (pack) - `--serve --pack ... --native ... --ple-gguf ... --expert-profile ... --expert-cache 64 --prefill auto --spec 2 --mtp ... --max-context 131072 --mmap-experts --conversation-cache-mib 16384 --conversation-cache-slots 4 --conversation-cache-min-tokens 20000 --vision --vram-reserve-mib 3072` ## What happens On 0.1.54 the conversation cache no longer parks a mid-sized conversation. Every park attempt over a ~24k-token conversation logs: ``` strata serve: conversation cache: skip parking (conversation snapshot: invalid checkpoint running-state payload) strata serve: prompt 24215 tokens = 0 reused + 24215 read in 50650 ms ``` The same conversation on engine `0.1.44.1` parks and restores in milliseconds, so this is a regression inside 0.1.45–0.1.54, not a property of the model or the machine. A larger conversation did park on 0.1.54 (`parked 66674 tokens in 57.5 ms`, `restored 66077 tokens ... in 28.9 ms`), so the cache is not broken outright — the checkpoint payload check refuses the smaller one. ## The failing check, and what disagrees With a temporary `STRATA_KV_DIAG` print in `conversation_checkpoint_validate` (`src/core/conversation_state.cpp`), the payload sizes are: ``` ckpt diag: ids=16384 gdn=117669888/117669888 ple=368640/368640 dead=6144/6144 tails=24576/18432 block_pos=0/48 layers=12 z.tail=1536 z.dead=512 z.block_pos=4 z.ple=368640 ple_hist=1 ``` Two fields disagree, both from the same source: **Metal's indexer ring layout is wider than the contract the checkpoint layer validates against, and Metal has no `block_pos` at all.** 1. `tails`: the session holds **24576** bytes, the check expects **18432** (`layers * z.tail = 12 * 1536`). - `z.tail = (idx_block - 1) * idx_dim * sizeof(float)` = `3 * 128 * 4` = **1536** — `conversation_state.cpp`, `conversation_state_sizes()` - Metal allocates and copies **2048** per layer: `metal/src/engine/window.cpp` line 563 `tail_.push_back(A(4 * 4 * 128))`, line 1511 `const size_t ts = 4 * 4 * 128`, line 1518 `c.tails.resize(tail_.size() * ts)` - 24576 − 18432 = **6144 = 12 × 512**, i.e. exactly one extra `idx_dim`-wide cell per layer. 2. `block_pos`: the session holds **0**, the check expects **48** (`layers * sizeof(int32_t)`). - `metal/src/engine/window.cpp`, `Window::checkpoint_save` ends with `c.block_pos.clear();` - It is the only mention of `block_pos` in the whole Metal tree. ## Why: three different contracts for the same ring The raw-tail ring is backend-owned, and the two backends legitimately lay it out differently. **CUDA** (`src/kernels/cuda/native_qsa_indexer.cu`) keeps `idx_block - 1` rows and passes the completing cell in a register: ```c if (slot < R - 1) tail[slot * D + d] = incoming; if (pos == 0 || slot == R - 1) { float sum = pos == 0 ? incoming : tail[d]; for (int j = 1; j < R; ++j) sum = __fadd_rn(sum, pos == 0 || j == R - 1 ? incoming : tail[j * D + d]); mean = __fmaf_rn(0.25f, sum, 0.0f); } ``` **Metal** (`metal/kernels/qsa.metal`, `qsa_prep`) keeps all `idx_block` and reads them back: ```c if (tid < ID) tail[(cell & 3) * ID + tid] = x; ... if (tid < ID) m = (((tail[tid] + tail[ID + tid]) + tail[2 * ID + tid]) + tail[3 * ID + tid]) * 0.25f; ``` The two means are identical — only the saved ring differs. `conversation_state.cpp` validates against the CUDA width, so Metal's image is refused. `block_pos` exists for a CUDA-only reason, and the header says so: it is a device-side record of the rotation position because **a CUDA graph bakes kernel arguments in at capture time** (`include/strata/kernels/qsa.hpp`, the `QsaIndexerBuffers` and `rope_neox` notes). Metal replays no graphs: `window.cpp` builds a fresh `QsaArgs` per dispatch and passes `pos_base` as a value argument, so its `block_pos` is never needed — and was never implemented. ## Blast radius Every Metal user who serves with `--mtp --conversation-cache*` hits this on conversations below whatever size the 66k case was above. It costs the whole prompt read on every conversation switch — 50.6 s against 0.3 s on the same conversation and machine. ## What I did not do I tried relaxing the check to the session's own ring width and stopped: **`z.tail` is also the serialization stride**, not only the check. `conversation_state.cpp` uses it as the per-layer stride at line 258 (`copy(s + ple + j * z.tail, st.idx_tail, z.tail, ...)`), line 302 (`c.tails.resize(layers * z.tail)`), and line 374 in the restore direction. Widening only the comparison would pass the check and then copy 1536 of 2048 bytes per layer, dropping `block_pos` and cropping the ring — a silent wrong-attention, worse than the refusal. So the three places that must agree are the two kernels and the serializer, and I do not have a CUDA box to validate a change to their shared contract. ## Options as I see them 1. **Bring Metal to the CUDA contract** — ring of `idx_block - 1`, ring-sum over three rows plus the completing cell in a register, and a real `block_pos` buffer filled per completed block. Cleanest for the on-disk format; touches the Metal indexer's arithmetic, so it wants `qsa_parity` at a context above the selection threshold. 2. **Let the serialized tail be backend-width** — derive the stride from the session, and put the width into `geometry_key` so an image is never read back by a backend with a different ring. Keeps both kernels as they are; changes the shared format and rules out cross-backend session files for this state. 3. **Metal declares no checkpoint** — `window_checkpoint_save` returns false and the image carries no checkpoints. Cheap, but I have not measured what parking still does without them. I have the `0.1.44.1` engine still built and can re-test any of these on this machine; what I cannot do is a CUDA/HIP build or run. Happy to follow whichever direction is correct here. Reproduction status: reproduced locally, on the hardware and build above.
Author
Owner

@hyspace commented on GitHub (Nov 2, 2026):

Confirming on stock engine 0.1.55-macos-arm64-metal (Apple M5 Ultra 96 GB, Qwen3.8-Flash-Next IQ3_S, --mtp --spec 2 --kv k8v4 --mmap-experts --conversation-cache-mib 8192): every park attempt on ~12K-token conversations logs

strata serve: conversation cache: skip parking (conversation snapshot: invalid checkpoint running-state payload)

so this is not limited to one model or one machine. I did not add a diagnostic print to our build, but reading the source agrees with your analysis: Window::checkpoint_save uses a per-layer tail stride of 4 * 4 * 128 and ends with c.block_pos.clear(), while conversation_checkpoint_validate expects (idx_block - 1) * idx_key_dim * sizeof(float) per layer and layers * sizeof(int32_t) for block_pos.

A small repro that alternates two growing conversations (A1, B1, A2 reuses 0 tokens; A1, A2 reuses 11,944 of 11,965) is in #4144. On this build the memory-guard gap is independent: with --memory-limit-mib and --conversation-cache-min-free-mib 0 set, the park still fails with the same payload error, so fixing only the macOS RAM reading would not make parking work.

<!-- gh-comment-id:6571652358 --> @hyspace commented on GitHub (Nov 2, 2026): Confirming on stock engine `0.1.55-macos-arm64-metal` (Apple M5 Ultra 96 GB, Qwen3.8-Flash-Next IQ3_S, `--mtp --spec 2 --kv k8v4 --mmap-experts --conversation-cache-mib 8192`): every park attempt on ~12K-token conversations logs ``` strata serve: conversation cache: skip parking (conversation snapshot: invalid checkpoint running-state payload) ``` so this is not limited to one model or one machine. I did not add a diagnostic print to our build, but reading the source agrees with your analysis: `Window::checkpoint_save` uses a per-layer tail stride of `4 * 4 * 128` and ends with `c.block_pos.clear()`, while `conversation_checkpoint_validate` expects `(idx_block - 1) * idx_key_dim * sizeof(float)` per layer and `layers * sizeof(int32_t)` for `block_pos`. A small repro that alternates two growing conversations (A1, B1, A2 reuses 0 tokens; A1, A2 reuses 11,944 of 11,965) is in #4144. On this build the memory-guard gap is independent: with `--memory-limit-mib` and `--conversation-cache-min-free-mib 0` set, the park still fails with the same payload error, so fixing only the macOS RAM reading would not make parking work.
Author
Owner

@hyspace commented on GitHub (Nov 3, 2026):

Correction to my comment above: this was already fixed on main in d01b1817 ("Metal: conversations park and restore (#4730, #4144)"), which shipped in 0.2.10 / engine 0.1.56.

Verified on engine 0.1.56-macos-arm64-metal (M5 Ultra 96 GB, Qwen3.8-Flash-Next IQ3_S, --mtp --spec 2 --kv k8v4): the invalid checkpoint running-state payload line is gone. In the A1, B1, A2 test (~12K tokens each), A2 now reuses 11,949 of 11,970 tokens after B ran in between; on 0.1.55 it reused 0. The log shows parked 11949 tokens in 24.4 ms and restored 11949 tokens (live) in 20.3 ms. Details in #4144.

<!-- gh-comment-id:6594741201 --> @hyspace commented on GitHub (Nov 3, 2026): Correction to my comment above: this was already fixed on `main` in d01b1817 ("Metal: conversations park and restore (#4730, #4144)"), which shipped in 0.2.10 / engine 0.1.56. Verified on engine `0.1.56-macos-arm64-metal` (M5 Ultra 96 GB, Qwen3.8-Flash-Next IQ3_S, `--mtp --spec 2 --kv k8v4`): the `invalid checkpoint running-state payload` line is gone. In the A1, B1, A2 test (~12K tokens each), A2 now reuses 11,949 of 11,970 tokens after B ran in between; on 0.1.55 it reused 0. The log shows `parked 11949 tokens in 24.4 ms` and `restored 11949 tokens (live) in 20.3 ms`. Details in #4144.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2169
No description provided.