Skip to content
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[next]: Extend astype to work with tuples #1352

Merged
merged 19 commits into from
Nov 17, 2023

Conversation

ninaburg
Copy link
Contributor

@ninaburg ninaburg commented Oct 19, 2023

This pull request extends the functionality of the built-in astype function in the ffront module such that we can write field_float32, field_float32 = astype((field_float64, field_float64), float32). The changes specifically impact the foast_to_itir.py file. Here's an overview of the modifications:

  • The astype function's argument and return types have been extended to accept tuples of fields in addition to individual fields.
  • In the foast_to_itir.py file, when casting tuples, each element is processed individually. The casted elements are then combined into a new tuple using make_tuple.
  • The code also includes test cases to ensure that the modified astype function correctly handles tuples of fields.

The motivation for these changes is to simplify and enhance code readability when performing operations that mix different types. In such scenarios, explicit type promotion is required, resulting in cluttered stencils with casting operations. This update makes the type conversion explicit and visible, reducing the need for extensive casting operations and improving code clarity.

@ninaburg ninaburg requested a review from nfarabullini October 19, 2023 15:54
@ninaburg ninaburg force-pushed the extend_astype_with_tuples branch from 3823227 to 048d4c0 Compare October 19, 2023 16:21
@ninaburg
Copy link
Contributor Author

cscs-ci run

@ninaburg
Copy link
Contributor Author

cscs-ci run

src/gt4py/next/ffront/fbuiltins.py Show resolved Hide resolved
src/gt4py/next/ffront/foast_passes/type_deduction.py Outdated Show resolved Hide resolved
src/gt4py/next/ffront/foast_to_itir.py Outdated Show resolved Hide resolved
src/gt4py/next/ffront/foast_to_itir.py Show resolved Hide resolved
@ninaburg
Copy link
Contributor Author

cscs-ci run

@ninaburg ninaburg requested a review from tehrengruber October 20, 2023 10:06
@ninaburg
Copy link
Contributor Author

cscs-ci run

@ninaburg
Copy link
Contributor Author

cscs-ci run

@ninaburg ninaburg force-pushed the extend_astype_with_tuples branch from 779ae8b to 4a11354 Compare November 16, 2023 19:02
@ninaburg
Copy link
Contributor Author

cscs-ci run

def cast_tuple(
a: cases.IFloatField,
b: cases.IFloatField,
a_casted_to_int_outside_of_gt4py: cases.IField,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
a_casted_to_int_outside_of_gt4py: cases.IField,
a_asint: cases.IField,

The naming was not a serious proposal of mine, but just to signify what it meant. Here it should be clear from the context.


a = cases.allocate(cartesian_case, cast_tuple, "a")()
b = cases.allocate(cartesian_case, cast_tuple, "b")()
a_casted_to_int_outside_of_gt4py = gtx.np_as_located_field(IDim)(np.asarray(a).astype(int32))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
a_casted_to_int_outside_of_gt4py = gtx.np_as_located_field(IDim)(np.asarray(a).astype(int32))
a_asint = gtx.np_as_located_field(IDim)(np.asarray(a).astype(int32))

As above.

@ninaburg ninaburg merged commit 67a6188 into GridTools:main Nov 17, 2023
19 checks passed
ninaburg added a commit that referenced this pull request Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants