"""Unit tests for tools/vinterp_hw_probe.py (SHADER-430). Standard library only; no GPU or game files needed.""" from contextlib import redirect_stderr, redirect_stdout from fractions import Fraction import io import sys import unittest import unittest.mock from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TOOLS = ROOT / "tools" sys.dont_write_bytecode = True if str(TOOLS) not in sys.path: sys.path.insert(0, str(TOOLS)) import vinterp_hw_probe as probe # noqa: E402 from hw_oracle import runner as hw_runner # noqa: E402 class VinterpHwProbeTests(unittest.TestCase): def test_lds_param_offset_layout(self): # Confirmed on AMD Radeon 680M (gfx1035): 48 bytes per attr, P0/P10 at 8*comp + {0, 4}, P20 at 32 + 4*comp. self.assertEqual(probe.lds_param_offset(0, 0, "p0"), 0) self.assertEqual(probe.lds_param_offset(0, 0, "p10"), 4) self.assertEqual(probe.lds_param_offset(0, 0, "p20"), 32) self.assertEqual(probe.lds_param_offset(0, 1, "p0"), 8) self.assertEqual(probe.lds_param_offset(0, 1, "p10"), 12) self.assertEqual(probe.lds_param_offset(0, 1, "p20"), 36) self.assertEqual(probe.lds_param_offset(0, 2, "p20"), 40) self.assertEqual(probe.lds_param_offset(0, 3, "p20"), 44) self.assertEqual(probe.lds_param_offset(1, 0, "p0"), 48) self.assertEqual(probe.lds_param_offset(1, 0, "p20"), 80) with self.assertRaises(ValueError): probe.lds_param_offset(0, 4, "p0") with self.assertRaises(ValueError): probe.lds_param_offset(0, 0, "p30") def test_hand_derivation_of_mixed_f16_and_f32_channel(self): # Analytical derivation of CASE_MIXED_F16_F32 matching RDNA2 gfx1035 hardware words: # P0 = 0xc0003e00 (f16 lo = 1.5, hi = -2.0), P10 = 0x49003c00 (f16 lo = 1.0, hi = 10.0), # P20 = 0x4c804000 (f16 lo = 2.0, hi = 18.0), i = 0.25, j = 0.50. i = Fraction(1, 4) j = Fraction(1, 2) f16_lo = probe.hbits_to_fraction(0x3E00) + i * probe.hbits_to_fraction(0x3C00) + j * probe.hbits_to_fraction(0x4000) f16_hi = probe.hbits_to_fraction(0xC000) + i * probe.hbits_to_fraction(0x4900) + j * probe.hbits_to_fraction(0x4C80) self.assertEqual(f16_lo, Fraction(11, 4)) # 2.75 self.assertEqual(f16_hi, Fraction(19, 2)) # 9.5 self.assertEqual(probe.SENTINEL_HI | probe.fraction_to_f16_rne(f16_lo), probe.CASE_MIXED_F16_F32.expected_hw[0]) self.assertEqual(probe.SENTINEL_HI | probe.fraction_to_f16_rne(f16_hi), probe.CASE_MIXED_F16_F32.expected_hw[1]) self.assertEqual(0xC0003E00, probe.CASE_MIXED_F16_F32.expected_hw[2]) p0_f32 = probe.fbits_to_fraction(0xC0003E00) p10_f32 = probe.fbits_to_fraction(0x49003C00) p20_f32 = probe.fbits_to_fraction(0x4C804000) partial = probe.round_rne_fraction(p0_f32 + i * p10_f32, 24, -126) full = probe.fraction_to_f32_rne(partial + j * p20_f32) self.assertEqual(full, probe.CASE_MIXED_F16_F32.expected_hw[3]) def test_single_vs_double_rounding_ties(self): # Verify the 4 near-tie cases in CASE_SINGLE_ROUND_TIES separate single rounding from f32-then-f16 rounding. # Tie A: 1 + 2^-11 + (-2^-40)*(-1) = 1 + 2^-11 + 2^-40 exact_a = Fraction(1) + Fraction(1, 1 << 11) + Fraction(1, 1 << 40) self.assertEqual(probe.fraction_to_f16_rne(exact_a), 0x3C01) self.assertEqual(probe.fraction_to_f16_rne(probe.round_rne_fraction(exact_a, 24, -126)), 0x3C00) # Tie B: 1 + 3*2^-11 + (2^-30)*(-1) = 1 + 3*2^-11 - 2^-30 exact_b = Fraction(1) + Fraction(3, 1 << 11) - Fraction(1, 1 << 30) self.assertEqual(probe.fraction_to_f16_rne(exact_b), 0x3C01) self.assertEqual(probe.fraction_to_f16_rne(probe.round_rne_fraction(exact_b, 24, -126)), 0x3C02) # Tie E (overflow): 65504 + 16 - 2^-20 = 65520 - 2^-20 exact_e = Fraction(65520) - Fraction(1, 1 << 20) self.assertEqual(probe.fraction_to_f16_rne(exact_e), 0x7BFF) self.assertEqual(probe.fraction_to_f16_rne(probe.round_rne_fraction(exact_e, 24, -126)), 0x7C00) # Tie D (subnormal): 2^-25 + 2^-60 exact_d = Fraction(1, 1 << 25) + Fraction(1, 1 << 60) self.assertEqual(probe.fraction_to_f16_rne(exact_d), 0x0001) self.assertEqual(probe.fraction_to_f16_rne(probe.round_rne_fraction(exact_d, 24, -126)), 0x0000) def test_translator_cli_and_mocked_hw_oracle(self): out = io.StringIO() with redirect_stdout(out): rc = probe.main(["--oracle", "translator"]) self.assertEqual(rc, 0) self.assertIn("translator checks: ok", out.getvalue()) # Mocked hardware oracle returning exact expected words in wave32 and wave64 per_wave = { 32: {c.name: c.expected_hw for c in probe.CASES}, 64: {c.name: c.expected_hw for c in probe.CASES}, } out_hw = io.StringIO() with unittest.mock.patch.object(probe, "run_hw", return_value=("mock-gfx1035", "gfx1035", per_wave)): with redirect_stdout(out_hw): rc_hw = probe.main(["--oracle", "both"]) self.assertEqual(rc_hw, 0) self.assertIn("mixed_f16_and_f32_same_channel", out_hw.getvalue()) # Mismatch on wave64 returns exit code 1 bad_wave = { 32: {c.name: c.expected_hw for c in probe.CASES}, 64: {c.name: (0, 0, 0, 0) for c in probe.CASES}, } out_bad = io.StringIO() with unittest.mock.patch.object(probe, "run_hw", return_value=("mock-gfx1035", "gfx1035", bad_wave)): with redirect_stdout(out_bad): rc_bad = probe.main(["--oracle", "hw"]) self.assertEqual(rc_bad, 1) self.assertIn("MISMATCH", out_bad.getvalue()) def test_hw_unavailable_returns_77(self): out = io.StringIO() with unittest.mock.patch.object(hw_runner, "open_oracle", side_effect=hw_runner.Unavailable("no AMD GPU")): with redirect_stdout(out): rc = probe.main(["--oracle", "hw"]) self.assertEqual(rc, 77) self.assertIn("no AMD GPU", out.getvalue()) def test_empty_hw_arch_rejected_and_translator_error_exits_2(self): for argv in (["--hw-arch", ""], ["--hw-arch", " "]): with self.subTest(argv=argv), redirect_stderr(io.StringIO()): with self.assertRaises(SystemExit) as cm: probe.main(argv) self.assertEqual(cm.exception.code, 2) out = io.StringIO() with unittest.mock.patch.object( probe, "run_translator_checks", side_effect=probe.rdna2spirv.Unsupported("unsupported op") ), redirect_stdout(out): rc = probe.main(["--oracle", "translator"]) self.assertEqual(rc, 2) self.assertIn("harness error: unsupported op", out.getvalue()) def test_run_hw_tuple_cu0_selection_malformed_batches_and_errors(self): class StubOracle: device = "mock-gfx1035" arch = "gfx1035" def __init__(self, run_fn): self._run_fn = run_fn self.closed = False def run(self, kernels, words=5): return self._run_fn(kernels) def close(self): self.closed = True # Tuple entries are accepted and CU0 (HW_ID1 bit 8 == 0) is preferred over CU1 (0x100). case_idx = {"i": 0} order = list(probe.CASES) * 2 def cu0_tuple_run(_ks): c = order[case_idx["i"]] case_idx["i"] += 1 cu1 = (0, 0, 0, 0, 0x100) cu0 = c.expected_hw + (0,) return (cu1, cu0, cu1, cu1) oracle = StubOracle(cu0_tuple_run) out = io.StringIO() with unittest.mock.patch.object(hw_runner, "open_oracle", return_value=oracle), redirect_stdout(out): rc = probe.main(["--oracle", "hw"]) self.assertEqual(rc, 0, out.getvalue()) self.assertTrue(oracle.closed) # Malformed batches and all-rejected batches return exit 2 (harness error) and close oracle. for label, bad_fn in ( ("empty_batch", lambda _ks: []), ("none_batch", lambda _ks: None), ("short_batch", lambda _ks: [(0, 0, 0, 0, 0)] * 2), ("short_words", lambda _ks: [[0, 1]] * 4), ("bool_words", lambda _ks: [[True, 0, 0, 0, 0]] * 4), ("all_rejected", lambda _ks: ["assembler rejected"] * 4), ): with self.subTest(case=label): oracle = StubOracle(bad_fn) out = io.StringIO() with unittest.mock.patch.object(hw_runner, "open_oracle", return_value=oracle), redirect_stdout(out): rc = probe.main(["--oracle", "hw"]) self.assertEqual(rc, 2, out.getvalue()) self.assertIn("harness error:", out.getvalue()) self.assertTrue(oracle.closed) # RuntimeError during oracle.run() exits 2 and closes oracle. def raise_runtime(_ks): raise RuntimeError("gpu hung") oracle = StubOracle(raise_runtime) out = io.StringIO() with unittest.mock.patch.object(hw_runner, "open_oracle", return_value=oracle), redirect_stdout(out): rc = probe.main(["--oracle", "hw"]) self.assertEqual(rc, 2, out.getvalue()) self.assertIn("harness error: gpu hung", out.getvalue()) self.assertTrue(oracle.closed) # runner.Unavailable during oracle.run() exits 77 and closes oracle. def raise_unavail(_ks): raise hw_runner.Unavailable("mode unsupported") oracle = StubOracle(raise_unavail) out = io.StringIO() with unittest.mock.patch.object(hw_runner, "open_oracle", return_value=oracle), redirect_stdout(out): rc = probe.main(["--oracle", "hw"]) self.assertEqual(rc, 77, out.getvalue()) self.assertIn("hardware oracle unavailable: mode unsupported", out.getvalue()) self.assertTrue(oracle.closed) if __name__ == "__main__": unittest.main()