Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
14d4476
Fix some newly-flagged UP031 issues
inducer Aug 25, 2024
ee96ff5
Drop deprecated actx.{empty,zeros}{,_like}
inducer Aug 5, 2024
0c24aad
Fix a return type in ArgSizeLimitingPytatoLoopyPyOpenCLTarget
inducer Aug 6, 2024
02ab097
Separate doc page for actx abstraction from doc page for implementations
inducer Aug 5, 2024
c888489
Give up on precisely typing Array.__getitem__
inducer Jul 31, 2024
cd124ba
Fix doc upload script to properly sync deletions
inducer Jul 31, 2024
5d8158d
Deprecate with_container_arithmetic's bcast_numpy_array arg
kaushikcfd Sep 27, 2021
228ef16
Implements NumpyArrayContext
kaushikcfd Sep 26, 2021
1dc8c94
ArrayContainer fixes for numpy arrays as leaf classes
kaushikcfd Sep 26, 2021
51b46bd
arithmetic fixes to account for np.ndarray being a leaf array
kaushikcfd Sep 27, 2021
6308dc1
test NumpyArrayContext
kaushikcfd Sep 26, 2021
b5ea270
test tweaks for NumpyArrayContext
kaushikcfd Sep 27, 2021
80c0672
Numpy actx: add arange, linspace
matthiasdiener May 24, 2024
6d3b02a
Numpy actx: add zeros_like, reshape
matthiasdiener Jun 20, 2023
4125e02
Numpy actx: better freeze/thaw
matthiasdiener Jun 20, 2023
5da96a8
Numpy actx: Narrow array_types to non-obj arrays
inducer Jul 31, 2024
aa53572
Numpy actx: improve type annotations
inducer Jul 31, 2024
cf3f4fb
Array container arithemtic: drop deprecated fail-safe actx retrieval
inducer Jul 12, 2024
1af76ce
Skip tagging test for numpy actx
inducer Jul 31, 2024
b58e38e
Skip numpy conversion tests when using the numpy actx
inducer Aug 1, 2024
eca314f
Don't expect unflatten failure from numpy array for numpy actx
inducer Jul 31, 2024
4b4ee86
Container serialization: iterable -> sequence, plus type aliases
inducer Jul 31, 2024
3d36c07
Improve, type, fix array_equal across all array contexts
inducer Jul 31, 2024
58acd1f
Clarify that actx.array_types allows ABCs
inducer Jul 31, 2024
0feaae1
Rework dataclass array container arithmetic
inducer Jul 31, 2024
4873ef4
Switch to __array_ufunc__ in tests as a way to avoid numpy broadcasting
inducer Aug 6, 2024
74cd298
outer: disallow non-object numpy arrays
inducer Aug 6, 2024
125e936
Fix ruff C409 failures
inducer Aug 11, 2024
9f1cad4
Fix a typo in the pytato actx
inducer Aug 27, 2024
8b1b795
Numpy actx: warn (not error) on no user-provided transforms
inducer Aug 27, 2024
510dc1b
with_container_arithmetic: Rename arguments to signal who broadcasts …
inducer Sep 4, 2024
bc323fc
Numpy actx: cache execuctor
inducer Sep 4, 2024
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
Prev Previous commit
Next Next commit
Array container arithemtic: drop deprecated fail-safe actx retrieval
  • Loading branch information
inducer committed Aug 25, 2024
commit cf3f4fbc94e3e8e77ae723645a024fe317ae6dbe
107 changes: 23 additions & 84 deletions arraycontext/container/arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# mypy: disallow-untyped-defs
from __future__ import annotations


"""
.. currentmodule:: arraycontext
Expand Down Expand Up @@ -32,7 +34,7 @@
THE SOFTWARE.
"""

from typing import Any, Callable, Optional, Tuple, Type, TypeVar, Union
from typing import Any, Callable, Optional, Tuple, TypeVar, Union

import numpy as np

