|
| 1 | +import triton |
| 2 | +from triton import knobs |
| 3 | + |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | + |
| 7 | + |
| 8 | +def test_inspection(monkeypatch, tmp_path: pathlib.Path): |
| 9 | + stage_name = 'make_ttgir' |
| 10 | + curr_repro_path = tmp_path / ("repro_prefix." + stage_name + ".repro.mlir") |
| 11 | + repro_path = tmp_path / "repro_prefix" |
| 12 | + |
| 13 | + monkeypatch.setenv("TRITON_ALWAYS_COMPILE", "1") |
| 14 | + monkeypatch.setenv("TRITON_REPRODUCER_PATH", str(repro_path)) |
| 15 | + |
| 16 | + inspect_stages_hook_called = False |
| 17 | + make_ttgir_wrapper_called = False |
| 18 | + |
| 19 | + def inspect_stages_hook(self, stages, options, language, capability): |
| 20 | + nonlocal inspect_stages_hook_called |
| 21 | + inspect_stages_hook_called = True |
| 22 | + |
| 23 | + def make_ttgir_wrapper(src, metadata, options, capability): |
| 24 | + nonlocal make_ttgir_wrapper_called |
| 25 | + make_ttgir_wrapper_called = True |
| 26 | + return self.make_ttgir(src, metadata, options, capability) |
| 27 | + |
| 28 | + stages["ttgir"] = lambda src, metadata: make_ttgir_wrapper(src, metadata, options, capability) |
| 29 | + |
| 30 | + @triton.jit |
| 31 | + def k1(): |
| 32 | + return |
| 33 | + |
| 34 | + @triton.jit |
| 35 | + def k2(): |
| 36 | + return |
| 37 | + |
| 38 | + # Run once to get the clean/golden repro dump |
| 39 | + k1[(1, )]() |
| 40 | + assert not inspect_stages_hook_called and not make_ttgir_wrapper_called |
| 41 | + assert os.path.exists(curr_repro_path) |
| 42 | + golden_repro = curr_repro_path.read_text() |
| 43 | + curr_repro_path.unlink() |
| 44 | + |
| 45 | + # Setup hook and call again, check if hooks got called |
| 46 | + knobs.runtime.add_stages_inspection_hook = inspect_stages_hook |
| 47 | + k2[(1, )]() |
| 48 | + assert inspect_stages_hook_called and make_ttgir_wrapper_called |
| 49 | + assert os.path.exists(curr_repro_path) |
| 50 | + hook_repro = curr_repro_path.read_text() |
| 51 | + |
| 52 | + # Check that repros match |
| 53 | + assert golden_repro.replace('k1', 'dummy') == hook_repro.replace('k2', 'dummy') |
0 commit comments