omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / tests / test_gpu_spiral_gb10.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Verification tests for Spiral-2048 GB10 SM121 GPU scaling, parity, and gfcache. |
| 3 | |
| 4 | Always safe on macOS / Linux x86_64 / Spark without touching /dev/nvidia0: |
| 5 | - Test 1: Pure-Omega CPU dynamic-pool scaling for D=2048 (direct-oww) and |
| 6 | looped Spiral L_phys=8 x NP=4 (LV=32, dynamic opab overflow). |
| 7 | - Test 2: SM121 nvdisasm disassembly gate (skipped if nvdisasm unavailable) |
| 8 | checking all 78 kernel slots at default, D=1536, and D=2048 T=512 F=8192 |
| 9 | with bad=0, RMSF/RMSB/CE_BF16 <= 72 GPRs, vectorized LDG/STG.E.64 epilogues, |
| 10 | and OMMA.SF.16864 NVFP4 Tensor Core codegen (slot 84). |
| 11 | - Test 3 (opt-in via OLLM_GB10_LIVE=1 inside spark-window): live physical |
| 12 | GB10 gbfrag, gacmp, gbcmp2 (L_phys=8 x NP=4 and D=2048), live nvfp4 |
| 13 | OMMA.SF.16864, and gfcache OLLMFH01 parity against CPU fcache. |
| 14 | """ |
| 15 | |
| 16 | from __future__ import annotations |
| 17 | |
| 18 | import os |
| 19 | import platform |
| 20 | import shutil |
| 21 | import struct |
| 22 | import subprocess |
| 23 | import tempfile |
| 24 | import unittest |
| 25 | from pathlib import Path |
| 26 | |
| 27 | ROOT = Path(__file__).resolve().parent.parent |
| 28 | |
| 29 | |
| 30 | def _resolve_cpu_bin() -> Path: |
| 31 | env_bin = os.environ.get("OLLM_BIN") |
| 32 | if env_bin: |
| 33 | p = Path(env_bin) |
| 34 | if p.is_file() and os.access(p, os.X_OK): |
| 35 | return p |
| 36 | sys_name = platform.system() |
| 37 | mach = platform.machine() |
| 38 | if sys_name == "Darwin": |
| 39 | name = "ollm-macos" |
| 40 | target = "macos" |
| 41 | elif mach in ("x86_64", "amd64"): |
| 42 | name = "ollm-x86linux" |
| 43 | target = "x86linux" |
| 44 | else: |
| 45 | name = "ollm" |
| 46 | target = "aarch64" |
| 47 | cands = [ |
| 48 | ROOT / "build" / "verify-om" / name, |
| 49 | ROOT / "build" / name, |
| 50 | ] |
| 51 | out_env = os.environ.get("OUT") |
| 52 | if out_env: |
| 53 | cands.insert(0, Path(out_env) / name) |
| 54 | for c in cands: |
| 55 | if c.is_file() and os.access(c, os.X_OK): |
| 56 | return c |
| 57 | out_dir = Path(out_env) if out_env else ROOT / "build" |
| 58 | subprocess.run( |
| 59 | ["sh", str(ROOT / "scripts" / "build.sh")], |
| 60 | cwd=str(ROOT), |
| 61 | env={**os.environ, "TARGET": target, "OUT": str(out_dir)}, |
| 62 | check=True, |
| 63 | ) |
| 64 | return out_dir / name |
| 65 | |
| 66 | |
| 67 | def _resolve_gb10_bin() -> Path | None: |
| 68 | cands: list[Path] = [] |
| 69 | env_gb10 = os.environ.get("OLLM_GB10_BIN") |
| 70 | if env_gb10: |
| 71 | cands.append(Path(env_gb10)) |
| 72 | out_env = os.environ.get("OUT") |
| 73 | if out_env: |
| 74 | cands.append(Path(out_env) / "ollm-gb10") |
| 75 | env_bin = os.environ.get("OLLM_BIN") |
| 76 | if env_bin: |
| 77 | cands.append(Path(env_bin).parent / "ollm-gb10") |
| 78 | cands.extend( |
| 79 | ( |
| 80 | ROOT / "build" / "ollm-gb10", |
| 81 | ROOT / "build" / "verify-om" / "ollm-gb10", |
| 82 | Path.home() / ".local" / "opt" / "omega-helix" / "bin" / "ollm-gb10", |
| 83 | ) |
| 84 | ) |
| 85 | for c in cands: |
| 86 | if c.is_file() and os.access(c, os.X_OK): |
| 87 | return c |
| 88 | return None |
| 89 | |
| 90 | |
| 91 | def _find_nvdisasm() -> str | None: |
| 92 | p = shutil.which("nvdisasm") |
| 93 | if p: |
| 94 | return p |
| 95 | cand = Path("/usr/local/cuda/bin/nvdisasm") |
| 96 | return str(cand) if cand.is_file() and os.access(cand, os.X_OK) else None |
| 97 | |
| 98 | |
| 99 | class TestGpuSpiralGB10(unittest.TestCase): |
| 100 | def setUp(self) -> None: |
| 101 | self.tmp = Path(tempfile.mkdtemp(prefix="omega-gpu-spiral-")) |
| 102 | self.corp = self.tmp / "corp.bin" |
| 103 | parts = [] |
| 104 | for rel in ("README.md", "docs/LOOP.md"): |
| 105 | p = ROOT / rel |
| 106 | if p.is_file(): |
| 107 | parts.append(p.read_bytes()) |
| 108 | raw = b"\n".join(parts) |
| 109 | while len(raw) < 32768: |
| 110 | raw += raw |
| 111 | self.corp.write_bytes(raw[:32768]) |
| 112 | |
| 113 | def tearDown(self) -> None: |
| 114 | shutil.rmtree(self.tmp, ignore_errors=True) |
| 115 | |
| 116 | def test_cpu_dynamic_pool_d2048_and_loop8x4(self) -> None: |
| 117 | if platform.machine() in ("x86_64", "amd64"): |
| 118 | self.skipTest("x86linux uses static CONTRACT-V3 pools") |
| 119 | ollm = _resolve_cpu_bin() |
| 120 | |
| 121 | # 1. Looped Spiral L_phys=8 x NP=4 (LV=32) exercises dynamic opab overflow |
| 122 | loop_ck = self.tmp / "loop8x4.ck" |
| 123 | r = subprocess.run( |
| 124 | [str(ollm), "init", str(loop_ck), "256", "32", "64", "4", "128", "8", "2", "1", "10", "3000", "1", "0", "8", "4"], |
| 125 | capture_output=True, |
| 126 | text=True, |
| 127 | check=True, |
| 128 | ) |
| 129 | self.assertTrue(r.stdout.strip().startswith("I "), r.stdout) |
| 130 | ev = subprocess.run( |
| 131 | [str(ollm), "eval", str(loop_ck), str(self.corp), "2"], |
| 132 | capture_output=True, |
| 133 | text=True, |
| 134 | check=True, |
| 135 | ) |
| 136 | self.assertTrue(ev.stdout.strip().startswith("E 0 2 "), ev.stdout) |
| 137 | |
| 138 | # 2. D=2048 exercises direct-oww mode (LSTR > OWC - LAY0 - D) |
| 139 | d2048_ck = self.tmp / "d2048.ck" |
| 140 | r2 = subprocess.run( |
| 141 | [str(ollm), "init", str(d2048_ck), "256", "32", "2048", "32", "4096", "1", "1", "1", "10", "3000", "1"], |
| 142 | capture_output=True, |
| 143 | text=True, |
| 144 | check=True, |
| 145 | ) |
| 146 | self.assertTrue(r2.stdout.strip().startswith("I "), r2.stdout) |
| 147 | ev2 = subprocess.run( |
| 148 | [str(ollm), "eval", str(d2048_ck), str(self.corp), "1"], |
| 149 | capture_output=True, |
| 150 | text=True, |
| 151 | check=True, |
| 152 | ) |
| 153 | self.assertTrue(ev2.stdout.strip().startswith("E 0 1 "), ev2.stdout) |
| 154 | |
| 155 | def test_sm121_disasm_d2048_and_swizzle(self) -> None: |
| 156 | nvdisasm = _find_nvdisasm() |
| 157 | if not nvdisasm or platform.system() != "Linux" or platform.machine() != "aarch64": |
| 158 | self.skipTest("requires Spark Linux aarch64 + nvdisasm -b SM121") |
| 159 | |
| 160 | for shape in ("256 32 64 2 128 2", "4096 256 576 9 1152 4", "4096 256 1536 24 4096 16", "4096 512 2048 32 8192 8"): |
| 161 | out_dir = self.tmp / f"disasm_{shape.replace(' ', '_')}" |
| 162 | r = subprocess.run( |
| 163 | ["sh", str(ROOT / "tests" / "gpu-disasm.sh"), "--all"], |
| 164 | cwd=str(ROOT), |
| 165 | env={**os.environ, "SHAPE": shape, "OUT": str(out_dir), "NVDISASM": nvdisasm}, |
| 166 | capture_output=True, |
| 167 | text=True, |
| 168 | check=True, |
| 169 | ) |
| 170 | self.assertIn("GPU_DISASM_OK slots=86 bad=0", r.stdout) |
| 171 | |
| 172 | # Verify D=2048 RMSF/RMSB/RMSF_BF16/RMSB_BF16/CE_BF16 stay inside 72 GPRs, |
| 173 | # slot 31 uses LDG/STG.E.64, slot 84 emits OMMA.SF.16864 + IMAD.WIDE.U32 R14, R123, 0x20, R10, |
| 174 | # and slot 85 predicates pointer advance on @P1. |
| 175 | out_dir = self.tmp / "disasm_detail" |
| 176 | r_rms = subprocess.run( |
| 177 | ["sh", str(ROOT / "tests" / "gpu-disasm.sh"), "10", "11", "31", "63", "75", "76", "77", "82", "84", "85", "87"], |
| 178 | cwd=str(ROOT), |
| 179 | env={ |
| 180 | **os.environ, |
| 181 | "SHAPE": "4096 512 2048 32 8192 8", |
| 182 | "OUT": str(out_dir), |
| 183 | "NVDISASM": nvdisasm, |
| 184 | "VERBOSE": "1", |
| 185 | }, |
| 186 | capture_output=True, |
| 187 | text=True, |
| 188 | check=True, |
| 189 | ) |
| 190 | self.assertIn("GPR 10 max R66 need 69 declared 72 OK", r_rms.stdout) |
| 191 | self.assertIn("GPR 11 max R67 need 70 declared 72 OK", r_rms.stdout) |
| 192 | self.assertIn("GPR 75 max R66 need 69 declared 72 OK", r_rms.stdout) |
| 193 | self.assertIn("GPR 76 max R67 need 70 declared 72 OK", r_rms.stdout) |
| 194 | self.assertIn("GPR 87 max R68 need 71 declared 72 OK", r_rms.stdout) |
| 195 | self.assertIn("LDG.E.64", r_rms.stdout) |
| 196 | self.assertIn("STG.E.64", r_rms.stdout) |
| 197 | self.assertIn("OMMA.SF.16864", r_rms.stdout) |
| 198 | self.assertIn("IMAD.WIDE.U32 R14, R123, 0x20, R10", r_rms.stdout) |
| 199 | self.assertIn("@P1 IMAD.WIDE.U32", r_rms.stdout) |
| 200 | |
| 201 | def test_nvfp4_cli_bounds(self) -> None: |
| 202 | if platform.system() != "Linux" or platform.machine() != "aarch64": |
| 203 | self.skipTest("nvfp4 command is built on Linux aarch64 gb10 target") |
| 204 | gb10 = _resolve_gb10_bin() |
| 205 | if gb10 is None: |
| 206 | out_dir = self.tmp / "gb10_build" |
| 207 | subprocess.run( |
| 208 | ["sh", str(ROOT / "scripts" / "build.sh")], |
| 209 | cwd=str(ROOT), |
| 210 | env={**os.environ, "TARGET": "gb10", "OUT": str(out_dir)}, |
| 211 | check=True, |
| 212 | ) |
| 213 | gb10 = out_dir / "ollm-gb10" |
| 214 | bad_cases = [ |
| 215 | ["128", "128", "64", "1000001"], |
| 216 | ["2147483648", "2147483648", "64", "1"], |
| 217 | ["128", "4294967296", "64", "1"], |
| 218 | ["128", "128", "274877906944", "1"], |
| 219 | ["65536", "65536", "64", "1"], |
| 220 | ["65664", "128", "64", "1"], |
| 221 | ["128", "65664", "64", "1"], |
| 222 | ["128", "128", "16777280", "1"], |
| 223 | ["0", "128", "64", "1"], |
| 224 | ["128", "0", "64", "1"], |
| 225 | ["128", "128", "0", "1"], |
| 226 | ["128", "128", "64", "0"], |
| 227 | ["64", "128", "64", "1"], |
| 228 | ["128", "64", "64", "1"], |
| 229 | ["128", "128", "32", "1"], |
| 230 | ["-128", "128", "64", "1"], |
| 231 | ["128", "128", "64"], |
| 232 | ] |
| 233 | for args in bad_cases: |
| 234 | with self.subTest(args=args): |
| 235 | r = subprocess.run([str(gb10), "nvfp4", *args], capture_output=True, text=True, check=False) |
| 236 | self.assertEqual(r.returncode, 2, f"args={args} rc={r.returncode} stdout={r.stdout!r} stderr={r.stderr!r}") |
| 237 | |
| 238 | def test_live_gb10_silicon_parity_and_gfcache(self) -> None: |
| 239 | if os.environ.get("OLLM_GB10_LIVE") != "1": |
| 240 | self.skipTest("set OLLM_GB10_LIVE=1 inside an exclusive spark-window to run live GB10 checks") |
| 241 | gb10 = _resolve_gb10_bin() |
| 242 | self.assertIsNotNone(gb10, "ollm-gb10 binary not found") |
| 243 | assert gb10 is not None |
| 244 | |
| 245 | # 1. GBFRAG exact 0-error check across all 10 64x64 + 10 128x128 BF16 HMMA kernels + slot 85 3-way GEMM |
| 246 | frag_ck = self.tmp / "frag.ck" |
| 247 | subprocess.run([str(gb10), "init", str(frag_ck), "4096", "256", "256", "4", "512", "2", "8", "1", "10", "3000", "1"], check=True, capture_output=True) |
| 248 | r = subprocess.run([str(gb10), "gbfrag", str(frag_ck)], capture_output=True, text=True, check=True) |
| 249 | self.assertIn("GF85 128 128 128 0", r.stdout) |
| 250 | self.assertIn("GBFRAG PASS", r.stdout) |
| 251 | |
| 252 | # 1a. Full end-to-end ggbf_ok == true step parity (gbcmp2h) across E=64, E=32, and nh=16>8 |
| 253 | for tag, dims in ( |
| 254 | ("e64", ["256", "64", "256", "4", "512", "2", "2", "1", "10", "3000", "1"]), |
| 255 | ("e32", ["256", "64", "384", "12", "1536", "2", "2", "1", "10", "3000", "1"]), |
| 256 | ("nh16", ["256", "64", "1024", "16", "1024", "1", "2", "1", "10", "3000", "1"]), |
| 257 | ): |
| 258 | with self.subTest(tag=tag): |
| 259 | bfh_ck = self.tmp / f"bfh_{tag}.ck" |
| 260 | subprocess.run([str(gb10), "init", str(bfh_ck), *dims], check=True, capture_output=True) |
| 261 | rh = subprocess.run([str(gb10), "gbcmp2h", str(bfh_ck), str(self.corp), "0"], capture_output=True, text=True, check=True) |
| 262 | self.assertIn("GBH 1", rh.stdout) |
| 263 | self.assertIn("GBCMP2 PASS", rh.stdout) |
| 264 | |
| 265 | # 1b. GACMP AdamW parity (256-CTA 128-bit SQN + 256-thr 128-bit ADAM) |
| 266 | ac_ck = self.tmp / "ac_gpu.ck" |
| 267 | subprocess.run([str(gb10), "init", str(ac_ck), "256", "64", "256", "8", "512", "2", "2", "1", "10", "3000", "1"], check=True, capture_output=True) |
| 268 | ra = subprocess.run([str(gb10), "gacmp", str(ac_ck), str(self.corp), "0"], capture_output=True, text=True, check=True) |
| 269 | self.assertIn("GACMP PASS", ra.stdout) |
| 270 | |
| 271 | # 1c. Live SM121 OMMA.SF.16864 NVFP4 in-register instruction-rate microbenchmark (multi-lane + warp-7 + NaN sentinels) |
| 272 | for nv_args in (["4096", "4096", "8192", "5"], ["256", "256", "128", "2"]): |
| 273 | rnv = subprocess.run([str(gb10), "nvfp4", *nv_args], capture_output=True, text=True, check=True) |
| 274 | self.assertIn(f"NVFP4 {' '.join(nv_args)} ", rnv.stdout) |
| 275 | self.assertIn(" 68 0 INREG_OMMA_MICROBENCH PASS", rnv.stdout) |
| 276 | |
| 277 | # 2. GBCMP2 on Looped Spiral L_phys=8 x NP=4 (LV=32) |
| 278 | loop_ck = self.tmp / "loop8x4_gpu.ck" |
| 279 | subprocess.run([str(gb10), "init", str(loop_ck), "256", "64", "512", "8", "1024", "8", "2", "1", "10", "3000", "1", "0", "8", "4"], check=True, capture_output=True) |
| 280 | r2 = subprocess.run([str(gb10), "gbcmp2", str(loop_ck), str(self.corp), "0"], capture_output=True, text=True, check=True) |
| 281 | self.assertIn("GBCMP2 PASS", r2.stdout) |
| 282 | |
| 283 | # 3. GBCMP2 on D=2048 (n=64 tiled RMS + direct-oww + batched) |
| 284 | d2048_ck = self.tmp / "d2048_gpu.ck" |
| 285 | subprocess.run([str(gb10), "init", str(d2048_ck), "256", "64", "2048", "32", "4096", "1", "2", "1", "10", "3000", "1"], check=True, capture_output=True) |
| 286 | r3 = subprocess.run([str(gb10), "gbcmp2", str(d2048_ck), str(self.corp), "0"], capture_output=True, text=True, check=True) |
| 287 | self.assertIn("GBCMP2 PASS", r3.stdout) |
| 288 | |
| 289 | # 4. gfcache OLLMFH01 parity vs CPU fcache on looped checkpoint |
| 290 | fc_ck = self.tmp / "fc_loop.ck" |
| 291 | cpu_hc = self.tmp / "cpu.hc" |
| 292 | gpu_hc = self.tmp / "gpu.hc" |
| 293 | subprocess.run([str(gb10), "init", str(fc_ck), "256", "64", "256", "4", "512", "4", "4", "1", "10", "3000", "1", "0", "4", "2"], check=True, capture_output=True) |
| 294 | subprocess.run([str(gb10), "fcache", str(fc_ck), str(self.corp), "8", str(cpu_hc)], check=True, capture_output=True) |
| 295 | r4 = subprocess.run([str(gb10), "gfcache", str(fc_ck), str(self.corp), "8", str(gpu_hc), "0"], capture_output=True, text=True, check=True) |
| 296 | self.assertIn("GFCACHE 8 64 256 4 ", r4.stdout) |
| 297 | |
| 298 | a = cpu_hc.read_bytes() |
| 299 | b = gpu_hc.read_bytes() |
| 300 | self.assertEqual(len(a), len(b)) |
| 301 | self.assertEqual(a[:48], b[:48]) |
| 302 | W, T, D, _L = struct.unpack("<4Q", a[8:40]) |
| 303 | rec = (T + T * D) * 8 |
| 304 | max_diff = 0.0 |
| 305 | for w in range(W): |
| 306 | off = 48 + w * rec |
| 307 | self.assertEqual(a[off : off + T * 8], b[off : off + T * 8]) |
| 308 | h0 = struct.unpack(f"<{T * D}d", a[off + T * 8 : off + rec]) |
| 309 | h1 = struct.unpack(f"<{T * D}d", b[off + T * 8 : off + rec]) |
| 310 | md = max(abs(x - y) for x, y in zip(h0, h1)) |
| 311 | if md > max_diff: |
| 312 | max_diff = md |
| 313 | self.assertLess(max_diff, 1e-4) |
| 314 | |
| 315 | |
| 316 | if __name__ == "__main__": |
| 317 | unittest.main() |