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] Embedded field remove __array__ #1366

Merged
merged 30 commits into from
Nov 21, 2023

Conversation

havogt
Copy link
Contributor

@havogt havogt commented Nov 20, 2023

Add .asnumpy to Field. Implicit conversion via __array__ creates a problem, because expression np.float*field will return ndarray instead of field, because np.float's multiply operator will asarray(rhs).

@havogt havogt force-pushed the embedded_field_remove_array branch from d7f31ee to 8234894 Compare November 20, 2023 11:17
return impl


# TODO(havogt): consider moving to module like `field_utils`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am introducing this in #1365

@@ -1119,13 +1122,6 @@ def _invalid_unpack() -> tuple[int32, float64, int32]:


def test_constant_closure_vars(cartesian_case):
if cartesian_case.backend is None:
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 is why we are doing the PR

src/gt4py/next/utils.py Outdated Show resolved Hide resolved

def is_tuple_of(v: Any, t: type[_T]) -> TypeGuard[tuple[_T, ...]]:
return isinstance(v, tuple) and all(isinstance(e, t) for e in v)


def apply_to_tuple_elements(fun: Callable[_P, _R]) -> Callable[..., _R | tuple[_R | tuple, ...]]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
def apply_to_tuple_elements(fun: Callable[_P, _R]) -> Callable[..., _R | tuple[_R | tuple, ...]]:
def deep_map(fun: Callable[_P, _R]) -> Callable[..., _R | tuple[_R | tuple, ...]]:

maybe?

Copy link
Contributor

Choose a reason for hiding this comment

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

Either deep_map or tree_map (jax calls tree_map to something similar)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll take tree_map then

Copy link
Contributor

@egparedes egparedes left a comment

Choose a reason for hiding this comment

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

A couple of optional suggestions/comments but otherwise it looks good to me.

@@ -464,6 +464,9 @@ def dtype(self) -> core_defs.DType[core_defs.ScalarT]:
def ndarray(self) -> core_defs.NDArrayObject:
...

def asnumpy(self) -> np.ndarray:
return np.asarray(self.ndarray)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be default implementation or just leave it blank?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

forgot to remove...


def is_tuple_of(v: Any, t: type[_T]) -> TypeGuard[tuple[_T, ...]]:
return isinstance(v, tuple) and all(isinstance(e, t) for e in v)


def apply_to_tuple_elements(fun: Callable[_P, _R]) -> Callable[..., _R | tuple[_R | tuple, ...]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Either deep_map or tree_map (jax calls tree_map to something similar)

@havogt havogt merged commit 6375445 into GridTools:main Nov 21, 2023
18 checks passed
tehrengruber added a commit that referenced this pull request Dec 14, 2023
Changes to the quickstart guide to use `field.asnumpy()` (introduced in #1366) instead of `np.asarray(field)`. The quickstart guide is still broken though since the embedded backend (used by default) does not support skip neighbors connectivities.
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