diff --git a/docs/dev/HW_ORACLE.md b/docs/dev/HW_ORACLE.md index 917efdc65..464033fa2 100644 --- a/docs/dev/HW_ORACLE.md +++ b/docs/dev/HW_ORACLE.md @@ -30,8 +30,20 @@ from the GPU (`HW_ORACLE_TARGET` overrides it). - Bytes passed as `extra` follow the rows of each dispatch, at `s[4:5] + 16 * rows`: buffer contents, texels, etc. Build buffer and image descriptors in SGPRs from that address. -The FLOAT_MODE of the dispatch is set with `ieee` (0/1), `denorm32` and `denorm16` (0-3, 3 = denormals kept). Float -atomics need `coarse`: on fine-grained system memory they do nothing. +Every run must explicitly set all floating-point controls, including runs of integer instructions. The CLI flags +use hyphens; Python keywords use underscores. Record these settings alongside the GPU and measured results. + +| Python keyword | Values | +| --- | --- | +| `ieee` | 0 or 1: IEEE mode disabled or enabled | +| `dx10_clamp` | 0 or 1: DX10 clamp disabled or enabled | +| `denorm32`, `denorm16` | 0: flush input/output; 1: preserve input, flush output; 2: flush input, preserve output; 3: preserve both | +| `round32`, `round16` | 0: nearest even; 1: toward +infinity; 2: toward -infinity; 3: toward zero | +| `fp16_overflow` | 0: overflow to infinity; 1: clamp computed overflow to the largest finite value (infinite inputs and division by zero still produce infinity) | + +`denorm16` and `round16` control both f16 and f64. These values follow the +[LLVM AMDGPU kernel descriptor documentation](https://llvm.org/docs/AMDGPUUsage.html#amdhsa-kernel-descriptor). +Float atomics need `coarse`: on fine-grained system memory they do nothing. ## Use @@ -40,7 +52,9 @@ cat > body.s <<'EOF' v_add_f16 v10, v4, v5 EOF printf '0x3c00 0x3c00 0 0\n0x7e01 0x3c00 0 0\n' > rows.txt -python3 tools/hw-oracle/hw_oracle.py body.s rows.txt --outs 1 --ieee 0 +python3 tools/hw-oracle/hw_oracle.py body.s rows.txt --outs 1 \ + --ieee 0 --dx10-clamp 1 --denorm32 0 --denorm16 3 \ + --round32 0 --round16 0 --fp16-overflow 0 ``` prints one line per row with the output dwords in hex. From Python: @@ -51,7 +65,8 @@ sys.path.insert(0, "tools/hw-oracle") from hw_oracle import run rows = [(a, b, 0, 0) for a in values for b in values] -results = run(body, rows, wave64=True, denorm32=0) +results = run(body, rows, wave64=True, ieee=0, dx10_clamp=1, + denorm32=0, denorm16=3, round32=0, round16=0, fp16_overflow=0) ``` Usual workflow: run the instruction on rows that separate the candidate models (edge values, NaN payloads, rounding @@ -62,3 +77,9 @@ ties), write a model that matches every row, implement it, and put measured rows Only compute kernels. Pixel-shader ops that read their inputs from LDS also work: `v_interp_*` reads the attribute parameters from LDS at `M0`, so writing them there by hand is enough. Behaviour that depends on the graphics pipeline (parameter cache, exports, rasterization, `SPI_*` state) can't be measured with it. + +Argument handling can be tested without a GPU: + +```sh +python3 tools/hw-oracle/test_hw_oracle.py +``` diff --git a/tools/hw-oracle/hw_oracle.py b/tools/hw-oracle/hw_oracle.py index eee171f50..3b4407407 100644 --- a/tools/hw-oracle/hw_oracle.py +++ b/tools/hw-oracle/hw_oracle.py @@ -50,10 +50,18 @@ def target(): return os.environ.get("HW_ORACLE_TARGET") or subprocess.run([oracle(), "--target"], capture_output=True, text=True, check=True).stdout.strip() -def assemble(body, work, wave64, ieee, denorm32, denorm16): +def assemble(body, work, wave64, *, ieee, denorm32, denorm16, dx10_clamp, round32, round16, fp16_overflow): + modes = (("ieee", ieee, 2), ("denorm32", denorm32, 4), ("denorm16", denorm16, 4), + ("dx10_clamp", dx10_clamp, 2), ("round32", round32, 4), ("round16", round16, 4), + ("fp16_overflow", fp16_overflow, 2)) + for name, value, limit in modes: + if not isinstance(value, int) or value not in range(limit): + raise ValueError(f"{name} must be an integer from 0 to {limit - 1}") text = (HERE / "template.s").read_text() for key, value in (("@TARGET@", target()), ("@WAVE32@", "0" if wave64 else "1"), ("@WAVESIZE@", "64" if wave64 else "32"), - ("@DENORM@", str(denorm32)), ("@DENORM16@", str(denorm16)), ("@IEEE@", str(ieee)), ("@BODY@", body)): + ("@DENORM@", str(int(denorm32))), ("@DENORM16@", str(int(denorm16))), ("@IEEE@", str(int(ieee))), + ("@DX10_CLAMP@", str(int(dx10_clamp))), ("@ROUND32@", str(int(round32))), ("@ROUND16@", str(int(round16))), + ("@FP16_OVERFLOW@", str(int(fp16_overflow))), ("@BODY@", body)): text = text.replace(key, value) (work / "k.s").write_text(text) subprocess.run([tool("clang"), "-x", "assembler", "-target", "amdgcn-amd-amdhsa", f"-mcpu={target()}", "-c", str(work / "k.s"), "-o", str(work / "k.o")], check=True) @@ -61,7 +69,7 @@ def assemble(body, work, wave64, ieee, denorm32, denorm16): return work / "k.co" -def run(body, rows, extra=b"", wave64=False, ieee=1, denorm32=3, denorm16=3, coarse=False): +def run(body, rows, extra=b"", wave64=False, coarse=False, *, ieee, denorm32, denorm16, dx10_clamp, round32, round16, fp16_overflow): rows = [tuple(r) for r in rows] wave = 64 if wave64 else 32 padded = rows + [(0, 0, 0, 0)] * (-len(rows) % wave) @@ -72,7 +80,8 @@ def run(body, rows, extra=b"", wave64=False, ieee=1, denorm32=3, denorm16=3, coa out = [] with tempfile.TemporaryDirectory() as tmp: work = Path(tmp) - code = assemble(body, work, wave64, ieee, denorm32, denorm16) + code = assemble(body, work, wave64, ieee=ieee, denorm32=denorm32, denorm16=denorm16, + dx10_clamp=dx10_clamp, round32=round32, round16=round16, fp16_overflow=fp16_overflow) for start in range(0, len(padded), ROWS_PER_DISPATCH): chunk = padded[start:start + ROWS_PER_DISPATCH] (work / "in.bin").write_bytes(b"".join(struct.pack("<4I", *r) for r in chunk) + extra) @@ -87,9 +96,13 @@ def main(): parser.add_argument("body", type=Path, help="assembly inserted into template.s") parser.add_argument("rows", type=Path, help="one row per line: 4 u32 values (decimal or 0x hex)") parser.add_argument("--wave64", action="store_true") - parser.add_argument("--ieee", type=int, choices=(0, 1), default=1) - parser.add_argument("--denorm32", type=int, choices=range(4), default=3) - parser.add_argument("--denorm16", type=int, choices=range(4), default=3) + parser.add_argument("--ieee", type=int, choices=(0, 1), required=True) + parser.add_argument("--denorm32", type=int, choices=range(4), required=True) + parser.add_argument("--denorm16", type=int, choices=range(4), required=True) + parser.add_argument("--dx10-clamp", type=int, choices=(0, 1), required=True) + parser.add_argument("--round32", type=int, choices=range(4), required=True) + parser.add_argument("--round16", type=int, choices=range(4), required=True) + parser.add_argument("--fp16-overflow", type=int, choices=(0, 1), required=True) parser.add_argument("--coarse", action="store_true", help="input and output in coarse-grained GPU memory") parser.add_argument("--extra", type=Path, help="bytes appended after the rows") parser.add_argument("--outs", type=int, choices=range(1, 17), default=16, metavar="N", help="print v10..v(10+N-1)") @@ -98,7 +111,10 @@ def main(): if any(len(r) != 4 for r in rows): sys.exit("every row needs 4 values") extra = args.extra.read_bytes() if args.extra else b"" - for result in run(args.body.read_text(), rows, extra, args.wave64, args.ieee, args.denorm32, args.denorm16, args.coarse): + for result in run(args.body.read_text(), rows, extra, args.wave64, args.coarse, + ieee=args.ieee, denorm32=args.denorm32, denorm16=args.denorm16, + dx10_clamp=args.dx10_clamp, round32=args.round32, round16=args.round16, + fp16_overflow=args.fp16_overflow): print(" ".join(f"{v:08x}" for v in result[:args.outs])) diff --git a/tools/hw-oracle/template.s b/tools/hw-oracle/template.s index ad5426247..915409b61 100644 --- a/tools/hw-oracle/template.s +++ b/tools/hw-oracle/template.s @@ -47,6 +47,10 @@ probe: .amdhsa_float_denorm_mode_32 @DENORM@ .amdhsa_float_denorm_mode_16_64 @DENORM16@ .amdhsa_ieee_mode @IEEE@ + .amdhsa_dx10_clamp @DX10_CLAMP@ + .amdhsa_float_round_mode_32 @ROUND32@ + .amdhsa_float_round_mode_16_64 @ROUND16@ + .amdhsa_fp16_overflow @FP16_OVERFLOW@ .end_amdhsa_kernel .amdgpu_metadata diff --git a/tools/hw-oracle/test_hw_oracle.py b/tools/hw-oracle/test_hw_oracle.py new file mode 100644 index 000000000..b0625f61b --- /dev/null +++ b/tools/hw-oracle/test_hw_oracle.py @@ -0,0 +1,66 @@ +import contextlib +import io +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +import hw_oracle + + +MODES = dict(ieee=0, denorm32=0, denorm16=3, dx10_clamp=1, round32=0, round16=0, fp16_overflow=0) + + +class FloatModeTests(unittest.TestCase): + def test_python_requires_each_mode(self): + for name in MODES: + with self.subTest(name=name): + modes = {key: value for key, value in MODES.items() if key != name} + with self.assertRaises(TypeError), patch.object(hw_oracle, "assemble") as assemble: + hw_oracle.run("s_nop 0", [], **modes) + assemble.assert_not_called() + + def test_invalid_modes_fail_before_tool_lookup(self): + with tempfile.TemporaryDirectory() as tmp, patch.object(hw_oracle, "target") as target: + for name in MODES: + limit = 4 if name in ("denorm32", "denorm16", "round32", "round16") else 2 + for value in (-1, limit, 0.5, "0"): + with self.subTest(name=name, value=value): + with self.assertRaisesRegex(ValueError, name): + hw_oracle.assemble("s_nop 0", Path(tmp), False, **(MODES | {name: value})) + target.assert_not_called() + + def test_cli_requires_each_mode(self): + flags = {name: ["--" + name.replace("_", "-"), str(value)] for name, value in MODES.items()} + for name in MODES: + argv = ["hw_oracle.py", "missing.s", "missing.txt"] + argv += [item for key, flag in flags.items() if key != name for item in flag] + with self.subTest(name=name), patch("sys.argv", argv), contextlib.redirect_stderr(io.StringIO()) as errors: + with self.assertRaises(SystemExit) as error: + hw_oracle.main() + self.assertEqual(error.exception.code, 2) + self.assertIn(flags[name][0], errors.getvalue()) + + def test_cli_forwards_explicit_modes(self): + with tempfile.TemporaryDirectory() as tmp: + body = Path(tmp) / "body.s" + rows = Path(tmp) / "rows.txt" + body.write_text("s_nop 0") + rows.write_text("1 2 3 4\n") + modes = dict(ieee=1, denorm32=2, denorm16=1, dx10_clamp=0, round32=3, round16=2, fp16_overflow=1) + argv = ["hw_oracle.py", str(body), str(rows), "--wave64", "--coarse"] + argv += [item for name, value in modes.items() for item in ["--" + name.replace("_", "-"), str(value)]] + with patch("sys.argv", argv), patch.object(hw_oracle, "run", return_value=[]) as run: + hw_oracle.main() + run.assert_called_once_with("s_nop 0", [(1, 2, 3, 4)], b"", True, True, **modes) + + def test_run_forwards_modes_to_assembler(self): + with patch.object(hw_oracle, "assemble") as assemble: + hw_oracle.run("s_nop 0", [], wave64=True, **MODES) + self.assertEqual(assemble.call_args.args[0], "s_nop 0") + self.assertTrue(assemble.call_args.args[2]) + self.assertEqual(assemble.call_args.kwargs, MODES) + + +if __name__ == "__main__": + unittest.main()