Skip to content

GLM: a new stream takes the free slot whose cache holds no kept prefix it would overwrite - #622

Merged
ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:glm-slot-choice
Oct 10, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:glm-slot-choice

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

GLM's prompt cache loses most of its kept states as soon as a second conversation comes in, even with idle slots and no concurrency. This picks the slot a new stream takes so it doesn't write over them.

Why states go stale

A resident prompt state keeps only its KDA part in its own buffer. Its MLA prefix stays in the slot that made it (Snap.home), and Slots.overwrite marks the state stale once that slot writes below it. A stale state fails usable, so the restore is counted as failed and the pass prefills from 0.

Every stream took Slots.free(), the first free slot. So nearly every kept state lived in slot 0, and the first prompt the cache had nothing for went to slot 0 and wrote over all of them, while slots 1 to 3 sat idle. Conversation A, then an unrelated B, then A again: A resumes nothing.

The change

Slots.pick(reuse) chooses the slot:

  • Resuming a kept state: the stream takes that state's own slot when it's free. Its prefix is already there, so there's no cross-slot copy and no other state goes stale.
  • Anything else: the free slot whose cache holds the fewest usable resident states, the lowest index among ties.

Backend.prefill calls pick with the stream's reuse.saved instead of free(). The slot index still travels to a pair's rank 1 in the begin message, as before.

Measured

On an M5 Ultra under memguard, with GLM_LAYERS=8 --parallel 4 --context 32768. Each conversation has a 120-rule system prompt, so each turn is 1,750 to 2,160 tokens. The run is 16 requests: conversations A, B and C in turn, then four conversations at once, then each one's next turn.

main this PR
prompt tokens resumed from the cache 3,660 of 31,670 23,092 of 31,670
failed restores (the store's failed) 8 0
A's turn after an unrelated B 0 1,750
each of the four concurrent turns (three with a kept state) 0, 0, 0, 0 1,750, 2,072, 2,072, 0

The 12 replies the two runs both make in order are identical, so this changes only where a state resumes. zig build test passes.

What's left

While all slots hold streams, a state's slot can still be written over. Keeping those states would mean copying a resident state's MLA prefix into its own buffer before its slot is reused: about 18 KB a token ((kv_lora + 2 * index_head_dim) * 2 bytes across 12 MLA caches, the MTP head's among them), next to the 140 MiB of KDA state every kept state already holds. That's 42 MB at 2,300 tokens and 600 MB at 32k. I'd rather ask before changing the resident design that much. Would you take it as a follow-up?

…x it would overwrite

A resident prompt state keeps its MLA prefix in the slot that made it, and
goes stale once that slot writes below it. Every stream took the first free
slot, so nearly every kept state lived in slot 0 and any prompt the cache had
nothing for wrote over them all, with the other slots idle.

Slots.pick: a stream that resumes a kept state takes that state's own slot
when it is free (no copy, no other state stale); otherwise the free slot
holding the fewest usable resident states.

On GLM_LAYERS=8 with 4 streams, three conversations in turn, then four at
once, then each one's next turn (16 requests): cached prompt tokens 3,660 of
31,670 before, 23,092 after; failed restores 8 before, 0 after; the 12
replies compared are identical.
@ashhart ashhart mentioned this pull request Oct 10, 2026
@ashhart
ashhart merged commit 037f20b into ashhart:main Oct 10, 2026
1 of 2 checks passed
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thanks, @BobClawblaw. This is in as a merge of your head, with one commit of ours that folds the doc comment on Slots.pick to one line (we keep comments to one line of at most 120 columns). No code changed.

On two M5 Ultras with GLM-5.3-Flash, we sent a 3.2k-token prompt A, an unrelated prompt B, then A's system prompt with a new question. Main read the third request from token 0. With your change, A's state survived B in its own slot and the third request resumed 3,165 of its 3,181 tokens. Its reply's token SHA is the same with the cache on, with the cache off, and on main. Resumed turns equal fresh ones on 12 chat steps, and four concurrent replies equal their solo runs.

AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
The three-line doc comment on Slots.pick folds to one line under 120 columns. No code changes.
AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 11, 2026
… 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.
BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
… 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.
BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
… 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.
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)
BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 15, 2026
… 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.
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 three-line doc comment on Slots.pick folds to one line under 120 columns. No code changes.
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
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
… 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.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… 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.
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