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.

Serve: requeue long replies instead of reserving the whole context per request

YanissAmzabc825d16fe6
diff --git a/exllamav3/generator/job.py b/exllamav3/generator/job.py
index 86347b9..f4ea0b9 100644
--- a/exllamav3/generator/job.py
+++ b/exllamav3/generator/job.py
@@ -1121,7 +1121,7 @@ class Job:
"time_enqueued": self.time_enqueued,
"time_prefill": self.time_prefill,
"time_generate": self.time_generate,
- "rq_new_tokens": self.new_tokens, # every token accepted so far counts; the requeued segment starts after them
+ "rq_new_tokens": self.rq_new_tokens + self.new_tokens, # every token accepted so far counts, over any number of requeues; the requeued segment starts after them
"accepted_draft_tokens": self.accepted_draft_tokens,
"rejected_draft_tokens": self.rejected_draft_tokens,
"prompt_tokens": self.rq_prompt_tokens or len(seq.input_ids),
diff --git a/tools/glm/SERVE.md b/tools/glm/SERVE.md
index 8af6c82..bb13db2 100644
--- a/tools/glm/SERVE.md
+++ b/tools/glm/SERVE.md
@@ -36,7 +36,7 @@ call, a streamed call, a tool call, and two 400-token benchmark calls.
| `--image-cache-dir-max-gb` | `0` | GiB cap on that directory; `0` = disabled. There is no unlimited setting |
| `--image-cache-dir-lock-s` | `300` | seconds a conversation keeps the room its files took before another may take it; `0` = no tenure |
| `--image-cache-format` | `fp32` | row storage: `fp32` as the vision merger emits them, `fp16` (half the bytes, bit-exact on this pack) or `q8` (8 bit with one fp16 scale per row, a quarter, 0.86 % error) |
-| `--sessions` | `1` | requests decoded together through the one model (multi-user). `1` = one request at a time, others wait |
+| `--sessions` | `1` | requests decoded together through the one model (multi-user). `1` = one request at a time, others wait. With N > 1 a request without `max_tokens` holds only its share of the context (`-c` / N, at least 2048 tokens of reply) and is requeued when the reply goes on, so it does not block the other sessions |
| `--row-budget` | `5` when `--sessions` > 1 | caps the verify rows per round by lowering the draft depth when several sessions are busy; `0` = off |
| `--num-draft` | `2` | draft tokens per speculative round; `0` = plain decode, no drafter |
| `--exact-prefix-cache` | off | a request resumed from the prompt cache gives the same greedy text as its cold prefill. Costs about 5-12 % of cold prefill speed at 7-14K tokens |
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index 1911a26..700169a 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -958,8 +958,10 @@ class ResidentEngine:
sampler = ComboSampler(temperature=max(temperature, 1e-6), top_p=top_p, min_p=0.0, **kw)
generator = self.greedy_generator
conditions = ([] if ignore_eos else list(self.config.eos_token_id_list)) + stop
+ rq = serve_compat.rq_budget(getattr(self, "ctx", None), self.sessions, int(ids.numel()), max_tokens, getattr(self, "num_draft", 0))
job = self.Job(input_ids=ids, max_new_tokens=max_tokens, sampler=sampler,
- stop_conditions=conditions, embeddings=embs or None)
+ stop_conditions=conditions, embeddings=embs or None,
+ **({} if rq is None else {"max_rq_tokens": rq}))
if self.sessions > 1:
async for piece in self._run_shared(job, ids, cancel, stats if stats is not None else {}, t0, probe, prompt, ignore_eos, loop_guard):
yield piece
diff --git a/tools/qwen/SERVE.md b/tools/qwen/SERVE.md
index 911cc45..7fc761f 100644
--- a/tools/qwen/SERVE.md
+++ b/tools/qwen/SERVE.md
@@ -158,8 +158,13 @@ new text. `timings.prompt_n` shows how many tokens were prefilled. By default re
## Concurrent sessions
`--sessions N` decodes up to N requests together (one recurrent slot each); more requests wait in order. A request
-reserves the pages for its prompt plus `max_tokens` when it starts, so one that does not fit next to the running ones
-waits. With N > 1, a request with `speculative: false` or `cache_prompt: false` answers 400 (both rebuild the Generator), and slot
+reserves the pages for its prompt plus its reply budget when it starts, so one that does not fit next to the running ones
+waits. A client that sends no `max_tokens` (Hermes, most chat UIs) asks for a reply as long as the context: with N > 1 such a
+request holds only its share of the context (`-c` / N, at least 2048 tokens of reply) and the generator requeues it when the
+reply goes on: the pages are released, the request re-enters the queue at the front and continues as soon as pages are free
+(the full pages are found by hash, only the tail is prefilled again). The other sessions are not blocked, and a request
+running alone still reaches the end of the context. A reply that fits its share is untouched; `--sessions 1` is unchanged.
+`--default-max-tokens` still caps the reply when you want a hard limit. With N > 1, a request with `speculative: false` or `cache_prompt: false` answers 400 (both rebuild the Generator), and slot
save/restore/erase wait until the running requests finish.
Measured with `--ndt 3`, `-c 262144`, temperature 0, two short prompts sent together (four for `--sessions 4`):
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index 46d8530..a59a776 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -1168,9 +1168,12 @@ class QwenEngine:
job_ids = ids
while True:
- job = self.Job(input_ids=job_ids, max_new_tokens=max(max_tokens - generated + discount, 1),
+ max_new = max(max_tokens - generated + discount, 1)
+ rq = serve_compat.rq_budget(ctx, self.sessions, int(job_ids.numel()), max_new, int(getattr(self.generator, "num_draft_tokens", 0)), PAGE_TOKENS)
+ job = self.Job(input_ids=job_ids, max_new_tokens=max_new,
sampler=self._sampler(sampling), stop_conditions=([] if ignore_eos else self.eos) + list(stop),
embeddings=embs or None, decode_special_tokens=True, seed=seed,
+ **({} if rq is None else {"max_rq_tokens": rq}),
**({} if logprobs is None else {"return_probs": True, "return_top_tokens": logprobs}))
_mine.append(job)
q = await self._submit(job)
@@ -2175,7 +2178,8 @@ def main() -> None:
parser.add_argument("--slot-save-path", default="~/cache/llama-slots")
parser.add_argument("--sessions", type=int, default=1,
help="requests decoded together (default 1: one at a time, in order). Every session reserves the "
- "pages for prompt + max_tokens when it starts, so a request that does not fit waits")
+ "pages for its prompt + reply budget when it starts, so a request that does not fit waits. A reply longer than "
+ "its share of the context (-c / sessions) is requeued, not reserved up front")
args = parser.parse_args()
if args.sessions < 1:
parser.error("--sessions must be at least 1")
diff --git a/tools/serve_compat.py b/tools/serve_compat.py
index 53729d9..4557032 100644
--- a/tools/serve_compat.py
+++ b/tools/serve_compat.py
@@ -53,3 +53,26 @@ def usage(prompt_tokens: int, completion_tokens: int, cached_tokens: int | None
if cached_tokens is not None:
out["prompt_tokens_details"] = {"cached_tokens": max(0, min(int(cached_tokens), prompt_tokens))}
return out
+
+
+RQ_MIN_TOKENS = 2048 # smallest reply span a job holds pages for before it yields them (each yield costs a short re-prefill)
+
+
+def rq_budget(ctx: int | None, sessions: int, prompt_tokens: int, max_new: int, draft: int = 0, page: int = 256) -> int | None:
+ """Job `max_rq_tokens` for a request, or None to leave the generator's default.
+
+ The generator reserves the cache pages for prompt + max_new_tokens + 1 + draft when a job starts, so a request that
+ allows a reply as long as the context (no max_tokens) takes every page and the other sessions wait for it to end.
+ With several sessions each job holds pages for its fair share of the context (ctx // sessions rounded down to pages, at
+ least RQ_MIN_TOKENS of reply) and, if the reply goes on, the generator requeues it: the pages are released, the job re-enters the queue
+ at the front and takes the pages again when they are free (the full pages are found by hash, only the tail is prefilled).
+ A lone request therefore still reaches the end of the context; a reply that fits the share is untouched.
+ None for one session (nothing competes) and when the whole reply fits the share."""
+ if sessions <= 1 or not ctx:
+ return None
+ room = ctx // page * page - prompt_tokens # slots the page table can give this prompt's reply
+ share = ctx // sessions // page * page - prompt_tokens # the same for a whole number of pages of the fair share
+ budget = min(max(share, RQ_MIN_TOKENS), room)
+ if budget < 1 or max_new + 1 + draft <= budget:
+ return None
+ return budget
diff --git a/tools/test_serve_compat.py b/tools/test_serve_compat.py
index da5bda0..5aff46e 100644
--- a/tools/test_serve_compat.py
+++ b/tools/test_serve_compat.py
@@ -24,6 +24,23 @@ import serve_compat
RIGMARK = Path(os.environ.get("RIGMARK_SRC", "/nonexistent"))
+class RqBudgetTests(unittest.TestCase):
+ def test_one_session_or_no_ctx_keeps_the_default(self):
+ self.assertIsNone(serve_compat.rq_budget(65536, 1, 1000, 60000))
+ self.assertIsNone(serve_compat.rq_budget(None, 4, 1000, 60000))
+
+ def test_reply_that_fits_the_share_is_untouched(self):
+ self.assertIsNone(serve_compat.rq_budget(65536, 4, 1000, 8000)) # share 16384 - 1000 = 15384
+
+ def test_open_ended_reply_holds_its_share_only(self):
+ self.assertEqual(serve_compat.rq_budget(65536, 4, 1000, 64000), 65536 // 4 - 1000) # 16384 is whole pages
+
+ def test_long_prompt_gets_the_minimum_span_within_the_page_table(self):
+ self.assertEqual(serve_compat.rq_budget(65536, 4, 30000, 35000), serve_compat.RQ_MIN_TOKENS)
+ self.assertEqual(serve_compat.rq_budget(16384, 4, 40, 9000), 4096 - 40) # four jobs fit the page table
+ self.assertEqual(serve_compat.rq_budget(65536, 4, 64000, 5000), 65536 - 64000) # never beyond the cache
+
+
class HelperTests(unittest.TestCase):
def test_cache_salt(self):
self.assertIsNone(serve_compat.cache_salt({}))