Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

glm serve: stop a job that runs past its budget or stalls (#162)

Yaniss Amazouzaf327b42f9e6
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index 513085f..52ab922 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -515,6 +515,34 @@ def guard_stats(hit: dict, n_new: int) -> dict:
return {"eos_reason": "loop_guard", "new_tokens": n_new, "loop_guard": hit}
+RUNAWAY_SLACK = 64 # tokens past max_tokens before a job counts as runaway (a speculative step may overshoot by its drafts)
+
+
+class RunawayWatch:
+ """Stops a single-session job that went wrong without ending: more tokens than its max_tokens, or no new token for
+ EXL3_STALL_S seconds (default 300, 0 = off) once decoding has started. A job that streams within its budget is never
+ touched, and the prefill (no token yet, however long) is never timed. Checked between two generator steps, so a
+ step that never returns is out of its reach. (06/11/2026: a title call kept GLM at 99 % GPU for 25 min.)"""
+
+ def __init__(self, max_tokens: int, stall_s: float | None = None, clock=time.monotonic):
+ self.max_tokens = int(max_tokens)
+ self.stall_s = float(os.environ.get("EXL3_STALL_S", "300")) if stall_s is None else float(stall_s)
+ self.clock = clock
+ self.seen = 0
+ self.last = None # time of the last new token; None until the first one
+
+ def check(self, seen: int) -> str | None:
+ """'over_budget' or 'stalled' when the job must stop, None otherwise. `seen` = tokens produced so far."""
+ now = self.clock()
+ if seen > self.seen:
+ self.seen, self.last = seen, now
+ if seen > self.max_tokens + RUNAWAY_SLACK:
+ return "over_budget"
+ if self.stall_s > 0 and self.last is not None and now - self.last > self.stall_s:
+ return "stalled"
+ return None
+
+
def held_tail(text: str, markers: list[str]) -> int:
"""Characters at the end of `text` that could still become one of `markers` (a proper prefix of one). A stream holds back only
these: holding a marker-length tail delayed the first visible text by several tokens, which under load is several seconds."""
@@ -1006,6 +1034,7 @@ class ResidentEngine:
guard_on = loop_guard_default() if loop_guard is None else bool(loop_guard) # loop_guard=False in the request turns it off
tap = AnswerTap(opens_thinking(prompt)) if guard_on and not ignore_eos else None
seen = 0 # tokens streamed so far, for the guard stop report
+ watch = RunawayWatch(max_tokens)
generator.enqueue(job)
try:
while generator.num_remaining_jobs():
@@ -1034,6 +1063,12 @@ class ResidentEngine:
probe.request(int(ids.numel()), int(event.get("new_tokens", 0)),
time.perf_counter() - t0)
return
+ why = watch.check(seen)
+ if why is not None:
+ generator.cancel(job)
+ self.last_stats = {"eos_reason": "max_new_tokens", "new_tokens": seen, "runaway": why}
+ print(f"serve: job stopped ({why}): {seen} tokens for max_tokens {max_tokens}", file=sys.stderr, flush=True)
+ return
torch.cuda.synchronize()
finally:
# Whatever way the call ends (error, client gone, cancelled task) no job stays in the Generator:
@@ -1672,7 +1707,7 @@ def create_app(engine: Any, model_id: str, template: str, admin_knobs: bool = Fa
def finish_for(message: dict[str, Any], raw: str) -> str:
# A reply cut by max_tokens is "length", not "stop" (clients use it to tell a truncated answer).
finish = message.pop("finish_reason", "stop")
- if finish == "stop" and (request_stats.get("eos_reason") == "loop_guard"
+ if finish == "stop" and (request_stats.get("eos_reason") == "loop_guard" or request_stats.get("runaway")
or engine.count_tokens(raw) >= token_limit(body)):
finish = "length"
return finish
diff --git a/tools/glm/test_serve.py b/tools/glm/test_serve.py
index 2152b39..9d23d1c 100644
--- a/tools/glm/test_serve.py
+++ b/tools/glm/test_serve.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import json
+import os
import sys
import unittest
from pathlib import Path
@@ -1012,5 +1013,54 @@ class VllmCompatTests(unittest.TestCase):
serve.template_kwargs({"chat_template_kwargs": "x"})
+class RunawayWatchTests(unittest.TestCase):
+ """06/11/2026: the guard that stops a job gone wrong must never touch a normal one."""
+
+ def watch(self, max_tokens=512, stall_s=300.0):
+ clock = [0.0]
+ return serve.RunawayWatch(max_tokens, stall_s=stall_s, clock=lambda: clock[0]), clock
+
+ def test_a_long_normal_generation_is_never_stopped(self):
+ w, clock = self.watch(max_tokens=200_000)
+ for step in range(1, 100_001): # 2 tokens a step, 30 s between steps: slow but moving
+ clock[0] += 30
+ self.assertIsNone(w.check(2 * step))
+
+ def test_a_long_prefill_is_never_timed(self):
+ w, clock = self.watch()
+ for _ in range(100): # 100 steps of 60 s without a token: the prefill of a huge prompt
+ clock[0] += 60
+ self.assertIsNone(w.check(0))
+ clock[0] += 1
+ self.assertIsNone(w.check(1))
+
+ def test_the_budget_allows_a_speculative_overshoot(self):
+ w, _ = self.watch(max_tokens=512)
+ self.assertIsNone(w.check(512 + serve.RUNAWAY_SLACK))
+ self.assertEqual(w.check(512 + serve.RUNAWAY_SLACK + 1), "over_budget")
+
+ def test_no_new_token_after_decoding_started_is_a_stall(self):
+ w, clock = self.watch(stall_s=300)
+ self.assertIsNone(w.check(5))
+ clock[0] += 300
+ self.assertIsNone(w.check(5)) # exactly the limit: still allowed
+ clock[0] += 1
+ self.assertEqual(w.check(5), "stalled")
+
+ def test_stall_zero_turns_the_stall_check_off(self):
+ w, clock = self.watch(stall_s=0)
+ w.check(5)
+ clock[0] += 10_000
+ self.assertIsNone(w.check(5))
+
+ def test_stall_default_comes_from_the_environment(self):
+ from unittest import mock
+ with mock.patch.dict(os.environ, {"EXL3_STALL_S": "42"}):
+ self.assertEqual(serve.RunawayWatch(10).stall_s, 42.0)
+ with mock.patch.dict(os.environ, {}, clear=False):
+ os.environ.pop("EXL3_STALL_S", None)
+ self.assertEqual(serve.RunawayWatch(10).stall_s, 300.0)
+
+
if __name__ == "__main__":
unittest.main()