Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(datapoint): fresh-process mode talked to legacy engines in the locale's code page
kevin9327f15360f4f4c1
diff --git a/c/tests/test_datapoint_fresh_locale.py b/c/tests/test_datapoint_fresh_locale.pynew file mode 100644index 00000000..4aa6bbc2--- /dev/null+++ b/c/tests/test_datapoint_fresh_locale.py@@ -0,0 +1,93 @@+"""`tools/datapoint.py --mode fresh-process` must speak UTF-8 to a legacy engine whatever the locale.++run_fresh_engine drives olmoe, inkling and colibri through their stdin chat+loop and captured the pipes in text mode, i.e. in the locale's code page. The+engines read and write UTF-8, so on a code page that is not UTF-8 (cp949,+cp932, or the C locale with UTF-8 mode off):++ * a prompt with a character the code page cannot encode died with+ UnicodeEncodeError before the engine started measuring, and one it can+ encode (é on cp1252) reached the engine as non-UTF-8 bytes;+ * an engine line the code page cannot decode (colibri's "[prefill] layer+ 1/78 · 12 token" progress line) made the reader thread raise, proc.stdout+ came back None, and the datapoint died with a TypeError.++The test runs run_fresh_engine in a child Python whose locale is not UTF-8+against a fake legacy engine that records the bytes it reads.+"""+import json+import os+import subprocess+import sys+import tempfile+import textwrap+import unittest+from pathlib import Path++TOOLS = Path(__file__).resolve().parent.parent / "tools"+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)+ sys.stderr.buffer.write("[prefill] layer 1/4 \\u00b7 12 token \\u00b7 +0.00s\\n".encode("utf-8"))+ sys.stderr.buffer.write(b"resident weights loaded in 1.5s | RSS after load: 19.0 GB\\n")+ sys.stdout.buffer.write("answer \\u2014 done\\n".encode("utf-8"))+""")++DRIVER = textwrap.dedent("""+ import json, locale, subprocess, sys+ from unittest import mock+ tools, engine, prompt = sys.argv[1], sys.argv[2], json.loads(sys.argv[3])+ sys.path.insert(0, tools)+ import datapoint++ real_run = subprocess.run+ def run(command, **kwargs):+ return real_run([sys.executable, engine, *command[1:]], **kwargs)++ error = rows = None+ with mock.patch.object(datapoint.subprocess, "run", run):+ try:+ rows = datapoint.run_fresh_engine("/engines/olmoe", "/model", prompt,+ max_new=7, runs=1, cap=3, bits=8)+ except BaseException as exc:+ error = f"{type(exc).__name__}: {exc}"+ print(json.dumps({"locale": locale.getpreferredencoding(False), "error": error,+ "rows": rows}, ensure_ascii=True))+""")+++class DatapointFreshLocaleTest(unittest.TestCase):+ def test_legacy_engine_gets_utf8_prompt_and_its_output_is_read(self):+ with tempfile.TemporaryDirectory() as scratch:+ 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")+ result = subprocess.run(+ [sys.executable, str(driver), str(TOOLS), str(engine), json.dumps(PROMPT)],+ env={**CHILD_ENV, "RECORD": str(record)},+ capture_output=True, timeout=120)+ if result.returncode:+ raise AssertionError(result.stderr.decode("utf-8", "replace")[-3000:])+ report = json.loads(result.stdout.decode("ascii").strip().splitlines()[-1])+ where = f"child locale {report['locale']}"+ self.assertIsNone(report["error"], where)+ # A text-mode pipe writes the platform's line ending ("\r\n" on+ # Windows, as before this fix); the engine strips it. The prompt+ # bytes themselves must be UTF-8.+ self.assertEqual(record.read_bytes().rstrip(b"\r\n"), PROMPT.encode("utf-8"), where)+ self.assertEqual(report["rows"][0]["rss"], 19.0, where)+ self.assertEqual(report["rows"][0]["load_s"], 1.5, where)+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tools/datapoint.py b/c/tools/datapoint.pyindex 04e79aff..32ed3617 100644--- a/c/tools/datapoint.py+++ b/c/tools/datapoint.py@@ -458,8 +458,11 @@ def run_fresh_engine(engine, snap, prompt, max_new, runs, cap, bits, memory_gb=Nstdin_input = prompt + "\n"try:+ # The engines read and write UTF-8. Text mode in the locale's code+ # page could not encode a non-ASCII prompt on cp949/cp932 or the C+ # locale, and died decoding lines like "[prefill] layer 1/78 · ...".proc = subprocess.run(cmd, input=stdin_input, capture_output=True,- text=True, env=env)+ encoding="utf-8", errors="replace", env=env)finally:if prompt_path:try: