|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import linecache |
| 4 | +import os |
| 5 | +import unittest |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +import helion |
| 11 | +from helion._testing import DEVICE |
| 12 | +from helion._testing import RefEagerTestDisabled |
| 13 | +from helion._testing import TestCase |
| 14 | +import helion.language as hl |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(autouse=True) |
| 18 | +def _store_capfd_on_class(request, capfd): |
| 19 | + """ |
| 20 | + Expose pytest's capfd fixture as `self._capfd` inside the TestDebugUtils class |
| 21 | + (works for unittest.TestCase-style tests). |
| 22 | + """ |
| 23 | + if request.cls is not None: |
| 24 | + request.cls._capfd = capfd |
| 25 | + |
| 26 | + |
| 27 | +class TestDebugUtils(RefEagerTestDisabled, TestCase): |
| 28 | + def test_print_repro_env_var(self): |
| 29 | + """Ensure HELION_PRINT_REPRO=1 emits an executable repro script.""" |
| 30 | + original = os.environ.get("HELION_PRINT_REPRO") |
| 31 | + os.environ["HELION_PRINT_REPRO"] = "1" |
| 32 | + try: |
| 33 | + |
| 34 | + @helion.kernel( |
| 35 | + config=helion.Config( |
| 36 | + block_sizes=[2, 2], |
| 37 | + flatten_loops=[False], |
| 38 | + indexing=["pointer", "pointer"], |
| 39 | + l2_groupings=[1], |
| 40 | + load_eviction_policies=[""], |
| 41 | + loop_orders=[[0, 1]], |
| 42 | + num_stages=1, |
| 43 | + num_warps=4, |
| 44 | + pid_type="flat", |
| 45 | + range_flattens=[None], |
| 46 | + range_multi_buffers=[None], |
| 47 | + range_num_stages=[0], |
| 48 | + range_unroll_factors=[0], |
| 49 | + ), |
| 50 | + static_shapes=True, |
| 51 | + ) |
| 52 | + def kernel1(x: torch.Tensor) -> torch.Tensor: |
| 53 | + out = torch.empty_like(x) |
| 54 | + m, n = x.shape |
| 55 | + for tile_m, tile_n in hl.tile([m, n]): |
| 56 | + out[tile_m, tile_n] = x[tile_m, tile_n] + 1 |
| 57 | + return out |
| 58 | + |
| 59 | + torch.manual_seed(0) |
| 60 | + x = torch.randn([2, 2], dtype=torch.float32, device=DEVICE) |
| 61 | + |
| 62 | + if hasattr(self, "_capfd"): |
| 63 | + self._capfd.readouterr() |
| 64 | + |
| 65 | + result = kernel1(x) |
| 66 | + torch.testing.assert_close(result, x + 1) |
| 67 | + |
| 68 | + if not hasattr(self, "_capfd"): |
| 69 | + return # Cannot test without capture |
| 70 | + |
| 71 | + captured = "".join(self._capfd.readouterr()) |
| 72 | + |
| 73 | + # Extract repro script |
| 74 | + lines = captured.splitlines() |
| 75 | + start = next( |
| 76 | + i |
| 77 | + for i, line in enumerate(lines) |
| 78 | + if "# === HELION KERNEL REPRO ===" in line |
| 79 | + ) |
| 80 | + end = next( |
| 81 | + i |
| 82 | + for i, line in enumerate(lines[start:], start) |
| 83 | + if "# === END HELION KERNEL REPRO ===" in line |
| 84 | + ) |
| 85 | + repro_script = "\n".join(lines[start : end + 1]) |
| 86 | + |
| 87 | + # Normalize range_warp_specializes=[None] to [] for comparison |
| 88 | + normalized_script = repro_script.replace( |
| 89 | + "range_warp_specializes=[None]", "range_warp_specializes=[]" |
| 90 | + ) |
| 91 | + |
| 92 | + # Verify repro script matches expected script |
| 93 | + self.assertExpectedJournal(normalized_script) |
| 94 | + |
| 95 | + # Extract the actual code (without the comment markers) for execution |
| 96 | + repro_lines = repro_script.splitlines() |
| 97 | + code_start = 1 if repro_lines[0].startswith("# === HELION") else 0 |
| 98 | + code_end = len(repro_lines) - ( |
| 99 | + 1 if repro_lines[-1].startswith("# === END") else 0 |
| 100 | + ) |
| 101 | + repro_code = "\n".join(repro_lines[code_start:code_end]) |
| 102 | + |
| 103 | + # Setup linecache so inspect.getsource() works on exec'd code |
| 104 | + filename = "<helion_repro_test>" |
| 105 | + linecache.cache[filename] = ( |
| 106 | + len(repro_code), |
| 107 | + None, |
| 108 | + [f"{line}\n" for line in repro_code.splitlines()], |
| 109 | + filename, |
| 110 | + ) |
| 111 | + |
| 112 | + # Execute the repro script |
| 113 | + namespace = {} |
| 114 | + exec(compile(repro_code, filename, "exec"), namespace) |
| 115 | + |
| 116 | + # Call the generated helper and verify it runs successfully |
| 117 | + helper = namespace["helion_repro_caller"] |
| 118 | + repro_result = helper() |
| 119 | + |
| 120 | + # Verify the output |
| 121 | + torch.testing.assert_close(repro_result, x + 1) |
| 122 | + |
| 123 | + linecache.cache.pop(filename, None) |
| 124 | + finally: |
| 125 | + if original is None: |
| 126 | + os.environ.pop("HELION_PRINT_REPRO", None) |
| 127 | + else: |
| 128 | + os.environ["HELION_PRINT_REPRO"] = original |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + unittest.main() |
0 commit comments