openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / oal / entrants / triton.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | """Triton entrant host: python -m oal.entrants.triton WORKLOAD < case > outputs. |
| 3 | |
| 4 | Imports workloads/<name>/<backends.triton.src> and calls its launch(p, t): p maps parameter names to |
| 5 | ints, t maps every input, output and scratch name to a CPU torch tensor. In CI TRITON_INTERPRET=1 is |
| 6 | set, so the kernel runs in Triton's numpy interpreter: a correctness entrant, never a GPU timing. |
| 7 | Output tensors start as 0xFF bytes (NaN for f32). Exit 3 on an exception in launch. |
| 8 | |
| 9 | INTERIM host glue (Python + torch). Successor: none planned; Triton is a foreign entrant and its |
| 10 | host is part of what is measured. |
| 11 | """ |
| 12 | import importlib.util |
| 13 | import os |
| 14 | import sys |
| 15 | import time |
| 16 | import traceback |
| 17 | |
| 18 | import numpy as np |
| 19 | |
| 20 | from oal import case, manifest |
| 21 | |
| 22 | TORCH_DT = {"f32": "float32", "f64": "float64", "i32": "int32", "i8": "int8", "u8": "uint8"} |
| 23 | |
| 24 | |
| 25 | def main(): |
| 26 | import torch |
| 27 | import triton |
| 28 | name = sys.argv[1] |
| 29 | m = manifest.load(name) |
| 30 | spec = m["backends"]["triton"] |
| 31 | blob = sys.stdin.buffer.read() |
| 32 | pvals, sizes, ins, warm, reps = case.unpack(blob) |
| 33 | params = dict(zip(manifest.params_of(m).keys(), pvals)) |
| 34 | sys.stderr.write("oal-version triton %s torch %s interpret=%s\n" % ( |
| 35 | triton.__version__, torch.__version__, os.environ.get("TRITON_INTERPRET", "0"))) |
| 36 | path = os.path.join(m["_dir"], spec["src"]) |
| 37 | mod_spec = importlib.util.spec_from_file_location("oal_triton_" + name, path) |
| 38 | mod = importlib.util.module_from_spec(mod_spec) |
| 39 | mod_spec.loader.exec_module(mod) |
| 40 | t = {} |
| 41 | for k, s in enumerate(m["inputs"]): |
| 42 | arr = np.frombuffer(ins[k], dtype=manifest.DTYPES[s["dtype"]][0]).reshape(manifest.shape(s, params)).copy() |
| 43 | t[s["name"]] = torch.from_numpy(arr) |
| 44 | for s in m["outputs"]: |
| 45 | dims = manifest.shape(s, params) |
| 46 | raw = np.full(manifest.nbytes(s, params), 0xFF, np.uint8) |
| 47 | t[s["name"]] = torch.from_numpy(raw.view(manifest.DTYPES[s["dtype"]][0]).reshape(dims).copy()) |
| 48 | for s in m.get("scratch", []): |
| 49 | t[s["name"]] = torch.zeros(manifest.shape(s, params), dtype=getattr(torch, TORCH_DT[s["dtype"]])) |
| 50 | try: |
| 51 | for r in range(warm + reps): |
| 52 | t0 = time.perf_counter_ns() |
| 53 | mod.launch(params, t) |
| 54 | t1 = time.perf_counter_ns() |
| 55 | if r >= warm: |
| 56 | sys.stderr.write("oal-time-ns %d\n" % (t1 - t0)) |
| 57 | except Exception: |
| 58 | traceback.print_exc(file=sys.stderr) |
| 59 | sys.exit(3) |
| 60 | w = sys.stdout.buffer |
| 61 | for s in m["outputs"]: |
| 62 | raw = t[s["name"]].contiguous().numpy().tobytes() |
| 63 | w.write(raw) |
| 64 | w.write(b"\0" * (case.pad8(len(raw)) - len(raw))) |
| 65 | w.flush() |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |