Skip to content

server : add stuck-loop escape for ngram-mod (WIP) - #25819

Draft
yychyo wants to merge 1 commit into
ggml-org:masterfrom
yychyo:ngram-mod-stuck-fix
Draft

yychyo wants to merge 1 commit into
ggml-org:masterfrom
yychyo:ngram-mod-stuck-fix

Conversation

@yychyo

@yychyo yychyo commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

❗ This is WiP and a mitigation PR, not an actual fix.

Overview

When ngram-mod speculative decoding fails verification, spec_draft is set to the accepted tokens (including the correction token) and the checkpoint is restored. On the next iteration the draft is reused instead of regenerated and sometimes fails again, creating a loop with non-deterministic end condition.

This PRs adds logs to diagnose the problem, and a "band-aid" mitigation - it detects loop, and breaks out of it. However, the root cause of the loop is not yet clear.

Logs

Here are the logs that shows the issue:

❯ grep -B 60 -r "^8.36.225.102" debug-logs/ | grep 'issue#23268'
debug-logs/llama-20260717-144519.log-8.34.175.998 D slot   operator(): id  7 | task 0 | [issue#23268] PRE_DRAFT: spec_draft.empty() = no, n_past = 47206
debug-logs/llama-20260717-144519.log-8.34.690.806 D [issue#23268] VERIFY_FAIL: idx=20 draft=63 (logit=24.2188) sampled=7561 (logit=24.2500)
debug-logs/llama-20260717-144519.log-8.34.690.816 D slot   operator(): id  7 | task 0 | [issue#23268] ROLLBACK: streak = 2, spec_draft = 21 tokens
debug-logs/llama-20260717-144519.log-8.34.696.216 D slot   operator(): id  7 | task 0 | [issue#23268] PRE_DRAFT: spec_draft.empty() = no, n_past = 47206
debug-logs/llama-20260717-144519.log-8.35.205.159 D [issue#23268] VERIFY_FAIL: idx=20 draft=7561 (logit=24.2188) sampled=63 (logit=24.3125)
debug-logs/llama-20260717-144519.log-8.35.205.170 D slot   operator(): id  7 | task 0 | [issue#23268] ROLLBACK: streak = 3, spec_draft = 21 tokens
debug-logs/llama-20260717-144519.log-8.35.210.298 D slot   operator(): id  7 | task 0 | [issue#23268] PRE_DRAFT: spec_draft.empty() = no, n_past = 47206
debug-logs/llama-20260717-144519.log-8.35.717.474 D [issue#23268] VERIFY_FAIL: idx=20 draft=63 (logit=24.2188) sampled=7561 (logit=24.2500)
debug-logs/llama-20260717-144519.log-8.35.717.489 W slot   operator(): id  7 | task 0 | [issue#23268] STUCK speculative loop: 4 consecutive checkpoint restores with no progress (matched 20/21 draft tokens), diverging at draft index 20: draft 63 ('`', logit 24.2188) vs sampled 7561 ('`,', logit 24.2500). Applying mitigation to force progress - please report with server logs at https://github.com/ggml-org/llama.cpp/issues/23268
debug-logs/llama-20260717-144519.log-8.35.717.491 D slot   operator(): id  7 | task 0 | [issue#23268] ROLLBACK: streak = 4, spec_draft = 20 tokens
debug-logs/llama-20260717-144519.log-8.35.721.472 D slot   operator(): id  7 | task 0 | [issue#23268] PRE_DRAFT: spec_draft.empty() = no, n_past = 47206
debug-logs/llama-20260717-144519.log:8.36.225.102 I slot   operator(): id  7 | task 0 | [issue#23268] recovered from stuck speculative loop after 4 iterations

Additional information

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, llama.cpp + qwen3.6 35b-a3b + pi

@yychyo
yychyo force-pushed the ngram-mod-stuck-fix branch 4 times, most recently from 22d35b3 to c01a427 Compare July 23, 2026 07:27
@yychyo
yychyo force-pushed the ngram-mod-stuck-fix branch 5 times, most recently from e8e80ca to 22becd0 Compare August 3, 2026 20:23
@yychyo
yychyo force-pushed the ngram-mod-stuck-fix branch 3 times, most recently from d7f3b60 to 14b56c9 Compare August 12, 2026 15:16
@Janno

Janno commented Aug 13, 2026 •

Copy link
Copy Markdown

This PR fixes reproducable (endless?) loops I get on Nemotron-3.5-Lightning-30B-A3B.

EDIT: The PR title implies this is related to ngram-mod but even with only MTP I get loops that are fixed by the changes here.

Mitigation for a freeze where ngram-mod enters an infinite verification
loop. Does not fix the underlying state leak.

ggml-org#23268
@yychyo
yychyo force-pushed the ngram-mod-stuck-fix branch from 14b56c9 to e96c2a4 Compare August 13, 2026 13:42
@leinad-sch

Copy link
Copy Markdown

Same for Qwen3.x models, using ngram-mod.
Funnily I did not face the issue until I applied the fixes for the slot and checkpoint save/restore.

Would be great to have a fix merged.

@onnokort

Copy link
Copy Markdown

FWIW, as just another user I like to confirm that this fixes (at least in the sense of working around the issue) the repeated, sporadic hangs I had with llama-server. It was quite hard to hit this issue, so maybe a couple keywords for the search engines: "llama-server hangs, llama-server infinite-loop, llama-server stuck, ngram-mod, speculative decoding causes hangs". It helped me for ngram-mod.

Ninozzz95 added a commit to Ninozzz95/talos that referenced this pull request Sep 20, 2026
…misurano — KV cache decisa da llama-fit-params, decodifica speculativa a n-grammi, un motore morto non aspetta più il timeout

BC-13, owner 12/09: «rendere il motore di llm locale estremamente rapido e meglio dei competitor». Banco: Qwen3-4B-Q4_K_M, RX 9070 XT Vulkan, preambolo 11.022 token, 3 ripetizioni, mediane dai timings del server.

Prima → dopo: primo token a freddo 4.212 → 2.991 ms (−29 %); generazione nel compito d'agente 123,5 → 445,5 t/s (×3,6); chiamata intera primo turno 6.285 → 3.566 ms (−43 %); turno con cache calda 2.374 → 710 ms (−70 %); memoria di picco 4.378 → 6.060 MB.

Le tre cure (solo llama-server-supervisor.mjs e il suo test):
1. Il tipo della KV cache non è più fisso a q8_0 (costava il 39 % del primo token su un modello vero: la misura del 03/09 era sul giocattolo da 0,6B). Si chiede a llama-fit-params, spedito nella stessa build: f16 se entra tutto, altrimenti q8_0, e q8_0 se il fitter non risponde. Tre modelli, tre esiti diversi (4B/16k → f16, 4B/128k → q8_0, 27B/16k → q8_0), ognuno con il suo perché su stderr. Nessun modello predefinito: la regola vale per un GGUF qualunque.
2. Decodifica speculativa a n-grammi (--spec-type ngram-mod): non vuole un secondo modello; uscita identica byte per byte su 3 domande su 3; accesa solo se il --help del binario la dichiara; interruttore in un posto solo per il difetto aperto ggml-org/llama.cpp#25819; al contrario costa fino a −10,5 % sulla prima chiamata e si ripaga dalla seconda.
3. Un motore già morto non diventa pronto: il ciclo guardava solo entry.failure; il 27B muore in 4,9 s dicendo cosa non va e il supervisore aspettava 245 s. Ora fallisce subito con le ultime righe del motore.
Scartate perché misurate: -b/-ub (±1,7 %, rumore), --cache-reuse 256 (6.873 vs 6.906 ms). La cache del prefisso funziona già (11.140 su 11.157 token riusati al secondo turno).

Test 17/17 nel supervisore (8 nuovi nei due versi: la leva che si accende e quella che NON si accende quando il binario non la offre), 52 nei test vicini del runtime locale.
NON verificato: il 27B dell'owner non si carica su questa macchina oggi (VRAM occupata da dwm e da tre Chrome di altre sessioni): nessun prima/dopo sul modello vero; confronto con LM Studio/Ollama documentario (LM Studio senza GGUF, Ollama non installato); aggiornamento b10517 → b10921 non misurato; #25819 non cercato apposta.

Fonti (lette il 12/09/2026, fonti primarie: WebSearch esaurita): --help del binario b10517; ggml-org/llama.cpp tools/server README e docs (cache_prompt, --cache-reuse, --spec-type ngram-mod, llama-fit-params); ggml-org/llama.cpp#25819 (17/07/2026); LM Studio docs (speculative decoding a due modelli); Ollama docs (nessuna speculativa); Hermes Agent (nessun motore locale spedito: «Use any model you want»).
Rapporto: .claude/RAPPORTO-BC13-MOTORE-LOCALE-2026-09-12.md · misure: .claude/banco-bc13/

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants