Repository navigation
GLM: a new stream takes the free slot whose cache holds no kept prefix it would overwrite - #622
Merged
Merged
Conversation
…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.
Closed
Owner
|
Thanks, @BobClawblaw. This is in as a merge of your head, with one commit of ours that folds the doc comment on 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
…olds no kept prefix it would overwrite
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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), andSlots.overwritemarks the state stale once that slot writes below it. A stale state failsusable, 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:Backend.prefillcallspickwith the stream'sreuse.savedinstead offree(). The slot index still travels to a pair's rank 1 in thebeginmessage, 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.failed)The 12 replies the two runs both make in order are identical, so this changes only where a state resumes.
zig build testpasses.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) * 2bytes 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?