[PR #572] glm: keep the snapshot before the generation prompt, not before the last token #604

Open
opened 2026-10-29 21:27:23 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/572
Author: @rushisan
Created: 10/27/2026
Status: 🔄 Open

Base: main ← Head: glm-snapshot-before-generation-prompt


📝 Commits (1)

  • 42b3e69 glm: keep the snapshot before the generation prompt, not before the last token

📊 Changes

2 files changed (+29 additions, -12 deletions)

View changed files

📝 docs/SETTINGS.md (+1 -0)
📝 src/program/generate.cpp (+28 -12)

📄 Description

Problem

The snapshot (the state the next turn continues from) is saved before the prompt's last token. A GLM prompt ends with its generation prompt, <|assistant|><think> (or <think></think> with thinking off), so the snapshot holds ... <|assistant|>. That is fine when the next prompt carries the answer (<|assistant|> is there again), but not when it doesn't:

  • an answer the client cancelled (an agent that timed out, a user who stopped it): the client sends the conversation again with a new message after the last one and without the cancelled answer, so the new prompt has <|user|> where the snapshot has <|assistant|>. While the conversation is still in the model the turn-start checkpoint covers this, but once another conversation has taken the model the conversation's slot is not a prefix of the new prompt any more and the whole prompt is read again.

On our server (2x RTX 3080, two agents on one engine, prompts of 115-180K tokens) that was 2-3 minutes of prefill per occurrence, often enough to time out the client again.

Change

prep_prompt saves the snapshot before the trailing <|assistant|> (looked for in the last 4 tokens, at or past reuse) instead of before the last token; the prompt path reads up to there and the token path reads the 2-3 tokens of the generation prompt. Everything that continued the old snapshot continues the new one: the next turn, a tool result, the same prompt again (it now reads 3 tokens instead of 1). MAYA_GLM_SNAP_GEN=0 restores the old position; a row in docs/SETTINGS.md.

The several-conversation path (--parallel) is unchanged. No CUDA calls, no change to the model arithmetic or the slot / checkpoint formats.

Measured

v1.1.33, 4x RTX 2080 Ti 22 GB (sm_75, CUDA 12.2), 2x Xeon E5-2673 v4, 247 GB RAM, GLM-5.3 · Maya-S24, 131072 context, one conversation, greedy, thinking off, MAYA_GLM_CKPTS=16. Same binary, the setting on and off; cache_n from the response timings:

scenario MAYA_GLM_SNAP_GEN=1 =0 (before)
A (4.5K tokens) answer cancelled after 40 chunks, another chat B (3.7K), then A + a new user message 4540 of 4551 cached 0 of 4551
the same prompt again right after its answer 4492 of 4495 (reads 3) 4494 of 4495 (reads 1)
an ordinary next turn 4516 of 4528 4516 of 4528

The same scenarios on 2x RTX 3080 (huihui's abliterated GLM-5.3-Flash UD-Q4_K_XL, 1M context, --kv int8, layer split) give the same numbers. Answers are unchanged: the tokens the model reads are the same, only the position the state is saved at moves.

Not measured: Windows / AMD (no code specific to them is touched).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/572 **Author:** [@rushisan](https://github.com/rushisan) **Created:** 10/27/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `glm-snapshot-before-generation-prompt` --- ### 📝 Commits (1) - [`42b3e69`](https://github.com/mw00/project-maya/commit/42b3e69bc9e599906bd30feb480e2bb95ece9b73) glm: keep the snapshot before the generation prompt, not before the last token ### 📊 Changes **2 files changed** (+29 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+1 -0) 📝 `src/program/generate.cpp` (+28 -12) </details> ### 📄 Description ## Problem The snapshot (the state the next turn continues from) is saved before the prompt's **last token**. A GLM prompt ends with its generation prompt, `<|assistant|><think>` (or `<think></think>` with thinking off), so the snapshot holds `... <|assistant|>`. That is fine when the next prompt carries the answer (`<|assistant|>` is there again), but not when it doesn't: - an answer the client **cancelled** (an agent that timed out, a user who stopped it): the client sends the conversation again with a new message after the last one and without the cancelled answer, so the new prompt has `<|user|>` where the snapshot has `<|assistant|>`. While the conversation is still in the model the turn-start checkpoint covers this, but once another conversation has taken the model the conversation's slot is not a prefix of the new prompt any more and the whole prompt is read again. On our server (2x RTX 3080, two agents on one engine, prompts of 115-180K tokens) that was 2-3 minutes of prefill per occurrence, often enough to time out the client again. ## Change `prep_prompt` saves the snapshot before the trailing `<|assistant|>` (looked for in the last 4 tokens, at or past `reuse`) instead of before the last token; the prompt path reads up to there and the token path reads the 2-3 tokens of the generation prompt. Everything that continued the old snapshot continues the new one: the next turn, a tool result, the same prompt again (it now reads 3 tokens instead of 1). `MAYA_GLM_SNAP_GEN=0` restores the old position; a row in `docs/SETTINGS.md`. The several-conversation path (`--parallel`) is unchanged. No CUDA calls, no change to the model arithmetic or the slot / checkpoint formats. ## Measured v1.1.33, 4x RTX 2080 Ti 22 GB (sm_75, CUDA 12.2), 2x Xeon E5-2673 v4, 247 GB RAM, GLM-5.3 · Maya-S24, 131072 context, one conversation, greedy, thinking off, `MAYA_GLM_CKPTS=16`. Same binary, the setting on and off; `cache_n` from the response timings: | scenario | `MAYA_GLM_SNAP_GEN=1` | `=0` (before) | |---|---:|---:| | A (4.5K tokens) answer cancelled after 40 chunks, another chat B (3.7K), then A + a new user message | **4540 of 4551** cached | 0 of 4551 | | the same prompt again right after its answer | 4492 of 4495 (reads 3) | 4494 of 4495 (reads 1) | | an ordinary next turn | 4516 of 4528 | 4516 of 4528 | The same scenarios on 2x RTX 3080 (huihui's abliterated GLM-5.3-Flash UD-Q4_K_XL, 1M context, `--kv int8`, layer split) give the same numbers. Answers are unchanged: the tokens the model reads are the same, only the position the state is saved at moves. Not measured: Windows / AMD (no code specific to them is touched). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#604
No description provided.