omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / tests / test_spiral_weave.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Independent verification suite for Spiral-2048 Pure-Omega Razor MoE (native/spiral.om). |
| 3 | |
| 4 | Tests: |
| 5 | 1. Direct analytical crystallization (`spcryst`) in both W4 (4-bit block-32) and exact f64, |
| 6 | including on a looped `OLLMCK03` core (`LP=1, LC=2, NP=2`). |
| 7 | 2. Sparse-FFN Helix Router (`spweval`) Top-K routing drops cross-entropy loss vs base. |
| 8 | 3. Autoregressive woven sampling (`spsamp` auto-dispatch + `spwsamp`) runs cleanly. |
| 9 | 4. Merkle integrity: 1-bit tamper in header OR payload is rejected with rc=61; |
| 10 | base checkpoint mismatch is rejected with rc=62. |
| 11 | 5. W4 block-32 compression ratio vs exact f64 payload. |
| 12 | """ |
| 13 | import os |
| 14 | import platform |
| 15 | import shutil |
| 16 | import struct |
| 17 | import subprocess |
| 18 | import tempfile |
| 19 | import unittest |
| 20 | |
| 21 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 22 | |
| 23 | |
| 24 | def resolve_bin() -> str: |
| 25 | env_bin = os.environ.get("OLLM_BIN") |
| 26 | if env_bin and os.path.isfile(env_bin): |
| 27 | return env_bin |
| 28 | sys_name = platform.system() |
| 29 | mach = platform.machine() |
| 30 | if sys_name == "Darwin": |
| 31 | name = "ollm-macos" |
| 32 | elif mach in ("x86_64", "amd64"): |
| 33 | name = "ollm-x86linux" |
| 34 | else: |
| 35 | name = "ollm" |
| 36 | for cand in ( |
| 37 | os.path.join(ROOT, "build", "verify-om", name), |
| 38 | os.path.join(ROOT, "build", name), |
| 39 | os.path.expanduser(f"~/.local/opt/omega-helix/bin/{name}"), |
| 40 | ): |
| 41 | if os.path.isfile(cand) and os.access(cand, os.X_OK): |
| 42 | return cand |
| 43 | raise RuntimeError(f"Could not locate {name}; run sh scripts/verify-om.sh first") |
| 44 | |
| 45 | |
| 46 | class TestSpiralWeave(unittest.TestCase): |
| 47 | @classmethod |
| 48 | def setUpClass(cls) -> None: |
| 49 | cls.bin = resolve_bin() |
| 50 | cls.tmp = tempfile.mkdtemp(prefix="omega-spiral-test-") |
| 51 | cls.corpus = os.path.join(cls.tmp, "corpus.txt") |
| 52 | with open(os.path.join(ROOT, "README.md"), "rb") as f1, open( |
| 53 | os.path.join(ROOT, "docs", "LOOP.md"), "rb" |
| 54 | ) as f2: |
| 55 | data = f1.read() + b"\n" + f2.read() |
| 56 | with open(cls.corpus, "wb") as out: |
| 57 | out.write(data) |
| 58 | # Base OLLMCK03 looped core: V=256 T=16 D=16 H=2 F=32 L=3 B=4 LP=1 LC=2 NP=2 |
| 59 | cls.base_ck = os.path.join(cls.tmp, "loop_base.ck") |
| 60 | subprocess.run( |
| 61 | [ |
| 62 | cls.bin, "init", cls.base_ck, |
| 63 | "256", "16", "16", "2", "32", "3", "4", |
| 64 | "1", "20", "4000", "1", "1", "2", "2", |
| 65 | ], |
| 66 | check=True, |
| 67 | stdout=subprocess.PIPE, |
| 68 | stderr=subprocess.PIPE, |
| 69 | ) |
| 70 | subprocess.run( |
| 71 | [cls.bin, "train", cls.base_ck, cls.corpus, "5", "5"], |
| 72 | check=True, |
| 73 | stdout=subprocess.PIPE, |
| 74 | stderr=subprocess.PIPE, |
| 75 | ) |
| 76 | |
| 77 | @classmethod |
| 78 | def tearDownClass(cls) -> None: |
| 79 | shutil.rmtree(cls.tmp, ignore_errors=True) |
| 80 | |
| 81 | def test_crystallize_w4_f64_weval_samp_and_tamper(self) -> None: |
| 82 | s1 = os.path.join(self.tmp, "strand1_w4.sp2") |
| 83 | s2 = os.path.join(self.tmp, "strand2_f64.sp2") |
| 84 | |
| 85 | r1 = subprocess.run( |
| 86 | [self.bin, "spcryst", self.base_ck, self.corpus, s1, "32", "100", "256", "1", "4"], |
| 87 | check=True, |
| 88 | text=True, |
| 89 | capture_output=True, |
| 90 | ) |
| 91 | parts1 = r1.stdout.strip().split() |
| 92 | self.assertEqual(parts1[0], "SPCRYST") |
| 93 | self.assertEqual(int(parts1[1]), 3) # L_phys |
| 94 | self.assertEqual(int(parts1[2]), 16) # D |
| 95 | self.assertEqual(int(parts1[3]), 32) # F_exp |
| 96 | self.assertEqual(int(parts1[4]), 1) # strand_id |
| 97 | self.assertEqual(int(parts1[5]), 4) # quant_bits W4 |
| 98 | self.assertLess(int(parts1[8]), int(parts1[7]), "W4 calibration loss must drop vs base") |
| 99 | |
| 100 | r2 = subprocess.run( |
| 101 | [self.bin, "spcryst", self.base_ck, self.corpus, s2, "32", "100", "256", "2", "64"], |
| 102 | check=True, |
| 103 | text=True, |
| 104 | capture_output=True, |
| 105 | ) |
| 106 | parts2 = r2.stdout.strip().split() |
| 107 | self.assertEqual(parts2[0], "SPCRYST") |
| 108 | self.assertEqual(int(parts2[5]), 64) # quant_bits f64 |
| 109 | self.assertLess(int(parts2[8]), int(parts2[7]), "f64 calibration loss must drop vs base") |
| 110 | |
| 111 | # Verify OLLMSP02 magic and W4 compression |
| 112 | with open(s1, "rb") as f: |
| 113 | hdr1 = f.read(80) |
| 114 | self.assertEqual(hdr1[:8], b"OLLMSP02") |
| 115 | w4_size = os.path.getsize(s1) |
| 116 | f64_size = os.path.getsize(s2) |
| 117 | self.assertLess(w4_size * 5, f64_size, "W4 pack should be >5x smaller than f64 pack") |
| 118 | |
| 119 | # Sparse-FFN Helix Router eval (Top-2 across both W4 and f64 strands) |
| 120 | re = subprocess.run( |
| 121 | [self.bin, "spweval", self.base_ck, self.corpus, "2", "4", s1, s2], |
| 122 | check=True, |
| 123 | text=True, |
| 124 | capture_output=True, |
| 125 | ) |
| 126 | eparts = re.stdout.strip().split() |
| 127 | self.assertEqual(eparts[0], "SPWEVAL") |
| 128 | self.assertEqual(int(eparts[1]), 2) |
| 129 | self.assertEqual(int(eparts[2]), 2) |
| 130 | base_nats = int(eparts[4]) |
| 131 | woven_nats = int(eparts[5]) |
| 132 | self.assertLess(woven_nats, base_nats, f"Woven loss {woven_nats} should beat base {base_nats}") |
| 133 | |
| 134 | # Autoregressive sampling via spsamp auto-dispatch |
| 135 | rs = subprocess.run( |
| 136 | [self.bin, "spsamp", self.base_ck, "42", "16", "2", "0", s1, s2], |
| 137 | check=True, |
| 138 | capture_output=True, |
| 139 | ) |
| 140 | self.assertGreaterEqual(len(rs.stdout), 16) |
| 141 | |
| 142 | # 1-bit payload tamper -> rc=61 |
| 143 | tamper_pay = os.path.join(self.tmp, "tamper_pay.sp2") |
| 144 | with open(s1, "rb") as f: |
| 145 | raw = bytearray(f.read()) |
| 146 | raw[80] ^= 0x01 |
| 147 | with open(tamper_pay, "wb") as f: |
| 148 | f.write(raw) |
| 149 | tp = subprocess.run( |
| 150 | [self.bin, "spweval", self.base_ck, self.corpus, "1", "1", tamper_pay], |
| 151 | capture_output=True, |
| 152 | ) |
| 153 | self.assertEqual(tp.returncode, 61) |
| 154 | |
| 155 | # 1-bit header tamper -> rc=61 |
| 156 | tamper_hdr = os.path.join(self.tmp, "tamper_hdr.sp2") |
| 157 | with open(s1, "rb") as f: |
| 158 | raw_h = bytearray(f.read()) |
| 159 | raw_h[40] ^= 0x01 |
| 160 | with open(tamper_hdr, "wb") as f: |
| 161 | f.write(raw_h) |
| 162 | th = subprocess.run( |
| 163 | [self.bin, "spweval", self.base_ck, self.corpus, "1", "1", tamper_hdr], |
| 164 | capture_output=True, |
| 165 | ) |
| 166 | self.assertEqual(th.returncode, 61) |
| 167 | |
| 168 | # Base checkpoint mismatch -> rc=62 |
| 169 | other_ck = os.path.join(self.tmp, "other.ck") |
| 170 | subprocess.run( |
| 171 | [self.bin, "init", other_ck, "256", "16", "16", "2", "32", "3", "4", "99", "20", "4000"], |
| 172 | check=True, |
| 173 | stdout=subprocess.PIPE, |
| 174 | stderr=subprocess.PIPE, |
| 175 | ) |
| 176 | tm = subprocess.run( |
| 177 | [self.bin, "spweval", other_ck, self.corpus, "1", "1", s1], |
| 178 | capture_output=True, |
| 179 | ) |
| 180 | self.assertEqual(tm.returncode, 62) |
| 181 | |
| 182 | |
| 183 | if __name__ == "__main__": |
| 184 | unittest.main() |