-
Notifications
You must be signed in to change notification settings - Fork 49
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
feat[cartesian]: K offset write #1452
Merged
FlorianDeconinck
merged 36 commits into
GridTools:main
from
FlorianDeconinck:cartesian/k_write_offset
Sep 23, 2024
Merged
Changes from 14 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b2e46a1
Allow frontend to accept K write offset for FORWARD/BACKWARD
FlorianDeconinck 6b9d127
Unit test on frontend & code generation
FlorianDeconinck 1980fe8
lint
FlorianDeconinck 6e8c69f
Remove debug code
FlorianDeconinck d07a2de
Add conditional example simplified from existing column physics
FlorianDeconinck e90ec94
Restore ALL_BACKENDS, straighten utest.
FlorianDeconinck 5fff068
Verbose DaCeIR coding error when a tasklet variable cannot find the i…
FlorianDeconinck 2147811
Allow re-use of offset K write ins
FlorianDeconinck 3c6b466
Split utest to acknowledge dace:cpu issue
FlorianDeconinck 239093c
Add GPU init/read support in test
FlorianDeconinck 52366f1
Merge `main`
FlorianDeconinck d956202
lint
FlorianDeconinck 899a5da
Verbose current `dace:X` bug around while loop
FlorianDeconinck b5f112a
lint
FlorianDeconinck 6916121
Allow K offset writes in `dace` backends
FlorianDeconinck 9803336
Fix Assign guard on K for I and IJ fields
FlorianDeconinck 4930115
Merge branch 'main' into cartesian/k_write_offset
FlorianDeconinck 5e2a622
Make `get_array_library` public
FlorianDeconinck 7eab8c8
Merge branch 'main' into cartesian/k_write_offset
FlorianDeconinck 6d2c932
Merge branch 'main' into cartesian/k_write_offset
FlorianDeconinck faba6ef
Merge branch 'main' into cartesian/k_write_offset
FlorianDeconinck 637fcd6
Merge branch 'main' into cartesian/k_write_offset
FlorianDeconinck 79b2fdc
Merge branch 'main' into cartesian/k_write_offset
havogt f51f7ee
Merge branch 'main' into cartesian/k_write_offset
284497a
Merge branch 'main' into cartesian/k_write_offset
2846713
Merge branch 'main' into cartesian/k_write_offset
8fb7452
Error out cleanly for `gt:gpu` and CUDA < 12
0a376c0
Lint
a07ea46
Change cupy call to lower level
d5f3c25
Missed update to base class, skip test for gt:gpu
0f15c86
lint
ffab0d2
GCC 10.3 + NVCC brings a bug to DaCe. Side step it.
c15cb13
Restore previous variable offset. tracking and add write in K tracking
07ca0a6
Merge remote-tracking branch 'origin/main' into cartesian/k_write_offset
8e9ffe8
Deactivate K write test for CUDA11+dace
c6b30fd
Fix backend checks
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 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 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 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 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 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 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 |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
) | ||
from gt4py.storage.cartesian import utils as storage_utils | ||
|
||
from cartesian_tests.definitions import ALL_BACKENDS, CPU_BACKENDS | ||
from cartesian_tests.definitions import ALL_BACKENDS, CPU_BACKENDS, _get_array_library | ||
from cartesian_tests.integration_tests.multi_feature_tests.stencil_definitions import ( | ||
EXTERNALS_REGISTRY as externals_registry, | ||
REGISTRY as stencil_definitions, | ||
|
@@ -196,12 +196,20 @@ def stencil( | |
assert field_3d.shape == full_shape[:] | ||
|
||
field_2d = gt_storage.zeros( | ||
full_shape[:-1], dtype, backend=backend, aligned_index=aligned_index[:-1], dimensions="IJ" | ||
full_shape[:-1], | ||
dtype, | ||
backend=backend, | ||
aligned_index=aligned_index[:-1], | ||
dimensions="IJ", | ||
) | ||
assert field_2d.shape == full_shape[:-1] | ||
|
||
field_1d = gt_storage.ones( | ||
full_shape[-1:], dtype, backend=backend, aligned_index=(aligned_index[-1],), dimensions="K" | ||
full_shape[-1:], | ||
dtype, | ||
backend=backend, | ||
aligned_index=(aligned_index[-1],), | ||
dimensions="K", | ||
) | ||
assert list(field_1d.shape) == [full_shape[-1]] | ||
|
||
|
@@ -279,7 +287,8 @@ def copy_2to3( | |
def test_lower_dimensional_inputs_2d_to_3d_forward(backend): | ||
@gtscript.stencil(backend=backend) | ||
def copy_2to3( | ||
inp: gtscript.Field[gtscript.IJ, np.float_], outp: gtscript.Field[gtscript.IJK, np.float_] | ||
inp: gtscript.Field[gtscript.IJ, np.float_], | ||
outp: gtscript.Field[gtscript.IJK, np.float_], | ||
): | ||
with computation(FORWARD), interval(...): | ||
outp[0, 0, 0] = inp | ||
|
@@ -589,6 +598,115 @@ def test(out: Field[np.float64], inp: Field[np.float64]): | |
test(out, inp) | ||
|
||
|
||
@pytest.mark.parametrize("backend", ALL_BACKENDS) | ||
def test_K_offset_write(backend): | ||
# Cuda generates bad code for the K offset | ||
if backend == "cuda": | ||
pytest.skip("cuda K-offset write generates bad code") | ||
|
||
arraylib = _get_array_library(backend) | ||
array_shape = (1, 1, 4) | ||
K_values = arraylib.arange(start=40, stop=44) | ||
|
||
# Simple case of writing ot an offset. | ||
# A is untouched | ||
# B is written in K+1 and should have K_values, except for the first element (FORWARD) | ||
@gtscript.stencil(backend=backend) | ||
def simple(A: Field[np.float64], B: Field[np.float64]): | ||
with computation(FORWARD), interval(...): | ||
B[0, 0, 1] = A | ||
|
||
A = gt_storage.zeros( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
A[:, :, :] = K_values | ||
B = gt_storage.zeros( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
simple(A, B) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a bit confused. Why is this not an oob write? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extent calculation is doing the right job by excluding the first element from the loop |
||
assert (B[:, :, 0] == 0).all() | ||
assert (B[:, :, 1:3] == K_values[0:2]).all() | ||
|
||
# Order of operations: FORWARD with negative offset | ||
# means while A is update B will have non-updated values of A | ||
# Because of the interval, value of B[0] is 0 | ||
@gtscript.stencil(backend=backend) | ||
def forward(A: Field[np.float64], B: Field[np.float64], scalar: np.float64): | ||
with computation(FORWARD), interval(1, None): | ||
A[0, 0, -1] = scalar | ||
B[0, 0, 0] = A | ||
|
||
A = gt_storage.zeros( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
A[:, :, :] = K_values | ||
B = gt_storage.zeros( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
forward(A, B, 2.0) | ||
assert (A[:, :, :3] == 2.0).all() | ||
assert (A[:, :, 3] == K_values[3]).all() | ||
assert (B[:, :, 0] == 0).all() | ||
assert (B[:, :, 1:] == K_values[1:]).all() | ||
|
||
FlorianDeconinck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Order of operations: BACKWARD with negative offset | ||
# means A is update B will get the updated values of A | ||
@gtscript.stencil(backend=backend) | ||
def backward(A: Field[np.float64], B: Field[np.float64], scalar: np.float64): | ||
with computation(BACKWARD), interval(1, None): | ||
A[0, 0, -1] = scalar | ||
B[0, 0, 0] = A | ||
|
||
A = gt_storage.zeros( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
A[:, :, :] = K_values | ||
B = gt_storage.empty( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
backward(A, B, 2.0) | ||
assert (A[:, :, :3] == 2.0).all() | ||
assert (A[:, :, 3] == K_values[3]).all() | ||
assert (B[:, :, :] == A[:, :, :]).all() | ||
|
||
|
||
@pytest.mark.parametrize("backend", ALL_BACKENDS) | ||
def test_K_offset_write_conditional(backend): | ||
# While loop have a bug in `dace:X` backends where | ||
FlorianDeconinck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# the read-connector is used which means you can never | ||
# update the field in a while. | ||
# Logged in: https://github.com/GridTools/gt4py/issues/1496 | ||
if backend.startswith("dace") or backend == "cuda": | ||
pytest.skip("DaCe backends have a bug when handling while loop.") | ||
|
||
arraylib = _get_array_library(backend) | ||
array_shape = (1, 1, 4) | ||
K_values = arraylib.arange(start=40, stop=44) | ||
|
||
@gtscript.stencil(backend=backend) | ||
def column_physics_conditional(A: Field[np.float64], B: Field[np.float64], scalar: np.float64): | ||
with computation(BACKWARD), interval(1, None): | ||
if A > 0 and B > 0: | ||
A[0, 0, -1] = scalar | ||
B[0, 0, 1] = A | ||
lev = 1 | ||
while A >= 0 and B >= 0: | ||
A[0, 0, lev] = -1 | ||
B = -1 | ||
lev = lev + 1 | ||
|
||
A = gt_storage.zeros( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
A[:, :, :] = K_values | ||
B = gt_storage.ones( | ||
backend=backend, aligned_index=(0, 0, 0), shape=array_shape, dtype=np.float64 | ||
) | ||
column_physics_conditional(A, B, 2.0) | ||
assert (A[0, 0, :] == arraylib.array([2, 2, -1, -1])).all() | ||
assert (B[0, 0, :] == arraylib.array([1, -1, 2, 42])).all() | ||
|
||
|
||
@pytest.mark.parametrize("backend", ALL_BACKENDS) | ||
def test_direct_datadims_index(backend): | ||
F64_VEC4 = (np.float64, (2, 2, 2, 2)) | ||
|
Oops, something went wrong.
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.
are these the only changes to make it work? that's nice. It means we "only" have to sort out the conceptual problems
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 was expecting more too! So far though, it's all it takes. Next I'll put in some integration tests with conditionals and I'll simplify Oliver's dual for-loop example he pulled from SHiELD microphysics to bring something we've encountered in the wild.