-
Notifications
You must be signed in to change notification settings - Fork 15
[Type] Tensor 10: Layout aliasing across supported Quadrants p… #530
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
Draft
hughperkins
wants to merge
5
commits into
hp/tensor-stork-9
Choose a base branch
from
hp/tensor-stork-10
base: hp/tensor-stork-9
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.
+198
−2
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8c9fbd
[FlexibleTensors] PR 10: Layout aliasing across supported Quadrants p…
hughperkins fb939c4
[Tensors] Rename flexible-tensors -> tensors throughout
hughperkins 70c6dc7
Fix CI lint and broken markdown anchor
hughperkins b7bc284
Merge remote-tracking branch 'origin/hp/tensor-stork-9' into hp/tenso…
hughperkins 0abc455
Merge remote-tracking branch 'origin/hp/tensor-stork-9' into hp/tenso…
hughperkins 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| """Aliasing of layout metadata across the supported Quadrants aliasing | ||
| patterns. | ||
|
|
||
| Note: in-kernel rebinding (``y = x; y[i, j] = ...``) is **not** supported | ||
| by Quadrants for any ndarray — that's an upstream limitation not specific | ||
| to tensor (it raises ``QuadrantsTypeError: Invalid constant | ||
| scalar data type: <class 'quadrants.lang.any_array.AnyArray'>``). So this | ||
| file pins down the aliasing patterns Quadrants *does* support and that | ||
| tensor layout metadata must propagate through: | ||
|
|
||
| 1. Same ``Ndarray`` passed twice to the same kernel — two distinct | ||
| ``AnyArray`` instances inside the kernel, both must carry the same | ||
| layout. | ||
| 2. Same ``Ndarray`` shared across two consecutive kernel calls — the | ||
| layout cannot leak or get lost between calls. | ||
| 3. Repeated access through ``.grad`` inside a single kernel — every call | ||
| must return an ``AnyArray`` with the same layout (an earlier change covered the | ||
| single-access path; this exercises the repeated-access cache). | ||
| 4. The same ``Ndarray`` via two different kernel signatures (one | ||
| annotated as ``qd.types.ndarray()`` directly, one via a wrapper) — | ||
| metadata must travel via the runtime feature tuple, not the | ||
| annotation. | ||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
| import quadrants as qd | ||
| from quadrants._tensor import _with_layout | ||
|
|
||
| from tests import test_utils | ||
|
|
||
|
|
||
| def _allocate_layout10(M, N, dtype=qd.i32, needs_grad=False): | ||
| a = qd.tensor(dtype, shape=(N, M), backend=qd.Backend.NDARRAY, needs_grad=needs_grad) | ||
| _with_layout(a, (1, 0)) | ||
| return a | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # 1. Same Ndarray passed twice to the same kernel | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @test_utils.test(arch=qd.cpu) | ||
| def test_layout_same_ndarray_passed_twice(): | ||
| """Both AnyArrays inside the kernel see the same layout.""" | ||
| M, N = 2, 3 | ||
| a = _allocate_layout10(M, N) | ||
|
|
||
| @qd.kernel | ||
| def write_via_two_handles(x: qd.types.ndarray(), y: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x[i, j] = 7 | ||
| # `y` aliases `x` — write through it; result must agree. | ||
| y[i, j] = x[i, j] + (i * 10 + j) | ||
|
|
||
| write_via_two_handles(a, a) | ||
| arr = a.to_numpy() | ||
| assert arr.shape == (N, M) | ||
| for i in range(M): | ||
| for j in range(N): | ||
| assert arr[j, i] == 7 + i * 10 + j | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # 2. Same Ndarray across two consecutive kernel calls | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @test_utils.test(arch=qd.cpu) | ||
| def test_layout_persists_across_kernel_calls(): | ||
| M, N = 3, 4 | ||
| a = _allocate_layout10(M, N) | ||
|
|
||
| @qd.kernel | ||
| def init(x: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x[i, j] = i * 10 + j | ||
|
|
||
| @qd.kernel | ||
| def add(x: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x[i, j] += 1000 | ||
|
|
||
| init(a) | ||
| add(a) | ||
| arr = a.to_numpy() | ||
| for i in range(M): | ||
| for j in range(N): | ||
| assert arr[j, i] == 1000 + i * 10 + j | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # 3. Repeated .grad access inside the same kernel | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @test_utils.test(arch=qd.cpu) | ||
| def test_layout_repeated_grad_access_in_kernel(): | ||
| M, N = 2, 3 | ||
| a = _allocate_layout10(M, N, dtype=qd.f32, needs_grad=True) | ||
|
|
||
| @qd.kernel | ||
| def write_grad_repeatedly(x: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x.grad[i, j] = float(i * 100) | ||
| # Re-access .grad in the same iteration — must hit same layout. | ||
| x.grad[i, j] += float(j * 10) | ||
| x.grad[i, j] += 1.0 | ||
|
|
||
| write_grad_repeatedly(a) | ||
| grad = a.grad.to_numpy() | ||
| assert grad.shape == (N, M) | ||
| for i in range(M): | ||
| for j in range(N): | ||
| assert grad[j, i] == i * 100 + j * 10 + 1 | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # 4. Same Ndarray via two kernels with different (compatible) annotations | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @test_utils.test(arch=qd.cpu) | ||
| def test_layout_consistent_across_different_kernel_signatures(): | ||
| """Layout metadata travels with the value, not the kernel annotation.""" | ||
| M, N = 2, 3 | ||
| a = _allocate_layout10(M, N) | ||
|
|
||
| @qd.kernel | ||
| def kernel_a(x: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x[i, j] = i * 10 + j | ||
|
|
||
| @qd.kernel | ||
| def kernel_b(arr: qd.types.ndarray()): # different param name, same type | ||
| for i, j in qd.ndrange(M, N): | ||
| arr[i, j] += 100 | ||
|
|
||
| kernel_a(a) | ||
| kernel_b(a) | ||
| arr_np = a.to_numpy() | ||
| for i in range(M): | ||
| for j in range(N): | ||
| assert arr_np[j, i] == 100 + i * 10 + j | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # 5. Untagged + tagged + .grad in one kernel: metadata isolation per arg | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @test_utils.test(arch=qd.cpu) | ||
| def test_layout_isolated_between_args(): | ||
| """One tagged + one untagged ndarray in the same kernel: each carries | ||
| its own (or no) layout.""" | ||
| M, N = 2, 3 | ||
| untagged = qd.tensor(qd.i32, shape=(M, N), backend=qd.Backend.NDARRAY) # canonical (M, N) | ||
| tagged = _allocate_layout10(M, N) | ||
|
|
||
| @qd.kernel | ||
| def k(u: qd.types.ndarray(), t: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| u[i, j] = i * 10 + j # no rewrite | ||
| t[i, j] = i * 10 + j # rewrite to t[j, i] | ||
|
|
||
| k(untagged, tagged) | ||
| np.testing.assert_array_equal(untagged.to_numpy(), tagged.to_numpy().T) | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # 6. Two separately-allocated layout-tagged ndarrays — independence | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @test_utils.test(arch=qd.cpu) | ||
| def test_layout_two_tagged_ndarrays_independent(): | ||
| M, N = 2, 3 | ||
| a = _allocate_layout10(M, N) | ||
| b = _allocate_layout10(M, N) | ||
|
|
||
| @qd.kernel | ||
| def init_a(x: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x[i, j] = 1 | ||
|
|
||
| @qd.kernel | ||
| def init_b(x: qd.types.ndarray()): | ||
| for i, j in qd.ndrange(M, N): | ||
| x[i, j] = 2 | ||
|
|
||
| init_a(a) | ||
| init_b(b) | ||
| assert (a.to_numpy() == 1).all() | ||
| assert (b.to_numpy() == 2).all() | ||
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.
lets not mention pr numbers
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.
also lets put all these tests into a single file probably, across all the prs.