Skip to content

Drop traversal.{to,from}_numpy #327

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
498 changes: 21 additions & 477 deletions .basedpyright/baseline.json

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions arraycontext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
"""

from .container import (
ArithArrayContainer,
ArrayContainer,
ArrayContainerT,
NotAnArrayContainerError,
SerializationKey,
SerializedContainer,
Expand All @@ -54,7 +51,6 @@
flat_size_and_dtype,
flatten,
freeze,
from_numpy,
map_array_container,
map_reduce_array_container,
mapped_over_array_containers,
Expand All @@ -69,14 +65,30 @@
rec_multimap_reduce_array_container,
stringify_array_container_tree,
thaw,
to_numpy,
unflatten,
with_array_context,
)
from .context import (
Array,
ArrayContext,
ArrayContextFactory,
tag_axes,
)
from .impl.jax import EagerJAXArrayContext
from .impl.numpy import NumpyArrayContext
from .impl.pyopencl import PyOpenCLArrayContext
from .impl.pytato import PytatoJAXArrayContext, PytatoPyOpenCLArrayContext
from .loopy import make_loopy_program
from .pytest import (
PytestArrayContextFactory,
PytestPyOpenCLArrayContextFactory,
pytest_generate_tests_for_array_contexts,
)
from .transform_metadata import CommonSubexpressionTag, ElementwiseMapKernelTag
from .typing import (
ArithArrayContainer,
Array,
ArrayContainer,
ArrayContainerT,
ArrayOrArithContainer,
ArrayOrArithContainerOrScalar,
ArrayOrArithContainerOrScalarT,
Expand All @@ -88,21 +100,10 @@
ArrayOrScalar,
ArrayOrScalarT,
ArrayT,
ContainerOrScalarT,
Scalar,
ScalarLike,
tag_axes,
)
from .impl.jax import EagerJAXArrayContext
from .impl.numpy import NumpyArrayContext
from .impl.pyopencl import PyOpenCLArrayContext
from .impl.pytato import PytatoJAXArrayContext, PytatoPyOpenCLArrayContext
from .loopy import make_loopy_program
from .pytest import (
PytestArrayContextFactory,
PytestPyOpenCLArrayContextFactory,
pytest_generate_tests_for_array_contexts,
)
from .transform_metadata import CommonSubexpressionTag, ElementwiseMapKernelTag


__all__ = (
Expand All @@ -125,6 +126,7 @@
"ArrayT",
"BcastUntilActxArray",
"CommonSubexpressionTag",
"ContainerOrScalarT",
"EagerJAXArrayContext",
"ElementwiseMapKernelTag",
"NotAnArrayContainerError",
Expand All @@ -143,7 +145,6 @@
"flat_size_and_dtype",
"flatten",
"freeze",
"from_numpy",
"get_container_context_opt",
"get_container_context_recursively",
"get_container_context_recursively_opt",
Expand All @@ -168,7 +169,6 @@
"stringify_array_container_tree",
"tag_axes",
"thaw",
"to_numpy",
"unflatten",
"with_array_context",
"with_container_arithmetic",
Expand Down
76 changes: 18 additions & 58 deletions arraycontext/container/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
This should be considered experimental for now, and it may well change.

.. autoclass:: ArithArrayContainer
.. class:: ArrayContainerT

A type variable with a lower bound of :class:`ArrayContainer`.
.. autoclass:: ArrayContainerT

.. autoexception:: NotAnArrayContainerError

Expand Down Expand Up @@ -95,12 +93,6 @@

from __future__ import annotations

from types import GenericAlias, UnionType

from numpy.typing import NDArray

from arraycontext.context import ArrayOrArithContainer, ArrayOrContainerOrScalar


__copyright__ = """
Copyright (C) 2020-1 University of Illinois Board of Trustees
Expand Down Expand Up @@ -128,70 +120,38 @@

from collections.abc import Hashable, Sequence
from functools import singledispatch
from types import GenericAlias, UnionType
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Protocol,
TypeAlias,
TypeVar,
get_origin,
)

# 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 Self, TypeIs


if TYPE_CHECKING:
from pymbolic.geometric_algebra import CoeffT, MultiVector
from typing_extensions import TypeIs

from arraycontext.context import ArrayContext, ArrayOrScalar
from pytools.obj_array import ObjectArrayND as ObjectArrayND


