CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / om / codegen / moe_ragged_mutants.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | """Source mutants of the ragged-K FP8 GEMV (omega_blackwell_codegen_fp8_gemv_ragged_prog) and its host policy (op 25). |
| 3 | |
| 4 | Host verification: each mutant alters ONE site in the seeded .om input (never generated C, never a test), is regenerated to C through the |
| 5 | pinned bridge, linked ahead of the prebuilt library and run through the test that must kill it. A mutant must compile, the baseline must |
| 6 | pass, and the kill must be the test's own failure (exit 1 and the expected failure text), not a build error or a crash. |
| 7 | Needs OMEGA_OMC and OMEGA_BRIDGE (om/units.sh pins) and a built build/libomega_gpu.a. Run through the device-free wrapper. |
| 8 | """ |
| 9 | import os |
| 10 | from pathlib import Path |
| 11 | import signal |
| 12 | import shutil |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
| 16 | |
| 17 | ROOT = Path(__file__).resolve().parents[2] |
| 18 | OMC = os.environ["OMEGA_OMC"] |
| 19 | BRIDGE = Path(os.environ["OMEGA_BRIDGE"]) |
| 20 | CORE = ("cg_moe_emit.om", "cg_moe_core.om", "cg_moe.om") |
| 21 | SIM = ("codegen_moe_ragged_test", "CG_MOE_SRC", CORE, "om/codegen") |
| 22 | API = ("fp8_ragged_api_test", "RAGGED_API_OVERRIDE", ("hostrest.om",), "om/apiviews") |
| 23 | # (name, target, before, after, expected text in the test output). A value mutant (VALUE) must be killed by wrong results while the build of the |
| 24 | # positive twin still works: its output must not contain 'codegen refused' (a kill by refusing every ragged spec is not a kill of the tail logic). |
| 25 | VALUE = "codegen refused" |
| 26 | MUTANTS = ( |
| 27 | ("tail-lane-count-uses-8", SIM, "put z ulong 121 div rem get z ulong 0 mul ulong 32 get z ulong 1 get z ulong 1", |
| 28 | "put z ulong 121 div rem get z ulong 0 mul ulong 32 get z ulong 1 ulong 8", "FAIL: ragged"), |
| 29 | ("last-iteration-test-off-by-one", SIM, "put x ulong 6 ulong 4294967294\nput x ulong 4 get z ulong 63", |
| 30 | "put x ulong 6 ulong 4294967295\nput x ulong 4 get z ulong 63", "FAIL: ragged"), |
| 31 | ("last-flag-shift", SIM, "put x ulong 2 get z ulong 123\nput x ulong 6 ulong 31", "put x ulong 2 get z ulong 123\nput x ulong 6 ulong 30", "FAIL: ragged"), |
| 32 | ("one-iteration-too-many", SIM, "put z ulong 122 add get z ulong 122 ulong 1", "put z ulong 122 add get z ulong 122 ulong 2", "FAIL: ragged"), |
| 33 | ("loop-count-ignores-tail", SIM, "if and ne get z ulong 121 ulong 0 ne get z ulong 8 ulong 7", "if and ne get z ulong 121 ulong 0 ne get z ulong 8 ulong 99", "FAIL: ragged"), |
| 34 | ("mask-branch-not-taken-polarity", SIM, "put x ulong 9 ulong 1\nput x ulong 6 ulong 1\nmoe_emit b m x ulong 8\nend\n\nfn moe_rclose", |
| 35 | "put x ulong 9 ulong 1\nput x ulong 10 ulong 1\nput x ulong 6 ulong 1\nmoe_emit b m x ulong 8\nend\n\nfn moe_rclose", "FAIL: ragged"), |
| 36 | ("deep-kernel-allowed", SIM, "if or or ne get z ulong 120 ulong 1 ne get z ulong 9 ulong 1 ne d ulong 0", |
| 37 | "if or or ne get z ulong 120 ulong 1 ne get z ulong 9 ulong 1 gt d ulong 99", "FAIL: ragged with a deep kernel"), |
| 38 | ("ragged-permission-never-granted", SIM, "if or or ne get z ulong 120 ulong 1 ne get z ulong 9 ulong 1 ne d ulong 0", |
| 39 | "if or or ne get z ulong 120 ulong 2 ne get z ulong 9 ulong 1 ne d ulong 0", "FAIL: twin: ragged k=640 lb8 builds"), |
| 40 | ("not-a-multiple-of-128-allowed", SIM, "if ne rem k ulong 128 ulong 0\nret false\nend\nif ne rem k mul ulong 32 lb ulong 0", |
| 41 | "if ne rem k ulong 64 ulong 0\nret false\nend\nif ne rem k mul ulong 32 lb ulong 0", "FAIL: ragged k not a multiple of 128 is refused"), |
| 42 | ("steps-forget-the-tail", SIM, "n = add n ulong 1\nend\nret n", "n = add n ulong 0\nend\nret n", "FAIL: k=640 lb8 runs 3 steps"), |
| 43 | ("policy-op25-k-multiple-of-64", API, "if ne rem get p 0 128 0\nret 0\nend\nret 1", "if ne rem get p 0 64 0\nret 0\nend\nret 1", ("FAIL: op25", "hostrest differential op=25")), |
| 44 | ("policy-op25-wrong-variant", API, "if or eq get p 2 0 ne get p 3 1\nret 0", "if or eq get p 2 0 ne get p 3 2\nret 0", ("FAIL: op25", "hostrest differential op=25")), |
| 45 | ("policy-op25-deep-allowed", API, "if or eq get p 1 0 ne get p 4 0\nret 0", "if or eq get p 1 0 gt get p 4 9\nret 0", ("FAIL: op25", "hostrest differential op=25")), |
| 46 | ("policy-op25-ignores-permission", API, "if or eq get p 2 0 ne get p 3 1\nret 0", "if or eq get p 2 9 ne get p 3 1\nret 0", ("FAIL: op25", "hostrest differential op=25")), |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | FORBID = {name: VALUE for name in ( |
| 51 | "tail-lane-count-uses-8", "last-iteration-test-off-by-one", "last-flag-shift", "one-iteration-too-many", "loop-count-ignores-tail", |
| 52 | "mask-branch-not-taken-polarity")} |
| 53 | |
| 54 | |
| 55 | def run(argv, **kwargs): |
| 56 | return subprocess.run(argv, cwd=ROOT, text=True, capture_output=True, **kwargs) |
| 57 | |
| 58 | |
| 59 | def build(out, test, variable, c_file): |
| 60 | extra = [f"{variable}={c_file}"] if c_file else [] |
| 61 | result = run(["make", "-j3", "-o", str(out / "libomega_gpu.a"), f"OUT_DIR={out}", *extra, |
| 62 | "CFLAGS=-std=gnu11 -Wall -Wextra -Werror -O1 -D_GNU_SOURCE -Isrc -I./m16 -I./nvrm", str(out / test)]) |
| 63 | if result.returncode: |
| 64 | raise RuntimeError(f"build {test} {variable}: rc={result.returncode}\n{result.stdout[-2000:]}{result.stderr[-2000:]}") |
| 65 | |
| 66 | |
| 67 | def main(): |
| 68 | prefixes = tuple(sys.argv[1:]) |
| 69 | unknown = [p for p in prefixes if not p or not any(m[0].startswith(p) for m in MUTANTS)] |
| 70 | if unknown: |
| 71 | raise SystemExit(f"unknown mutant prefixes: {unknown}") |
| 72 | selected = [m for m in MUTANTS if not prefixes or m[0].startswith(prefixes)] |
| 73 | lib = Path(os.environ.get("OUT_DIR", ROOT / "build")) / "libomega_gpu.a" |
| 74 | if not lib.exists(): |
| 75 | raise RuntimeError(f"{lib} is missing: run make first") |
| 76 | with tempfile.TemporaryDirectory(prefix="omega-ragged-base-") as directory: |
| 77 | base = Path(directory) |
| 78 | shutil.copy(lib, base / "libomega_gpu.a") |
| 79 | for test in {m[1][0] for m in selected}: |
| 80 | build(base, test, "", None) |
| 81 | result = run([str(base / test)]) |
| 82 | if result.returncode: |
| 83 | raise RuntimeError(f"baseline {test}: rc={result.returncode}\n{result.stdout}{result.stderr}") |
| 84 | killed = 0 |
| 85 | for name, (test, variable, parts, folder), before, after, expect in selected: |
| 86 | source = "\n".join((ROOT / folder / part).read_text() for part in parts) |
| 87 | if source.count(before) != 1: |
| 88 | raise RuntimeError(f"{name}: mutation site is not unique ({source.count(before)})") |
| 89 | with tempfile.TemporaryDirectory(prefix="omega-ragged-mutant-") as directory: |
| 90 | tmp = Path(directory) |
| 91 | om = tmp / "mutant.om" |
| 92 | om.write_text(source.replace(before, after)) |
| 93 | env = dict(os.environ, OMEGA_SEEDED_OMC=OMC) |
| 94 | result = run(["sh", str(BRIDGE / "tools/bridge-seeded"), "c", str(om)], env=env) |
| 95 | if result.returncode: |
| 96 | raise RuntimeError(f"{name}: mutant must compile: rc={result.returncode}\n{result.stderr}") |
| 97 | c = tmp / "mutant.c" |
| 98 | c.write_text(result.stdout) |
| 99 | out = tmp / "build" |
| 100 | out.mkdir() |
| 101 | shutil.copy(lib, out / "libomega_gpu.a") |
| 102 | build(out, test, variable, c) |
| 103 | result = run([str(out / test)]) |
| 104 | text = result.stdout + result.stderr |
| 105 | forbid = FORBID.get(name) |
| 106 | witness = expect if isinstance(expect, str) else expect[0] |
| 107 | allowed_status = (1,) |
| 108 | if isinstance(expect, tuple) and expect[1] in text: |
| 109 | witness = expect[1] |
| 110 | allowed_status = (-signal.SIGABRT, 128 + signal.SIGABRT) |
| 111 | if result.returncode not in allowed_status or witness not in text or (forbid and forbid in text): |
| 112 | raise RuntimeError(f"{name}: mutant survived, crashed or failed for another reason (wanted '{expect}', forbidden '{forbid}'): rc={result.returncode}\n{text[-3000:]}") |
| 113 | print(f"KILLED {name} rc={result.returncode} ('{witness}')", flush=True) |
| 114 | killed += 1 |
| 115 | scope = " SUBSET" if len(selected) < len(MUTANTS) else "" |
| 116 | print(f"CODEGEN MOE RAGGED MUTANTS{scope} PASS ({killed} source-mutant controls)") |
| 117 | |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | main() |