diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-21 21:49:00 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-21 22:52:35 +0100 |
| commit | cdaef79a7d9cdc5179fb21c38ff518c1ab0b31a4 (patch) | |
| tree | cb8e0272a5cdac811cdafd3bc2c37015ba7e038e /scripts/bench_wave64.py | |
| parent | 4e390e0871d7265e629b51aca8ff99a15a6a6845 (diff) | |
Wave64 emulation: fewer rounds (reuse mask exchanges, chain voted branches), benchmark
A whole-mask read of a register value already exchanged in the block reuses
the exchange; a block ending in a voted branch runs its successor in the
round that reads the vote. Adds scripts/bench_wave64.py, cs_bench_mix,
VKC_BENCH timing in vk_compute_test and the cs_w64x_mask_reuse test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'scripts/bench_wave64.py')
| -rw-r--r-- | scripts/bench_wave64.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/scripts/bench_wave64.py b/scripts/bench_wave64.py new file mode 100644 index 000000000..ddb1799a3 --- /dev/null +++ b/scripts/bench_wave64.py | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | """Time a wave64 compute shader (runtime/test/shaders/cs_bench_mix.asm) native and emulated, on the AMD and NVIDIA GPU. | ||
| 2 | |||
| 3 | python -I scripts/bench_wave64.py [--build DIR] [--llvm-mc PATH] [--groups N] [--reps N] [--procs N] [--shader NAME] | ||
| 4 | |||
| 5 | Three configurations: AMD with its native 64-wide subgroup (no flag), AMD with --wave64-emulate, NVIDIA with | ||
| 6 | --wave64-emulate (the GPU is chosen with VK_LOADER_DRIVERS_SELECT='*amd*' / '*nv*'). Each runs `procs` processes of | ||
| 7 | vk_compute_test with VKC_BENCH=`reps` timed dispatches of `groups` workgroups; the table gives the median over all | ||
| 8 | dispatches, the spread of the per-process medians (other programs share the GPU, so look at it), the number of | ||
| 9 | translated segments (static: blocks + continuation segments; every continuation segment is one more workgroup barrier | ||
| 10 | on the path through it) and whether the output bytes are the same in every configuration (they must be). | ||
| 11 | """ | ||
| 12 | import argparse | ||
| 13 | import json | ||
| 14 | import os | ||
| 15 | import re | ||
| 16 | import statistics | ||
| 17 | import subprocess | ||
| 18 | import sys | ||
| 19 | import tempfile | ||
| 20 | |||
| 21 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 22 | BUILD_PY = os.path.join(ROOT, "runtime", "test", "shaders", "build.py") | ||
| 23 | CONFIGS = [("AMD native", "*amd*", False), ("AMD --wave64-emulate", "*amd*", True), | ||
| 24 | ("NVIDIA --wave64-emulate", "*nv*", True)] | ||
| 25 | |||
| 26 | |||
| 27 | def build(work, name, emulate, llvm_mc): | ||
| 28 | out = os.path.join(work, "emu" if emulate else "native") | ||
| 29 | env = dict(os.environ, LLVM_MC=llvm_mc) | ||
| 30 | env.pop("LEONIDA_W64_EMULATE", None) | ||
| 31 | if emulate: | ||
| 32 | env["LEONIDA_W64_EMULATE"] = "1" | ||
| 33 | r = subprocess.run([sys.executable, "-I", BUILD_PY, out, name], env=env, capture_output=True, text=True) | ||
| 34 | if r.returncode: | ||
| 35 | sys.exit(r.stdout + r.stderr) | ||
| 36 | return os.path.join(out, "spirv", name + ".spv") | ||
| 37 | |||
| 38 | |||
| 39 | def segments(spv): | ||
| 40 | """(segments, continuation segments) of an emulated module: the cases of the scheduler's OpSwitch.""" | ||
| 41 | text = open(spv[:-4] + ".spvasm", encoding="utf-8").read() | ||
| 42 | m = re.search(r"OpSwitch %pcv %sw_default ([^\n]+)", text) | ||
| 43 | if not m: | ||
| 44 | return None | ||
| 45 | cases = [int(x) for x in re.findall(r"(\d+) %blk\d+", m.group(1))] | ||
| 46 | blocks = next((i for i, c in enumerate(cases) if c != i), len(cases)) # blocks 0..n-1, then "done" = n, then segments | ||
| 47 | return len(cases), len(cases) - blocks | ||
| 48 | |||
| 49 | |||
| 50 | def run(tools, spv, groups, reps, driver, emulate): | ||
| 51 | env = dict(os.environ, VK_LOADER_DRIVERS_SELECT=driver, VKC_BENCH=str(reps)) | ||
| 52 | args = [os.path.join(tools, "vk_compute_test" + (".exe" if os.name == "nt" else "")), spv, str(groups), "dump"] | ||
| 53 | if not emulate: | ||
| 54 | args.append("--require-wave64") | ||
| 55 | r = subprocess.run(args, env=env, capture_output=True, text=True) | ||
| 56 | out = r.stdout + r.stderr | ||
| 57 | m = re.search(r"bench ms: median=([\d.]+) min=([\d.]+) max=([\d.]+)", out) | ||
| 58 | h = re.search(r"bench out fnv1a: (\w+)", out) | ||
| 59 | dev = re.search(r"device: (.*) \(subgroup", out) | ||
| 60 | if r.returncode or not m or "0 mismatches" not in out: | ||
| 61 | return None, out.strip().splitlines()[-1:] or ["no output"], None | ||
| 62 | return (float(m.group(1)), float(m.group(2))), h.group(1), dev.group(1) if dev else "?" | ||
| 63 | |||
| 64 | |||
| 65 | def main(): | ||
| 66 | ap = argparse.ArgumentParser() | ||
| 67 | ap.add_argument("--build", default=os.path.join(ROOT, "runtime", "build")) | ||
| 68 | ap.add_argument("--llvm-mc", default=os.environ.get("LLVM_MC", "llvm-mc")) | ||
| 69 | ap.add_argument("--groups", type=int, default=65536) | ||
| 70 | ap.add_argument("--reps", type=int, default=30) | ||
| 71 | ap.add_argument("--procs", type=int, default=5) | ||
| 72 | ap.add_argument("--shader", default="cs_bench_mix") | ||
| 73 | a = ap.parse_args() | ||
| 74 | with tempfile.TemporaryDirectory(prefix="bench-w64-") as work: | ||
| 75 | spv = {False: build(work, a.shader, False, a.llvm_mc), True: build(work, a.shader, True, a.llvm_mc)} | ||
| 76 | seg = segments(spv[True]) | ||
| 77 | print(f"{a.shader}: {a.groups} workgroups of the shader's group size, {a.procs} x {a.reps} timed dispatches") | ||
| 78 | if seg: | ||
| 79 | print(f"emulated: {seg[0]} segments, {seg[1]} of them continuation segments (one extra round each)") | ||
| 80 | hashes, rows = set(), [] | ||
| 81 | for name, driver, emulate in CONFIGS: | ||
| 82 | med, mins, dev = [], [], None | ||
| 83 | for _ in range(a.procs): | ||
| 84 | t, h, dev = run(a.build, spv[emulate], a.groups, a.reps, driver, emulate) | ||
| 85 | if t is None: | ||
| 86 | med = None | ||
| 87 | print(f"{name}: failed or unsupported: {h}") | ||
| 88 | break | ||
| 89 | med.append(t[0]) | ||
| 90 | mins.append(t[1]) | ||
| 91 | hashes.add(h) | ||
| 92 | if med: | ||
| 93 | rows.append((name, dev, statistics.median(med), min(mins), min(med), max(med))) | ||
| 94 | for name, dev, m, lo, a_, b_ in rows: | ||
| 95 | print(f"{name:26} {dev:34} median {m:8.3f} ms best {lo:8.3f} process medians {a_:.3f}..{b_:.3f}") | ||
| 96 | print("output identical in every configuration" if len(hashes) == 1 else f"OUTPUT DIFFERS: {sorted(hashes)}") | ||
| 97 | return 0 if len(hashes) <= 1 else 1 | ||
| 98 | |||
| 99 | |||
| 100 | if __name__ == "__main__": | ||
| 101 | sys.exit(main()) | ||