# {{{ ArrayContainer

class _UserDefinedArrayContainer(Protocol):
# This is used as a type annotation in dataclasses that are processed
# by dataclass_array_container, where it's used to recognize attributes
# that are container-typed.

# This method prevents ArrayContainer from matching any object, while
# matching numpy object arrays and many array containers.
__array_ufunc__: ClassVar[None]


ArrayContainer: TypeAlias = NDArray[Any] | _UserDefinedArrayContainer


class _UserDefinedArithArrayContainer(_UserDefinedArrayContainer, Protocol):
# This is loose and permissive, assuming that any array can be added
# to any container. The alternative would be to plaster type-ignores
# on all those uses. Achieving typing precision on what broadcasting is
# allowable seems like a huge endeavor and is likely not feasible without
# a mypy plugin. Maybe some day? -AK, November 2024

def __neg__(self) -> Self: ...
def __abs__(self) -> Self: ...
def __add__(self, other: ArrayOrScalar | Self) -> Self: ...
def __radd__(self, other: ArrayOrScalar | Self) -> Self: ...
def __sub__(self, other: ArrayOrScalar | Self) -> Self: ...
def __rsub__(self, other: ArrayOrScalar | Self) -> Self: ...
def __mul__(self, other: ArrayOrScalar | Self) -> Self: ...
def __rmul__(self, other: ArrayOrScalar | Self) -> Self: ...
def __truediv__(self, other: ArrayOrScalar | Self) -> Self: ...
def __rtruediv__(self, other: ArrayOrScalar | Self) -> Self: ...
def __pow__(self, other: ArrayOrScalar | Self) -> Self: ...
def __rpow__(self, other: ArrayOrScalar | Self) -> Self: ...
from arraycontext.typing import (
ArrayContainer,
ArrayContainerT,
ArrayOrArithContainer,
ArrayOrArithContainerOrScalar as ArrayOrArithContainerOrScalar,
ArrayOrContainerOrScalar,
)


ArithArrayContainer: TypeAlias = NDArray[Any] | _UserDefinedArithArrayContainer
if TYPE_CHECKING:
from pymbolic.geometric_algebra import CoeffT, MultiVector

from arraycontext.context import ArrayContext
from arraycontext.typing import ArrayOrScalar as ArrayOrScalar

ArrayContainerT = TypeVar("ArrayContainerT", bound=ArrayContainer)

# {{{ ArrayContainer traversals

class NotAnArrayContainerError(TypeError):
""":class:`TypeError` subclass raised when an array container is expected."""
Expand Down Expand Up @@ -307,9 +267,9 @@ def get_container_context_opt(ary: ArrayContainer) -> ArrayContext | None:

# {{{ object arrays as array containers

# Sadly, ObjectArray is not usable here.
@serialize_container.register(np.ndarray)
def _serialize_ndarray_container(
ary: numpy.ndarray) -> SerializedContainer:
def _serialize_ndarray_container(ary: numpy.ndarray) -> SerializedContainer:
if ary.dtype.char != "O":
raise NotAnArrayContainerError(
f"cannot serialize '{type(ary).__name__}' with dtype '{ary.dtype}'")
Expand Down
27 changes: 13 additions & 14 deletions arraycontext/container/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
from warnings import warn

import numpy as np
from typing_extensions import override

from pytools.obj_array import (
ObjectArray,
# for backward compatibility
ObjectArray as NumpyObjectArray, # noqa: F401 # pyright: ignore[reportUnusedImport]
)

