Files
mandy/tests/test_d3d12_ab_compare.py
TheGeogeo 3b45610f9d tests: d3d12_ab_compare's sign rule has a test of its own (D3D12-AB-COMPARE-AGREE-TEST)
"B faster" or "B slower" needs the interval to leave 0 out and all pairs but at most one to agree
in sign. No test caught removing the sign part (the verification of #2783 found it): the odd-run
test's interval already held 0. Eight pairs, two against the rest, now give no verdict, and one
against the rest still does.
2026-11-30 22:55:55 +01:00

164 lines
7.8 KiB
Python

"""tools/d3d12_ab_compare.py on synthetic numbers and synthetic logs (no game data)."""
import io
import json
import os
import sys
import tempfile
import unittest
from contextlib import redirect_stdout
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import d3d12_ab_compare as ab # noqa: E402
def items(order, values):
return [(arm, v, 4000.0, None) for arm, v in zip(order, values)]
class Pairs(unittest.TestCase):
def test_the_worked_example_is_b_faster(self):
# A B B A A B B A with A about 1.2 ms slower and a drift between runs.
r = ab.abba(items("ABBAABBA", [20.4, 19.1, 19.6, 20.9, 21.3, 19.8, 20.2, 21.0]))
e = r["result"]
self.assertEqual(r["method"], "pairs")
self.assertAlmostEqual(e["effect"], -1.225, places=3)
self.assertAlmostEqual(e["se"], 0.1493, places=3)
self.assertAlmostEqual(e["ci95"][0], -1.700, places=2)
self.assertAlmostEqual(e["ci95"][1], -0.750, places=2)
self.assertEqual(e["agree"], 4)
self.assertEqual(r["verdict"], "B faster")
def test_a_drift_alone_in_an_unbalanced_order_is_no_effect(self):
# A B A B A B A B with every run 0.25 ms slower than the one before and no effect at all: each pair would
# read B 0.25 ms slower. The pairs are not balanced, so the side and a trend are fitted instead.
r = ab.abba(items("ABABABAB", [20.0 + 0.25 * i for i in range(8)]))
self.assertNotEqual(r["method"], "pairs")
self.assertAlmostEqual(r["result"]["effect"], 0.0, places=6)
self.assertEqual(r["verdict"], "no clear difference")
def test_two_pairs_against_the_rest_give_no_verdict(self):
# Eight pairs: six with B 2 ms faster, two with B 0.1 ms slower. The interval leaves 0 out, but two pairs
# disagree in sign, one more than the rule allows: no verdict.
order = "ABBA" * 4
b = [18.0] * 6 + [20.1] * 2
values = []
for pair in range(8):
a_first = order[2 * pair] == "A"
values += [20.0, b[pair]] if a_first else [b[pair], 20.0]
r = ab.abba(items(order, values))
e = r["result"]
self.assertEqual(r["method"], "pairs")
self.assertLess(e["ci95"][1], 0)
self.assertEqual((e["agree"], e["k"]), (6, 8))
self.assertEqual(r["verdict"], "no clear difference")
# One pair against the rest is allowed.
r = ab.abba(items(order, values[:-2] + [18.0, 20.0]))
self.assertEqual(r["verdict"], "B faster")
def test_one_odd_run_gives_no_verdict(self):
r = ab.abba(items("ABBAABBA", [20.0, 16.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0]))
self.assertEqual(r["verdict"], "no clear difference")
def test_three_pairs_are_not_enough_for_pairs(self):
r = ab.abba(items("ABBAAB", [20.4, 19.1, 19.6, 20.9, 21.3, 19.8]))
self.assertNotEqual(r["method"], "pairs")
def test_three_runs_a_side_fit_the_side_and_a_trend(self):
# B is 1 ms faster, and every run is 0.3 ms slower than the one before.
order = "ABBAAB"
values = [20.0 + 0.3 * i - (1.0 if arm == "B" else 0.0) for i, arm in enumerate(order)]
r = ab.abba(items(order, values))
self.assertEqual(r["method"], "side and linear trend")
self.assertAlmostEqual(r["result"]["effect"], -1.0, places=6)
self.assertAlmostEqual(r["result"]["trend_per_run"], 0.3, places=6)
def test_a_run_left_out_drops_its_pair(self):
it = items("ABBAABBAAB", [20.4, 19.1, 19.6, 20.9, 21.3, 19.8, 20.2, 21.0, 20.6, 19.4])
it[2] = ("B", None, None, "gate FAIL")
r = ab.abba(it)
self.assertEqual(len(r["pairs"]), 4)
self.assertEqual(r["runs"][2]["left_out"], "gate FAIL")
class Runs(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp(prefix="ab-test-")
def gate(self, name, fps, result="PASS"):
d = os.path.join(self.dir, name)
os.makedirs(d)
with open(os.path.join(d, "summary.json"), "w", encoding="utf-8") as f:
json.dump({"fps": {"median": fps, "draws_median": 4400}, "verdict": {"result": result}}, f)
return d
def log(self, name, fps_values, draws):
path = os.path.join(self.dir, name)
with open(path, "w", encoding="utf-8") as f:
for fps in fps_values:
f.write(f"2026-11-30T18:00:00.000000Z INFO leonida_gpu::host: {fps:.1f} fps, {1000 / fps:.1f} ms/frame "
f"(1.0 preparing: copies 0.1 ms 0.1 MB, textures 0.1 ms 0.1 MB; 5.0 on the GPU; 0.0 copying back "
f"0.0 MB), {draws} draws/frame, dropped 0 without a host pipeline (0 being made) and 0 indirect\n")
f.write("a line the title printed: keep it out\n")
return path
def test_a_gate_folder_gives_its_world_median_and_a_failed_gate_is_left_out(self):
self.assertAlmostEqual(ab.run_value(self.gate("ok", 25.0))[0], 40.0)
self.assertEqual(ab.run_value(self.gate("bad", 25.0, "FAIL"))[2], "gate FAIL")
def test_a_log_gives_the_median_of_its_world_lines(self):
menus = self.log("menus.log", [60.0] * 5, 13)
world = self.log("world.log", [20.0, 25.0, 25.0], 4000)
self.assertEqual(ab.run_value(menus)[2], "no frame line in the world")
self.assertAlmostEqual(ab.run_value(world)[0], 40.0)
def test_the_command_prints_numbers_only(self):
runs = []
for i, (arm, fps) in enumerate(zip("ABBAABBA", [25, 26, 26, 25, 25, 26, 26, 25])):
runs.append(f"{arm}:" + self.gate(f"r{i}", float(fps)))
out = io.StringIO()
with redirect_stdout(out):
self.assertEqual(ab.main(["abba"] + runs), 0)
text = out.getvalue()
self.assertIn("verdict: B faster", text)
self.assertNotIn(self.dir, text)
class Phases(unittest.TestCase):
def test_a_switch_effect_and_its_counters_per_pair_of_blocks(self):
path = os.path.join(tempfile.mkdtemp(prefix="ab-phases-"), "run.log")
with open(path, "w", encoding="utf-8") as f:
for n in range(12):
on = n % 2
median = 30.0 - (1.0 if on else 0.0) + 0.01 * n
f.write(f"2026-11-30T18:00:{n:02d}.000000Z INFO leonida_gpu::ab: A/B phase {n} bits {on} on [{'LEONIDA_X' if on else ''}]: "
f"200 frames, mean {median:.1f} ms, median {median:.2f}, p90 {median + 5:.1f}, 4400 draws a frame; rest\n")
f.write(f"2026-11-30T18:00:{n:02d}.500000Z INFO leonida_gpu::ab: A/B counters phase {n} bits {on}: "
f"protect_ms={(2.0 if on else 8.0):.3f} kernel_asks=3.00\n")
r = ab.phase_effects(ab.read_phases(path))
s = r["switches"][0]
self.assertEqual(s["pairs"], 6)
self.assertAlmostEqual(s["effects"]["median"]["effect"], -0.99, places=2)
self.assertAlmostEqual(s["effects"]["protect_ms"]["effect"], -6.0, places=6)
self.assertEqual(s["verdict"], "B faster")
def test_a_scene_change_between_blocks_is_left_out(self):
phases = {0: {"bits": 0, "median": 30.0, "draws": 4000.0, "counters": {}},
1: {"bits": 1, "median": 20.0, "draws": 9000.0, "counters": {}}}
s = ab.phase_effects(phases)["switches"][0]
self.assertEqual((s["pairs"], s["left_out_for_draws"]), (0, 1))
class Plan(unittest.TestCase):
def test_builds_end_the_warm_up_on_the_first_measured_side(self):
p = ab.plan(4, switch=False)
self.assertEqual(p["warm_up_and_discarded"], ["warm-up A", "warm-up B", "discarded B", "discarded A"])
self.assertEqual("".join(p["measured"]), "ABBAABBA")
def test_one_exe_two_settings_discards_one_run(self):
self.assertEqual(ab.plan(2, switch=True)["warm_up_and_discarded"], ["warm-up A", "warm-up B", "discarded A"])
if __name__ == "__main__":
unittest.main()