diff --git a/serve/engine_llama.py b/serve/engine_llama.py new file mode 100644 index 00000000..948c8086 --- /dev/null +++ b/serve/engine_llama.py @@ -0,0 +1,300 @@ +"""serve/engine_llama.py - Strata's engine boundary over llama.cpp's `llama-server`. + +The Strata engine (`strata --serve`) is CUDA: sm_80+ tensor-core kernels, cudaGraph, cuBLAS. On a GPU without +CUDA - the Intel Arc Pro B70 this was written for - the same GGUF weights run through llama.cpp's SYCL backend. +This class puts that engine behind the same boundary the server already speaks: + + Engine.generate(prompt_ids, max_new, sampling, cancel) -> iterator of token ids (None = heartbeat) + +so everything above it - the OpenAI and Anthropic endpoints, streaming, tool calls, MCP, the web app - is +unchanged. It talks to llama-server's `/completion` with the prompt AS TOKEN IDS (the server's own tokenizer +produced them, from the same GGUF, so the ids agree) and `return_tokens`, which streams ids back one chunk +at a time with the timings Strata reports. + +Two ways to get an engine: + + LlamaEngine.attach("http://127.0.0.1:8081") a llama-server someone else runs (a container, say) + LlamaEngine.spawn([exe, *args], log=..., cwd=...) start `llama-server` ourselves and own its lifetime + +Both settle `max_context` from `/props` and expose the same `info` the Monitor tab reads. + +What it does not do (yet): images. Strata's `Vision` encodes images with `strata-vision` into embeddings the +CUDA engine reads (`GENI`); llama-server has its own multimodal path (`--mmproj`) that takes the image itself. +Until that is wired, run the server with no `vision` in the config and it refuses image requests cleanly. +""" +from __future__ import annotations + +import http.client +import json +import os +import subprocess +import threading +import time +import urllib.error +import urllib.parse +import urllib.request +from pathlib import Path +from typing import Iterator + +HEARTBEAT_S = 10 # yield None this often while the server is quiet (reading a long prompt) +READY_TIMEOUT_S = 20 * 60 # a 30 GB model from a cold SSD can take this long to load +STOP_TYPES = {"eos": "stop", "word": "stop", "limit": "length"} # llama-server's stop_type -> Strata's finish + + +class EngineDied(RuntimeError): + """The engine went away in the middle of a request.""" + + +class LlamaEngine: + def __init__(self, url: str, spawn: list[str] | None = None, cwd: str | None = None, log: str | None = None, + env: dict | None = None, model_path: str = "", name: str = "llama.cpp"): + self.url = url.rstrip("/") + self.spawn_cmd, self.cwd, self.log_path, self.env = spawn, cwd, log, env + self.model_path = model_path + self.proc: subprocess.Popen | None = None + self.log = None + self.ended = False + self.max_context = 0 + self.can_stop = True # closing the HTTP response stops llama-server's generation + self.last: dict = {} + self.info: dict = {"engine": name} + self.progress = None # llama-server reports no prompt-reading progress on this path + self._lock = threading.Lock() + if spawn: + self._start() + self._wait_ready() + + # ------------------------------------------------------------------------------------ construction + @classmethod + def attach(cls, url: str, model_path: str = "") -> "LlamaEngine": + """Use a llama-server that is already running (started by hand, by a container, by the render front end).""" + return cls(url, model_path=model_path) + + @classmethod + def spawn(cls, cmd: list[str], url: str, cwd: str | None = None, log: str | None = None, + env: dict | None = None, model_path: str = "") -> "LlamaEngine": + """Start `llama-server` ourselves. `cmd` is the whole command line, `url` where it will listen.""" + return cls(url, spawn=cmd, cwd=cwd, log=log, env=env, model_path=model_path) + + def _start(self): + self.log = open(self.log_path, "a", encoding="utf-8") if self.log_path else subprocess.DEVNULL + self.proc = subprocess.Popen(self.spawn_cmd, cwd=self.cwd, stdout=self.log, stderr=subprocess.STDOUT, + env=self.env) + self.ended = False + + def _wait_ready(self): + """`/health` says 503 until the model is loaded; `/props` answers once it is and carries n_ctx.""" + t0 = time.time() + while True: + if self.proc is not None and self.proc.poll() is not None: + raise EngineDied(f"llama-server exited during load (code {self.proc.returncode})" + + (f"; see {self.log_path}" if self.log_path else "")) + try: + props = self._get("/props", timeout=5) + gen = props.get("default_generation_settings") or {} + self.max_context = int(gen.get("n_ctx") or 0) + if self.max_context: + self.info.update( + version=(props.get("build_info") or "").strip() or None, + n_ctx=self.max_context, + model=os.path.basename(props.get("model_path") or self.model_path or ""), + modalities=",".join(k for k, v in (props.get("modalities") or {}).items() if v) or "text", + ) + if not self.model_path: + self.model_path = props.get("model_path") or "" + return + except (urllib.error.URLError, http.client.HTTPException, OSError, ValueError): + pass + if time.time() - t0 > READY_TIMEOUT_S: + raise EngineDied(f"llama-server at {self.url} did not become ready in {READY_TIMEOUT_S // 60} min") + elapsed = int(time.time() - t0) + if elapsed and elapsed % 30 == 0: + print(f"[strata] still loading ({elapsed} s) - a 30 GB model takes a few minutes ...", flush=True) + time.sleep(1) + + # ------------------------------------------------------------------------------------------ http + def _get(self, path: str, timeout: float = 30): + with urllib.request.urlopen(self.url + path, timeout=timeout) as r: + return json.loads(r.read().decode("utf-8")) + + def _post_stream(self, path: str, body: dict, timeout: float): + req = urllib.request.Request(self.url + path, data=json.dumps(body).encode("utf-8"), + headers={"content-type": "application/json"}) + return urllib.request.urlopen(req, timeout=timeout) + + # -------------------------------------------------------------------------------------- generate + @staticmethod + def sampling_body(sampling: dict) -> dict: + """Strata's request sampling -> llama-server's field names. Absent temperature means greedy, as the + CUDA engine does; a client's own values always win over the server's defaults.""" + out: dict = {} + t = sampling.get("temperature") + out["temperature"] = float(t) if isinstance(t, (int, float)) and not isinstance(t, bool) and t > 0 else 0.0 + for src, dst, lo in (("top_p", "top_p", 0.0), ("min_p", "min_p", 0.0), + ("presence_penalty", "presence_penalty", None), + ("frequency_penalty", "frequency_penalty", None), + ("repetition_penalty", "repeat_penalty", None)): + v = sampling.get(src) + if isinstance(v, (int, float)) and not isinstance(v, bool): + out[dst] = float(v) + tk = sampling.get("top_k") + if isinstance(tk, int) and not isinstance(tk, bool) and tk >= 0: + out["top_k"] = tk + n = sampling.get("penalty_last_n") + if isinstance(n, int) and not isinstance(n, bool): + out["repeat_last_n"] = n + seed = sampling.get("seed") + if isinstance(seed, int) and not isinstance(seed, bool): + out["seed"] = seed + return out + + def generate(self, ids: list[int], max_new: int, sampling: dict, cancel: threading.Event, + embeddings=None) -> Iterator[int | None]: + """Yields token ids as llama-server streams them, and None every HEARTBEAT_S while it is quiet. + A consumer that stops early, or `cancel`, closes the response, which stops the generation server-side.""" + if embeddings: + raise ValueError("images are not supported on the llama.cpp engine yet (start without vision)") + body = {"prompt": [int(t) for t in ids], "n_predict": int(max_new), "stream": True, + "return_tokens": True, "cache_prompt": True, **self.sampling_body(sampling or {})} + self.last = {} + self.progress = None + try: + resp = self._post_stream("/completion", body, timeout=HEARTBEAT_S) + except urllib.error.HTTPError as e: + detail = e.read().decode("utf-8", "replace")[:300] + raise ValueError(f"llama-server refused the request ({e.code}): {detail}") from None + except (urllib.error.URLError, OSError) as e: + raise EngineDied(f"llama-server is not answering: {e}") from None + finished = False + try: + buf = b"" + while True: + if cancel.is_set(): + return + try: + chunk = resp.read1(65536) if hasattr(resp, "read1") else resp.read(65536) + except TimeoutError: + yield None # quiet: reading a long prompt + continue + except (http.client.IncompleteRead, OSError) as e: + raise EngineDied(f"the connection to llama-server broke mid-answer: {e}") from None + if not chunk: + raise EngineDied("llama-server closed the stream without finishing") + buf += chunk + while b"\n" in buf: + line, buf = buf.split(b"\n", 1) + line = line.strip() + if not line.startswith(b"data: "): + continue + ev = json.loads(line[6:]) + if "error" in ev: + raise ValueError(str(ev["error"])) + for t in ev.get("tokens") or []: + if cancel.is_set(): + return + yield int(t) + if ev.get("stop"): + tm = ev.get("timings") or {} + self.last = {"generated": int(tm.get("predicted_n") or ev.get("tokens_predicted") or 0), + "prompt_tokens": int(tm.get("prompt_n") or ev.get("tokens_evaluated") or 0), + "prompt_ms": float(tm.get("prompt_ms") or 0.0), + "decode_ms": float(tm.get("predicted_ms") or 0.0), + "finish": STOP_TYPES.get(ev.get("stop_type"), "stop"), + "reused": int(tm.get("cache_n") or 0)} + finished = True + return + finally: + try: + resp.close() # early stop: dropping the connection stops the server + except Exception: + pass + if not finished and not self.last: + self.last = {"finish": "cancelled"} + + # ------------------------------------------------------------------------------------- lifecycle + def alive(self) -> bool: + if self.ended: + return False + if self.proc is not None: + return self.proc.poll() is None + try: + self._get("/health", timeout=3) + return True + except Exception: + return False + + def exit_code(self): + if self.proc is None: + return None + try: + return self.proc.wait(timeout=5) + except subprocess.TimeoutExpired: + return None + + def death_note(self) -> str: + tail = "" + try: + with open(self.log_path, "rb") as f: + f.seek(0, 2) + f.seek(max(0, f.tell() - 4096)) + tail = f.read().decode("utf-8", "replace") + except (OSError, TypeError): + pass + low = tail.lower() + if "out of memory" in low or "sycl_out_of_memory" in low or "failed to allocate" in low: + return "the GPU ran out of memory (a smaller context, or fewer layers on the card)" + if "sigsegv" in low or "segmentation fault" in low: + return "llama-server crashed (if SYCL_CACHE_PERSISTENT is set, unset it: the JIT cache segfaults on Xe2)" + if self.proc is None: + return f"the llama-server at {self.url} stopped answering (it is not managed by this server)" + return "llama-server stopped; the end of its log:\n" + tail[-1200:] + + def restart(self): + if self.proc is None: + self.ended = False + self._wait_ready() # attached: wait for whoever runs it to bring it back + return + try: + self.proc.kill() + except OSError: + pass + info = dict(self.info) + self._start() + self._wait_ready() + self.info = {**info, **self.info} + + def close(self): + self.ended = True + if self.proc is None: + return + try: + self.proc.terminate() + self.proc.wait(timeout=15) + except Exception: + try: + self.proc.kill() + except OSError: + pass + + +def engine_from_config(cfg: dict, env: dict | None = None) -> LlamaEngine: + """Build the engine the run config describes. + + "llama": {"url": "http://127.0.0.1:8081"} attach to a running llama-server + "llama": {"exe": ".../llama-server", "args": [...], "port": 8081} spawn it (args after -m/--port are ours) + + `model` (the GGUF) is optional and only labels the Monitor tab when attached.""" + L = cfg.get("llama") or {} + model = L.get("model") or cfg.get("model") or "" + if L.get("exe"): + port = int(L.get("port") or 8081) + host = L.get("host") or "127.0.0.1" + cmd = [L["exe"], *[str(a) for a in (L.get("args") or [])], "--host", host, "--port", str(port)] + if model and "-m" not in cmd and "--model" not in cmd: + cmd += ["-m", model] + return LlamaEngine.spawn(cmd, f"http://{host}:{port}", cwd=cfg.get("cwd"), log=cfg.get("log"), env=env, + model_path=model) + url = L.get("url") + if not url: + raise ValueError('the config\'s "llama" block needs either "url" (attach) or "exe" (spawn)') + return LlamaEngine.attach(url, model_path=model) diff --git a/serve/server.py b/serve/server.py index 7054d799..b79047b0 100644 --- a/serve/server.py +++ b/serve/server.py @@ -1636,7 +1636,8 @@ def sampling_defaults_from_config(cfg: dict) -> dict: def main() -> int: ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) - ap.add_argument("--engine", choices=["mock", "strata"], default="mock") + ap.add_argument("--engine", choices=["mock", "strata", "llama"], default="mock", + help="strata = the CUDA engine; llama = llama.cpp's llama-server (any GPU llama.cpp runs on, e.g. Intel Arc via SYCL); see serve/engine_llama.py") ap.add_argument("--config", help="strata engine config (JSON: exe, args, cwd, tokenizer, model_name), " "written by setup.py") ap.add_argument("--host", default=None, @@ -1673,7 +1674,7 @@ def main() -> int: a.tokenizer = cfg["tokenizer"] tok = ByteTokenizer() tpath = Path(a.tokenizer) - if a.engine == "strata" and not (tpath / "vocab.json").exists(): + if a.engine in ("strata", "llama") and not (tpath / "vocab.json").exists(): ap.error(f"the model's tokenizer is missing ({tpath / 'vocab.json'}); run setup again") if (tpath / "vocab.json").exists(): import strata_tokenizer as ST @@ -1703,6 +1704,22 @@ def main() -> int: print(f"[strata] layer split across GPUs {gpu_list(cfg)} ({cfg.get('layer_split') or 'auto'})", flush=True) engine = StrataEngine(cfg["exe"], engine_args(cfg), cwd=cfg.get("cwd"), log=cfg.get("log"), env=env) warn_tight_ram(engine.info.get("arena_mib")) + elif a.engine == "llama": + if not cfg: + ap.error("--engine llama needs --config (with a \"llama\" block: url to attach, or exe to spawn)") + from serve.engine_llama import engine_from_config as llama_engine + vision = None # images: not on this engine yet (engine_llama.py) + sampling_defaults = sampling_defaults_from_config(cfg) + if sampling_defaults: + pretty = ", ".join(f"{k}={v}" for k, v in sampling_defaults.items()) + print(f"[strata] sampling defaults from the config: {pretty}", flush=True) + if cfg.get("vision"): + print("[strata] note: \"vision\" in the config is ignored on the llama engine (images not wired yet)", + flush=True) + print("connecting to llama-server (or starting it: a 30 GB model takes a few minutes) ...", flush=True) + engine = llama_engine(cfg, env=child_env(cfg)) + print(f"[strata] llama.cpp engine up: {engine.info.get('model') or engine.model_path}, " + f"context {engine.max_context}, build {engine.info.get('version') or '?'}", flush=True) else: engine, vision, sampling_defaults = MockEngine(tok, a.script or [ "Thinking about it.\n\nHello from the mock engine."]), None, {} diff --git a/serve/test_engine_llama.py b/serve/test_engine_llama.py new file mode 100644 index 00000000..77369ff3 --- /dev/null +++ b/serve/test_engine_llama.py @@ -0,0 +1,197 @@ +"""serve/test_engine_llama.py - the llama.cpp engine boundary, against a scripted llama-server (no GPU). + + python -m unittest serve.test_engine_llama -v + STRATA_LLAMA_URL=http://:8081 python -m unittest serve.test_engine_llama -v (+ the live test) + +The stub speaks exactly what the real llama-server was observed to send on 2026-09-29 (llama.cpp b29c606): +`/props` with default_generation_settings.n_ctx, and `/completion` streaming `data: {...}` lines with +`tokens`, `stop`, and on the last line `stop_type` and `timings`. +""" +from __future__ import annotations + +import json +import os +import sys +import threading +import time +import unittest +import urllib.request +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from serve.engine_llama import EngineDied, LlamaEngine, engine_from_config # noqa: E402 + +N_CTX = 4096 + + +class Stub(BaseHTTPRequestHandler): + """A llama-server that streams a scripted list of ids, one per chunk, slowly enough to test cancel.""" + script: list[int] = [] + delay_s = 0.0 + seen: list[dict] = [] + error: str | None = None + stop_type = "eos" + + def log_message(self, *a): + pass + + def do_GET(self): + if self.path == "/props": + body = {"default_generation_settings": {"n_ctx": N_CTX}, "build_info": "stub-1", + "model_path": "/models/stub.gguf", "modalities": {"vision": False}} + elif self.path == "/health": + body = {"status": "ok"} + else: + self.send_response(404); self.end_headers(); return + b = json.dumps(body).encode() + self.send_response(200); self.send_header("content-type", "application/json") + self.send_header("content-length", str(len(b))); self.end_headers(); self.wfile.write(b) + + def do_POST(self): + n = int(self.headers.get("content-length") or 0) + req = json.loads(self.rfile.read(n)) + Stub.seen.append(req) + self.send_response(200); self.send_header("content-type", "text/event-stream"); self.end_headers() + if Stub.error: + self.wfile.write(f"data: {json.dumps({'error': Stub.error})}\n\n".encode()); self.wfile.flush(); return + ids = Stub.script[: int(req["n_predict"])] + for i, t in enumerate(ids): + last = i == len(ids) - 1 + ev = {"tokens": [t], "content": "x", "stop": last, "tokens_predicted": i + 1, + "tokens_evaluated": len(req["prompt"])} + if last: + ev["stop_type"] = Stub.stop_type if len(ids) == len(Stub.script) else "limit" + ev["timings"] = {"prompt_n": len(req["prompt"]), "prompt_ms": 12.5, "predicted_n": len(ids), + "predicted_ms": 40.0, "cache_n": 3} + try: + self.wfile.write(f"data: {json.dumps(ev)}\n\n".encode()); self.wfile.flush() + except (BrokenPipeError, ConnectionResetError): + Stub.seen[-1]["broken_at"] = i + return + if Stub.delay_s and not last: + time.sleep(Stub.delay_s) + + +class Offline(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.httpd = ThreadingHTTPServer(("127.0.0.1", 0), Stub) + threading.Thread(target=cls.httpd.serve_forever, daemon=True).start() + cls.url = f"http://127.0.0.1:{cls.httpd.server_address[1]}" + + @classmethod + def tearDownClass(cls): + cls.httpd.shutdown(); cls.httpd.server_close() + + def setUp(self): + Stub.script, Stub.delay_s, Stub.seen, Stub.error, Stub.stop_type = [11, 22, 33, 44, 55], 0.0, [], None, "eos" + + def test_attach_reads_context_and_info(self): + e = LlamaEngine.attach(self.url) + self.assertEqual(e.max_context, N_CTX) + self.assertEqual(e.info["version"], "stub-1") + self.assertEqual(e.info["model"], "stub.gguf") + self.assertTrue(e.alive()) + + def test_generate_streams_ids_and_reports_timings(self): + e = LlamaEngine.attach(self.url) + out = [t for t in e.generate([1, 2, 3], 10, {"temperature": 0.7, "top_k": 20}, threading.Event()) + if t is not None] + self.assertEqual(out, [11, 22, 33, 44, 55]) + self.assertEqual(e.last["generated"], 5) + self.assertEqual(e.last["prompt_tokens"], 3) + self.assertEqual(e.last["finish"], "stop") + self.assertEqual(e.last["reused"], 3) + self.assertAlmostEqual(e.last["prompt_ms"], 12.5) + req = Stub.seen[-1] + self.assertEqual(req["prompt"], [1, 2, 3]) # ids, not text + self.assertTrue(req["return_tokens"] and req["stream"] and req["cache_prompt"]) + self.assertAlmostEqual(req["temperature"], 0.7) + self.assertEqual(req["top_k"], 20) + + def test_max_new_caps_and_maps_to_length(self): + e = LlamaEngine.attach(self.url) + out = [t for t in e.generate([1], 2, {}, threading.Event()) if t is not None] + self.assertEqual(out, [11, 22]) + self.assertEqual(e.last["finish"], "length") + + def test_greedy_when_no_temperature(self): + e = LlamaEngine.attach(self.url) + list(e.generate([1], 1, {}, threading.Event())) + self.assertEqual(Stub.seen[-1]["temperature"], 0.0) + + def test_cancel_stops_early_and_closes_the_stream(self): + Stub.delay_s = 0.05 + e = LlamaEngine.attach(self.url) + cancel = threading.Event() + got = [] + for t in e.generate([1], 5, {}, cancel): + if t is not None: + got.append(t) + if len(got) == 2: + cancel.set() + self.assertEqual(got, [11, 22]) + self.assertEqual(e.last, {"finish": "cancelled"}) + time.sleep(0.3) # the stub notices the closed socket on its next write + self.assertIn("broken_at", Stub.seen[-1]) + + def test_consumer_stopping_early_closes_the_stream(self): + Stub.delay_s = 0.05 + e = LlamaEngine.attach(self.url) + gen = e.generate([1], 5, {}, threading.Event()) + self.assertEqual(next(t for t in gen if t is not None), 11) + gen.close() + time.sleep(0.3) + self.assertIn("broken_at", Stub.seen[-1]) + + def test_server_error_event_is_a_value_error(self): + Stub.error = "context too long" + e = LlamaEngine.attach(self.url) + with self.assertRaises(ValueError): + list(e.generate([1], 1, {}, threading.Event())) + + def test_images_refused_clearly(self): + e = LlamaEngine.attach(self.url) + with self.assertRaises(ValueError): + list(e.generate([1], 1, {}, threading.Event(), embeddings="/tmp/x.sve")) + + def test_sampling_body_mapping(self): + b = LlamaEngine.sampling_body({"temperature": 0, "top_p": 0.9, "min_p": 0.05, "repetition_penalty": 1.1, + "presence_penalty": 1.5, "penalty_last_n": 64, "seed": 7, "top_k": 0}) + self.assertEqual(b, {"temperature": 0.0, "top_p": 0.9, "min_p": 0.05, "repeat_penalty": 1.1, + "presence_penalty": 1.5, "repeat_last_n": 64, "seed": 7, "top_k": 0}) + + def test_engine_from_config_attach(self): + e = engine_from_config({"llama": {"url": self.url, "model": "/m/x.gguf"}}) + self.assertEqual(e.model_path, "/m/x.gguf") + with self.assertRaises(ValueError): + engine_from_config({"llama": {}}) + + def test_attach_to_nothing_dies_quickly(self): + import serve.engine_llama as M + old = M.READY_TIMEOUT_S + M.READY_TIMEOUT_S = 2 + try: + with self.assertRaises(EngineDied): + LlamaEngine.attach("http://127.0.0.1:9") + finally: + M.READY_TIMEOUT_S = old + + +@unittest.skipUnless(os.environ.get("STRATA_LLAMA_URL"), "set STRATA_LLAMA_URL to test against a live llama-server") +class Live(unittest.TestCase): + def test_round_trip(self): + e = LlamaEngine.attach(os.environ["STRATA_LLAMA_URL"]) + self.assertGreater(e.max_context, 0) + ids = json.loads(urllib.request.urlopen(urllib.request.Request( + e.url + "/tokenize", data=json.dumps({"content": "def fib(n):", "add_special": True}).encode(), + headers={"content-type": "application/json"}), timeout=30).read())["tokens"] + out = [t for t in e.generate(ids, 8, {"temperature": 0}, threading.Event()) if t is not None] + self.assertEqual(len(out), 8) + self.assertEqual(e.last["generated"], 8) + self.assertGreater(e.last["decode_ms"], 0) + + +if __name__ == "__main__": + unittest.main()