Expand Down Expand Up @@ -125,10 +127,6 @@ def _format_binary_op_str(op_str: str,
return op_str.format(arg1, arg2)


class _FailSafe:
pass


def with_container_arithmetic(
*,
bcast_number: bool = True,
Expand Down Expand Up @@ -266,34 +264,28 @@ def numpy_pred(name: str) -> str:
# }}}

def wrap(cls: Any) -> Any:
cls_has_array_context_attr: Optional[Union[bool, Type[_FailSafe]]] = \
cls_has_array_context_attr: bool | None = \
_cls_has_array_context_attr
bcast_actx_array_type: Optional[Union[bool, Type[_FailSafe]]] = \
bcast_actx_array_type: bool | None = \
_bcast_actx_array_type

if cls_has_array_context_attr is None:
if hasattr(cls, "array_context"):
cls_has_array_context_attr = _FailSafe
warn(f"{cls} has an 'array_context' attribute, but it does not "
"set '_cls_has_array_context_attr' to 'True' when calling "
"'with_container_arithmetic'. This is being interpreted "
"as 'array_context' being permitted to fail. Tolerating "
"these failures comes at a substantial cost. It is "
"deprecated and will stop working in 2023. "
"Having a working 'array_context' attribute is desirable "
"to enable arithmetic with other array types supported "
"by the array context. "
f"If '{cls.__name__}.array_context' will not fail, pass "
raise TypeError(
f"{cls} has an 'array_context' attribute, but it does not "
"set '_cls_has_array_context_attr' to *True* when calling "
"with_container_arithmetic. This is being interpreted "
"as 'array_context' being permitted to fail with an exception, "
"which is no longer allowed. "
f"If {cls.__name__}.array_context will not fail, pass "
Comment thread
inducer marked this conversation as resolved.
"'_cls_has_array_context_attr=True'. "
"If you do not want container arithmetic to make "
"use of the array context, set "
"'_cls_has_array_context_attr=False'.",
stacklevel=2)
"'_cls_has_array_context_attr=False'.")

if bcast_actx_array_type is None:
if cls_has_array_context_attr:
if bcast_number:
# copy over _FailSafe if present
bcast_actx_array_type = cls_has_array_context_attr
else:
bcast_actx_array_type = False
Expand All @@ -310,66 +302,19 @@ def wrap(cls: Any) -> Any:
"'_deserialize_init_arrays_code'. If this is a dataclass, "
"use the 'dataclass_array_container' decorator first.")

if cls_has_array_context_attr is _FailSafe:
def actx_getter_code(arg: str) -> str:
return f"_get_actx({arg})"
else:
def actx_getter_code(arg: str) -> str:
return f"{arg}.array_context"

from pytools.codegen import CodeGenerator, Indentation
gen = CodeGenerator()
gen("""
from numbers import Number
import numpy as np
from arraycontext import (
ArrayContainer, get_container_context_recursively)
from arraycontext import ArrayContainer
from warnings import warn

def _raise_if_actx_none(actx):
if actx is None:
raise ValueError("array containers with frozen arrays "
"cannot be operated upon")
return actx

def _get_actx(ary):
try:
return ary.array_context
except Exception as e:
warn(f"Accessing '{type(ary).__name__}.array_context' failed "
f"({type(e)}: {e}). This should not happen and is "
"deprecated. "
"Please fix the implementation of "
f"'{type(ary).__name__}.array_context' "
"and then set _cls_has_array_context_attr=True when "
"calling with_container_arithmetic to avoid the run time "
"cost of the check that gave you this warning. "
"Using expensive recovery for now.",
DeprecationWarning, stacklevel=3)

return get_container_context_recursively(ary)

def _get_actx_array_types_failsafe(ary):
try:
actx = ary.array_context
except Exception as e:
warn(f"Accessing '{type(ary).__name__}.array_context' failed "
f"({type(e)}: {e}). This should not happen and is "
"deprecated. "
"Please fix the implementation of "
f"'{type(ary).__name__}.array_context' "
"and then set _cls_has_array_context_attr=True when "
"calling with_container_arithmetic to avoid the run time "
"cost of the check that gave you this warning. "
"Using expensive recovery for now.",
DeprecationWarning, stacklevel=3)

actx = get_container_context_recursively(ary)

if actx is None:
return ()

return actx.array_types
""")
gen("")

Expand Down Expand Up @@ -459,9 +404,9 @@ def {fname}(arg1):
gen("if arg2.__class__ is cls:")
with Indentation(gen):
if __debug__ and cls_has_array_context_attr:
gen(f"""
arg1_actx = {actx_getter_code("arg1")}
arg2_actx = {actx_getter_code("arg2")}
gen("""
arg1_actx = arg1.array_context
arg2_actx = arg2.array_context
if arg1_actx is not arg2_actx:
msg = ("array contexts of both arguments "
"must match")
Expand All @@ -477,17 +422,14 @@ def {fname}(arg1):
raise ValueError(msg)""")
gen(f"return cls({zip_init_args})")

if bcast_actx_array_type is _FailSafe:
bcast_actx_ary_types: Tuple[str, ...] = (
"*_get_actx_array_types_failsafe(arg1)",)
elif bcast_actx_array_type:
if bcast_actx_array_type:
if __debug__:
bcast_actx_ary_types = (
"*_raise_if_actx_none("
f"{actx_getter_code('arg1')}).array_types",)
"arg1.array_context).array_types",)
else:
bcast_actx_ary_types = (
f"*{actx_getter_code('arg1')}.array_types",)
"*arg1.array_context.array_types",)
else:
bcast_actx_ary_types = ()

Expand Down Expand Up @@ -521,17 +463,14 @@ def {fname}(arg1):
cls._serialize_init_arrays_code("arg2").items()
})

if bcast_actx_array_type is _FailSafe:
bcast_actx_ary_types = (
"*_get_actx_array_types_failsafe(arg2)",)
elif bcast_actx_array_type:
if bcast_actx_array_type:
if __debug__:
bcast_actx_ary_types = (
"*_raise_if_actx_none("
f"{actx_getter_code('arg2')}).array_types",)
"arg2.array_context).array_types",)
else:
bcast_actx_ary_types = (
f"*{actx_getter_code('arg2')}.array_types",)
"*arg2.array_context.array_types",)
else:
bcast_actx_ary_types = ()

Expand Down