"""tools/d16_image_probe.py: the textures and samplers, the kernels (they assemble for gfx1031), the comparison and its exit codes through a fake oracle that plays the GPU, and the exit-77 contract. The last class runs on the hardware when the oracle tools are present and the GPU is GFX10, and is skipped otherwise. Standard library only.""" import io import json import os from contextlib import redirect_stderr, redirect_stdout from pathlib import Path import re import struct import subprocess import sys import tempfile import unittest from unittest import mock TOOLS = Path(__file__).resolve().parents[1] / "tools" sys.path.insert(0, str(TOOLS)) import d16_format_probe as fp # noqa: E402 import d16_image_probe as ip # noqa: E402 from hw_oracle import asm, runner # noqa: E402 class Textures(unittest.TestCase): def test_self_test(self): with redirect_stdout(io.StringIO()): self.assertEqual(ip.self_test(), 0) def test_every_value_of_a_16_bit_format_is_one_texel(self): seen = [v for b in range(ip.blocks("unorm16")) for v in ip.block_values("unorm16", b)] self.assertEqual(seen, list(range(65536))) def test_the_float32_set_starts_with_the_two_unorm_halfway_quotients(self): v, ties = ip.float32_texels() self.assertEqual([fp.f32_bits(fp.to_f32(fp.Fraction(65519, 65535))), fp.f32_bits(fp.to_f32(fp.Fraction(65455, 65535)))], [v[0], v[2]]) self.assertTrue(all((b & 0x1FFF) == 0x1000 for b in v[:ties]), "the ties come first") self.assertEqual(len(v), ip.TEXELS) def test_t_sharp_carries_the_format(self): for fmt, (code, _, _, width) in ip.FORMATS.items(): w = ip.t_words("gfx1031", fmt) self.assertEqual((w[1] >> 20) & 0x1FF, code, fmt) self.assertEqual(w[2] >> 31, 1) # RESOURCE_LEVEL on GFX10 self.assertEqual(((w[2] & 0xFFF) << 2 | w[1] >> 30) + 1, width) class Kernels(unittest.TestCase): def test_kernels_assemble_for_rdna2(self): try: clang = runner.find_clang() except runner.Unavailable as e: self.skipTest(str(e)) for wave in (32, 64): kernels = [asm.Kernel(ip.kernel_lines("gfx1031", f"{f}_0", f, ip.block_values(f, 0), 0.5), wave, ip.TEXELS, ip.TEXELS * ip.WORDS_PER_LANE, raw=True, vgprs=24, sgprs=32) for f in ip.FORMATS] text, _, _ = asm.module_text(kernels, "gfx1031", wave=wave) with tempfile.TemporaryDirectory() as d: s = Path(d) / "k.s" s.write_text(text, encoding="utf-8") r = subprocess.run([clang, "--target=amdgcn-amd-amdhsa", "-mcpu=gfx1031", *(["-mwavefrontsize64"] if wave == 64 else []), "-c", str(s), "-o", str(Path(d) / "k.o")], capture_output=True, text=True) self.assertEqual(r.returncode, 0, r.stderr[:800]) self.assertIn("image_sample_lz v13, v[16:17], s[8:15], s[20:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm d16", text) def _val(kind, n, v, rule): return fp.models16(kind, n, v)[rule] class FakeOracle: """Plays the GPU from each kernel's label (.Lpc_) and its texture: d16 results by `rule`, f32 loads correctly rounded, bilinear f32 the rounded exact mix and its d16 rounded from that f32 to nearest even.""" arch = "gfx1031" device = "fake gfx1031" def __init__(self, rule="rte", f32_off=False, wave64_flip=False, tie="even"): self.rule, self.f32_off, self.wave64_flip, self.tie = rule, f32_off, wave64_flip, tie def run(self, kernels, words=4): out = [] for k in kernels: fmt, block = re.match(r"\.Lpc(\w+?)_(\d+):", k.lines[1]).groups() _, kind, n, width = ip.FORMATS[fmt] vals = ip.block_values(fmt, int(block)) frac = struct.unpack("