Skip to content

lanes: --loop-guard also closes a long passage repeated verbatim in the reasoning - #1075

Closed
BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:loop-guard-long
Closed

BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:loop-guard-long

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

--loop-guard closes a reasoning loop only when it's a cycle of at most 8 tokens held for 256. Greedy reasoning also loops on longer passages, and those run to the token limit without an answer. This extends the guard to them.

What we saw

  • Qwen3.5-2B (MLX 4-bit), thinking on, greedy, max_tokens 16384: 27 of 30 GSM8K and GPQA-Diamond prompts ran to the limit. The cycles were 36 to 96 tokens long, plus one of 879.
  • DeepSeek-V4.1-Flash on our engine branch: two GPQA-Diamond questions repeated paragraph-length passages (2,996 and 5,352 tokens). They used every reasoning token of a 48k reserve and never answered.

The change (zig/src/core/lanes/long_loop.zig, hooked into the existing guard in stream.zig)

  • Index: long_loop.Index records where each 32-token run of the reasoning ended, chained by hash.
  • Rule: a token closes the reasoning when the latest earlier end of its run, at least 9 tokens back, gives a period p, and the last max(p, 1,024) tokens each equal the token p before. That is a whole period, and at least 1,024 tokens, repeated verbatim.
  • Drafted windows: the rule depends only on the tokens, so loopCut cuts a drafted window at the same token a one-token-at-a-time run fires on.
  • Unchanged: it still acts only with --loop-guard and only while the think block is open, closes the block the same way, and reports the period in runtime.loop and the server log.
  • Docs: the README's flag row and docs/api.md's loop guard paragraph describe the new rule.

Checks (M5 Ultra; the only machine we ran)

  • long_loop.zig tests:
    • a 4,500-token passage fires as its second copy completes;
    • a 100-token one fires 1,024 tokens past its first copy;
    • both fire at the same token in windows of 1 and 6;
    • nothing fires on text that doesn't repeat, or that falls one token short of the rule.
  • long_loop_test.zig: through Stream.commit and loopCut, a 300-token passage closes the reasoning at token 1,524, one token at a time and in windows of six, with the think close forced.
  • Qwen3.5-2B, served from this branch with and without --loop-guard, on 20 GSM8K and 10 GPQA-Diamond prompts:
    • The guard fired on 27. Each of those replies' reasoning is byte-identical to the unguarded reply's up to the fire.
    • The other 3 replies are byte-identical between the two servers.
    • GSM8K's first 20: 3 right (3 finished) without the guard, 16 right (17 finished) with it.
    • Eleven guarded replies still reached the limit: the reasoning closed, then the answer itself looped. The guard doesn't watch the answer, and this PR doesn't change that.
  • DeepSeek-V4.1-Flash, on our branch with the same lanes code:
    • Both looping GPQA questions closed (at periods of 5,352 and 2,996 tokens) and answered.
    • One of them, run drafted and with "draft": false, gives the identical reply (same token hash).
    • Seven GSM8K thinking replies were identical with the guard and without.
  • zig build test: 351/351 steps succeeded; 4927/4949 tests passed (22 skipped). zig fmt is clean, and the lean check is unchanged from main (444).

…he reasoning

The guard closed a cycle of at most 8 tokens held for 256. Greedy reasoning also loops on longer passages, which it
missed: Qwen3.5-2B (MLX 4-bit) thinking on, greedy, ran 27 of 30 GSM8K and GPQA-Diamond prompts to a 16,384-token
limit, on cycles of 36 to 96 tokens and one of 879.

long_loop.Index keeps where each 32-token run of the reasoning ended, chained by hash. A token closes the reasoning
when the latest earlier end of its run at least 9 back gives a period p, and the last max(p, 1,024) tokens each equal
the token p before: a whole period, and at least 1,024 tokens, repeated verbatim. It is a function of the tokens
alone, so loopCut cuts a drafted window at the token a token at a time fires on. As before, it acts only with
--loop-guard and only while the think block is open, and reports the period.

Checks:
- long_loop.zig: a 4,500-token passage fires as its second copy completes, a 100-token one 1,024 tokens past its
  first copy, both at the same token in windows of 1 and 6; no fire on text that does not repeat, or that repeats a
  token short of the rule.
- long_loop_test.zig: through Stream.commit and loopCut, a 300-token passage closes the reasoning at token 1,524 a
  token at a time and in windows of six, with the think close forced.
- Qwen3.5-2B served with and without --loop-guard (M5 Ultra, thinking on, greedy, max_tokens 16384), 20 GSM8K and
  10 GPQA-Diamond prompts: the guard fired on 27, each reply's reasoning the unguarded one's up to the fire; the
  other 3 are byte-identical. GSM8K's first 20: 3 right (3 finished) without the guard, 16 right (17 finished) with it.
  Eleven guarded replies still reached the limit, looping in the answer after the close; the guard does not watch
  the answer.
- zig build test: 351/351 steps succeeded, 4,927 of 4,949 tests passed (22 skipped).
ashhart added a commit that referenced this pull request Oct 21, 2026
…dits

The 1.1.2 section had no entries for the contributed pull requests carried into this release, so neither their
changes nor their authors reached the release notes. Add an entry for each, written from its commits: Flash Next's
CUDA prompt tiles and structured output (#1046, #1062), --thinking-reserve, the long-loop guard and the --vision
template diagnostic (#1064, #1075, #1076), GLM's Metal eh_proj tile and CUDA waiting ranks (#1066, #1053), M1's
simdgroup cap (#1055), Nemotron's CUDA image and shared-window fixes (#1057, #1058), Qwen's live-row expert routing
(#1047), the shared CUDA operators and contract fixture (#1056, #1068, #1070, #1071, #1073), the ROCm receipt,
drafter and draft-count work (#1043, #1044, #1045), the ModelOpt recipe numbers (#1042), the Windows host build
(#1034) and the session store's allocation-free rollback (#637). Thank #643's and #1060's reporters as the other
entries thank theirs.
@ashhart

ashhart commented Oct 21, 2026

Copy link
Copy Markdown
Owner

The long repeated-reasoning check for --loop-guard is included in 1.1.2 (https://github.com/ashhart/TensorFold/releases/tag/v1.1.2), with your original authorship retained. When enabled, it closes a long verbatim loop inside reasoning so the reply can continue to its answer. Our follow-up reserves the loop index before publishing tokens, so that allocation can fail without changing the stream's context length. Closing this as carried, and thank you.

It landed as e9ba70f.

- Synthema, Ash's AI Assistant.

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