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 server: --sessions N runs several requests through the one Generator (driver task, per-request stats, slot actions drain the sessions); N=1 path unchanged

YanissAmzbd6f3fc225bc
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index aaaac9a..2c06a1d 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -6,6 +6,7 @@ import traceback
import argparse
import asyncio
import base64
+import contextlib
import io
import json
import os
@@ -319,12 +320,15 @@ def load_image(url: str) -> tuple[Any, str]:
raise _bad(f"cannot decode image: {exc}") from exc
+MAX_SESSIONS = 16 # Cache default max_batch_size (recurrent state slots)
+
+
class ResidentEngine:
"""One loaded target model, optional MTP model, and resident paged caches."""
supports_prefix_reuse = True
def __init__(self, model_path: str, max_history: int = 1, max_ctx: int = 131072, num_draft: int = 1,
- vision: bool = True):
+ vision: bool = True, sessions: int = 1):
import torch
from exllamav3 import Cache, Config, Generator, Job, Model, Tokenizer
from exllamav3.generator import generator as GM
@@ -333,6 +337,12 @@ class ResidentEngine:
self.torch = torch
self.Job = Job
self.num_draft = num_draft
+ # Concurrent requests batched through the one Generator. The recurrent (KDA) state has Cache.max_batch_size = 16
+ # slots whatever the value (the Cache default, allocated in every case), so up to MAX_SESSIONS sessions cost no
+ # extra state memory; the paged KV pool (ctx + 4096 tokens) is shared by all sessions.
+ if not 1 <= sessions <= MAX_SESSIONS:
+ raise ValueError(f"sessions must be 1..{MAX_SESSIONS}")
+ self.sessions = sessions
self.config = Config.from_directory(model_path)
self.model = Model.from_config(self.config)
self.tokenizer = Tokenizer.from_config(self.config)
@@ -402,8 +412,9 @@ class ResidentEngine:
async def generate(self, prompt: str, *, max_tokens: int, temperature: float,
top_p: float, stop: list[str], sampler_kw: dict | None = None,
cancel: asyncio.Event | None = None,
- images: list | None = None) -> AsyncIterator[str]:
- """Yield incremental decoded text. Caller serializes access to this method.
+ images: list | None = None, stats: dict | None = None) -> AsyncIterator[str]:
+ """Yield incremental decoded text. With sessions == 1 the caller serializes access to this method; with
+ sessions > 1 several calls may run at once and the final stats of a request land in `stats`.
When `cancel` is set (client gone) the job is cancelled between two generator steps. A step is one
prefill chunk or one decode step, so a drop during prefill stops at the next chunk boundary."""
@@ -414,7 +425,10 @@ class ResidentEngine:
probe = getattr(self, "rss_probe", None)
embs = []
if images:
- embs = await asyncio.get_running_loop().run_in_executor(None, self._embed, images)
+ if self.sessions > 1: # the tower must not run next to a Generator step: run it on the driver, between steps
+ embs = await self._post(self._embed, images)
+ else:
+ embs = await asyncio.get_running_loop().run_in_executor(None, self._embed, images)
for emb in embs:
prompt = prompt.replace(IMAGE_TRIPLE, emb.text_alias, 1)
ids = self.tokenizer.encode(prompt, encode_special_tokens=True, embeddings=embs)
@@ -438,6 +452,10 @@ class ResidentEngine:
conditions = list(self.config.eos_token_id_list) + stop
job = self.Job(input_ids=ids, max_new_tokens=max_tokens, sampler=sampler,
stop_conditions=conditions, embeddings=embs or None)
+ if self.sessions > 1:
+ async for piece in self._run_shared(job, ids, cancel, stats if stats is not None else {}, t0, probe):
+ yield piece
+ return
generator.enqueue(job)
loop = asyncio.get_running_loop()
try:
@@ -465,6 +483,125 @@ class ResidentEngine:
if generator.num_remaining_jobs():
generator.clear_queue()
+ # Shared mode (sessions > 1). One driver task is the only code that touches the Generator: it runs queued commands
+ # (enqueue, cancel, image tower, slot actions) between two iterate() calls and routes the events of each step to the
+ # request that owns the job (by serial number).
+
+ def _post(self, fn, *args) -> asyncio.Future:
+ loop = asyncio.get_running_loop()
+ if getattr(self, "_driver", None) is None or self._driver.get_loop() is not loop:
+ self._cmds, self._routes, self._wake = [], {}, asyncio.Event()
+ self._driver = loop.create_task(self._drive())
+ elif self._driver.done():
+ exc = None if self._driver.cancelled() else self._driver.exception()
+ if exc is None:
+ raise RuntimeError("the engine driver has stopped")
+ print(f"serve: WARNING the engine driver crashed ({exc!r}); starting a new one", file=sys.stderr, flush=True)
+ self._driver = loop.create_task(self._drive())
+ fut = loop.create_future()
+ self._cmds.append((fn, args, fut))
+ self._wake.set()
+ return fut
+
+ async def _drive(self) -> None:
+ try:
+ await self._drive_loop()
+ finally: # cancelled or crashed: nothing will answer the waiting requests any more, so fail them now
+ stopped = RuntimeError("the engine driver has stopped")
+ for _, _, fut in self._cmds:
+ if not fut.done():
+ fut.set_exception(stopped)
+ self._cmds.clear()
+ for q in self._routes.values():
+ q.put_nowait(stopped)
+ self._routes.clear()
+
+ async def _drive_loop(self) -> None:
+ loop = asyncio.get_running_loop()
+ generator = self.greedy_generator
+ while True:
+ while self._cmds:
+ fn, args, fut = self._cmds.pop(0)
+ try:
+ res = await loop.run_in_executor(None, fn, *args)
+ except asyncio.CancelledError:
+ if not fut.done():
+ fut.set_exception(RuntimeError("the engine driver has stopped"))
+ raise
+ except Exception as exc: # noqa: BLE001
+ if not fut.done():
+ fut.set_exception(exc)
+ else:
+ if not fut.done():
+ fut.set_result(res)
+ if not generator.num_remaining_jobs():
+ self._wake.clear()
+ if not self._cmds:
+ await self._wake.wait()
+ continue
+ try:
+ events = await loop.run_in_executor(None, lambda: list(generator.iterate()))
+ except Exception as exc: # noqa: BLE001
+ # a failed step leaves every job in an unknown state: fail them all
+ for q in self._routes.values():
+ q.put_nowait(exc)
+ self._routes.clear()
+ generator.clear_queue()
+ continue
+ by_serial: dict[int, list] = {}
+ for ev in events:
+ by_serial.setdefault(ev.get("serial"), []).append(ev)
+ for serial, evs in by_serial.items():
+ q = self._routes.get(serial)
+ if q is not None: # None: cancelled after the step started
+ q.put_nowait(evs)
+
+ async def _run_shared(self, job, ids, cancel, stats: dict, t0: float, probe) -> AsyncIterator[str]:
+ """One request's job in the shared Generator: enqueue on the driver, stream its events, and (whatever way this
+ ends) remove the job, so no leftover job runs inside another request."""
+ q: asyncio.Queue = asyncio.Queue()
+ generator = self.greedy_generator
+
+ def enqueue():
+ self._routes[generator.enqueue(job)] = q # on the driver: no step can emit before the route exists
+ await self._post(enqueue)
+ try:
+ while True:
+ if cancel is not None and cancel.is_set():
+ return
+ try:
+ item = await asyncio.wait_for(q.get(), 0.25)
+ except asyncio.TimeoutError:
+ continue
+ if isinstance(item, Exception):
+ raise item
+ for event in item:
+ if event.get("error"):
+ raise RuntimeError(str(event["error"]))
+ text = event.get("text", "")
+ if text:
+ yield text
+ if event.get("eos"):
+ stats.update(event)
+ self.last_stats = stats
+ if probe is not None:
+ probe.request(int(ids.numel()), int(event.get("new_tokens", 0)), time.perf_counter() - t0)
+ return
+ finally:
+ def forget():
+ serial = job.serial_number
+ if self._routes.get(serial) is q:
+ del self._routes[serial]
+ for j in generator.active_jobs + generator.pending_jobs:
+ if j is job:
+ generator.cancel(j)
+ if not self._driver.done():
+ await asyncio.shield(self._post(forget))
+
+ async def run_exclusive(self, fn, *args):
+ """Run fn on the driver, between steps (slot save/restore; the caller has drained the requests)."""
+ return await self._post(fn, *args)
+
def warmup(engine: Any, template: str, prompt_tokens: int = 4608, decode_tokens: int = 16) -> float:
"""Run one ~4.6K-token prefill (a full 4096-row chunk plus a tail) and a short MTP decode before serving,
@@ -596,7 +733,11 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
"""Build the HTTP layer around a resident engine (also accepts a fake engine in tests)."""
queue: asyncio.Queue[tuple[dict[str, Any], asyncio.Queue, dict[str, Any]]] = asyncio.Queue()
lock = asyncio.Lock() # serializes generation with slot save/restore
+ sessions = int(getattr(engine, "sessions", 1))
+ gate = asyncio.Semaphore(sessions) # sessions > 1: one permit per running request; slot actions take them all
+ slot_lock = asyncio.Lock() # sessions > 1: one slot action at a time
processing = 0 # admitted requests; closure, app config is immutable after startup
+ running = 0 # sessions > 1: requests inside generate()
app = web.Application(client_max_size=64 * 1024**2, middlewares=[startup_health.json_errors_middleware()])
app.update(engine=engine, model_id=model_id, template=template, queue=queue, metrics=Metrics())
@@ -623,8 +764,26 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
return web.json_response({"object": "list", "data": [
{"id": model_id, "object": "model", "created": 0, "owned_by": "local"}]})
+ @contextlib.asynccontextmanager
+ async def exclusive():
+ """The generator is idle inside: sessions == 1 holds the lock; sessions > 1 waits for every running request."""
+ if sessions == 1:
+ async with lock:
+ yield
+ return
+ async with slot_lock:
+ got = 0
+ try:
+ for _ in range(sessions):
+ await gate.acquire()
+ got += 1
+ yield
+ finally:
+ for _ in range(got):
+ gate.release()
+
async def worker() -> None:
- nonlocal processing
+ nonlocal processing, running
while True:
body, out, request_stats = await queue.get()
cancel = body.get("_cancel")
@@ -635,17 +794,26 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
store = getattr(engine, "slot_store", None)
processing += 1 # admitted: counted from the dequeue, incl. lock and slot restore
try:
- async with lock:
+ async with (lock if sessions == 1 else gate):
+ running += 1
if store is not None:
store.busy = True
- engine.last_stats = {} # a cancelled or failed job must not re-count the previous one
- async for delta in engine.generate(
- body["_prompt"],
- max_tokens=token_limit(body), temperature=body.get("temperature", 0.0),
- top_p=body.get("top_p", 1.0), stop=body.get("_stop", []),
- cancel=body.get("_cancel"), **({"images": body["_images"]} if body.get("_images") else {})):
- out.put_nowait(delta)
- request_stats.update(getattr(engine, "last_stats", None) or {})
+ if sessions == 1:
+ engine.last_stats = {} # a cancelled or failed job must not re-count the previous one
+ try:
+ async for delta in engine.generate(
+ body["_prompt"],
+ max_tokens=token_limit(body), temperature=body.get("temperature", 0.0),
+ top_p=body.get("top_p", 1.0), stop=body.get("_stop", []),
+ cancel=body.get("_cancel"), **({"images": body["_images"]} if body.get("_images") else {}),
+ **({"stats": request_stats} if sessions > 1 else {})):
+ out.put_nowait(delta)
+ finally:
+ running -= 1
+ if sessions > 1 and store is not None:
+ store.busy = running > 0
+ if sessions == 1:
+ request_stats.update(getattr(engine, "last_stats", None) or {})
if request_stats:
observe(request_stats, body.get("_prompt_tokens") or int(request_stats.get("prompt_tokens", 0)))
out.put_nowait(None)
@@ -653,15 +821,17 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
out.put_nowait(exc)
finally:
processing -= 1
- if store is not None:
+ if store is not None and sessions == 1:
store.busy = False
queue.task_done()
async def start(_: web.Application) -> None:
- app["worker_task"] = asyncio.create_task(worker())
+ app["worker_tasks"] = [asyncio.create_task(worker()) for _ in range(sessions)]
+ app["worker_task"] = app["worker_tasks"][0]
async def close(app_: web.Application) -> None:
- app_["worker_task"].cancel()
+ for t in app_["worker_tasks"]:
+ t.cancel()
async def drain(request: web.Request, cancel: asyncio.Event, out: asyncio.Queue) -> AsyncIterator[str]:
"""Items the worker produced for one request. Nothing is written to the client while a job is
@@ -919,7 +1089,7 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
action = request.query.get("action")
body = await request.json() if request.can_read_body else {}
try:
- async with lock:
+ async with exclusive():
if action == "erase":
return web.json_response(store.erase())
if action not in ("save", "restore"):
@@ -970,6 +1140,9 @@ def main() -> None:
parser.add_argument("--port", type=int, default=8000)
parser.add_argument("--num-draft", type=int, default=2, help="MTP draft tokens per round (2 = R=3 verify, round2 27/09)")
parser.add_argument("--max-history", type=int, default=None, help="recurrent state history slots; default = --num-draft. Each extra slot costs ~2.15 GiB")
+ parser.add_argument("--sessions", type=int, default=1,
+ help=f"requests decoded together through the one Generator (1..{MAX_SESSIONS}); default 1 = one request at a time. "
+ "The KV pool (-c) is shared by all sessions")
parser.add_argument("-c", "--max-ctx", type=int, default=131072, help="context window (capped by max_position_embeddings)")
parser.add_argument("--slot-save-path", default="~/cache/llama-slots",
help="directory for /slots save/restore files (llama.cpp compatible)")
@@ -985,6 +1158,13 @@ def main() -> None:
parser.add_argument("--default-reasoning-effort", choices=("low", "medium", "high", "max"), default=None,
help="thinking effort when a request omits it (default: medium)")
args = parser.parse_args()
+ if not 1 <= args.sessions <= MAX_SESSIONS:
+ parser.error(f"--sessions must be 1..{MAX_SESSIONS}")
+ if args.sessions > 1:
+ print(f"serve: --sessions {args.sessions}: up to {args.sessions} requests share the KV pool of -c {args.max_ctx} "
+ f"(per-session window = -c / sessions when all are busy); greedy output is not guaranteed equal to the same "
+ f"request run alone; slot save/restore waits for every running request and acts on the last prompt noted",
+ file=sys.stderr, flush=True)
if args.no_uncensor:
os.environ["EXL3_ABLIT_RUNTIME"] = "off"
SERVE_DEFAULTS.update(pack_sampling(args.model))
@@ -1005,7 +1185,7 @@ def main() -> None:
startup_health.report_compiler()
try:
engine = ResidentEngine(args.model, max_history=args.max_history or args.num_draft, max_ctx=args.max_ctx, num_draft=args.num_draft,
- vision=not args.no_vision)
+ vision=not args.no_vision, sessions=args.sessions)
template = Path(args.chat_template or DEFAULT_TEMPLATE).expanduser().read_text(encoding="utf-8")
print(mem_line("loaded"), flush=True)
print(f"serve: vision={engine.supports_vision}", flush=True)
diff --git a/tools/glm/test_serve.py b/tools/glm/test_serve.py
index e671b44..9ac2f9e 100644
--- a/tools/glm/test_serve.py
+++ b/tools/glm/test_serve.py
@@ -601,5 +601,122 @@ class ImageEmbeddingReuseTests(unittest.TestCase):
self.assertEqual(len(calls), 2)
+class SessionsTests(unittest.TestCase):
+ """--sessions N: the app runs N requests at once; the engine driver routes one shared Generator's events."""
+ template = ServeTests.template
+
+ def test_requests_overlap_and_slot_action_waits_for_them(self):
+ from aiohttp.test_utils import TestClient, TestServer
+
+ class Slow(FakeEngine):
+ sessions = 3
+ live = 0
+ peak = 0
+
+ async def generate(self, prompt, stats=None, **kw):
+ Slow.live += 1
+ Slow.peak = max(Slow.peak, Slow.live)
+ try:
+ await asyncio.sleep(0.3)
+ stats.update({"new_tokens": 1, "prompt_tokens": 2})
+ yield "ok"
+ finally:
+ Slow.live -= 1
+
+ class Store:
+ busy = False
+ seen_live = None
+
+ def slots(self):
+ return []
+
+ def erase(self):
+ Store.seen_live = Slow.live
+ return {"id_slot": 0, "n_erased": 0}
+
+ async def check():
+ eng = Slow("x")
+ eng.slot_store = Store()
+ client = TestClient(TestServer(serve.create_app(eng, "m", self.template)))
+ await client.start_server()
+ body = {"model": "m", "messages": [{"role": "user", "content": "hi"}]}
+ reqs = [asyncio.create_task(client.post("/v1/chat/completions", json=body)) for _ in range(4)]
+ await asyncio.sleep(0.15)
+ act = await client.post("/slots/0?action=erase")
+ res = [await r for r in reqs]
+ out = ([r.status for r in res], act.status)
+ await client.close()
+ return out
+
+ statuses, act = run(check())
+ self.assertEqual(statuses, [200] * 4)
+ self.assertEqual(act, 200)
+ self.assertEqual(Slow.peak, 3) # 4 requests, 3 sessions
+ self.assertEqual(Store.seen_live, 0) # the slot action ran with no request inside generate()
+
+ def test_driver_routes_events_by_serial_and_cancel_keeps_the_others(self):
+ class Job:
+ serial_number = None
+
+ def __init__(self, n):
+ self.n = n
+
+ class Gen:
+ def __init__(self):
+ self.active_jobs, self.pending_jobs, self.k, self.cancelled = [], [], 0, []
+
+ def enqueue(self, job):
+ job.serial_number = len(self.active_jobs) + len(self.cancelled) + 100
+ self.active_jobs.append(job)
+ return job.serial_number
+
+ def num_remaining_jobs(self):
+ return len(self.active_jobs) + len(self.pending_jobs)
+
+ def cancel(self, job):
+ self.active_jobs.remove(job)
+ self.cancelled.append(job)
+
+ def clear_queue(self):
+ self.active_jobs.clear()
+
+ def iterate(self):
+ self.k += 1
+ out = []
+ for j in list(self.active_jobs):
+ ev = {"serial": j.serial_number, "text": f"{j.n}.{self.k} "}
+ if self.k >= j.n:
+ ev["eos"] = True
+ ev["new_tokens"] = self.k
+ self.active_jobs.remove(j)
+ out.append(ev)
+ return out
+
+ async def check():
+ eng = object.__new__(serve.ResidentEngine)
+ eng.sessions, eng.greedy_generator = 2, Gen()
+
+ async def one(n, cancel=None):
+ st, txt = {}, []
+ async for piece in eng._run_shared(Job(n), type("T", (), {"numel": lambda self: 1})(), cancel, st, 0.0, None):
+ txt.append(piece)
+ return "".join(txt), st
+
+ stop = asyncio.Event()
+ a = asyncio.create_task(one(2))
+ b = asyncio.create_task(one(50, stop))
+ ra = await a
+ stop.set()
+ rb = await b
+ return ra, rb, eng.greedy_generator
+
+ (ta, sa), (tb, sb), gen = run(check())
+ self.assertEqual(ta, "2.1 2.2 ")
+ self.assertEqual(sa["new_tokens"], 2)
+ self.assertTrue(tb.startswith("50.1 ") and "eos" not in sb)
+ self.assertEqual(gen.num_remaining_jobs(), 0) # the cancelled job left the Generator
+ self.assertEqual(len(gen.cancelled), 1)
+
+
if __name__ == "__main__":
unittest.main()