Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(coli): coli run on OLMoE sent the prompt in the locale's code page

kevin9327ec51ce971a10
diff --git a/c/coli b/c/coli
index 32030804..998e15a5 100755
--- a/c/coli
+++ b/c/coli
@@ -1332,9 +1332,13 @@ def cmd_run(a):
# emits the answer and exits cleanly. No reason to spin up the gateway
# for a single non-interactive prompt.
e=env_for_engine(a,arch)
+ # The engine tokenizes the bytes it reads as UTF-8. A text-mode pipe
+ # would encode the prompt in the locale's code page instead: a
+ # UnicodeEncodeError on cp949/cp932 or the C locale, garbled bytes on
+ # cp1252. Hand it the bytes, like the V4 path's UTF-8 prompt file.
result=subprocess.run(
[engine, str(cap_for_launch(a.cap,e,16)), "8"],
- input=prompt+"\n", text=True, env=e, check=False)
+ input=(prompt+"\n").encode("utf-8"), env=e, check=False)
sys.exit(result.returncode)
# template ufficiale GLM-5.2: niente \n dopo i ruoli; <think></think> = risposta diretta (nothink).
# THINK=1 lascia <think> aperto, stessa convenzione del serve mode (glm.c). EN: THINK=1 leaves
diff --git a/c/tests/test_coli_run_olmoe_prompt.py b/c/tests/test_coli_run_olmoe_prompt.py
new file mode 100644
index 00000000..860dd78c
--- /dev/null
+++ b/c/tests/test_coli_run_olmoe_prompt.py
@@ -0,0 +1,101 @@
+"""`coli run` on OLMoE must hand the engine the prompt as UTF-8 whatever the locale.
+
+cmd_run feeds the one-shot prompt to olmoe's stdin chat loop, which reads raw
+bytes and tokenizes them as UTF-8. The pipe was opened in text mode, so Python
+encoded the prompt in the locale's code page instead:
+
+ * where the code page cannot encode a character of the prompt (an accented
+ letter or an emoji on cp949/cp932, anything non-ASCII in the C locale),
+ `coli run` died with a UnicodeEncodeError traceback before the engine saw
+ a byte;
+ * where it can (é on cp1252, Hangul on cp949), the engine received the code
+ page's bytes, which are not UTF-8, and answered a garbled prompt.
+
+The test runs the real cmd_run in a child Python whose locale is not UTF-8
+(PYTHONUTF8=0 and the C locale, or the Windows ANSI code page) against a fake
+engine that records the bytes it reads on stdin.
+"""
+import json
+import os
+import subprocess
+import sys
+import tempfile
+import textwrap
+import unittest
+from pathlib import Path
+
+CLI = Path(__file__).resolve().parent.parent / "coli"
+PROMPT = "café — 世界 \U0001F600"
+
+CHILD_ENV = {**os.environ, "PYTHONUTF8": "0", "PYTHONCOERCECLOCALE": "0",
+ "LC_ALL": "C", "LANG": "C"}
+CHILD_ENV.pop("PYTHONIOENCODING", None)
+
+ENGINE = textwrap.dedent("""
+ import os, sys
+ data = sys.stdin.buffer.read()
+ with open(os.environ["RECORD"], "wb") as record:
+ record.write(data)
+""")
+
+DRIVER = textwrap.dedent("""
+ import argparse, importlib.util, json, locale, subprocess, sys
+ from importlib.machinery import SourceFileLoader
+ from pathlib import Path
+ from unittest import mock
+
+ cli, engine, model, prompt = sys.argv[1], sys.argv[2], sys.argv[3], json.loads(sys.argv[4])
+ loader = SourceFileLoader("coli_run_olmoe_prompt", cli)
+ spec = importlib.util.spec_from_loader(loader.name, loader)
+ coli = importlib.util.module_from_spec(spec)
+ loader.exec_module(coli)
+
+ real_run = subprocess.run
+ def run(command, **kwargs):
+ return real_run([sys.executable, engine, *command[1:]], **kwargs)
+
+ args = argparse.Namespace(model=model, prompt=[prompt], ngen=8, ram=0,
+ temp=0.0, ctx=0, cap=None)
+ code = None
+ with mock.patch.object(coli, "need_model"), \\
+ mock.patch.object(coli, "banner"), \\
+ mock.patch.object(coli, "engine_for", return_value="olmoe"), \\
+ mock.patch.object(coli.subprocess, "run", run):
+ try:
+ coli.cmd_run(args)
+ except SystemExit as exit_:
+ code = exit_.code
+ print(json.dumps({"exit": code, "locale": locale.getpreferredencoding(False)}))
+""")
+
+
+class ColiRunOlmoePromptTest(unittest.TestCase):
+ def test_the_engine_reads_the_prompt_as_utf8(self):
+ with tempfile.TemporaryDirectory() as scratch:
+ model = Path(scratch, "model")
+ model.mkdir()
+ (model / "config.json").write_text(json.dumps({"model_type": "olmoe"}),
+ encoding="utf-8")
+ (model / "tokenizer.json").write_text("{}", encoding="utf-8")
+ engine = Path(scratch, "engine.py")
+ engine.write_text(ENGINE, encoding="utf-8")
+ driver = Path(scratch, "driver.py")
+ driver.write_text(DRIVER, encoding="utf-8")
+ record = Path(scratch, "stdin.bin")
+ # The prompt reaches the child as ASCII-escaped JSON, so the child's
+ # own argv decoding (also locale-bound) is not what is under test.
+ result = subprocess.run(
+ [sys.executable, str(driver), str(CLI), str(engine), str(model),
+ json.dumps(PROMPT)],
+ env={**CHILD_ENV, "RECORD": str(record)},
+ capture_output=True, timeout=60)
+ if result.returncode:
+ raise AssertionError(result.stderr.decode("utf-8", "replace")[-3000:])
+ report = json.loads(result.stdout.decode("ascii", "replace").strip().splitlines()[-1])
+ self.assertEqual(report["exit"], 0, report)
+ self.assertEqual(record.read_bytes(), (PROMPT + "\n").encode("utf-8"),
+ f"child locale {report['locale']}")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/c/tests/test_v4_cli.py b/c/tests/test_v4_cli.py
index e44ea3f6..402fe50f 100644
--- a/c/tests/test_v4_cli.py
+++ b/c/tests/test_v4_cli.py
@@ -102,8 +102,8 @@ class V4CliTest(unittest.TestCase):
self.cli.cmd_run(args)
self.assertEqual(stopped.exception.code, 0)
self.assertEqual(captured["command"], ["/engines/olmoe", "16", "8"])
- self.assertEqual(captured["input"], "hello world\n")
- self.assertTrue(captured["text"])
+ self.assertEqual(captured["input"], b"hello world\n")
+ self.assertNotIn("text", captured)
self.assertEqual(captured["env"]["CHAT"], "1")
self.assertEqual(captured["env"]["MAX_NEW"], "32")
finally: