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-bridge/blob/main/attach/torch/test_cudapp_torch.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings 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-bridge / attach / torch / test_cudapp_torch.py` 84 lines ยท 3578 bytes Raw Blame History 1 """test_cudapp_torch.py: torch.ops.cudapp.mm / swiglu on the GB10 against torch CPU in float64. 2 3 usage: CUDAPP_TORCH_LIB= python test_cudapp_torch.py RECEIPT.json 4 Runs on the chip (CUDA++ opens the GB10): take /tmp/aien-gb10.lock first. Exit 0 only when every case is inside 5 its bound, the routes are the stated ones, and the negative control (gemm_ex mutant 1: the last k element of every 6 product dropped) goes red. 7 8 Bounds, reference = float64 product of the same (exactly representable) inputs, S = (|a| @ |b|) in float64: 9 bf16 / f16 inputs (native HMMA, fp32 accumulation over k terms): |c - ref| <= k * 2^-24 * S 10 f32 inputs (bf16x3: 9 exact plane products per k, fp32 accumulation over 9k terms, k parts summed in double): 11 |c - ref| <= 9 k * 2^-24 * S 12 swiglu (CUDApp tests/gpu_elementwise_test.c tolerance): |y - ref| <= 1e-5 |ref| + 1e-6 13 """ 14 import json 15 import sys 16 17 import torch 18 19 import cudapp_torch 20 21 U = 2.0 ** -24 22 torch.manual_seed(`20261027`) 23 rows = [] 24 25 26 def mm_case(m, k, n, dt, mutant=0): 27 a = torch.randn(m, k, dtype=torch.float64).to(dt) 28 b = torch.randn(k, n, dtype=torch.float64).to(dt) 29 cudapp_torch.set_gemm_mutant(mutant) 30 try: 31 c = torch.ops.cudapp.mm(a, b).double() 32 finally: 33 cudapp_torch.set_gemm_mutant(0) 34 ref = a.double() @ b.double() 35 s = a.double().abs() @ b.double().abs() 36 terms = 9 * k if dt == torch.float32 else k 37 err = (c - ref).abs() 38 ratio = float((err / (terms * U * s + 1e-300)).max()) 39 route_want = 3 if dt == torch.float32 else 1 40 ok = ratio <= 1.0 and cudapp_torch.last_route == route_want 41 rows.append({"op": "cudapp::mm", "m": m, "k": k, "n": n, "dtype": str(dt).split(".")[1], "mutant": mutant, 42 "route": cudapp_torch.last_route, "route_expected": route_want, 43 "max_abs_err": float(err.max()), "max_err_over_bound": ratio, "inside_bound": ok}) 44 return ok 45 46 47 def swiglu_case(n): 48 g = torch.randn(n, dtype=torch.float32) * 4 49 u = torch.randn(n, dtype=torch.float32) 50 y = torch.ops.cudapp.swiglu(g, u).double() 51 gd, ud = g.double(), u.double() 52 ref = gd / (1 + torch.exp(-gd)) * ud 53 ratio = float(((y - ref).abs() / (1e-5 * ref.abs() + 1e-6)).max()) 54 ok = ratio <= 1.0 55 rows.append({"op": "cudapp::swiglu", "n": n, "max_abs_err": float((y - ref).abs().max()), 56 "max_err_over_bound": ratio, "inside_bound": ok}) 57 return ok 58 59 60 good = True 61 for m, k, n in [(1, 1, 1), (3, 257, 129), (17, 2048, 96), (64, 64, 64), (5, 1809, 33)]: 62 good &= mm_case(m, k, n, torch.float32) 63 for dt in (torch.bfloat16, torch.float16): 64 for m, k, n in [(5, 300, 7), (32, 128, 64)]: 65 good &= mm_case(m, k, n, dt) 66 for n in (1, 1000, 65536): 67 good &= swiglu_case(n) 68 69 # torch.library's own operator checks (schema, fake tensor, autograd registration) on one small case each 70 torch.library.opcheck(torch.ops.cudapp.mm.default, (torch.randn(3, 5), torch.randn(5, 4))) 71 torch.library.opcheck(torch.ops.cudapp.swiglu.default, (torch.randn(7), torch.randn(7))) 72 73 # negative control: must leave the bound 74 caught = not mm_case(3, 257, 129, torch.float32, mutant=1) 75 rows[-1]["negative_control"] = True 76 rows[-1]["caught"] = caught 77 78 verdict = "PASS" if good and caught else "FAIL" 79 with open(sys.argv[1], "w") as f: 80 json.dump({"schema": "CUDAPP_TORCH_V1", "torch": torch.__version__, "verdict": verdict, "cases": rows}, f, indent=1) 81 for r in rows: 82 print(json.dumps(r)) 83 print(verdict) 84 sys.exit(0 if verdict == "PASS" else 1)