ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

[Feature request] GLM 5.3: server-side reasoning budget / forced think-close (repro data + working client-side pattern included)

Open nomishbhardwaj opened this issue 3 months ago • 2 comments

Setup: glm-5.3-flash branch @ a60a2a0, GLM-5.3-Flash-Q2.gguf (sha256 e81fd624…9705b32), M5 Max 128GB, --ctx 393216 --warm-weights, glm-5.3-flash-reasoner with reasoning_effort: high.

Problem

On dense multi-requirement codegen specs, GLM 5.3 at High effort can think without bound and return an empty reply: finish_reason: "length", content: "", with the whole max_tokens spent inside <think>. On the attached repro prompt (a 9-clause config-parser spec, ~2KB), 4 of 5 runs at max_tokens=60000 returned empty (~223K–232K chars of reasoning_content each, ~40 min/run at ~25 t/s).

It is not a degeneration loop: shingle-repetition analysis of the reasoning shows zero verbatim repetition, and with max_tokens=120000 the model finished at ~66K thinking tokens with fully correct code. It's unbounded (but progressing) deliberation — there is just no way to bound it server-side today: effort levels are prompt prefixes, and neither budget_tokens (Anthropic /v1/messages thinking object) nor any reasoning_max_tokens knob is honored.

Request

A server-side thinking budget that force-closes </think> when N reasoning tokens are reached, so generation must produce the final answer. Suggested surface: honor thinking: {type: "enabled", budget_tokens: N} on /v1/messages, and/or a reasoning_max_tokens field on /v1/chat/completions.

Working client-side pattern (validated — may be worth building in)

Two-phase, exploiting two properties of render_glm_chat_prompt_text:

  1. an assistant message whose content starts with a think tag is rendered verbatim (append_glm_assistant_message_prefix early-return);
  2. a trailing assistant turn gets no end-of-turn token, so generation continues it.

Phase 1: normal reasoner call with max_tokens = budget. On finish_reason=length + empty content, phase 2: resend with a trailing assistant message "<think>" + captured_reasoning + "\n\n[budget reached — committing to an answer]</think>" using the nothink model alias. On the repro spec, a 24K budget produced correct, test-passing code on 2/2 runs in ~40% of the unforced wall time (1123s vs 2838s); 16K terminated but degraded quality.

(Phase 2 must use the nothink alias due to a separate output-classification bug filed as #897: in reasoner mode the continued answer is misfiled into reasoning_content.)

Repro prompt and the two-phase script attached / available on request.

nomishbhardwaj avatar Aug 29 '26 18:08 nomishbhardwaj

Adding a data point from a DGX Spark (GLM-5.3-Flash-Q2), plus a lever that is missing from the server.

GLM-5.3-Flash takes three effort levels — low, high, max (chat template: reasoning_effort in ['low','high'] else 'max'; the model card says the same). ds4's ds4_think_mode only has NONE/HIGH/MAX and the parser folds low/minimal/medium into HIGH (the comment there says "DS4 only exposes HIGH and MAX" — true for DeepSeek, not for GLM). Actual render on the glm-5.3-flash branch @767e517 (parser is identical on main @b0a147a):

effort=low   -> [gMASK]<sop><|system|>Reasoning Effort: High<|user|>...
effort=high  -> [gMASK]<sop><|system|>Reasoning Effort: High<|user|>...
effort=max   -> [gMASK]<sop><|system|>Reasoning Effort: Max<|user|>...

So the model's low mode is unreachable over the API. It is also a cheap way out of the runaway described here. Same codegen prompt (a small CLI + pytest spec), ds4-server defaults:

  • high, max_tokens 4096 → all 4096 tokens spent in <think>, empty content, finish_reason: length
  • low unpatched (branch build) → same empty result (it is High underneath)
  • low patched (main @b0a147a), max_tokens 12288 → 928 chars of thinking, then the full answer, finish_reason: stop, 3903 tokens (at 4096 it gets cut mid-answer, but it answers)

Patch we run locally: add DS4_THINK_LOW; GLM renders Reasoning Effort: Low; DeepSeek keeps treating LOW as HIGH (it has no low tier); parser maps low|minimal → LOW, medium|high|xhigh → HIGH, max → MAX; --think low in CLI/agent. Render after the patch: low → Low, high → High, max → Max. ~7 files + tests, ds4_test --server and ds4_agent_test pass on CUDA (sm_121). Patch (main @b0a147a, 7 files + tests): https://gist.github.com/hyungraekim-bagelcode/c52e9c004f190091a305fd71474f222d. It is orthogonal to the reply-budget/forced-close work in #958.

(Patch and measurements were prepared with a coding agent and verified by hand on the Spark.)

hyungraekim-bagelcode avatar Sep 05 '26 09:09 hyungraekim-bagelcode

Follow-up to my comment above (that one was about the low effort tier; this one is the budget itself).

We implemented the server-side absolute thinking budget requested here, on top of #958, and have been running it in production on a DGX Spark (CUDA sm_121, GLM-5.3-Flash-Q2, --ctx 524288):

  • --think-budget-tokens N — once N tokens have been generated inside <think>, force </think> regardless of max_tokens. Needed because #958's reply budget is relative to max_tokens; with max_tokens near the context size (our agent setup) it never fires.
  • --think-budget-soft-rank N (default 3) / --think-budget-hard-extra N (default 1024): from the budget on, close as soon as </think> is in the model's top-N (a natural boundary), and force it at budget+N. Same soft/hard shape as #958, on the absolute axis.
  • --think-budget-message TEXT — optional notice placed inside <think> right before the close (the pattern from your client-side workaround; empty = bare close).

One finding that also affects #958 and ds4-eval: ds4_tokenize_text("</think>") is plain BPE, so the forced close is emitted as text pieces, not the </think> special token. The stream splitter accepts both, so everything looks right server-side, but the model then sees a textual close and answers with its own real </think> — a stray marker at the start of content. Measured with a 48-token budget to exercise the forced path on every request: textual close → stray </think> in 11/15 requests; close built with ds4_tokenize_rendered_chat (special token) → 0/15 (7 soft closes at a natural boundary

  • 8 hard closes mid-sentence with --think-budget-soft-rank 0, with and without the notice). The top-N soft check likewise only works with the special id (with text pieces it compares the > piece and never fires). Suggest #958 switch to ds4_tokenize_rendered_chat for the close marker.

With the budget at 48 the log shows think budget 48 reached (soft|hard close) and the answer completes with finish_reason: stop; in production we run 65536.

Patch (main @b0a147a + #958, ~320 lines incl. unit tests, CUDA-tested): https://gist.github.com/hyungraekim-bagelcode/2eb259a8fc4b6675c089dedfe8093d1e (edited: added the hard-close run; the gist now also accepts 0 for the three flags as the help text says.)

hyungraekim-bagelcode avatar Sep 05 '26 12:09 hyungraekim-bagelcode