#!/usr/bin/env python3 """ptxas wrapper: rewrite a too-new PTX .version directive, then call the real ptxas. The installed CUDA 12.9 toolkit's ptxas accepts at most PTX 8.7 while llvmlite emits 8.8 for the kernels this project uses. Those kernels use no 8.8-only feature, so the directive alone is downgraded. If the PTX genuinely used a newer ISA this would fail inside ptxas, loudly, rather than produce a wrong result -- which is the reason this shim is acceptable for a measurement. It forwards all arguments and the exit status, and passes through byte-for-byte when the input already declares <= 8.7 (or is not PTX). """ import os import subprocess import sys import shutil VERSIONS = (".version 8.8", b".version 8.8") def real_ptxas(): here = os.path.dirname(os.path.abspath(__file__)) cand = os.path.join(here, "..", "..", "..", "Lib", "site-packages", "nvidia", "cuda_nvcc", "bin", "ptxas.exe") cand = os.path.abspath(cand) if os.path.exists(cand) and os.path.abspath(__file__) != cand: return cand # fall back to the first ptxas on PATH that is not this script for p in os.environ.get("PATH", "").split(os.pathsep): c = os.path.join(p, "ptxas.exe" if os.name == "nt" else "ptxas") if os.path.exists(c) and os.path.abspath(c) != os.path.abspath(__file__): return c return shutil.which("ptxas") def main(): args = sys.argv[1:] inp = None for a in args: if not a.startswith("-") and os.path.exists(a) and a.lower().endswith(".ptx"): inp = a break if inp: with open(inp, "rb") as f: data = f.read() patched = data for v in VERSIONS: if v in patched: patched = patched.replace(v, v.replace("8.8", "8.7")) if patched != data: tmp = inp + ".v87.ptx" with open(tmp, "wb") as f: f.write(patched) args = [tmp if a == inp else a for a in args] exe = real_ptxas() if not exe: sys.stderr.write("ptxas shim: real ptxas not found\n") return 127 return subprocess.call([exe] + args) if __name__ == "__main__": sys.exit(main())