Skip to content

GLM: a kept state copies its prefixes out of its slot before the slot writes below it - #644

Closed
BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:glm-cache-spill
Closed

BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:glm-cache-spill

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

The follow-up #622 left open: once every slot holds a stream, a new prompt pass can still write over a slot that a kept state's MLA prefix lives in, and that state goes stale. This copies the prefix out of the slot first, when the prompt cache has room for it.

The change

A resident prompt state holds only its KDA states. Its MLA prefixes stay in the slot that made it (Snap.home).

  • Slots.overwrite now asks the prompt cache for the prefix bytes before the slot writes below a resident state. Store.grow grants them only from the budget's free room and never evicts during a pass.
  • With the room, the slot's MLA rows are copied into a whole-state buffer beside the KDA states, and the state no longer has a home slot.
  • Without it, the state goes stale as before.
  • A whole state restored into a slot now protects that slot's resident states the same way another slot's state does.
  • A pair keeps its states resident as before.

The copy is about 18 KB a prompt token ((kv_lora + 2 * index_head_dim) * 2 bytes across 12 MLA caches, the MTP head's among them).

Measured

GLM_LAYERS=8 --parallel 4, six conversations on four slots: turns in order, then four at once, then in order again.

main this PR
prompt tokens resumed from the cache 18,162 of 47,150 34,872 of 47,150
failed restores 9 0

All 24 replies equal main's. zig build test passes, with a new prompt_cache_test.zig case for Store.grow.

… writes below it

A resident prompt state holds only its KDA states; its MLA prefixes stay in the
slot that made it. Once every slot holds a stream, a new prompt pass can still
write over one, and the state goes stale (ashhart#622 picks a slot that avoids this
while one is free). Now Slots.overwrite first asks the prompt cache for the
prefix bytes (Store.grow: only from the budget's room, never evicting during a
pass), copies the slot's MLA rows into a whole-state buffer beside the KDA
states, and the state no longer has a home slot. Without the room it goes stale
as before. A whole state restored into a slot now protects that slot's resident
states the same way another slot's state does. A pair keeps its states resident
as before.

Six conversations on four slots (GLM_LAYERS=8, --parallel 4), turns in order,
then four at once, then in order: 34,872 of 47,150 prompt tokens cached against
main's 18,162, failed restores 9 to 0, about 18 KB a prompt token copied out,
and all 24 replies equal main's.
@ashhart

ashhart commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thank you, @BobClawblaw. This landed in 1.0.8 as your own commit (20799e6a6), rebased over #639's shared pool. The one conflict was Slots.restore, where #639 had added a line for the pool's held blocks. Your snap.home != i check covers that case too, so that line went. We also merged this PR's head as it was then (06a56b703, in e9231efa3) without changing the tree. The branch has since been rebased onto 1.0.7 with the same change, so its new head isn't in 1.0.8's history and GitHub won't mark this PR merged. Nothing is left to merge, so I'm closing it with thanks.

Our commit on top (dc07d8d8d) is a lean pass. With your change slots.zig came to 631 lines, past our 600-line limit, so the kept-state functions move unchanged into families/glm/kept.zig: save, restore, the overwrite with your spill, the probe, the learned-state reads and writes, and the slot pick. Slots keeps their names as aliases, so no caller changes. The commit also makes four doc comments one line each and drops the spilled counter, which nothing read; the log line still records each copy. It lists you as co-author because it moves your lines.

After #639 this applies to one Mac with GLM_POOL=0. In the default pool a leaving stream's kept prefixes turn into held blocks, so no free slot holds a resident state and overwrite never finds one to copy.

We measured it on one M5 Ultra with GLM_LAYERS=8 --parallel 4 and a client shaped like your receipt: six conversations with a 6,000-character system prompt each, four turns each (two in order, one four at once, one in order), 24 replies and 46,465 prompt tokens.

server prompt tokens resumed failed restores states copied out replies
GLM_POOL=0, without this PR 15,466 (33%) 6 0 reference
GLM_POOL=0, this PR 32,531 (70%) 0 7 24/24 equal
default pool, with and without this PR 32,531 (70%) 0 0 24/24 equal

GLM_POOL=0 now resumes exactly what the pool does, turn for turn. Each copy took 29 to 38 MiB, about 18 KB a prompt token, as you said.

The pair followed in 1.0.8 too. Its first Mac sends each slot command before running it, so the copy is decided before the send: the first Mac picks the states the budget has room for and sends their ids, and the second copies its half of the same states (3e06c40d1, with you as co-author). A new stream also takes the least recently used slot (6828c00c7), the pick the 1.0.6 and 1.0.7 notes promised, which builds on your #622. Your copies are what make that pick safe for conversations that take turns in a fixed order. On two M5 Ultras, six conversations taking turns on four streams resumed 32,526 of 46,301 prompt tokens with no failed restore, against 15,430 and 17,745 in two 1.0.7 runs. In the release gate, after a busy session, a prompt A, an unrelated B and A again resumed 3,165 of A's 3,181 tokens, where 1.0.7 resumed none.

Copying only from the budget's free room, and never evicting in the middle of a pass, made this an easy change to trust.

@ashhart ashhart closed this Oct 12, 2026
BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
GLM's kept prompt states move from slots.zig (631 lines with ashhart#644) into kept.zig: save, restore, the overwrite that
copies a resident state's prefixes out of its slot or marks it stale, the probe, the learned-state reads and writes,
and the pick of a new stream's slot. Slots keeps their names as aliases, so no caller changes. ashhart#644's comments are one
line each, its write-only spill counter goes (the log line records each copy), and the unused Slots.free goes. No
behaviour changes.

Co-authored-by: BobClawblaw <[email protected]>
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
The change landed rebased over ashhart#639 as 3e02e29d4 with its author kept; this keeps the PR's head in the history with no change to the tree.
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
… the slot writes below them

ashhart#644 made one Mac's slot mode copy a resident kept state's MLA prefixes into a whole state, when the prompt cache's
budget has room, before a new stream writes over its slot. A pair could not: rank 0 sends each slot command before it
runs it, so the copy has to be decided before the send. The backend now decides before a restore that writes a slot's
rows and before each prompt chunk. The states it makes whole (Store.grow, from free room, never evicting during a pass)
go to rank 1 as one copy_out command with their ids, and rank 1 makes the same states whole from its own slot: its
share of the KDA heads, and the MLA prefix, which TP does not split. The overwrite inside the chunk or restore then marks
stale only what was not copied, on both Macs alike. If a copy fails, the ids already copied are sent before the error
returns, so the Macs never disagree. One Mac takes the same path with nothing to send, so its copies are ashhart#644's.

Co-authored-by: BobClawblaw <[email protected]>
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
…nd the least-recently-used GLM slot pick (extends ashhart#622)
@ashhart ashhart mentioned this pull request Oct 14, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
GLM's kept prompt states move from slots.zig (631 lines with ashhart#644) into kept.zig: save, restore, the overwrite that
copies a resident state's prefixes out of its slot or marks it stale, the probe, the learned-state reads and writes,
and the pick of a new stream's slot. Slots keeps their names as aliases, so no caller changes. ashhart#644's comments are one
line each, its write-only spill counter goes (the log line records each copy), and the unused Slots.free goes. No
behaviour changes.

Co-authored-by: BobClawblaw <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The change landed rebased over ashhart#639 as 3e02e29d4 with its author kept; this keeps the PR's head in the history with no change to the tree.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… the slot writes below them

ashhart#644 made one Mac's slot mode copy a resident kept state's MLA prefixes into a whole state, when the prompt cache's
budget has room, before a new stream writes over its slot. A pair could not: rank 0 sends each slot command before it
runs it, so the copy has to be decided before the send. The backend now decides before a restore that writes a slot's
rows and before each prompt chunk. The states it makes whole (Store.grow, from free room, never evicting during a pass)
go to rank 1 as one copy_out command with their ids, and rank 1 makes the same states whole from its own slot: its
share of the KDA heads, and the MLA prefix, which TP does not split. The overwrite inside the chunk or restore then marks
stale only what was not copied, on both Macs alike. If a copy fails, the ids already copied are sent before the error
returns, so the Macs never disagree. One Mac takes the same path with nothing to send, so its copies are ashhart#644's.

Co-authored-by: BobClawblaw <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…nd the least-recently-used GLM slot pick (extends ashhart#622)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants