-
Notifications
You must be signed in to change notification settings - Fork 77
Add HELION_PRINT_REPRO=1 to print Helion kernel repro script to console
#1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| This file is automatically generated by assertExpectedJournal calls in test_debug_utils.py. | ||
| Update expected outputs by running tests with the EXPECTTEST_ACCEPT=1 environment variable set. | ||
|
|
||
| --- assertExpectedJournal(TestDebugUtils.test_print_repro_env_var) | ||
| # === HELION KERNEL REPRO === | ||
| import helion | ||
| import helion.language as hl | ||
| import torch | ||
| from torch._dynamo.testing import rand_strided | ||
|
|
||
| @helion.kernel(config=helion.Config(block_sizes=[2, 2], flatten_loops=[False], indexing=['pointer', 'pointer'], l2_groupings=[1], load_eviction_policies=[''], loop_orders=[[0, 1]], num_stages=1, num_warps=4, pid_type='flat', range_flattens=[None], range_multi_buffers=[None], range_num_stages=[0], range_unroll_factors=[0], range_warp_specializes=[]), static_shapes=True) | ||
| def kernel1(x: torch.Tensor) -> torch.Tensor: | ||
| out = torch.empty_like(x) | ||
| m, n = x.shape | ||
| for tile_m, tile_n in hl.tile([m, n]): | ||
| out[tile_m, tile_n] = x[tile_m, tile_n] + 1 | ||
| return out | ||
|
|
||
| def helion_repro_caller(): | ||
| torch.manual_seed(0) | ||
| x = rand_strided((2, 2), (2, 1), dtype=torch.float32, device=DEVICE) | ||
| return kernel1(x) | ||
| # === END HELION KERNEL REPRO === |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import linecache | ||
| import os | ||
| import unittest | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| import helion | ||
| from helion._testing import DEVICE | ||
| from helion._testing import RefEagerTestDisabled | ||
| from helion._testing import TestCase | ||
| import helion.language as hl | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _store_capfd_on_class(request, capfd): | ||
| """ | ||
| Expose pytest's capfd fixture as `self._capfd` inside the TestDebugUtils class | ||
| (works for unittest.TestCase-style tests). | ||
| """ | ||
| if request.cls is not None: | ||
| request.cls._capfd = capfd | ||
|
|
||
|
|
||
| class TestDebugUtils(RefEagerTestDisabled, TestCase): | ||
| def test_print_repro_env_var(self): | ||
| """Ensure HELION_PRINT_REPRO=1 emits an executable repro script.""" | ||
| original = os.environ.get("HELION_PRINT_REPRO") | ||
| os.environ["HELION_PRINT_REPRO"] = "1" | ||
| try: | ||
|
|
||
| @helion.kernel( | ||
| config=helion.Config( | ||
| block_sizes=[2, 2], | ||
| flatten_loops=[False], | ||
| indexing=["pointer", "pointer"], | ||
| l2_groupings=[1], | ||
| load_eviction_policies=[""], | ||
| loop_orders=[[0, 1]], | ||
| num_stages=1, | ||
| num_warps=4, | ||
| pid_type="flat", | ||
| range_flattens=[None], | ||
| range_multi_buffers=[None], | ||
| range_num_stages=[0], | ||
| range_unroll_factors=[0], | ||
| ), | ||
| static_shapes=True, | ||
| ) | ||
| def kernel1(x: torch.Tensor) -> torch.Tensor: | ||
| out = torch.empty_like(x) | ||
| m, n = x.shape | ||
| for tile_m, tile_n in hl.tile([m, n]): | ||
| out[tile_m, tile_n] = x[tile_m, tile_n] + 1 | ||
| return out | ||
|
|
||
| torch.manual_seed(0) | ||
| x = torch.randn([2, 2], dtype=torch.float32, device=DEVICE) | ||
|
|
||
| if hasattr(self, "_capfd"): | ||
| self._capfd.readouterr() | ||
|
|
||
| result = kernel1(x) | ||
| torch.testing.assert_close(result, x + 1) | ||
|
|
||
| if not hasattr(self, "_capfd"): | ||
| return # Cannot test without capture | ||
|
|
||
| captured = "".join(self._capfd.readouterr()) | ||
|
|
||
| # Extract repro script | ||
| lines = captured.splitlines() | ||
| start = next( | ||
| i | ||
| for i, line in enumerate(lines) | ||
| if "# === HELION KERNEL REPRO ===" in line | ||
| ) | ||
| end = next( | ||
| i | ||
| for i, line in enumerate(lines[start:], start) | ||
| if "# === END HELION KERNEL REPRO ===" in line | ||
| ) | ||
| repro_script = "\n".join(lines[start : end + 1]) | ||
|
|
||
| # Normalize range_warp_specializes=[None] to [] for comparison | ||
| normalized_script = repro_script.replace( | ||
| "range_warp_specializes=[None]", "range_warp_specializes=[]" | ||
| ) | ||
|
|
||
| # Verify repro script matches expected script | ||
| self.assertExpectedJournal(normalized_script) | ||
|
|
||
| # Extract the actual code (without the comment markers) for execution | ||
| repro_lines = repro_script.splitlines() | ||
| code_start = 1 if repro_lines[0].startswith("# === HELION") else 0 | ||
| code_end = len(repro_lines) - ( | ||
| 1 if repro_lines[-1].startswith("# === END") else 0 | ||
| ) | ||
| repro_code = "\n".join(repro_lines[code_start:code_end]) | ||
|
|
||
| # Setup linecache so inspect.getsource() works on exec'd code | ||
| filename = "<helion_repro_test>" | ||
| linecache.cache[filename] = ( | ||
| len(repro_code), | ||
| None, | ||
| [f"{line}\n" for line in repro_code.splitlines()], | ||
| filename, | ||
| ) | ||
|
|
||
| # Execute the repro script | ||
| namespace = {} | ||
| exec(compile(repro_code, filename, "exec"), namespace) | ||
|
|
||
| # Call the generated helper and verify it runs successfully | ||
| helper = namespace["helion_repro_caller"] | ||
| repro_result = helper() | ||
|
|
||
| # Verify the output | ||
| torch.testing.assert_close(repro_result, x + 1) | ||
|
|
||
| linecache.cache.pop(filename, None) | ||
| finally: | ||
| if original is None: | ||
| os.environ.pop("HELION_PRINT_REPRO", None) | ||
| else: | ||
| os.environ["HELION_PRINT_REPRO"] = original | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont think you need any of this, i added a mode to to_triton_code that prints repro?
there's even a unit test for it
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah so I actually want to get the Helion kernel (with config and caller) not the generated Triton kernel.
what internal folks usually provide is a buck command that's many layers deep, and it's very hard to extract out a clean Helion kernel repro. With this env var, it will print the Helion kernel with the right input tensors, so that I can just focus on debugging the minimal repro.