-
Notifications
You must be signed in to change notification settings - Fork 25
[Fix] fix make_coord to require tuple arg and add crd2idx tests #251
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
Open
jli-melchior
wants to merge
2
commits into
main
Choose a base branch
from
fix/crd2idx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| import flydsl.compiler as flyc | ||
| import flydsl.expr as fx | ||
|
|
||
|
|
||
| # --- jit kernels (not collected by pytest due to _run_ prefix) --- | ||
|
|
||
|
|
||
| @flyc.jit | ||
| def _run_crd2idx_col_major(): | ||
| """(4,8) col-major: idx = r + 4*c""" | ||
| layout = fx.make_layout((4, 8), (1, 4)) | ||
| for r in range_constexpr(4): | ||
| for c in range_constexpr(8): | ||
| idx = fx.crd2idx(fx.make_coord((r, c)), layout) | ||
| fx.printf("{}", idx) | ||
|
|
||
|
|
||
| @flyc.jit | ||
| def _run_crd2idx_row_major(): | ||
| """(4,8) row-major: idx = r*8 + c""" | ||
| layout = fx.make_layout((4, 8), (8, 1)) | ||
| for r in range_constexpr(4): | ||
| for c in range_constexpr(8): | ||
| idx = fx.crd2idx(fx.make_coord((r, c)), layout) | ||
| fx.printf("{}", idx) | ||
|
|
||
|
|
||
| @flyc.jit | ||
| def _run_crd2idx_1d(): | ||
| """1D layout: shape=(8,), stride=(2,)""" | ||
| layout = fx.make_layout((8,), (2,)) | ||
| for c in range_constexpr(8): | ||
| idx = fx.crd2idx(fx.make_coord((c,)), layout) | ||
| fx.printf("{}", idx) | ||
|
|
||
|
|
||
| @flyc.jit | ||
| def _run_crd2idx_3d(): | ||
| """3D layout: (2,3,4) row-major strides (12,4,1)""" | ||
| layout = fx.make_layout((2, 3, 4), (12, 4, 1)) | ||
| for i in range_constexpr(2): | ||
| for j in range_constexpr(3): | ||
| for k in range_constexpr(4): | ||
| idx = fx.crd2idx(fx.make_coord((i, j, k)), layout) | ||
| fx.printf("{}", idx) | ||
|
|
||
|
|
||
| # --- subprocess helper to capture C-level printf --- | ||
|
|
||
| JIT_KERNELS = { | ||
| "col_major": _run_crd2idx_col_major, | ||
| "row_major": _run_crd2idx_row_major, | ||
| "1d": _run_crd2idx_1d, | ||
| "3d": _run_crd2idx_3d, | ||
| } | ||
|
|
||
| EXPECTED = { | ||
| "col_major": [r + 4 * c for r in range(4) for c in range(8)], | ||
| "row_major": [r * 8 + c for r in range(4) for c in range(8)], | ||
| "1d": [c * 2 for c in range(8)], | ||
| "3d": [i * 12 + j * 4 + k for i in range(2) for j in range(3) for k in range(4)], | ||
| } | ||
|
|
||
|
|
||
| def _run_jit_and_capture(test_name): | ||
| """Run a jit kernel in a subprocess and return parsed int output.""" | ||
| env = os.environ.copy() | ||
| env["FLYDSL_RUNTIME_ENABLE_CACHE"] = "0" | ||
| result = subprocess.run( | ||
| [sys.executable, __file__, "--run", test_name], | ||
| capture_output=True, | ||
| text=True, | ||
| env=env, | ||
| ) | ||
| assert result.returncode == 0, f"subprocess failed:\n{result.stderr}" | ||
| lines = [l for l in result.stdout.strip().split("\n") if l.strip()] | ||
| return [int(x) for x in lines] | ||
|
|
||
|
|
||
| def _run_error_test(snippet_name): | ||
| """Run an error-test snippet in a subprocess, return (returncode, stderr).""" | ||
| env = os.environ.copy() | ||
| env["FLYDSL_RUNTIME_ENABLE_CACHE"] = "0" | ||
| result = subprocess.run( | ||
| [sys.executable, __file__, "--error", snippet_name], | ||
| capture_output=True, | ||
| text=True, | ||
| env=env, | ||
| ) | ||
| return result.returncode, result.stdout.strip(), result.stderr.strip() | ||
|
|
||
|
|
||
| # --- pytest test cases: correctness --- | ||
|
|
||
|
|
||
| def test_crd2idx_col_major(): | ||
| """(4,8) col-major layout: idx = r + 4*c""" | ||
| actual = _run_jit_and_capture("col_major") | ||
| assert actual == EXPECTED["col_major"] | ||
|
|
||
|
|
||
| def test_crd2idx_row_major(): | ||
| """(4,8) row-major layout: idx = r*8 + c""" | ||
| actual = _run_jit_and_capture("row_major") | ||
| assert actual == EXPECTED["row_major"] | ||
|
|
||
|
|
||
| def test_crd2idx_1d(): | ||
| """1D layout: shape=(8,), stride=(2,)""" | ||
| actual = _run_jit_and_capture("1d") | ||
| assert actual == EXPECTED["1d"] | ||
|
|
||
|
|
||
| def test_crd2idx_3d(): | ||
| """3D layout: (2,3,4) row-major strides (12,4,1)""" | ||
| actual = _run_jit_and_capture("3d") | ||
| assert actual == EXPECTED["3d"] | ||
|
|
||
|
|
||
| # --- pytest test cases: error handling (via subprocess to avoid conftest path issues) --- | ||
|
|
||
|
|
||
| def test_make_coord_rejects_varargs(): | ||
| """make_coord(r, c) must raise TypeError.""" | ||
| rc, stdout, stderr = _run_error_test("varargs") | ||
| assert rc != 0, f"Expected failure but succeeded.\nstdout: {stdout}" | ||
| assert "make_coord expects a tuple" in stderr, f"Wrong error message:\n{stderr}" | ||
|
|
||
|
|
||
| def test_make_coord_rejects_int(): | ||
| """make_coord(42) must raise TypeError.""" | ||
| rc, stdout, stderr = _run_error_test("int_arg") | ||
| assert rc != 0, f"Expected failure but succeeded.\nstdout: {stdout}" | ||
| assert "make_coord expects a tuple" in stderr, f"Wrong error message:\n{stderr}" | ||
|
|
||
|
|
||
| # --- subprocess entry point --- | ||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) >= 3 and sys.argv[1] == "--run": | ||
| JIT_KERNELS[sys.argv[2]]() | ||
|
|
||
| elif len(sys.argv) >= 3 and sys.argv[1] == "--error": | ||
| # These are intentionally broken snippets that should raise TypeError. | ||
| # Import fresh (no conftest interference). | ||
| import flydsl.compiler as flyc_fresh | ||
| import flydsl.expr as fx_fresh | ||
|
|
||
| if sys.argv[2] == "varargs": | ||
|
|
||
| @flyc_fresh.jit | ||
| def _bad(): | ||
| layout = fx_fresh.make_layout((4, 8), (1, 4)) | ||
| idx = fx_fresh.crd2idx(fx_fresh.make_coord(0, 1), layout) | ||
|
|
||
| _bad() | ||
|
|
||
| elif sys.argv[2] == "int_arg": | ||
|
|
||
| @flyc_fresh.jit | ||
| def _bad(): | ||
| layout = fx_fresh.make_layout((4,), (1,)) | ||
| idx = fx_fresh.crd2idx(fx_fresh.make_coord(0), layout) | ||
|
|
||
| _bad() | ||
|
|
||
| else: | ||
| pytest.main([__file__, "-v"]) |
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.
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.
make_coord doesn't only accept a tuple. It is the same as make_shape and make_stride. There is no need to change.
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.
so we can let make_coord not only accept a tuple, but also a variable-length arguments?