- Notifications
You must be signed in to change notification settings - Fork 11
Numpy array context rebased#190
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
32 commits
Select commit
Hold shift + click to select a range
14d4476
Fix some newly-flagged UP031 issues
inducer ee96ff5
Drop deprecated actx.{empty,zeros}{,_like}
inducer 0c24aad
Fix a return type in ArgSizeLimitingPytatoLoopyPyOpenCLTarget
inducer 02ab097
Separate doc page for actx abstraction from doc page for implementations
inducer c888489
Give up on precisely typing Array.__getitem__
inducer cd124ba
Fix doc upload script to properly sync deletions
inducer 5d8158d
Deprecate with_container_arithmetic's bcast_numpy_array arg
kaushikcfd 228ef16
Implements NumpyArrayContext
kaushikcfd 1dc8c94
ArrayContainer fixes for numpy arrays as leaf classes
kaushikcfd 51b46bd
arithmetic fixes to account for np.ndarray being a leaf array
kaushikcfd 6308dc1
test NumpyArrayContext
kaushikcfd b5ea270
test tweaks for NumpyArrayContext
kaushikcfd 80c0672
Numpy actx: add arange, linspace
matthiasdiener 6d3b02a
Numpy actx: add zeros_like, reshape
matthiasdiener 4125e02
Numpy actx: better freeze/thaw
matthiasdiener 5da96a8
Numpy actx: Narrow array_types to non-obj arrays
inducer aa53572
Numpy actx: improve type annotations
inducer cf3f4fb
Array container arithemtic: drop deprecated fail-safe actx retrieval
inducer 1af76ce
Skip tagging test for numpy actx
inducer b58e38e
Skip numpy conversion tests when using the numpy actx
inducer eca314f
Don't expect unflatten failure from numpy array for numpy actx
inducer 4b4ee86
Container serialization: iterable -> sequence, plus type aliases
inducer 3d36c07
Improve, type, fix array_equal across all array contexts
inducer 58acd1f
Clarify that actx.array_types allows ABCs
inducer 0feaae1
Rework dataclass array container arithmetic
inducer 4873ef4
Switch to __array_ufunc__ in tests as a way to avoid numpy broadcasting
inducer 74cd298
outer: disallow non-object numpy arrays
inducer 125e936
Fix ruff C409 failures
inducer 9f1cad4
Fix a typo in the pytato actx
inducer 8b1b795
Numpy actx: warn (not error) on no user-provided transforms
inducer 510dc1b
with_container_arithmetic: Rename arguments to signal who broadcasts …
inducer bc323fc
Numpy actx: cache execuctor
inducer 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 |
|---|---|---|
| @@ -12,6 +12,9 @@ | ||
| Serialization/deserialization | ||
| ----------------------------- | ||
| .. autoclass:: SerializationKey | ||
| .. autoclass:: SerializedContainer | ||
| .. autofunction:: is_array_container_type | ||
| .. autofunction:: serialize_container | ||
| .. autofunction:: deserialize_container | ||
| @@ -39,6 +42,14 @@ | ||
| .. class:: ArrayOrContainerT | ||
| :canonical: arraycontext.ArrayOrContainerT | ||
| .. class:: SerializationKey | ||
| :canonical: arraycontext.SerializationKey | ||
| .. class:: SerializedContainer | ||
| :canonical: arraycontext.SerializedContainer | ||
| """ | ||
| from __future__ import annotations | ||
| @@ -69,12 +80,23 @@ | ||
| """ | ||
| from functools import singledispatch | ||
| from typing import TYPE_CHECKING, Any, Iterable, Optional, Protocol, Tuple, TypeVar | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
| Any, | ||
| Hashable, | ||
| Iterable, | ||
| Optional, | ||
| Protocol, | ||
| Sequence, | ||
| Tuple, | ||
| TypeVar, | ||
| ) | ||
| # For use in singledispatch type annotations, because sphinx can't figure out | ||
| # what 'np' is. | ||
| import numpy | ||
| import numpy as np | ||
| from typing_extensions import TypeAlias | ||
| from arraycontext.context import ArrayContext | ||
| @@ -142,23 +164,27 @@ class NotAnArrayContainerError(TypeError): | ||
| """:class:`TypeError` subclass raised when an array container is expected.""" | ||
| SerializationKey: TypeAlias = Hashable | ||
| SerializedContainer: TypeAlias = Sequence[Tuple[SerializationKey, "ArrayOrContainer"]] | ||
| @singledispatch | ||
| def serialize_container( | ||
| ary: ArrayContainer) -> Iterable[Tuple[Any, ArrayOrContainer]]: | ||
| r"""Serialize the array container into an iterable over its components. | ||
| ary: ArrayContainer) -> SerializedContainer: | ||
| r"""Serialize the array container into a sequence over its components. | ||
| The order of the components and their identifiers are entirely under | ||
| the control of the container class. However, the order is required to be | ||
| deterministic, i.e. two calls to :func:`serialize_container` on | ||
| array containers of the same types with the same number of | ||
| sub-arrays must result in an iterable with the keys in the same | ||
| sub-arrays must result in a sequence with the keys in the same | ||
| order. | ||
| If *ary* is mutable, the serialization function is not required to ensure | ||
| that the serialization result reflects the array state at the time of the | ||
| call to :func:`serialize_container`. | ||
| :returns: an :class:`Iterable` of 2-tuples where the first | ||
| :returns: a :class:`Sequence` of 2-tuples where the first | ||
| entry is an identifier for the component and the second entry | ||
| is an array-like component of the :class:`ArrayContainer`. | ||
| Components can themselves be :class:`ArrayContainer`\ s, allowing | ||
| @@ -172,13 +198,13 @@ def serialize_container( | ||
| @singledispatch | ||
| def deserialize_container( | ||
| template: ArrayContainerT, | ||
| iterable: Iterable[Tuple[Any, Any]]) -> ArrayContainerT: | ||
| """Deserialize an iterable into an array container. | ||
| serialized: SerializedContainer) -> ArrayContainerT: | ||
| """Deserialize a sequence into an array container following a *template*. | ||
| :param template: an instance of an existing object that | ||
| can be used to aid in the deserialization. For a similar choice | ||
| see :attr:`~numpy.class.__array_finalize__`. | ||
| :param iterable: an iterable that mirrors the output of | ||
| :param serialized: a sequence that mirrors the output of | ||
| :meth:`serialize_container`. | ||
| """ | ||
| raise NotAnArrayContainerError( | ||
| @@ -218,7 +244,11 @@ def is_array_container(ary: Any) -> bool: | ||
| "cheaper option, see is_array_container_type.", | ||
| DeprecationWarning, stacklevel=2) | ||
| return (serialize_container.dispatch(ary.__class__) | ||
| is not serialize_container.__wrapped__) # type:ignore[attr-defined] | ||
| is not serialize_container.__wrapped__ # type:ignore[attr-defined] | ||
| # numpy values with scalar elements aren't array containers | ||
| and not (isinstance(ary, np.ndarray) | ||
| and ary.dtype.kind != "O") | ||
alexfikl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ) | ||
| @singledispatch | ||
| @@ -238,7 +268,7 @@ def get_container_context_opt(ary: ArrayContainer) -> Optional[ArrayContext]: | ||
| @serialize_container.register(np.ndarray) | ||
| def _serialize_ndarray_container( | ||
| ary: numpy.ndarray) -> Iterable[Tuple[Any, ArrayOrContainer]]: | ||
| ary: numpy.ndarray) -> SerializedContainer: | ||
| if ary.dtype.char != "O": | ||
| raise NotAnArrayContainerError( | ||
| f"cannot serialize '{type(ary).__name__}' with dtype '{ary.dtype}'") | ||
| @@ -252,20 +282,20 @@ def _serialize_ndarray_container( | ||
| for j in range(ary.shape[1]) | ||
| ] | ||
| else: | ||
| return np.ndenumerate(ary) | ||
| return list(np.ndenumerate(ary)) | ||
| @deserialize_container.register(np.ndarray) | ||
| # https://github.com/python/mypy/issues/13040 | ||
| def _deserialize_ndarray_container( # type: ignore[misc] | ||
| template: numpy.ndarray, | ||
| iterable: Iterable[Tuple[Any, ArrayOrContainer]]) -> numpy.ndarray: | ||
| serialized: SerializedContainer) -> numpy.ndarray: | ||
| # disallow subclasses | ||
| assert type(template) is np.ndarray | ||
| assert template.dtype.char == "O" | ||
| result = type(template)(template.shape, dtype=object) | ||
| for i, subary in iterable: | ||
| for i, subary in serialized: | ||
| result[i] = subary | ||
| return result | ||
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.