from arraycontext.container import (
NotAnArrayContainerError,
Expand All @@ -61,8 +68,8 @@
if TYPE_CHECKING:
from collections.abc import Callable

from arraycontext.context import (
ArrayContext,
from arraycontext.context import ArrayContext
from arraycontext.typing import (
ArrayOrContainer,
ArrayOrContainerOrScalar,
)
Expand Down Expand Up @@ -152,17 +159,9 @@ def _format_binary_op_str(op_str: str,
return op_str.format(arg1, arg2)


class NumpyObjectArrayMetaclass(type):
def __instancecheck__(cls, instance: Any) -> bool:
return isinstance(instance, np.ndarray) and instance.dtype == object


class NumpyObjectArray(metaclass=NumpyObjectArrayMetaclass):
pass


class ComplainingNumpyNonObjectArrayMetaclass(type):
def __instancecheck__(cls, instance: Any) -> bool:
@override
def __instancecheck__(cls, instance: object) -> bool:
if isinstance(instance, np.ndarray) and instance.dtype != object:
# Example usage site:
# https://github.com/illinois-ceesd/mirgecom/blob/f5d0d97c41e8c8a05546b1d1a6a2979ec8ea3554/mirgecom/inviscid.py#L148-L149
Expand Down Expand Up @@ -272,7 +271,7 @@ def _deserialize_init_arrays_code(cls, tmpl_instance_name, args):
# - Anything that special-cases np.ndarray by type is broken by design because:
# - np.ndarray is an array context array.
# - numpy object arrays can be array containers.
# Using NumpyObjectArray and NumpyNonObjectArray *may* be better?
# Using ObjectArray and NumpyNonObjectArray *may* be better?
# They're new, so there is no operational experience with them.
#
# - Broadcast rules are hard to change once established, particularly
Expand Down Expand Up @@ -374,7 +373,7 @@ def numpy_pred(name: str) -> str:
raise ValueError("If numpy.ndarray is part of bcast_container_types, "
"bcast_obj_array must be False.")

numpy_check_types: list[type] = [NumpyObjectArray, ComplainingNumpyNonObjectArray]
numpy_check_types: list[type] = [ObjectArray, ComplainingNumpyNonObjectArray]
container_types_bcast_across = tuple(
new_ct
for old_ct in container_types_bcast_across
Expand Down
33 changes: 28 additions & 5 deletions arraycontext/container/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@
get_args,
get_origin,
)
from warnings import warn

import numpy as np

from arraycontext.container import ArrayContainer, is_array_container_type
from pytools.obj_array import ObjectArray

from arraycontext.container import is_array_container_type
from arraycontext.typing import (
ArrayContainer,
ArrayOrContainer,
ArrayOrContainerOrScalar,
)


if TYPE_CHECKING:
Expand All @@ -73,7 +81,15 @@ class _Field(NamedTuple):
type: type


def is_array_type(tp: type, /) -> bool:
def _is_array_or_container_type(tp: type, /) -> bool:
if tp is np.ndarray:
warn("Encountered 'numpy.ndarray' in a dataclass_array_container. "
"This is deprecated and will stop working in 2026. "
"If you meant an object array, use pytools.obj_array.ObjectArray. "
"For other uses, file an issue to discuss.",
DeprecationWarning, stacklevel=3)
return True

from arraycontext import Array
return tp is Array or is_array_container_type(tp)

Expand Down Expand Up @@ -133,14 +149,21 @@ def is_array_field(f: _Field) -> bool:
# pyright has no idea what we're up to. :)
if field_type is ArrayContainer: # pyright: ignore[reportUnnecessaryComparison]
return True
if field_type is ArrayOrContainer: # pyright: ignore[reportUnnecessaryComparison]
return True
if field_type is ArrayOrContainerOrScalar: # pyright: ignore[reportUnnecessaryComparison]
return True

origin = get_origin(field_type)

if origin is ObjectArray:
return True

# NOTE: `UnionType` is returned when using `Type1 | Type2`
if origin in (Union, UnionType): # pyright: ignore[reportDeprecated]
for arg in get_args(field_type): # pyright: ignore[reportAny]
if not (
is_array_type(cast("type", arg))
_is_array_or_container_type(cast("type", arg))
or is_scalar_type(cast("type", arg))):
raise TypeError(
f"Field '{f.name}' union contains non-array container "
Expand Down Expand Up @@ -169,15 +192,15 @@ def is_array_field(f: _Field) -> bool:
if isinstance(field_type, GenericAlias | _BaseGenericAlias | _SpecialForm):
# NOTE: anything except a Union is not allowed
raise TypeError(
f"Typing annotation not supported on field '{f.name}': "
f"Type annotation not supported on field '{f.name}': "
f"'{field_type!r}'")

if not isinstance(field_type, type):
raise TypeError(
f"Field '{f.name}' not an instance of 'type': "
f"'{field_type!r}'")

return is_array_type(field_type)
return _is_array_or_container_type(field_type)

from pytools import partition

Expand Down
Loading
Loading