From c40a03e6dc82cc28127577858fdda3f5cbf8412e Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 22 Oct 2026 12:52:37 +0200 Subject: [PATCH] serve: a tool's picture with an elastic image encoder starts the encoder (I6); a failed encoder start carries its stderr tail, exit status and card hint (I3) A picture a tool returned wrote to an elastic encoder that was not running (AttributeError, then a 400 that every later turn repeated). _note_unreadable_tool_images now calls _vision_up and turns a start failure into the usual [image omitted: why] note; Vision.encode raises a clear ValueError when no reader runs. vision_start_error no longer returns a blank sentence: last stderr lines, the exit status in words, the seconds of a timeout, and a kernel-image error (sm_60 on the Windows cuda12 reader, #3669) names the card/architecture and the CPU way out. --- serve/server.py | 72 +++++++++++++++++++++++------ serve/test_vision_start_error.py | 78 ++++++++++++++++++++++++++++++++ serve/test_vision_tool_image.py | 75 ++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+), 13 deletions(-) create mode 100644 serve/test_vision_start_error.py create mode 100644 serve/test_vision_tool_image.py diff --git a/serve/server.py b/serve/server.py index 632d3c04..ddd968d6 100644 --- a/serve/server.py +++ b/serve/server.py @@ -2489,29 +2489,67 @@ def network_path(path: str) -> bool: return p.startswith("\\\\") or p.startswith("\\??\\") -def vision_start_error(line: str, log=None) -> str: - """The message when the image encoder answered something other than READY. #1445: a card in the Exclusive_Process - compute mode (or one another process holds) refuses the encoder's CUDA context with "busy or unavailable"; that - cause is named instead of leaving the user the first line of a traceback. `log` is the encoder's stderr file, whose - tail is searched too (the CUDA error is printed there).""" - text = line.strip() - tail = "" +def vision_log_tail(log=None, limit: int = 16384) -> str: + """The last `limit` bytes of the encoder's stderr file (`log` is the file object it was started with; "" when there + is none or it cannot be read).""" try: name = getattr(log, "name", None) if name: with open(name, "rb") as f: f.seek(0, 2) - f.seek(max(0, f.tell() - 16384)) - tail = f.read().decode("utf-8", "replace") + f.seek(max(0, f.tell() - limit)) + return f.read().decode("utf-8", "replace") except (OSError, ValueError): pass - if "busy or unavailable" in (text + tail).lower(): + return "" + + +# What the CUDA / HIP runtimes print when the reader has no code for the card it runs on (a binary built without that +# architecture: #3669, the Windows cuda12 package's reader on a Pascal card). +_VISION_ARCH_SIGNS = ("device kernel image is invalid", "no kernel image is available", "im2col failed", + "unsupported gpu architecture", "invalid device function", "unknown gpu architecture") + + +def vision_start_error(line: str, log=None, code: int | None = None, waited: float | None = None) -> str: + """The message when the image encoder answered something other than READY. #1445: a card in the Exclusive_Process + compute mode (or one another process holds) refuses the encoder's CUDA context with "busy or unavailable"; that + cause is named instead of leaving the user the first line of a traceback. `log` is the encoder's stderr file, whose + tail is searched too (the CUDA error is printed there). An encoder that dies before it prints anything (#3645, #3444, + #3669, #3697) used to give a blank sentence: now the last lines of its stderr, its exit status (`code`) and, for a + kernel-image error, the card/architecture hint come with it. `waited`: it said nothing for that many seconds.""" + text = line.strip() + tail = vision_log_tail(log) + both = (text + chr(10) + tail).lower() + if "busy or unavailable" in both: return ("the vision encoder did not start: the GPU refused a second CUDA context (\"CUDA-capable device(s) " "is/are busy or unavailable\"). This is usually the compute mode Exclusive_Process: only one process " "may use the card, and the engine already holds it. Check with: nvidia-smi --query-gpu=compute_mode " "--format=csv; set it back with: sudo nvidia-smi -c DEFAULT (or run the encoder on another card). " "First line from the encoder: " + (text or "(none)")) - return "the vision encoder did not start: " + text + lines = [x.strip() for x in tail.splitlines() if x.strip()] + if any(sign in both for sign in _VISION_ARCH_SIGNS): + evidence = next((x for x in reversed(lines) if any(sign in x.lower() for sign in _VISION_ARCH_SIGNS)), text) + return ("the vision encoder did not start: it has no code for this graphics card (a kernel-image error: the " + "picture reader in this engine package was built for other card generations than yours). Update the " + "engine, or read pictures on the CPU (Models, Read pictures, Image encoder device: CPU; or run setup " + "again with --vision cpu). From the encoder: " + evidence[:300]) + if text: + msg = "the vision encoder did not start: " + text + elif waited is not None: + msg = f"the vision encoder did not start: it said nothing for {waited:g} s (it never reported READY)" + else: + msg = "the vision encoder did not start: it ended without saying anything" + extra = [] + if code is not None: + words = engine_exit_words(code) + if words: + extra.append(words) + shown = [x for x in lines[-3:] if x != text] + if shown: + extra.append("last lines of its log: " + " | ".join(x[:200] for x in shown)) + if extra: + msg += " (" + "; ".join(extra) + ")" + return msg class Vision: @@ -2607,16 +2645,21 @@ class Vision: line = self._readline(VISION_READY_S, "the vision encoder did not start") except RuntimeError as e: # silent for VISION_READY_S: it was ended, and it is not left half-started self.proc, self.stopped = None, True + if VISION_READY_S: + raise RuntimeError(vision_start_error("", log, waited=VISION_READY_S)) from None raise RuntimeError(str(e)) from None if not line.startswith("READY"): proc, self.proc = self.proc, None self.stopped = True + code = proc.poll() if hasattr(proc, "poll") else None # an encoder that died by itself has its status already try: proc.kill() proc.wait(timeout=5) except (OSError, subprocess.TimeoutExpired): pass - raise RuntimeError(vision_start_error(line, self.spawn[1])) + if code is None: + code = getattr(proc, "returncode", None) + raise RuntimeError(vision_start_error(line, log, code)) self.stopped = False def _readline(self, timeout: float, what: str) -> str: @@ -2765,6 +2808,8 @@ class Vision: if key in self.cache: self.cache[key] = self.cache.pop(key) # most recently used last return self.cache[key] + if self.proc is None or self.proc.stdin is None: # an elastic encoder not started yet, or one that was unloaded + raise ValueError("the picture reader is not running") img, out = self.dir / f"{key}.img", self.dir / f"{key}.sve" img.write_bytes(data) try: @@ -4628,8 +4673,9 @@ class Service: if src.startswith(("http://", "https://")): src = fetched[item["source"]] = Vision.download(src) # outside the FIFO, as in prepare() with self._turn(cancel): # the encoder takes its turn with the requests (see below) + self._vision_up() # an elastic encoder is not running until a picture needs it self.vision.encode(src) - except (ValueError, OSError) as e: + except (ValueError, OSError, RuntimeError) as e: # RuntimeError: the encoder did not start why = str(e) if why is not None: print(f"[strata] a picture a tool returned was left out of the prompt: {why}", flush=True) diff --git a/serve/test_vision_start_error.py b/serve/test_vision_start_error.py new file mode 100644 index 00000000..7e5fc22e --- /dev/null +++ b/serve/test_vision_start_error.py @@ -0,0 +1,78 @@ +"""I3 (tools and images review, 2026-10-22): a picture reader that dies before it prints used to say only "the vision +encoder did not start: " (#3645, #3444, #3697, #3669). The message now carries the tail of its stderr, its exit status +and, for a kernel-image error, the card/architecture hint. + + python -m serve.test_vision_start_error +""" +import io +import tempfile +import unittest +from pathlib import Path +from types import SimpleNamespace +from unittest import mock + +import serve.server as server + + +def log_with(text): + f = tempfile.NamedTemporaryFile("w+", suffix=".log", delete=False) + f.write(text) + f.flush() + return f + + +class StartError(unittest.TestCase): + def test_an_empty_line_is_never_a_blank_sentence(self): + m = server.vision_start_error("") + self.assertNotEqual(m.strip(), "the vision encoder did not start:") + self.assertIn("without saying anything", m) + + def test_exit_code_and_stderr_tail_come_with_it(self): + f = log_with("loading mmproj\nggml_cuda_init: failed\nsome last words\n") + self.addCleanup(lambda: (f.close(), Path(f.name).unlink())) + m = server.vision_start_error("", f, 3) + self.assertIn("exit code 3", m) + self.assertIn("some last words", m) + self.assertIn("ggml_cuda_init: failed", m) + + def test_a_windows_crash_status_is_named(self): + m = server.vision_start_error("", None, 0xC0000135) + self.assertIn("0xC0000135", m) + + def test_kernel_image_error_names_the_card_and_the_way_out(self): + for sign in ("CUDA error: device kernel image is invalid", "IM2COL failed", "no kernel image is available for execution"): + with self.subTest(sign=sign): + f = log_with("warm-up\n" + sign + "\n") + try: + m = server.vision_start_error("", f, 1) + finally: + f.close() + Path(f.name).unlink() + self.assertIn("no code for this graphics card", m) + self.assertIn("CPU", m) + self.assertIn(sign.split(":")[-1].strip()[:12], m) + + def test_a_timeout_names_the_seconds_and_the_last_log_line(self): + f = log_with("loading weights\nstill loading the projector\n") + self.addCleanup(lambda: (f.close(), Path(f.name).unlink())) + m = server.vision_start_error("", f, waited=300) + self.assertIn("said nothing for 300 s", m) + self.assertIn("still loading the projector", m) + + def test_the_reader_that_ends_before_READY_raises_with_its_exit_code(self): + proc = SimpleNamespace(stdin=io.StringIO(), stdout=io.StringIO(""), kill=lambda: None, wait=lambda timeout=None: 0, + poll=lambda: 7, returncode=7) + v = server.Vision.__new__(server.Vision) + v.spawn = (["strata-vision"], None, None) + v.dir = Path(tempfile.gettempdir()) + with mock.patch.object(server, "popen", lambda *a, **k: proc): + with self.assertRaises(RuntimeError) as cm: + v._start() + self.assertIn("exit code 7", str(cm.exception)) + + def test_a_normal_message_is_unchanged(self): + self.assertEqual(server.vision_start_error("ERR no mmproj\n"), "the vision encoder did not start: ERR no mmproj") + + +if __name__ == "__main__": + unittest.main() diff --git a/serve/test_vision_tool_image.py b/serve/test_vision_tool_image.py new file mode 100644 index 00000000..eab440c3 --- /dev/null +++ b/serve/test_vision_tool_image.py @@ -0,0 +1,75 @@ +"""I6 (tools and images review, 2026-10-22): a picture a tool returned, with an elastic image encoder that is not running, +used to write to a process that did not exist (AttributeError, then a 400 "malformed request" that every later turn of the +conversation repeated). It starts the encoder like a picture of the user does; when it cannot start, the picture becomes +an "[image omitted: why]" note. + + python -m serve.test_vision_tool_image +""" +import tempfile +import unittest +from unittest import mock +from pathlib import Path + +from serve.frontend import ChatTemplate +from serve.server import ByteTokenizer, Service, Vision +from serve.test_vision_elastic import ElasticVision +from serve.test_vram import VramEngine + +ROOT = Path(__file__).resolve().parents[1] + + +def tool_turn(): + return [{"role": "user", "content": "look at it"}, + {"role": "tool", "tool_call_id": "t1", "content": [{"type": "image", "source": "x.png"}]}] + + +class FailingStart(ElasticVision): + def restart(self): + raise RuntimeError("the vision encoder did not start: it ended without saying anything (exit code 1)") + + +class ToolImage(unittest.TestCase): + def service(self, vision): + tok = ByteTokenizer() + engine = VramEngine(tok) + engine.loaded = True + return Service(engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=vision) + + def test_an_elastic_encoder_is_started_for_a_tool_picture(self): + vision = ElasticVision(tempfile.mkdtemp()) + svc = self.service(vision) + messages = tool_turn() + svc._note_unreadable_tool_images(messages) + self.assertEqual(vision.starts, 1) + self.assertEqual(messages[1]["content"][0]["type"], "image") # kept: it was read + svc._note_unreadable_tool_images(messages) # the running encoder is used as it is + self.assertEqual(vision.starts, 1) + + def test_an_encoder_that_cannot_start_leaves_a_note_not_a_400(self): + svc = self.service(FailingStart(tempfile.mkdtemp())) + messages = tool_turn() + svc._note_unreadable_tool_images(messages) # no exception + part = messages[1]["content"][0] + self.assertEqual(part["type"], "text") + self.assertIn("[image omitted:", part["text"]) + self.assertIn("did not start", part["text"]) + + def test_prepare_survives_a_tool_picture_with_an_elastic_encoder(self): + vision = ElasticVision(tempfile.mkdtemp()) + svc = self.service(vision) + self.addCleanup(lambda: getattr(svc.embeddings, "path", None) and svc.embeddings.path.unlink(missing_ok=True)) + svc.prepare(tool_turn(), None, {}) + self.assertEqual(vision.starts, 1) + + def test_encode_without_a_running_reader_is_a_clear_value_error(self): + v = Vision.__new__(Vision) + v.dir, v.cache, v.proc = Path(tempfile.mkdtemp()), {}, None + import threading + v.lock = threading.Lock() + with mock.patch.object(Vision, "normalize", return_value=b"png"), self.assertRaises(ValueError) as cm: + v.encode(b"bytes") + self.assertIn("picture reader is not running", str(cm.exception)) + + +if __name__ == "__main__": + unittest.main()