- Notifications
You must be signed in to change notification settings - Fork 11
PytatoArrayContext.freeze: support container freezes#158
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -65,13 +65,15 @@ | ||
| from typing import Any, Callable, Iterable, List, Optional, Union, Tuple | ||
| from functools import update_wrapper, partial, singledispatch | ||
| from warnings import warn | ||
| import numpy as np | ||
| from arraycontext.context import ArrayContext, Array, _ScalarLike | ||
| from arraycontext.container import ( | ||
| ArrayT, ContainerT, ArrayOrContainerT, NotAnArrayContainerError, | ||
| serialize_container, deserialize_container) | ||
| serialize_container, deserialize_container, | ||
| get_container_context_recursively_opt) | ||
| # {{{ array container traversal helpers | ||
| @@ -519,7 +521,6 @@ def _reduce_wrapper(ary: ContainerT, iterable: Iterable[Tuple[Any, Any]]) -> Any | ||
| # {{{ freeze/thaw | ||
| @singledispatch | ||
| def freeze( | ||
| ary: ArrayOrContainerT, | ||
| actx: Optional[ArrayContext] = None) -> ArrayOrContainerT: | ||
| @@ -533,23 +534,33 @@ def freeze( | ||
| See :meth:`ArrayContext.thaw`. | ||
| """ | ||
| try: | ||
| iterable = serialize_container(ary) | ||
| except NotAnArrayContainerError: | ||
| if actx is None: | ||
| raise TypeError( | ||
| f"cannot freeze arrays of type {type(ary).__name__} " | ||
| "when actx is not supplied. Try calling actx.freeze " | ||
| "directly or supplying an array context") | ||
| else: | ||
| return actx.freeze(ary) | ||
| if actx is None: | ||
| warn("Calling freeze(ary) without specifying actx is deprecated, explicitly" | ||
| " call actx.freeze(ary) instead. This will stop working in 2023.", | ||
| DeprecationWarning, stacklevel=2) | ||
| actx = get_container_context_recursively_opt(ary) | ||
| else: | ||
| return deserialize_container(ary, [ | ||
| (key, freeze(subary, actx=actx)) for key, subary in iterable | ||
| ]) | ||
| warn("Calling freeze(ary, actx) is deprecated, call actx.freeze(ary)" | ||
| " instead. This will stop working in 2023.", | ||
| DeprecationWarning, stacklevel=2) | ||
| if __debug__: | ||
| rec_actx = get_container_context_recursively_opt(ary) | ||
| if (rec_actx is not None) and (rec_actx is not actx): | ||
| raise ValueError("Supplied array context does not agree with" | ||
| " the one obtained by traversing 'ary'.") | ||
| if actx is None: | ||
| raise TypeError( | ||
| f"cannot freeze arrays of type {type(ary).__name__} " | ||
| "when actx is not supplied. Try calling actx.freeze " | ||
| "directly or supplying an array context") | ||
| return actx.freeze(ary) | ||
| @singledispatch | ||
| def thaw(ary: ArrayOrContainerT, actx: ArrayContext) -> ArrayOrContainerT: | ||
| r"""Thaws recursively by going through all components of the | ||
| :class:`ArrayContainer` *ary*. | ||
| @@ -570,14 +581,41 @@ def thaw(ary: ArrayOrContainerT, actx: ArrayContext) -> ArrayOrContainerT: | ||
| in :mod:`meshmode`. This was necessary because | ||
| :func:`~functools.singledispatch` only dispatches on the first argument. | ||
| """ | ||
| warn("Calling thaw(ary, actx) is deprecated, call actx.thaw(ary) instead." | ||
| " This will stop working in 2023.", | ||
| DeprecationWarning, stacklevel=2) | ||
| if __debug__: | ||
kaushikcfd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| rec_actx = get_container_context_recursively_opt(ary) | ||
| if rec_actx is not None: | ||
| raise ValueError("cannot thaw a container that already has an array" | ||
| " context.") | ||
| return actx.thaw(ary) | ||
| # }}} | ||
| # {{{ with_array_context | ||
| @singledispatch | ||
| def with_array_context(ary: ArrayOrContainerT, | ||
| actx: Optional[ArrayContext]) -> ArrayOrContainerT: | ||
| """ | ||
| Recursively associates *actx* to all the components of *ary*. | ||
| Array container types may use :func:`functools.singledispatch` ``.register`` | ||
| to register container-specific implementations. See `this issue | ||
| <https://github.com/inducer/arraycontext/issues/162>`__ for discussion of | ||
| the future of this functionality. | ||
| """ | ||
| try: | ||
| iterable = serialize_container(ary) | ||
| except NotAnArrayContainerError: | ||
| return actx.thaw(ary) | ||
| return ary | ||
| else: | ||
| return deserialize_container(ary, [ | ||
| (key, thaw(subary, actx)) for key, subary in iterable | ||
| ]) | ||
| return deserialize_container(ary, [(key, with_array_context(subary, actx)) | ||
| for key, subary in iterable]) | ||
| # }}} | ||
| @@ -842,7 +880,10 @@ def _to_numpy_with_check(subary: Any) -> Any: | ||
| f"array of type '{type(subary).__name__}' not in " | ||
| f"supported types {actx.array_types}") | ||
| return rec_map_array_container(_to_numpy_with_check, ary) | ||
| return rec_map_array_container(_to_numpy_with_check, | ||
| # do a freeze first, if 'actx' supports | ||
| # container-wide freezes | ||
| thaw(freeze(ary, actx), actx)) | ||
| # }}} | ||
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.