Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-helix/blob/main/tests/test_fast_ckpt.py # omega-helix This project has not described itself yet. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-helix / tests / test_fast_ckpt.py` 508 lines ยท 22701 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Verification tests for OLLMCK05 fast binary checkpoints and mmap cold-start.""" 3 4 from __future__ import annotations 5 6 import os 7 import platform 8 import shutil 9 import statistics 10 import struct 11 import subprocess 12 import tempfile 13 import time 14 import unittest 15 from pathlib import Path 16 17 ROOT = Path(__file__).resolve().parent.parent 18 19 OCK5_MAGIC = b"OLLMCK05" 20 OCK5_MAGIC_U64 = int.from_bytes(OCK5_MAGIC, "little") 21 OCK5_HDR_BYTES = 256 22 23 FLAG_F32_MASTER = 1 << 0 24 FLAG_BF16_SHADOW = 1 << 1 25 FLAG_NVFP4_WCACHE = 1 << 2 26 FLAG_ZERO_MV = 1 << 3 27 FLAG_LOOP_CK03 = 1 << 4 28 29 FNV64_BASIS = `14695981039346656037` 30 FNV64_PRIME = `1099511628211` 31 32 33 def _cmd_u64(name: str) -> int: 34 raw = name.encode("ascii") 35 assert len(raw) <= 8 36 return int.from_bytes(raw.ljust(8, b"\x00"), "little") 37 38 39 def _fnv1a_u64_words(data: bytes) -> int: 40 assert len(data) % 8 == 0 41 h = FNV64_BASIS 42 for (w,) in struct.iter_unpack(" Path | None: 48 sys_name = platform.system() 49 mach = platform.machine() 50 if sys_name == "Darwin": 51 names = ["ollm-macos"] 52 elif mach in ("x86_64", "amd64"): 53 names = ["ollm-x86linux"] 54 else: 55 names = ["ollm-gb10", "ollm"] 56 57 cands: list[Path] = [] 58 if os.environ.get("OLLM_BIN"): 59 cands.append(Path(os.environ["OLLM_BIN"])) 60 if out_dir is not None: 61 for n in names: 62 cands.append(out_dir / n) 63 if os.environ.get("OUT"): 64 for n in names: 65 cands.append(Path(os.environ["OUT"]) / n) 66 for n in names: 67 cands.append(ROOT / "build" / n) 68 cands.append(ROOT / "build" / "verify-om" / n) 69 for c in cands: 70 if c.is_file() and os.access(c, os.X_OK): 71 return c 72 return None 73 74 75 def _resolve_gb10_bin(out_dir: Path | None = None) -> Path | None: 76 cands: list[Path] = [] 77 if os.environ.get("OLLM_GB10_BIN"): 78 cands.append(Path(os.environ["OLLM_GB10_BIN"])) 79 if out_dir is not None: 80 cands.append(out_dir / "ollm-gb10") 81 if os.environ.get("OUT"): 82 cands.append(Path(os.environ["OUT"]) / "ollm-gb10") 83 cands.extend( 84 ( 85 ROOT / "build" / "ollm-gb10", 86 ROOT / "build" / "verify-om" / "ollm-gb10", 87 ) 88 ) 89 for c in cands: 90 if c.is_file() and os.access(c, os.X_OK): 91 return c 92 return None 93 94 95 def _parse_ck5_header(raw: bytes) -> dict[str, int]: 96 assert len(raw) >= OCK5_HDR_BYTES 97 vals = struct.unpack_from("<32Q", raw, 0) 98 return { 99 "magic": vals[0], 100 "version": vals[1], 101 "flags": vals[2], 102 "V": vals[3], 103 "T": vals[4], 104 "D": vals[5], 105 "H": vals[6], 106 "F": vals[7], 107 "L": vals[8], 108 "B": vals[9], 109 "seed": vals[10], 110 "step": vals[11], 111 "TOTAL": vals[12], 112 "lr_bits": vals[13], 113 "P": vals[14], 114 "G": vals[15], 115 "LP": vals[16], 116 "LC": vals[17], 117 "NP": vals[18], 118 "w_off": vals[19], 119 "w_len": vals[20], 120 "mv_off": vals[21], 121 "mv_len": vals[22], 122 "wbf_off": vals[23], 123 "wbf_len": vals[24], 124 "wnv_off": vals[25], 125 "wnv_len": vals[26], 126 "orig_type": vals[27], 127 "total_bytes": vals[28], 128 "reserved": vals[29], 129 "payload_fnv": vals[30], 130 "hdr_fnv": vals[31], 131 } 132 133 134 def _parse_t_lines(stdout: str) -> list[tuple[int, int]]: 135 out: list[tuple[int, int]] = [] 136 for line in stdout.splitlines(): 137 parts = line.split() 138 if len(parts) == 3 and parts[0] == "T": 139 out.append((int(parts[1]), int(parts[2]))) 140 return out 141 142 143 class TestFastCkpt(unittest.TestCase): 144 def setUp(self) -> None: 145 self.tmp = Path(tempfile.mkdtemp(prefix="omega-fast-ckpt-")) 146 self.corp = self.tmp / "corp.txt" 147 raw = b"\n".join( 148 (ROOT / p).read_bytes() 149 for p in ("README.md", "docs/LOOP.md") 150 if (ROOT / p).is_file() 151 ) 152 while len(raw) < 65536: 153 raw += raw 154 self.corp.write_bytes(raw[:65536]) 155 156 def tearDown(self) -> None: 157 shutil.rmtree(self.tmp, ignore_errors=True) 158 159 def _ensure_host_bin(self) -> Path: 160 b = _resolve_host_bin() 161 if b is not None: 162 return b 163 out_dir = self.tmp / "host_build" 164 sys_name = platform.system() 165 mach = platform.machine() 166 if sys_name == "Darwin": 167 tgt = "macos" 168 elif mach in ("x86_64", "amd64"): 169 tgt = "x86linux" 170 else: 171 tgt = "gb10" 172 subprocess.run( 173 ["sh", str(ROOT / "scripts" / "build.sh")], 174 cwd=str(ROOT), 175 env={**os.environ, "TARGET": tgt, "OUT": str(out_dir)}, 176 check=True, 177 capture_output=True, 178 text=True, 179 ) 180 b = _resolve_host_bin(out_dir) 181 assert b is not None 182 return b 183 184 def test_ck05_constants_and_sources(self) -> None: 185 ckpt_src = (ROOT / "native" / "ckpt.om").read_text(encoding="utf-8") 186 cli_src = (ROOT / "native" / "cli.om").read_text(encoding="utf-8") 187 gg_src = (ROOT / "native" / "gpu" / "gg.om").read_text(encoding="utf-8") 188 lio_src = (ROOT / "native" / "lio.om").read_text(encoding="utf-8") 189 190 self.assertIn(f"const OCK5MAG ulong {OCK5_MAGIC_U64}", ckpt_src) 191 for cmd, fn_name in ( 192 ("ckfast", "cmdckfast"), 193 ("ckf64", "cmdckf64"), 194 ("initfast", "cmdinitfast"), 195 ): 196 self.assertIn(f"if eq w {_cmd_u64(cmd)}\n{fn_name}\n", cli_src) 197 self.assertIn("fn ggload_ck5 ", gg_src) 198 self.assertIn("fn ggsave_ck5 ", gg_src) 199 self.assertIn("mapnew fd 0 ulong sz 1 2", lio_src) 200 201 def test_cross_compile_targets(self) -> None: 202 targets = ["gb10", "x86linux"] 203 if platform.system() == "Darwin": 204 targets.append("macos") 205 for tgt in targets: 206 with self.subTest(target=tgt): 207 out_dir = self.tmp / f"build_{tgt}" 208 subprocess.run( 209 ["sh", str(ROOT / "scripts" / "build.sh")], 210 cwd=str(ROOT), 211 env={**os.environ, "TARGET": tgt, "OUT": str(out_dir)}, 212 check=True, 213 capture_output=True, 214 text=True, 215 ) 216 if tgt == "gb10": 217 hdr = (out_dir / "ollm-gb10").read_bytes()[:20] 218 self.assertEqual(hdr[:4], b"\x7fELF") 219 self.assertEqual(int.from_bytes(hdr[18:20], "little"), 183) 220 elif tgt == "x86linux": 221 hdr = (out_dir / "ollm-x86linux").read_bytes()[:20] 222 self.assertEqual(hdr[:4], b"\x7fELF") 223 self.assertEqual(int.from_bytes(hdr[18:20], "little"), 62) 224 elif tgt == "macos": 225 hdr = (out_dir / "ollm-macos").read_bytes()[:4] 226 self.assertEqual(hdr, b"\xcf\xfa\xed\xfe") 227 228 def test_ck05_roundtrip_all_modes_and_ck_types(self) -> None: 229 binp = self._ensure_host_bin() 230 231 # 1. init (OLLMCK02, ZERO_MV) -> ckfast (f32/bf16/nvfp4) -> ckf64 + initfast -> ckf64 232 ck_init = self.tmp / "init.ck" 233 subprocess.run( 234 [str(binp), "init", str(ck_init), "256", "32", "64", "4", "128", "2", "4", "1", "10", "3000", "2"], 235 check=True, 236 capture_output=True, 237 ) 238 ck5_init = self.tmp / "initfast.ck5" 239 subprocess.run( 240 [str(binp), "initfast", str(ck5_init), "256", "32", "64", "4", "128", "2", "4", "1", "10", "3000", "2"], 241 check=True, 242 capture_output=True, 243 ) 244 245 # Read original f64 weights from init.ck (120-byte OLLMCK02 header) 246 init_raw = ck_init.read_bytes() 247 self.assertEqual(init_raw[:8], b"OLLMCK02") 248 p_count = struct.unpack_from(" None: 342 binp = self._ensure_host_bin() 343 ck = self.tmp / "base.ck" 344 ck5 = self.tmp / "base.ck5" 345 subprocess.run( 346 [str(binp), "init", str(ck), "256", "32", "64", "4", "128", "2", "4", "1", "10", "3000", "2"], 347 check=True, 348 capture_output=True, 349 ) 350 subprocess.run([str(binp), "ckfast", str(ck), str(ck5), "bf16"], check=True, capture_output=True) 351 raw = bytearray(ck5.read_bytes()) 352 out_p = self.tmp / "unused.ck" 353 354 # 1. Bad magic -> rc=11 355 bad_mag = bytearray(raw) 356 bad_mag[0] ^= 1 357 p_bad = self.tmp / "bad_magic.ck5" 358 p_bad.write_bytes(bad_mag) 359 r = subprocess.run([str(binp), "ckf64", str(p_bad), str(out_p)], capture_output=True) 360 self.assertEqual(r.returncode, 11) 361 362 # 2. Truncated (< 256 and < total_bytes) -> rc=10 363 for cut in (0, 128, len(raw) - 8, len(raw) - 1): 364 p_tr = self.tmp / f"trunc_{cut}.ck5" 365 p_tr.write_bytes(raw[:cut]) 366 r = subprocess.run([str(binp), "ckf64", str(p_tr), str(out_p)], capture_output=True) 367 self.assertEqual(r.returncode, 10, f"cut={cut} rc={r.returncode}") 368 369 # 3. Trailing bytes -> rc=15 370 p_over = self.tmp / "over.ck5" 371 p_over.write_bytes(bytes(raw) + b"\x00" * 8) 372 r = subprocess.run([str(binp), "ckf64", str(p_over), str(out_p)], capture_output=True) 373 self.assertEqual(r.returncode, 15) 374 375 # 4. 1-bit tamper in header and payload -> rc=14 376 for off in (8, 16, 24, 152, 224, 240, 248, 256, 512, len(raw) - 1): 377 t = bytearray(raw) 378 t[off] ^= 1 379 p_t = self.tmp / f"tamper_{off}.ck5" 380 p_t.write_bytes(t) 381 r = subprocess.run([str(binp), "ckf64", str(p_t), str(out_p)], capture_output=True) 382 self.assertEqual(r.returncode, 14, f"tamper offset={off} rc={r.returncode}") 383 384 # 5. Legacy OLLMCK02 accelerated mmap loader 1-bit payload tamper -> rc=14 385 leg = bytearray(ck.read_bytes()) 386 leg[-1] ^= 1 387 p_leg = self.tmp / "leg_tamper.ck" 388 p_leg.write_bytes(leg) 389 r = subprocess.run([str(binp), "eval", str(p_leg), str(self.corp), "2"], capture_output=True) 390 self.assertEqual(r.returncode, 14) 391 392 def test_live_gb10_ck05_parity_and_cold_start(self) -> None: 393 if os.environ.get("OLLM_GB10_LIVE") != "1": 394 self.skipTest("set OLLM_GB10_LIVE=1 inside an exclusive spark-window to run live GB10 checks") 395 gb10 = _resolve_gb10_bin() 396 self.assertIsNotNone(gb10, "ollm-gb10 binary not found") 397 assert gb10 is not None 398 399 # Deterministic u16 token stream for V=4096 (15.83M H2H shape) 400 tok_path = self.tmp / "tokens_4096.tok" 401 n_tok = 131072 402 tok_bytes = b"".join(struct.pack("