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

bug[next][dace]: Fix lowering of broadcast #1698

Merged
merged 93 commits into from
Oct 25, 2024

Conversation

edopao
Copy link
Contributor

@edopao edopao commented Oct 21, 2024

Fix lowering of as_fieldop with broadcast expression after changes in #1701.

Additional change:

  • Add support for GT4Py zero-dimensional fields, equivalent of numpy zero-dimensional arrays. Test case added.

@@ -26,6 +26,9 @@
def _convert_arg(arg: Any, sdfg_param: str, use_field_canonical_representation: bool) -> Any:
if not isinstance(arg, gtx_common.Field):
return arg
if len(arg.domain.dims) == 0:
# pass zero-dimensional fields as scalars
return arg.asnumpy().item()
Copy link
Contributor

Choose a reason for hiding this comment

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

@egparedes What was the correct pattern for avoiding type conversions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This type conversion is not affecting GT4Py. The GT4Py argument has still type ts.FieldType with empty dims list. This type conversion (zero-dim array to scalar) only affects the argument passed to the SDFG call, because in the SDFG I am representing the zero-dim array as a dace scalar object.

Copy link
Contributor

@egparedes egparedes Oct 25, 2024

Choose a reason for hiding this comment

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

I haven't reviewed the PR so I don't have a lot of context here, but I think Hannes' comment is about extracting the scalar value from the 0d numpy array without changing its type. ndarray.item() (https://devdocs.io/numpy~2.0/reference/generated/numpy.ndarray.item) always transforms the numpy scalar to a python scalar, which may change its precision. To avoid this, you should use an empty tuple as index for .__getitem__() like this.

Suggested change
return arg.asnumpy().item()
return arg.asnumpy()[()]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, thank you.

@edopao edopao changed the title feat[next][dace]: Add support for zero-dimensional fields fix[next][dace]: Fix lowering of broadcast Oct 24, 2024
@edopao edopao changed the title fix[next][dace]: Fix lowering of broadcast bug[next][dace]: Fix lowering of broadcast Oct 24, 2024
Copy link
Contributor

@philip-paul-mueller philip-paul-mueller left a comment

Choose a reason for hiding this comment

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

There is a small change ion a description and a suggestion about negative logic.
However, it LGTM.
But you should ping @egparedes again to address what Hannes has pointed out.

@edopao edopao merged commit db249bd into GridTools:main Oct 25, 2024
31 checks passed
@edopao edopao deleted the dece-gtir-zerodim_fields branch October 25, 2024 10:21
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.

4 participants