"""Auto-validation gate for canonical kernels: the correct implementation must survive every suite (T0 capped at 2 trials) before it may serve as a mutation substrate. Exits nonzero on any failure so the pipeline aborts. Usage: python3 validate_ext.py [problem ...] (default: extended problems only) """ import ctypes import sys import torch import kernels_def as K import problems_ext # noqa: F401 from nvrtc_tce import compile_ptx, device_source from screen_mutants import NvrtcKernel, compare, kernel_name, launch_spec, ref_fn_for EXT = ["leakyrelu", "gelu", "sigmoid", "logsoftmax", "matmul_mk", "matmul_ta", "mean", "max", "rmsnorm"] def main(): problems = sys.argv[1:] or EXT torch.zeros(1, device="cuda") cuda_lib = ctypes.CDLL("libcuda.so.1") failures = 0 for problem in problems: src = device_source(K.PROBLEMS[problem]["cuda"]).replace( "__global__ void", 'extern "C" __global__ void') ptx = compile_ptx(src, f"{problem}.cu") kern = NvrtcKernel(cuda_lib, ptx, kernel_name(problem)) ref_fn = ref_fn_for(problem) for sname, n_trials, builder in K.PROBLEMS[problem]["suites"](): for trial in range(min(n_trials, 2)): inputs = builder(trial) gpu_in = [t.cuda() for t in inputs] with torch.no_grad(): ref = ref_fn(*gpu_in) out = kern.launch(problem, gpu_in, torch) r = compare(ref, out, torch) status = "OK " if r == "survived" else "FAIL" if r != "survived": failures += 1 diff = (ref - out).abs().nan_to_num(nan=float("inf")).max() print(f"[validate] {status} {problem}/{sname} t{trial}: {r} " f"max_diff={float(diff):.4g}", flush=True) else: print(f"[validate] {status} {problem}/{sname} t{trial}", flush=True) del gpu_in, ref, out torch.cuda.empty_cache() kern.unload() print(f"[validate] failures: {failures}", flush=True) sys.exit(1 if failures else 0) if __name__ == "__main__": main()