omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / tests / test_megakernel.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: AGPL-3.0-only |
| 3 | """Pillar 1: Persistent sm_121 Megakernel verification suite. |
| 4 | |
| 5 | Checks: |
| 6 | 1. Pure-OM2 compilation with ~/.local/bin/omc for both `gb10` (`om2` + full |
| 7 | native/gpu/{context,vector,launch,batch,gk,gr,gb,gg,gm}.om stack) and |
| 8 | `macos` (`target a64macos` + `nogpu.om` + CPU-sim wrapper + `gm.om`). |
| 9 | 2. Built-in `gm_selftest` pass (`rc == 0`). |
| 10 | 3. SM121 SASS emitter invariants: single-slot fit (<= 16 KiB), double-buffered |
| 11 | slot parity (`gm_swap_buf`), `max_gpr <= 61`, `gkgpr >= max_gpr + 3`, |
| 12 | `nested_bssy == 0`, `+16 B` bank-padded SMEM stride (144 B/blk), |
| 13 | L2-resident weight budget (`<= 8 MB`), and Spark `nvdisasm -b SM121` |
| 14 | disassembly check (0 illegal/unknown insns + MEMBAR.SC.SYS / |
| 15 | LDG.E.MMIO.GPU / STG.E.STRONG.SYS / GPR headroom). |
| 16 | 4. Launch-count = 1 invariant (`gm_launch_persistent` & `gm_sim_steps`). |
| 17 | 5. Bit-exact parity between `gm_sim_steps` (persistent step loop) and |
| 18 | sequential `obatch -> ograd -> oadam` steps across weights, Adam moments, |
| 19 | and per-step losses (both synthetic and real `odopen` dataset file), plus |
| 20 | training loss decrease and boundary-error rejections. |
| 21 | |
| 22 | Usage: |
| 23 | python3 -W error::ResourceWarning tests/test_megakernel.py |
| 24 | """ |
| 25 | import hashlib |
| 26 | import os |
| 27 | import platform |
| 28 | import re |
| 29 | import shutil |
| 30 | import struct |
| 31 | import subprocess |
| 32 | import sys |
| 33 | import tempfile |
| 34 | |
| 35 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 36 | REPO = os.path.dirname(HERE) |
| 37 | SRC = os.path.join(REPO, "native") |
| 38 | GPU = os.path.join(SRC, "gpu") |
| 39 | |
| 40 | PASSES = [] |
| 41 | FAILS = [] |
| 42 | |
| 43 | |
| 44 | def check(cond, name, detail=""): |
| 45 | if cond: |
| 46 | PASSES.append(name) |
| 47 | print(f"PASS {name}" + (f" ({detail})" if detail else "")) |
| 48 | else: |
| 49 | FAILS.append(name) |
| 50 | print(f"FAIL {name}" + (f": {detail}" if detail else "")) |
| 51 | |
| 52 | |
| 53 | def find_omc(): |
| 54 | for cand in ( |
| 55 | os.environ.get("OMC"), |
| 56 | shutil.which("omc"), |
| 57 | os.path.expanduser("~/.local/bin/omc"), |
| 58 | os.path.expanduser("~/canon/omega-mv/seed/out/omc"), |
| 59 | os.path.join(REPO, "..", "omega-mv", "seed", "out", "omc"), |
| 60 | os.path.join(REPO, "..", "omega-mv-macos", "seed", "out", "omc"), |
| 61 | ): |
| 62 | if cand and os.path.isfile(cand) and os.access(cand, os.X_OK): |
| 63 | return cand |
| 64 | raise RuntimeError("seeded omc compiler not found") |
| 65 | |
| 66 | |
| 67 | def read_text(path): |
| 68 | with open(path, "r", encoding="utf-8") as f: |
| 69 | return f.read() |
| 70 | |
| 71 | |
| 72 | def compile_om_bytes(omc, src_text, out_path=None): |
| 73 | p = subprocess.run( |
| 74 | [omc], |
| 75 | input=src_text.encode("utf-8"), |
| 76 | stdout=subprocess.PIPE, |
| 77 | stderr=subprocess.PIPE, |
| 78 | ) |
| 79 | if out_path is not None and p.returncode == 0: |
| 80 | with open(out_path, "wb") as f: |
| 81 | f.write(p.stdout) |
| 82 | os.chmod(out_path, 0o755) |
| 83 | if platform.system() == "Darwin" and shutil.which("codesign"): |
| 84 | subprocess.run( |
| 85 | ["codesign", "-s", "-", "-f", out_path], |
| 86 | stdout=subprocess.DEVNULL, |
| 87 | stderr=subprocess.DEVNULL, |
| 88 | check=True, |
| 89 | ) |
| 90 | return p.returncode, p.stderr.decode("utf-8", errors="replace"), len(p.stdout) |
| 91 | |
| 92 | |
| 93 | MAC_SIM_SHIM = """ |
| 94 | global gkcode uchar 1048576 |
| 95 | global gkthr uint 64 |
| 96 | global gkgpr uint 64 |
| 97 | global gksm uint 64 |
| 98 | global gkarg uint 32 |
| 99 | global gkna uint 1 |
| 100 | global gkt long 4 |
| 101 | |
| 102 | fn gklaunch id uint gx uint gy uint gz uint to bool |
| 103 | put gkt 0 add get gkt 0 1 |
| 104 | ret true |
| 105 | end |
| 106 | """ |
| 107 | |
| 108 | DRIVER_MAIN = """ |
| 109 | fn gm_print_u tag ulong v ulong to void |
| 110 | oputx tag |
| 111 | oputc 32 |
| 112 | oputu v |
| 113 | oputc 10 |
| 114 | oflush |
| 115 | end |
| 116 | |
| 117 | fn gm_print_f tag ulong v f64 to void |
| 118 | oputx tag |
| 119 | oputc 32 |
| 120 | oputx bitcast ulong v |
| 121 | oputc 32 |
| 122 | oputl rmic v |
| 123 | oputc 10 |
| 124 | oflush |
| 125 | end |
| 126 | |
| 127 | fn gm_init_shape to void |
| 128 | put og 0 256 |
| 129 | put og 1 8 |
| 130 | put og 2 16 |
| 131 | put og 3 2 |
| 132 | put og 4 32 |
| 133 | put og 5 2 |
| 134 | put og 6 2 |
| 135 | put og 40 12345 |
| 136 | put og 41 0 |
| 137 | put og 42 100 |
| 138 | put og 43 bitcast ulong 0.001f64 |
| 139 | put og 44 1 |
| 140 | put og 59 1 |
| 141 | if not ogeom |
| 142 | exit 11 |
| 143 | end |
| 144 | oinit |
| 145 | end |
| 146 | |
| 147 | fn gm_cmd_d to int |
| 148 | if not gm_emit_megakernel 0 |
| 149 | ret 20 |
| 150 | end |
| 151 | if not gm_emit_megakernel 1 |
| 152 | ret 21 |
| 153 | end |
| 154 | oputu get gmcfg 1 |
| 155 | oputc 32 |
| 156 | oputu get gmcfg 2 |
| 157 | oputc 32 |
| 158 | oputu get gmcfg 3 |
| 159 | oputc 32 |
| 160 | oputu get gmcfg 5 |
| 161 | oputc 32 |
| 162 | oputu get gmcfg 6 |
| 163 | oputc 32 |
| 164 | oputu get gmcfg 7 |
| 165 | oputc 32 |
| 166 | oputu get gmstats 5 |
| 167 | oputc 10 |
| 168 | oflush |
| 169 | ret 0 |
| 170 | end |
| 171 | |
| 172 | fn gm_cmd_b to int |
| 173 | if not gm_emit_megakernel 0 |
| 174 | ret 20 |
| 175 | end |
| 176 | if not gm_emit_megakernel 1 |
| 177 | ret 21 |
| 178 | end |
| 179 | slot = ulong 0 |
| 180 | if ge oargc 3 |
| 181 | slot = oargu 2 |
| 182 | end |
| 183 | base = mul slot 16384 |
| 184 | n = get gmcfg add 1 slot |
| 185 | w = write 1 gmcode base n |
| 186 | if ne w long n |
| 187 | ret 22 |
| 188 | end |
| 189 | ret 0 |
| 190 | end |
| 191 | |
| 192 | fn gm_cmd_p to int |
| 193 | gm_init_shape |
| 194 | use_synth = true |
| 195 | if ge oargc 3 |
| 196 | odopen 2 |
| 197 | use_synth = false |
| 198 | end |
| 199 | nn = ulong 6 |
| 200 | lseq = gm_seq_steps nn use_synth |
| 201 | gm_save_state |
| 202 | put og 41 0 |
| 203 | oinit |
| 204 | lmeg = gm_sim_steps nn use_synth |
| 205 | diff = gm_parity_diff nn |
| 206 | oputc 80 |
| 207 | oputc 65 |
| 208 | oputc 82 |
| 209 | oputc 32 |
| 210 | oputu diff |
| 211 | oputc 32 |
| 212 | oputu get gmstats 0 |
| 213 | oputc 32 |
| 214 | oputu get gmstats 1 |
| 215 | oputc 32 |
| 216 | oputu get gmstats 2 |
| 217 | oputc 32 |
| 218 | oputu get gmstats 3 |
| 219 | oputc 32 |
| 220 | oputu get gmstats 4 |
| 221 | oputc 32 |
| 222 | oputu get gmstats 5 |
| 223 | oputc 32 |
| 224 | oputu get gmstats 6 |
| 225 | oputc 32 |
| 226 | oputx bitcast ulong lseq |
| 227 | oputc 32 |
| 228 | oputx bitcast ulong lmeg |
| 229 | oputc 32 |
| 230 | oputl rmic get gmlos_meg 0 |
| 231 | oputc 32 |
| 232 | oputl rmic get gmlos_meg sub nn 1 |
| 233 | oputc 32 |
| 234 | oputu get gmcfg 11 |
| 235 | oputc 10 |
| 236 | oflush |
| 237 | if ne diff 0 |
| 238 | ret 30 |
| 239 | end |
| 240 | ret 0 |
| 241 | end |
| 242 | |
| 243 | fn gm_cmd_l to int |
| 244 | gm_init_shape |
| 245 | if not gm_emit_megakernel 0 |
| 246 | ret 40 |
| 247 | end |
| 248 | if not gm_emit_megakernel 1 |
| 249 | ret 41 |
| 250 | end |
| 251 | sw1 = gm_swap_buf |
| 252 | sl1 = get gmcfg 13 |
| 253 | sw2 = gm_swap_buf |
| 254 | sl2 = get gmcfg 13 |
| 255 | ok1 = gm_launch_persistent 8 |
| 256 | ln1 = get gmstats 0 |
| 257 | oputc 76 |
| 258 | oputc 78 |
| 259 | oputc 32 |
| 260 | oputu ulong sw1 |
| 261 | oputc 32 |
| 262 | oputu sl1 |
| 263 | oputc 32 |
| 264 | oputu ulong sw2 |
| 265 | oputc 32 |
| 266 | oputu sl2 |
| 267 | oputc 32 |
| 268 | oputu ln1 |
| 269 | oputc 32 |
| 270 | oputu get gmstats 1 |
| 271 | oputc 32 |
| 272 | oputu get gmstats 2 |
| 273 | oputc 32 |
| 274 | oputu get gmcfg 12 |
| 275 | oputc 10 |
| 276 | oflush |
| 277 | if or not ok1 ne ln1 1 |
| 278 | ret 42 |
| 279 | end |
| 280 | if or gm_emit_megakernel 2 or gm_launch_persistent 0 gm_launch_persistent 65537 |
| 281 | ret 43 |
| 282 | end |
| 283 | if ge gm_sim_steps 0 true 0.0f64 |
| 284 | ret 44 |
| 285 | end |
| 286 | ret 0 |
| 287 | end |
| 288 | |
| 289 | fn gm_putb b bool to void |
| 290 | if b |
| 291 | oputc 49 |
| 292 | else |
| 293 | oputc 48 |
| 294 | end |
| 295 | end |
| 296 | |
| 297 | fn gm_cmd_r_neg a0 bool a1 bool to ulong |
| 298 | saved = gm_r_at 16384 |
| 299 | gm_w_at 16384 0x3a8 |
| 300 | ok = not gm_audit_no_fp_atomics 1 |
| 301 | gm_w_at 16384 saved |
| 302 | if ok |
| 303 | ok = gm_audit_probe 0x9a8 |
| 304 | end |
| 305 | if ok |
| 306 | ok = gm_audit_probe 0x3aa |
| 307 | end |
| 308 | if ok |
| 309 | ok = gm_audit_probe 0x9aa |
| 310 | end |
| 311 | if ok |
| 312 | ok = gm_audit_probe 0x3a9 |
| 313 | end |
| 314 | if ok |
| 315 | ok = gm_audit_probe 0x9a9 |
| 316 | end |
| 317 | a1p = gm_audit_no_fp_atomics 1 |
| 318 | gm_w_at 16384 0x3a8 |
| 319 | if ne gm_swap_buf 1 |
| 320 | gm_w_at 16384 saved |
| 321 | ret 0 |
| 322 | end |
| 323 | lb = not gm_launch_persistent 4 |
| 324 | gm_w_at 16384 saved |
| 325 | if ne gm_swap_buf 0 |
| 326 | ret 0 |
| 327 | end |
| 328 | oputc 76 |
| 329 | oputc 79 |
| 330 | oputc 67 |
| 331 | oputc 75 |
| 332 | oputc 49 |
| 333 | oputc 32 |
| 334 | gm_putb a0 |
| 335 | oputc 32 |
| 336 | gm_putb a1 |
| 337 | oputc 32 |
| 338 | gm_putb ok |
| 339 | oputc 32 |
| 340 | gm_putb a1p |
| 341 | oputc 32 |
| 342 | gm_putb lb |
| 343 | if and ok and a1p lb |
| 344 | ret 1 |
| 345 | end |
| 346 | ret 0 |
| 347 | end |
| 348 | |
| 349 | fn gm_cmd_r to int |
| 350 | if not gm_emit_megakernel 0 |
| 351 | ret 50 |
| 352 | end |
| 353 | if not gm_emit_megakernel 1 |
| 354 | ret 51 |
| 355 | end |
| 356 | a0 = gm_audit_no_fp_atomics 0 |
| 357 | a1 = gm_audit_no_fp_atomics 1 |
| 358 | if or not a0 not a1 |
| 359 | ret 52 |
| 360 | end |
| 361 | if eq gm_cmd_r_neg a0 a1 0 |
| 362 | ret 53 |
| 363 | end |
| 364 | fwd = new f64 24 0.0f64 |
| 365 | rev = new f64 24 0.0f64 |
| 366 | intl = new f64 24 0.0f64 |
| 367 | gm_init_cta_terms edit fwd |
| 368 | for i ulong 24 |
| 369 | put rev i get fwd sub 23 i |
| 370 | if lt i 12 |
| 371 | put intl i get fwd mul i 2 |
| 372 | else |
| 373 | put intl i get fwd add mul sub i 12 2 1 |
| 374 | end |
| 375 | end |
| 376 | qf = gm_reduce_i64_fixed view fwd 24 |
| 377 | qr = gm_reduce_i64_fixed view rev 24 |
| 378 | qi = gm_reduce_i64_fixed view intl 24 |
| 379 | oputc 32 |
| 380 | oputl qf |
| 381 | oputc 32 |
| 382 | oputl qr |
| 383 | oputc 32 |
| 384 | oputl qi |
| 385 | oputc 32 |
| 386 | oputx bitcast ulong gm_fixed_to_f64 qf |
| 387 | oputc 32 |
| 388 | oputx bitcast ulong gm_fixed_to_f64 qr |
| 389 | oputc 32 |
| 390 | oputx bitcast ulong gm_fixed_to_f64 qi |
| 391 | oputc 32 |
| 392 | oputx bitcast ulong gm_reduce_tree_f64 view fwd 24 |
| 393 | oputc 32 |
| 394 | oputx bitcast ulong gm_reduce_tree_f64 view fwd 19 |
| 395 | oputc 10 |
| 396 | oflush |
| 397 | if or ne qf qr ne qf qi |
| 398 | ret 54 |
| 399 | end |
| 400 | ret 0 |
| 401 | end |
| 402 | |
| 403 | fn main to int |
| 404 | cmd = oargr 1 |
| 405 | if lt cmd 0 |
| 406 | exit 2 |
| 407 | end |
| 408 | c0 = get oarg 0 |
| 409 | if eq c0 115 |
| 410 | rc = gm_selftest |
| 411 | oputu ulong rc |
| 412 | oputc 10 |
| 413 | oflush |
| 414 | ret rc |
| 415 | end |
| 416 | if eq c0 100 |
| 417 | ret gm_cmd_d |
| 418 | end |
| 419 | if eq c0 98 |
| 420 | ret gm_cmd_b |
| 421 | end |
| 422 | if eq c0 112 |
| 423 | ret gm_cmd_p |
| 424 | end |
| 425 | if eq c0 108 |
| 426 | ret gm_cmd_l |
| 427 | end |
| 428 | if eq c0 114 |
| 429 | ret gm_cmd_r |
| 430 | end |
| 431 | ret 2 |
| 432 | end |
| 433 | """ |
| 434 | |
| 435 | |
| 436 | def build_macos_source(): |
| 437 | parts = [ |
| 438 | "math.om", "data.om", "lpool.om", "model.om", "adam.om", |
| 439 | "ckpt.om", "bpe.om", "spec.om", "rev.om", "arboris.om", |
| 440 | "sp_solve.om", "spiral.om", "apu_weave.om", "omg.om", "pillars_cli.om", "cli.om", "mio.om", "noserve.om", "nogpu.om", "apu_fabric_macos.om", |
| 441 | ] |
| 442 | body = [] |
| 443 | for name in parts: |
| 444 | txt = read_text(os.path.join(SRC, name)) |
| 445 | txt = txt.replace("fn main to int", "fn ocpumain to int") |
| 446 | body.append(txt) |
| 447 | body.append(MAC_SIM_SHIM) |
| 448 | body.append(read_text(os.path.join(GPU, "gm.om"))) |
| 449 | body.append(DRIVER_MAIN) |
| 450 | return "target a64macos\nom2\n" + "".join(body) |
| 451 | |
| 452 | |
| 453 | def build_gb10_source(): |
| 454 | parts = [ |
| 455 | os.path.join(SRC, "net", "http.om"), |
| 456 | os.path.join(SRC, "net", "json.om"), |
| 457 | os.path.join(SRC, "net", "jsonw.om"), |
| 458 | os.path.join(SRC, "math.om"), |
| 459 | os.path.join(SRC, "data.om"), |
| 460 | os.path.join(SRC, "lpool.om"), |
| 461 | os.path.join(SRC, "model.om"), |
| 462 | os.path.join(SRC, "adam.om"), |
| 463 | os.path.join(SRC, "ckpt.om"), |
| 464 | os.path.join(SRC, "bpe.om"), |
| 465 | os.path.join(SRC, "spec.om"), |
| 466 | os.path.join(SRC, "rev.om"), |
| 467 | os.path.join(SRC, "arboris.om"), |
| 468 | os.path.join(SRC, "sp_solve.om"), |
| 469 | os.path.join(SRC, "spiral.om"), |
| 470 | os.path.join(SRC, "apu_weave.om"), |
| 471 | os.path.join(SRC, "omg.om"), |
| 472 | os.path.join(SRC, "pillars_cli.om"), |
| 473 | os.path.join(SRC, "cli.om"), |
| 474 | os.path.join(SRC, "lio.om"), |
| 475 | os.path.join(SRC, "serve.om"), |
| 476 | os.path.join(GPU, "context.om"), |
| 477 | os.path.join(GPU, "vector.om"), |
| 478 | os.path.join(GPU, "launch.om"), |
| 479 | os.path.join(GPU, "batch.om"), |
| 480 | os.path.join(GPU, "gk.om"), |
| 481 | os.path.join(GPU, "gr.om"), |
| 482 | os.path.join(GPU, "gb.om"), |
| 483 | os.path.join(GPU, "gg.om"), |
| 484 | os.path.join(GPU, "gt.om"), |
| 485 | os.path.join(GPU, "gm.om"), |
| 486 | os.path.join(GPU, "ring.om"), |
| 487 | os.path.join(GPU, "ejit.om"), |
| 488 | os.path.join(SRC, "apu_fabric_linux.om"), |
| 489 | os.path.join(SRC, "apu_fabric_gb10.om"), |
| 490 | ] |
| 491 | return "om2\n" + "".join(read_text(p) for p in parts) |
| 492 | |
| 493 | |
| 494 | def build_gb10_minimal_source(): |
| 495 | parts = [ |
| 496 | os.path.join(SRC, "math.om"), |
| 497 | os.path.join(SRC, "data.om"), |
| 498 | os.path.join(SRC, "lpool.om"), |
| 499 | os.path.join(SRC, "model.om"), |
| 500 | os.path.join(SRC, "adam.om"), |
| 501 | os.path.join(SRC, "ckpt.om"), |
| 502 | os.path.join(SRC, "bpe.om"), |
| 503 | os.path.join(SRC, "spec.om"), |
| 504 | os.path.join(SRC, "rev.om"), |
| 505 | os.path.join(SRC, "arboris.om"), |
| 506 | os.path.join(SRC, "sp_solve.om"), |
| 507 | os.path.join(SRC, "spiral.om"), |
| 508 | os.path.join(SRC, "apu_weave.om"), |
| 509 | os.path.join(SRC, "omg.om"), |
| 510 | os.path.join(SRC, "pillars_cli.om"), |
| 511 | os.path.join(SRC, "cli.om"), |
| 512 | os.path.join(SRC, "lio.om"), |
| 513 | os.path.join(SRC, "noserve.om"), |
| 514 | os.path.join(GPU, "context.om"), |
| 515 | os.path.join(GPU, "vector.om"), |
| 516 | os.path.join(GPU, "launch.om"), |
| 517 | os.path.join(GPU, "batch.om"), |
| 518 | os.path.join(GPU, "gk.om"), |
| 519 | os.path.join(GPU, "gr.om"), |
| 520 | os.path.join(GPU, "gb.om"), |
| 521 | os.path.join(GPU, "gg.om"), |
| 522 | os.path.join(GPU, "gt.om"), |
| 523 | os.path.join(GPU, "gm.om"), |
| 524 | os.path.join(GPU, "ring.om"), |
| 525 | os.path.join(GPU, "ejit.om"), |
| 526 | os.path.join(SRC, "apu_fabric_linux.om"), |
| 527 | os.path.join(SRC, "apu_fabric_gb10.om"), |
| 528 | ] |
| 529 | return "om2\n" + "".join(read_text(p) for p in parts) |
| 530 | |
| 531 | |
| 532 | def max_gpr_in_sass(sass_text): |
| 533 | mx = -1 |
| 534 | for line in sass_text.splitlines(): |
| 535 | m = re.search(r"/\*[0-9a-f]+\*/\s*(.*)", line) |
| 536 | if not m: |
| 537 | continue |
| 538 | rhs = re.sub(r";.*", "", m.group(1)).strip() |
| 539 | if rhs.startswith("@"): |
| 540 | parts = rhs.split(None, 1) |
| 541 | rhs = parts[1] if len(parts) > 1 else "" |
| 542 | parts = rhs.split(None, 1) |
| 543 | op = parts[0] if parts else "" |
| 544 | rest = parts[1] if len(parts) > 1 else "" |
| 545 | w = 1 |
| 546 | if ".128" in op: |
| 547 | w = 4 |
| 548 | elif ".64" in op or ".WIDE" in op: |
| 549 | w = 2 |
| 550 | # Bracketed address operands have width 0 extension unless .64 |
| 551 | for br in re.findall(r"\[[^\]]*\]", rest): |
| 552 | for rm in re.finditer(r"(?<![A-Za-z0-9_.])R(\d+)(\.64)?", br): |
| 553 | ext = 1 if rm.group(2) else 0 |
| 554 | mx = max(mx, int(rm.group(1)) + ext) |
| 555 | no_br = re.sub(r"\[[^\]]*\]", " ", rest) |
| 556 | for rm in re.finditer(r"(?<![A-Za-z0-9_.])R(\d+)(\.64)?", no_br): |
| 557 | ext = 1 if rm.group(2) else w - 1 |
| 558 | mx = max(mx, int(rm.group(1)) + ext) |
| 559 | return mx |
| 560 | |
| 561 | |
| 562 | def run_nvdisasm(code_bytes): |
| 563 | local_nv = shutil.which("nvdisasm") or "/usr/local/cuda/bin/nvdisasm" |
| 564 | if os.path.isfile(local_nv) and os.access(local_nv, os.X_OK): |
| 565 | with tempfile.NamedTemporaryFile(suffix=".bin") as tf: |
| 566 | tf.write(code_bytes) |
| 567 | tf.flush() |
| 568 | p = subprocess.run([local_nv, "-b", "SM121", tf.name], |
| 569 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 570 | return "local", p.returncode, p.stdout.decode("utf-8", errors="replace"), p.stderr.decode("utf-8", errors="replace") |
| 571 | if shutil.which("ssh"): |
| 572 | p = subprocess.run( |
| 573 | [ |
| 574 | "ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=5", "spark", |
| 575 | "tmp=$(mktemp /tmp/gm_disasm.XXXXXX) && cat > \"$tmp\" && /usr/local/cuda/bin/nvdisasm -b SM121 \"$tmp\"; rc=$?; rm -f \"$tmp\"; exit $rc", |
| 576 | ], |
| 577 | input=code_bytes, |
| 578 | stdout=subprocess.PIPE, |
| 579 | stderr=subprocess.PIPE, |
| 580 | ) |
| 581 | return "spark-ssh", p.returncode, p.stdout.decode("utf-8", errors="replace"), p.stderr.decode("utf-8", errors="replace") |
| 582 | return None, None, "", "nvdisasm unavailable" |
| 583 | |
| 584 | |
| 585 | def main(): |
| 586 | omc = find_omc() |
| 587 | print(f"Using omc: {omc}") |
| 588 | |
| 589 | # 1. Compile gb10 full stack + gm.om and gb10 minimal stack + gm.om |
| 590 | rc, err, nbytes = compile_om_bytes(omc, build_gb10_source()) |
| 591 | check(rc == 0 and nbytes > 0, "compile_gb10_full", f"rc={rc} bytes={nbytes} {err.strip()}") |
| 592 | |
| 593 | rc_m, err_m, nbytes_m = compile_om_bytes(omc, build_gb10_minimal_source()) |
| 594 | check(rc_m == 0 and nbytes_m > 0, "compile_gb10_noserve", f"rc={rc_m} bytes={nbytes_m} {err_m.strip()}") |
| 595 | |
| 596 | # 2. Compile and run macos target on Darwin (Linux omc only supports Linux targets) |
| 597 | if platform.system() != "Darwin": |
| 598 | print("Non-macOS host: skipping target a64macos compile/execution.") |
| 599 | return 0 if not FAILS else 1 |
| 600 | |
| 601 | with tempfile.TemporaryDirectory() as td: |
| 602 | bin_path = os.path.join(td, "gm_test") |
| 603 | rc_mac, err_mac, nbytes_mac = compile_om_bytes(omc, build_macos_source(), bin_path) |
| 604 | check(rc_mac == 0 and nbytes_mac > 0, "compile_macos_a64", f"rc={rc_mac} bytes={nbytes_mac} {err_mac.strip()}") |
| 605 | if rc_mac != 0: |
| 606 | return 1 |
| 607 | |
| 608 | # 3. Built-in gm_selftest |
| 609 | p = subprocess.run([bin_path, "s"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 610 | check(p.returncode == 0, "gm_selftest", f"rc={p.returncode} out={p.stdout.decode().strip()}") |
| 611 | |
| 612 | # 4. Emitter metadata & double-buffer checks |
| 613 | p = subprocess.run([bin_path, "d"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 614 | check(p.returncode == 0, "gm_emit_stats_run", f"rc={p.returncode}") |
| 615 | b0, b1, ins0, max_gpr, decl_gpr, smem_b, nested_bssy = [int(x) for x in p.stdout.decode().split()] |
| 616 | check(0 < b0 <= 16384 and b0 == b1, "single_slot_fit_double_buf", f"b0={b0} b1={b1} insns={ins0}") |
| 617 | check(max_gpr <= 61 and decl_gpr >= max_gpr + 3, "gpr_headroom", f"max_gpr=R{max_gpr} decl={decl_gpr}") |
| 618 | check(nested_bssy == 0, "zero_nested_bssy", f"nested_bssy={nested_bssy}") |
| 619 | check(smem_b == 4608, "smem_bank_padded_layout", f"smem_bytes={smem_b}") |
| 620 | |
| 621 | # 5. Extract slot 0 and slot 1 bytes, check sha256 parity |
| 622 | p_s0 = subprocess.run([bin_path, "b", "0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 623 | p_s1 = subprocess.run([bin_path, "b", "1"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 624 | check( |
| 625 | p_s0.returncode == 0 and p_s1.returncode == 0 and len(p_s0.stdout) == b0 |
| 626 | and hashlib.sha256(p_s0.stdout).hexdigest() == hashlib.sha256(p_s1.stdout).hexdigest(), |
| 627 | "double_buffer_code_parity", |
| 628 | f"sha256={hashlib.sha256(p_s0.stdout).hexdigest()[:16]}", |
| 629 | ) |
| 630 | |
| 631 | # 6. Spark nvdisasm -b SM121 verification (CPU-only, no GPU) |
| 632 | mode, d_rc, sass, d_err = run_nvdisasm(p_s0.stdout) |
| 633 | if mode is not None and d_rc == 0: |
| 634 | has_illegal = bool(re.search(r"illegal|unknown", sass, re.IGNORECASE)) |
| 635 | check(not has_illegal, f"nvdisasm_sm121_clean ({mode})", f"insns={ins0}") |
| 636 | req_insns = [ |
| 637 | "LDCU.64 UR4", |
| 638 | "LDG.E.MMIO.GPU", |
| 639 | "MEMBAR.SC.SYS", |
| 640 | "STG.E.STRONG.SYS", |
| 641 | "SHFL.BFLY", |
| 642 | "MUFU.RSQ", |
| 643 | "MUFU.EX2", |
| 644 | "MUFU.LG2", |
| 645 | "BAR.SYNC.DEFER_BLOCKING", |
| 646 | "LDS R34, [R48+URZ]", |
| 647 | "LDS R35, [R48+URZ+0x90]", |
| 648 | "BRA 0x270", |
| 649 | "EXIT", |
| 650 | ] |
| 651 | missing = [tok for tok in req_insns if tok not in sass] |
| 652 | check(not missing, "nvdisasm_sm121_required_ops", f"missing={missing}") |
| 653 | has_fp_atom = bool(re.search(r"\bATOM[GS]?\b|\bRED[GS]?\b", sass)) |
| 654 | check(not has_fp_atom, "nvdisasm_sm121_no_atomics", f"insns={ins0}") |
| 655 | dis_max_gpr = max_gpr_in_sass(sass) |
| 656 | check( |
| 657 | 0 <= dis_max_gpr <= 61 and decl_gpr >= dis_max_gpr + 3, |
| 658 | "nvdisasm_gpr_headroom", |
| 659 | f"dis_max=R{dis_max_gpr} need<={dis_max_gpr+3} decl={decl_gpr}", |
| 660 | ) |
| 661 | else: |
| 662 | print(f"SKIP nvdisasm_sm121 ({mode}: rc={d_rc} {d_err.strip()})") |
| 663 | |
| 664 | # 7. Launch-count=1, double-buffer swap, and negative boundary checks |
| 665 | p_ln = subprocess.run([bin_path, "l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 666 | check(p_ln.returncode == 0, "launch_persistent_and_bounds", p_ln.stdout.decode().strip()) |
| 667 | ln_toks = p_ln.stdout.decode().split() |
| 668 | check( |
| 669 | ln_toks[0] == "LN" and int(ln_toks[1]) == 1 and int(ln_toks[2]) == 61 |
| 670 | and int(ln_toks[3]) == 0 and int(ln_toks[4]) == 60 |
| 671 | and int(ln_toks[5]) == 1 and int(ln_toks[6]) == 8 and int(ln_toks[7]) == 16 and int(ln_toks[8]) == 2, |
| 672 | "swap_buf_and_single_launch", |
| 673 | " ".join(ln_toks), |
| 674 | ) |
| 675 | |
| 676 | # 8. Parity test with synthetic in-memory batch |
| 677 | p_ps = subprocess.run([bin_path, "p"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 678 | check(p_ps.returncode == 0, "sim_parity_synth_run", p_ps.stdout.decode().strip()) |
| 679 | pt = p_ps.stdout.decode().split() |
| 680 | check( |
| 681 | pt[0] == "PAR" and int(pt[1]) == 0 and int(pt[2]) == 1 and int(pt[3]) == 6 |
| 682 | and int(pt[7]) == 0 and int(pt[8]) == 6 and pt[9] == pt[10] |
| 683 | and int(pt[12]) < int(pt[11]) and int(pt[13]) <= 8388608, |
| 684 | "sim_parity_synth_bit_exact", |
| 685 | f"diff={pt[1]} launches={pt[2]} loss0={pt[11]} loss5={pt[12]} l2_bytes={pt[13]}", |
| 686 | ) |
| 687 | |
| 688 | # 9. Parity test with real odopen dataset file |
| 689 | ds_path = os.path.join(td, "train.bin") |
| 690 | # Deterministic byte corpus (> T + 1 tokens, V=256) |
| 691 | corpus = bytes(((i * 37 + 13) % 256) for i in range(512)) |
| 692 | with open(ds_path, "wb") as f: |
| 693 | f.write(corpus) |
| 694 | p_pf = subprocess.run([bin_path, "p", ds_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 695 | check(p_pf.returncode == 0, "sim_parity_odopen_run", p_pf.stdout.decode().strip()) |
| 696 | pf = p_pf.stdout.decode().split() |
| 697 | check( |
| 698 | pf[0] == "PAR" and int(pf[1]) == 0 and int(pf[2]) == 1 and int(pf[3]) == 6 |
| 699 | and int(pf[8]) == 6 and pf[9] == pf[10] and int(pf[12]) < int(pf[11]), |
| 700 | "sim_parity_odopen_bit_exact", |
| 701 | f"diff={pf[1]} launches={pf[2]} loss0={pf[11]} loss5={pf[12]}", |
| 702 | ) |
| 703 | |
| 704 | # 10. Byte-Traceability Lock 1: FP atomic audit + negative control + fixed/tree reductions |
| 705 | p_l1 = subprocess.run([bin_path, "r"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 706 | check(p_l1.returncode == 0, "lock1_audit_and_reduction_run", p_l1.stdout.decode().strip()) |
| 707 | lt = p_l1.stdout.decode().split() |
| 708 | check( |
| 709 | len(lt) == 14 and lt[0] == "LOCK1" |
| 710 | and int(lt[1]) == 1 and int(lt[2]) == 1 |
| 711 | and int(lt[3]) == 1 and int(lt[4]) == 1 and int(lt[5]) == 1, |
| 712 | "lock1_no_fp_atomics_and_neg_ctrl", |
| 713 | f"a0={lt[1]} a1={lt[2]} neg_ok={lt[3]} a1_post={lt[4]} launch_blocked={lt[5]}" if len(lt) >= 6 else str(lt), |
| 714 | ) |
| 715 | check( |
| 716 | len(lt) == 14 and int(lt[6]) == int(lt[7]) == int(lt[8]), |
| 717 | "lock1_i64_fixed_perm_invariant", |
| 718 | f"qf={lt[6]} qr={lt[7]} qi={lt[8]}" if len(lt) >= 9 else str(lt), |
| 719 | ) |
| 720 | check( |
| 721 | len(lt) == 14 and lt[9] == lt[10] == lt[11], |
| 722 | "lock1_f64_fixed_bit_identical", |
| 723 | f"ff={lt[9]} fr={lt[10]} fi={lt[11]}" if len(lt) >= 12 else str(lt), |
| 724 | ) |
| 725 | if len(lt) == 14: |
| 726 | f_fixed = struct.unpack(">d", bytes.fromhex(lt[9].zfill(16)))[0] |
| 727 | t24 = struct.unpack(">d", bytes.fromhex(lt[12].zfill(16)))[0] |
| 728 | t19 = struct.unpack(">d", bytes.fromhex(lt[13].zfill(16)))[0] |
| 729 | check( |
| 730 | abs(f_fixed - 2.3764) < 1e-6 and abs(t24 - 2.3764) < 1e-9 and abs(t19 - 13.7511) < 1e-9, |
| 731 | "lock1_tree_f64_accuracy", |
| 732 | f"f_fixed={f_fixed:.10f} t24={t24:.10f} t19={t19:.10f}", |
| 733 | ) |
| 734 | |
| 735 | print(f"\nSUMMARY: passed={len(PASSES)} failed={len(FAILS)}") |
| 736 | return 0 if not FAILS else 1 |
| 737 | |
| 738 | |
| 739 | if __name__ == "__main__": |
| 740 | sys.exit(main()) |