From 36e2a2ecc983b9b46388b871c0877205b8c3310d Mon Sep 17 00:00:00 2001 From: Lethephyr <59792666+Lethephyr@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:32:00 +0000 Subject: [PATCH 1/5] fix: align cross-backend non-float ops, matmul, indexing, and TF creation dtypes --- decent_array/_array.py | 8 +-- decent_array/_utils.py | 16 +++++ .../interoperability/_jax/jax_backend.py | 6 +- .../interoperability/_numpy/numpy_backend.py | 6 +- .../_pytorch/pytorch_backend.py | 15 +++-- .../_tensorflow/tensorflow_backend.py | 48 +++++++++++--- tests/test_array.py | 8 +++ tests/test_iop_functions.py | 64 +++++++++++++++++++ 8 files changed, 144 insertions(+), 27 deletions(-) diff --git a/decent_array/_array.py b/decent_array/_array.py index 73047b7..adeee8f 100644 --- a/decent_array/_array.py +++ b/decent_array/_array.py @@ -11,13 +11,13 @@ Hot-path notes: -* ``__add__``/``__sub__``/``__mul__``/``__truediv__``/``__matmul__``, ``__floordiv__``, ``__mod__``, +* ``__add__``/``__sub__``/``__mul__``/``__truediv__``, ``__floordiv__``, ``__mod__``, the unary ``__neg__``/``__abs__``/``__pow__``, the comparisons ``__eq__``/``__ne__``/``__lt__``/ ``__le__``/``__gt__``/``__ge__`` and the bitwise ``__and__``/``__rand__`` are inlined: every supported framework's tensor implements the equivalent operator natively with numpy-equivalent semantics, so routing through the interoperability layer introduces unnecessary overhead. -* Operators that *do* go through the backend (in-place math, indexing, properties +* Operators that *do* go through the backend (``__matmul__``, shifts, in-place math, indexing, properties like ``shape``/``transpose``) read the cached ``_backend`` slot. * Overriding ``__eq__`` makes :class:`Array` unhashable (Python clears ``__hash__`` automatically). This matches numpy/torch/jax/tf, where element-wise equality is @@ -144,11 +144,11 @@ def __rmod__(self, other: int | float | Array, /) -> Array: def __matmul__(self, other: Array, /) -> Array: """Return the matrix multiplication of the array with ``other``.""" - return Array(self.value @ other.value) + return self._backend.matmul(self, other) def __rmatmul__(self, other: Array, /) -> Array: """Return the matrix multiplication of ``other`` with the array.""" - return Array(other.value @ self.value) + return self._backend.matmul(other, self) def __pow__(self, other: int | float | complex | Array, /) -> Array: """Exponentiate the array element-wise.""" diff --git a/decent_array/_utils.py b/decent_array/_utils.py index adb0462..361b687 100644 --- a/decent_array/_utils.py +++ b/decent_array/_utils.py @@ -1,3 +1,4 @@ +import operator from typing import Any from decent_array._array import Array @@ -14,6 +15,21 @@ def unwrap(x: Any) -> Any: # noqa: ANN401 return x.value if type(x) is Array else x +def unwrap_key(key: Any) -> Any: # noqa: ANN401 + """Normalize an indexing key by unwrapping :class:`Array` items and custom ``__index__`` objects.""" + if type(key) is Array: + return key.value + if isinstance(key, tuple): + return tuple(unwrap_key(k) for k in key) + if ( + not isinstance(key, int | slice | type(None) | type(Ellipsis)) + and not hasattr(key, "shape") + and hasattr(key, "__index__") + ): + return operator.index(key) + return key + + def is_scalar(x: Array) -> bool: """Return True if ``x`` is a 0-dim Array.""" return x.ndim == 0 diff --git a/decent_array/interoperability/_jax/jax_backend.py b/decent_array/interoperability/_jax/jax_backend.py index 7da13eb..66e7d80 100644 --- a/decent_array/interoperability/_jax/jax_backend.py +++ b/decent_array/interoperability/_jax/jax_backend.py @@ -27,7 +27,7 @@ UnsupportedDTypeCreationError, stack_empty_error, ) -from decent_array._utils import is_scalar, unwrap +from decent_array._utils import is_scalar, unwrap, unwrap_key from decent_array.interoperability._abstracts import Backend from decent_array.interoperability._backend_manager import register_backend from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks @@ -384,10 +384,10 @@ def argmin(self, x: Array, axis: int | None = None, keepdims: bool = False) -> A def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex | Array) -> None: # JAX arrays are immutable; rebind the wrapper to a new array with `key` updated. - x.value = x.value.at[key].set(unwrap(value)) + x.value = x.value.at[unwrap_key(key)].set(unwrap(value)) def get_item(self, x: Array, key: ArrayKey) -> Array: - return Array(x.value[key]) + return Array(x.value[unwrap_key(key)]) # RNG diff --git a/decent_array/interoperability/_numpy/numpy_backend.py b/decent_array/interoperability/_numpy/numpy_backend.py index cda1625..a33bfe4 100644 --- a/decent_array/interoperability/_numpy/numpy_backend.py +++ b/decent_array/interoperability/_numpy/numpy_backend.py @@ -22,7 +22,7 @@ UnsupportedDTypeCreationError, stack_empty_error, ) -from decent_array._utils import is_scalar, unwrap +from decent_array._utils import is_scalar, unwrap, unwrap_key from decent_array.interoperability._abstracts import Backend from decent_array.interoperability._backend_manager import register_backend from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks @@ -401,10 +401,10 @@ def argmin(self, x: Array, axis: int | None = None, keepdims: bool = False) -> A return Array(np.argmin(v, axis=axis)) def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex | Array) -> None: - x.value[key] = unwrap(value) + x.value[unwrap_key(key)] = unwrap(value) def get_item(self, x: Array, key: ArrayKey) -> Array: - return Array(x.value[key]) + return Array(x.value[unwrap_key(key)]) # RNG diff --git a/decent_array/interoperability/_pytorch/pytorch_backend.py b/decent_array/interoperability/_pytorch/pytorch_backend.py index b78c2ce..83b81d1 100644 --- a/decent_array/interoperability/_pytorch/pytorch_backend.py +++ b/decent_array/interoperability/_pytorch/pytorch_backend.py @@ -16,7 +16,7 @@ from decent_array import Array from decent_array._errors import MatrixTransposeError, NDimError, UnsupportedDTypeCreationError, stack_empty_error -from decent_array._utils import is_scalar, unwrap +from decent_array._utils import is_scalar, unwrap, unwrap_key from decent_array.interoperability._abstracts import Backend from decent_array.interoperability._backend_manager import register_backend from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks @@ -234,9 +234,10 @@ def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: boo def mean(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: v = x.value + out_dtype = None if v.is_floating_point() or v.is_complex() else torch.get_default_dtype() if axis is None: - return Array(torch.mean(v)) - return Array(torch.mean(v, dim=axis, keepdim=keepdims)) + return Array(torch.mean(v, dtype=out_dtype)) + return Array(torch.mean(v, dim=axis, keepdim=keepdims, dtype=out_dtype)) def min(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: v = x.value @@ -251,10 +252,10 @@ def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: boo return Array(torch.amax(v, dim=axis, keepdim=keepdims)) def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - return Array(torch.any(x.value, dim=axis, keepdim=keepdims)) + return Array(torch.any(x.value, dim=axis, keepdim=keepdims).to(torch.bool)) def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - return Array(torch.all(x.value, dim=axis, keepdim=keepdims)) + return Array(torch.all(x.value, dim=axis, keepdim=keepdims).to(torch.bool)) # Math elementwise — operands may be Array or scalar (operator dunders pass either). # ``Array | float`` covers both: PEP 484's numeric tower implicitly admits ``int``. @@ -426,10 +427,10 @@ def argmin(self, x: Array, axis: int | None = None, keepdims: bool = False) -> A return Array(torch.argmin(x.value, dim=axis, keepdim=keepdims)) def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex | Array) -> None: - x.value[key] = unwrap(value) + x.value[unwrap_key(key)] = unwrap(value) def get_item(self, x: Array, key: ArrayKey) -> Array: - return Array(x.value[key]) + return Array(x.value[unwrap_key(key)]) # RNG diff --git a/decent_array/interoperability/_tensorflow/tensorflow_backend.py b/decent_array/interoperability/_tensorflow/tensorflow_backend.py index 8d961bb..dbd6429 100644 --- a/decent_array/interoperability/_tensorflow/tensorflow_backend.py +++ b/decent_array/interoperability/_tensorflow/tensorflow_backend.py @@ -25,7 +25,7 @@ UnsupportedDTypeCreationError, stack_empty_error, ) -from decent_array._utils import is_scalar, unwrap +from decent_array._utils import is_scalar, unwrap, unwrap_key from decent_array.interoperability._abstracts import Backend from decent_array.interoperability._backend_manager import register_backend from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks @@ -46,7 +46,7 @@ def __init__(self, device: Devices = Devices.CPU) -> None: def empty(self, shape: int | tuple[int, ...]) -> Array: with tf.device(self._native_device): - return Array(tf.experimental.numpy.empty(shape)) + return Array(tf.experimental.numpy.empty(shape, dtype=tf.float32)) def empty_like(self, x: Array, /) -> Array: with tf.device(self._native_device): @@ -68,7 +68,7 @@ def ones_like(self, x: Array) -> Array: def eye(self, n_rows: int, n_cols: int | None = None, /, *, k: int = 0) -> Array: with tf.device(self._native_device): - return Array(tf.experimental.numpy.eye(n_rows, n_cols, k=k)) + return Array(tf.experimental.numpy.eye(n_rows, n_cols, k=k, dtype=tf.float32)) def arange(self, start: int | float, /, stop: int | float | None = None, step: int | float = 1) -> Array: with tf.device(self._native_device): @@ -233,15 +233,23 @@ def eigvalsh(self, x: Array) -> Array: # Math reductions def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - return Array(tf.reduce_sum(x.value, axis=axis, keepdims=keepdims)) + v = tf.cast(x.value, tf.int64) if x.value.dtype == tf.bool else x.value + return Array(tf.reduce_sum(v, axis=axis, keepdims=keepdims)) def mean(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - return Array(tf.reduce_mean(x.value, axis=axis, keepdims=keepdims)) + v = x.value + if not v.dtype.is_floating and not v.dtype.is_complex: + v = tf.cast(v, tf.float32) + return Array(tf.reduce_mean(v, axis=axis, keepdims=keepdims)) def min(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: + if x.value.dtype == tf.bool: + return Array(tf.reduce_all(x.value, axis=axis, keepdims=keepdims)) return Array(tf.reduce_min(x.value, axis=axis, keepdims=keepdims)) def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: + if x.value.dtype == tf.bool: + return Array(tf.reduce_any(x.value, axis=axis, keepdims=keepdims)) return Array(tf.reduce_max(x.value, axis=axis, keepdims=keepdims)) def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: @@ -328,15 +336,30 @@ def where( def isfinite(self, x: Array) -> Array: """Element-wise test for finite values.""" - return Array(tf.math.is_finite(x.value)) + v = x.value + if v.dtype.is_complex: + return Array(tf.math.is_finite(tf.math.real(v)) & tf.math.is_finite(tf.math.imag(v))) + if not v.dtype.is_floating: + return Array(tf.ones_like(v, dtype=tf.bool)) + return Array(tf.math.is_finite(v)) def isinf(self, x: Array) -> Array: """Element-wise test for infinite values.""" - return Array(tf.math.is_inf(x.value)) + v = x.value + if v.dtype.is_complex: + return Array(tf.math.is_inf(tf.math.real(v)) | tf.math.is_inf(tf.math.imag(v))) + if not v.dtype.is_floating: + return Array(tf.zeros_like(v, dtype=tf.bool)) + return Array(tf.math.is_inf(v)) def isnan(self, x: Array) -> Array: """Element-wise test for NaN values.""" - return Array(tf.math.is_nan(x.value)) + v = x.value + if v.dtype.is_complex: + return Array(tf.math.is_nan(tf.math.real(v)) | tf.math.is_nan(tf.math.imag(v))) + if not v.dtype.is_floating: + return Array(tf.zeros_like(v, dtype=tf.bool)) + return Array(tf.math.is_nan(v)) # Comparisons @@ -442,12 +465,17 @@ def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex # that hammer set_item in tight loops should consider numpy or pytorch. original = x.value np_array = original.numpy().copy() - np_array[key] = np.asarray(unwrap(value)) + norm_key = unwrap_key(key) + if isinstance(norm_key, tuple): + norm_key = tuple(k.numpy() if isinstance(k, tf.Tensor) else k for k in norm_key) + elif isinstance(norm_key, tf.Tensor): + norm_key = norm_key.numpy() + np_array[norm_key] = np.asarray(unwrap(value)) with tf.device(self._native_device): x.value = tf.convert_to_tensor(np_array, dtype=original.dtype) def get_item(self, x: Array, key: ArrayKey) -> Array: - return Array(x.value[key]) + return Array(x.value[unwrap_key(key)]) # RNG diff --git a/tests/test_array.py b/tests/test_array.py index 13d6519..f6faacd 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -170,6 +170,14 @@ def test_matmul_array(backend: tuple) -> None: np.testing.assert_allclose(_np(a @ b), expected) +def test_matmul_vector(backend: tuple) -> None: + mat = _create_array([[1.0, 2.0], [3.0, 4.0]]) + vec = _create_array([5.0, 6.0]) + np.testing.assert_allclose(_np(mat @ vec), [17.0, 39.0]) + np.testing.assert_allclose(_np(vec @ mat), [23.0, 34.0]) + np.testing.assert_allclose(_np(vec @ vec), 61.0) + + def test_rmatmul_explicit_call(backend: tuple) -> None: # Python dispatch never picks Array.__rmatmul__ when both operands are Array, # so call it directly to exercise the code path. diff --git a/tests/test_iop_functions.py b/tests/test_iop_functions.py index 65c998f..a458a9b 100644 --- a/tests/test_iop_functions.py +++ b/tests/test_iop_functions.py @@ -522,6 +522,29 @@ def test_all_dim_keepdims(backend: tuple) -> None: np.testing.assert_array_equal(_np(iop.all(arr, axis=0, keepdims=True)), [[True, False]]) +def test_mean_bool_and_int(backend: tuple) -> None: + bool_arr = iop.from_numpy(np.array([[True, False], [True, True]], dtype=np.bool_)) + np.testing.assert_allclose(_np(iop.mean(bool_arr)), 0.75) + np.testing.assert_allclose(_np(iop.mean(bool_arr, axis=1)), [0.5, 1.0]) + + int_arr = iop.from_numpy(np.array([1, 2, 3, 4], dtype=np.int32)) + np.testing.assert_allclose(_np(iop.mean(int_arr)), 2.5) + + +def test_reductions_bool_and_uint8(backend: tuple) -> None: + bool_arr = iop.from_numpy(np.array([[True, False], [True, True]], dtype=np.bool_)) + np.testing.assert_array_equal(_np(iop.sum(bool_arr)), 3) + np.testing.assert_array_equal(_np(iop.sum(bool_arr, axis=0)), [2, 1]) + assert iop.min(bool_arr).dtype == da.bool_ + assert iop.max(bool_arr).dtype == da.bool_ + np.testing.assert_array_equal(_np(iop.min(bool_arr, axis=1)), [False, True]) + np.testing.assert_array_equal(_np(iop.max(bool_arr, axis=0)), [True, True]) + + uint8_arr = iop.from_numpy(np.array([0, 1, 2], dtype=np.uint8)) + assert iop.any(uint8_arr).dtype == da.bool_ + assert iop.all(uint8_arr).dtype == da.bool_ + + # Math elementwise ------------------------------------------------------- @@ -666,6 +689,22 @@ def test_isnan(backend: tuple) -> None: np.testing.assert_array_equal(_np(iop.isnan(arr)), [False, False, False, True]) +@pytest.mark.parametrize("dtype", [np.bool_, np.int32, np.uint8]) +def test_finiteness_checks_non_float(backend: tuple, dtype: type[np.generic]) -> None: + arr = iop.from_numpy(np.array([0, 1, 1], dtype=dtype)) + for fn, expected in ((iop.isfinite, True), (iop.isinf, False), (iop.isnan, False)): + out = fn(arr) + assert out.dtype == da.bool_ + np.testing.assert_array_equal(_np(out), [expected, expected, expected]) + + +def test_finiteness_checks_complex(backend: tuple) -> None: + arr = iop.from_numpy(np.array([1.0 + 2.0j, np.inf + 0.0j, 0.0 + np.nan * 1j], dtype=np.complex64)) + np.testing.assert_array_equal(_np(iop.isfinite(arr)), [True, False, False]) + np.testing.assert_array_equal(_np(iop.isinf(arr)), [False, True, False]) + np.testing.assert_array_equal(_np(iop.isnan(arr)), [False, False, True]) + + # Operators -------------------------------------------------------------- @@ -874,6 +913,31 @@ def test_get_item_function(backend: tuple) -> None: np.testing.assert_allclose(_np(get_item(arr, 1)), 2.0) +def test_supports_index_and_array_keys(backend: tuple) -> None: + class CustomIndex: + def __init__(self, idx: int) -> None: + self._idx = idx + + def __index__(self) -> int: + return self._idx + + mat = iop.from_numpy(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)) + i0, i1 = CustomIndex(0), CustomIndex(1) + np.testing.assert_allclose(_np(mat[i0, i1]), 2.0) + mat[i1, i0] = 9.0 + np.testing.assert_allclose(_np(mat[i1, i0]), 9.0) + + mask = mat > 2.5 + np.testing.assert_allclose(_np(mat[mask]), [9.0, 4.0]) + + +def test_creation_default_float_dtype_consistent(backend: tuple) -> None: + expected = iop.zeros((2, 2)).dtype + assert iop.ones((2, 2)).dtype == expected + assert iop.empty((2, 2)).dtype == expected + assert iop.eye(2).dtype == expected + + # No-backend errors ------------------------------------------------------ From 701f55e9397a358c3eb29019cb0ae342e3749bdb Mon Sep 17 00:00:00 2001 From: Lethephyr <59792666+Lethephyr@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:44:38 +0000 Subject: [PATCH 2/5] ref: slim non-float fixes to what decent-bench needs Drop branches no caller exercises: complex isfinite/isinf/isnan, TF bool sum/min/max, PyTorch uint8 any/all cast, and the tf.Tensor key conversion in TF set_item (numpy already accepts eager tensors). Simplify unwrap_key, keeping custom __index__ support for W[agent_i, agent_j] since TF and JAX reject such keys. --- decent_array/_utils.py | 16 +++++++------ .../_pytorch/pytorch_backend.py | 5 ++-- .../_tensorflow/tensorflow_backend.py | 23 ++++-------------- tests/test_iop_functions.py | 24 +++---------------- 4 files changed, 20 insertions(+), 48 deletions(-) diff --git a/decent_array/_utils.py b/decent_array/_utils.py index 361b687..a04031e 100644 --- a/decent_array/_utils.py +++ b/decent_array/_utils.py @@ -16,16 +16,18 @@ def unwrap(x: Any) -> Any: # noqa: ANN401 def unwrap_key(key: Any) -> Any: # noqa: ANN401 - """Normalize an indexing key by unwrapping :class:`Array` items and custom ``__index__`` objects.""" + """ + Normalize an indexing key for the native framework. + + Unwraps :class:`Array` items and converts custom ``SupportsIndex`` objects (e.g. ``W[agent_i, agent_j]``) + to ``int``, since TensorFlow and JAX reject them. Native arrays/scalars (which expose ``shape``) and + ``bool`` (which numpy treats as a new axis) are passed through unchanged. + """ if type(key) is Array: return key.value - if isinstance(key, tuple): + if type(key) is tuple: return tuple(unwrap_key(k) for k in key) - if ( - not isinstance(key, int | slice | type(None) | type(Ellipsis)) - and not hasattr(key, "shape") - and hasattr(key, "__index__") - ): + if not isinstance(key, int) and hasattr(key, "__index__") and not hasattr(key, "shape"): return operator.index(key) return key diff --git a/decent_array/interoperability/_pytorch/pytorch_backend.py b/decent_array/interoperability/_pytorch/pytorch_backend.py index 83b81d1..b22cc66 100644 --- a/decent_array/interoperability/_pytorch/pytorch_backend.py +++ b/decent_array/interoperability/_pytorch/pytorch_backend.py @@ -234,6 +234,7 @@ def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: boo def mean(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: v = x.value + # torch.mean refuses bool/int inputs ("could not infer output dtype"); promote to float like numpy. out_dtype = None if v.is_floating_point() or v.is_complex() else torch.get_default_dtype() if axis is None: return Array(torch.mean(v, dtype=out_dtype)) @@ -252,10 +253,10 @@ def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: boo return Array(torch.amax(v, dim=axis, keepdim=keepdims)) def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - return Array(torch.any(x.value, dim=axis, keepdim=keepdims).to(torch.bool)) + return Array(torch.any(x.value, dim=axis, keepdim=keepdims)) def all(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - return Array(torch.all(x.value, dim=axis, keepdim=keepdims).to(torch.bool)) + return Array(torch.all(x.value, dim=axis, keepdim=keepdims)) # Math elementwise — operands may be Array or scalar (operator dunders pass either). # ``Array | float`` covers both: PEP 484's numeric tower implicitly admits ``int``. diff --git a/decent_array/interoperability/_tensorflow/tensorflow_backend.py b/decent_array/interoperability/_tensorflow/tensorflow_backend.py index dbd6429..1fbae0c 100644 --- a/decent_array/interoperability/_tensorflow/tensorflow_backend.py +++ b/decent_array/interoperability/_tensorflow/tensorflow_backend.py @@ -233,23 +233,19 @@ def eigvalsh(self, x: Array) -> Array: # Math reductions def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - v = tf.cast(x.value, tf.int64) if x.value.dtype == tf.bool else x.value - return Array(tf.reduce_sum(v, axis=axis, keepdims=keepdims)) + return Array(tf.reduce_sum(x.value, axis=axis, keepdims=keepdims)) def mean(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: + # tf.reduce_mean rejects bool and truncates integer means; promote to float like numpy. v = x.value if not v.dtype.is_floating and not v.dtype.is_complex: v = tf.cast(v, tf.float32) return Array(tf.reduce_mean(v, axis=axis, keepdims=keepdims)) def min(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - if x.value.dtype == tf.bool: - return Array(tf.reduce_all(x.value, axis=axis, keepdims=keepdims)) return Array(tf.reduce_min(x.value, axis=axis, keepdims=keepdims)) def max(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: - if x.value.dtype == tf.bool: - return Array(tf.reduce_any(x.value, axis=axis, keepdims=keepdims)) return Array(tf.reduce_max(x.value, axis=axis, keepdims=keepdims)) def any(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array: @@ -334,11 +330,11 @@ def where( ) -> Array: return Array(tf.where(condition.value, unwrap(x1), unwrap(x2))) + # tf.math.is_* only accept real floats; integer/bool values are always finite and never inf/nan. + def isfinite(self, x: Array) -> Array: """Element-wise test for finite values.""" v = x.value - if v.dtype.is_complex: - return Array(tf.math.is_finite(tf.math.real(v)) & tf.math.is_finite(tf.math.imag(v))) if not v.dtype.is_floating: return Array(tf.ones_like(v, dtype=tf.bool)) return Array(tf.math.is_finite(v)) @@ -346,8 +342,6 @@ def isfinite(self, x: Array) -> Array: def isinf(self, x: Array) -> Array: """Element-wise test for infinite values.""" v = x.value - if v.dtype.is_complex: - return Array(tf.math.is_inf(tf.math.real(v)) | tf.math.is_inf(tf.math.imag(v))) if not v.dtype.is_floating: return Array(tf.zeros_like(v, dtype=tf.bool)) return Array(tf.math.is_inf(v)) @@ -355,8 +349,6 @@ def isinf(self, x: Array) -> Array: def isnan(self, x: Array) -> Array: """Element-wise test for NaN values.""" v = x.value - if v.dtype.is_complex: - return Array(tf.math.is_nan(tf.math.real(v)) | tf.math.is_nan(tf.math.imag(v))) if not v.dtype.is_floating: return Array(tf.zeros_like(v, dtype=tf.bool)) return Array(tf.math.is_nan(v)) @@ -465,12 +457,7 @@ def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex # that hammer set_item in tight loops should consider numpy or pytorch. original = x.value np_array = original.numpy().copy() - norm_key = unwrap_key(key) - if isinstance(norm_key, tuple): - norm_key = tuple(k.numpy() if isinstance(k, tf.Tensor) else k for k in norm_key) - elif isinstance(norm_key, tf.Tensor): - norm_key = norm_key.numpy() - np_array[norm_key] = np.asarray(unwrap(value)) + np_array[unwrap_key(key)] = np.asarray(unwrap(value)) with tf.device(self._native_device): x.value = tf.convert_to_tensor(np_array, dtype=original.dtype) diff --git a/tests/test_iop_functions.py b/tests/test_iop_functions.py index a458a9b..8c7e22c 100644 --- a/tests/test_iop_functions.py +++ b/tests/test_iop_functions.py @@ -531,20 +531,6 @@ def test_mean_bool_and_int(backend: tuple) -> None: np.testing.assert_allclose(_np(iop.mean(int_arr)), 2.5) -def test_reductions_bool_and_uint8(backend: tuple) -> None: - bool_arr = iop.from_numpy(np.array([[True, False], [True, True]], dtype=np.bool_)) - np.testing.assert_array_equal(_np(iop.sum(bool_arr)), 3) - np.testing.assert_array_equal(_np(iop.sum(bool_arr, axis=0)), [2, 1]) - assert iop.min(bool_arr).dtype == da.bool_ - assert iop.max(bool_arr).dtype == da.bool_ - np.testing.assert_array_equal(_np(iop.min(bool_arr, axis=1)), [False, True]) - np.testing.assert_array_equal(_np(iop.max(bool_arr, axis=0)), [True, True]) - - uint8_arr = iop.from_numpy(np.array([0, 1, 2], dtype=np.uint8)) - assert iop.any(uint8_arr).dtype == da.bool_ - assert iop.all(uint8_arr).dtype == da.bool_ - - # Math elementwise ------------------------------------------------------- @@ -698,13 +684,6 @@ def test_finiteness_checks_non_float(backend: tuple, dtype: type[np.generic]) -> np.testing.assert_array_equal(_np(out), [expected, expected, expected]) -def test_finiteness_checks_complex(backend: tuple) -> None: - arr = iop.from_numpy(np.array([1.0 + 2.0j, np.inf + 0.0j, 0.0 + np.nan * 1j], dtype=np.complex64)) - np.testing.assert_array_equal(_np(iop.isfinite(arr)), [True, False, False]) - np.testing.assert_array_equal(_np(iop.isinf(arr)), [False, True, False]) - np.testing.assert_array_equal(_np(iop.isnan(arr)), [False, False, True]) - - # Operators -------------------------------------------------------------- @@ -914,6 +893,7 @@ def test_get_item_function(backend: tuple) -> None: def test_supports_index_and_array_keys(backend: tuple) -> None: + # Mirrors decent-bench agents, which implement __index__ so that ``W[agent_i, agent_j]`` works. class CustomIndex: def __init__(self, idx: int) -> None: self._idx = idx @@ -929,6 +909,8 @@ def __index__(self) -> int: mask = mat > 2.5 np.testing.assert_allclose(_np(mat[mask]), [9.0, 4.0]) + mat[mask] = 0.0 + np.testing.assert_allclose(_np(mat), [[1.0, 2.0], [0.0, 0.0]]) def test_creation_default_float_dtype_consistent(backend: tuple) -> None: From 7472aa8c63a4aa697222a97ca684c3748e2099c4 Mon Sep 17 00:00:00 2001 From: Lethephyr <59792666+Lethephyr@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:49:00 +0000 Subject: [PATCH 3/5] fix: handle complex values in TF isfinite/isinf/isnan --- .../_tensorflow/tensorflow_backend.py | 9 ++++++++- tests/test_iop_functions.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/decent_array/interoperability/_tensorflow/tensorflow_backend.py b/decent_array/interoperability/_tensorflow/tensorflow_backend.py index 1fbae0c..bbc6007 100644 --- a/decent_array/interoperability/_tensorflow/tensorflow_backend.py +++ b/decent_array/interoperability/_tensorflow/tensorflow_backend.py @@ -330,11 +330,14 @@ def where( ) -> Array: return Array(tf.where(condition.value, unwrap(x1), unwrap(x2))) - # tf.math.is_* only accept real floats; integer/bool values are always finite and never inf/nan. + # tf.math.is_* only accept real floats: complex values are checked component-wise (numpy semantics), and + # integer/bool values are always finite and never inf/nan. def isfinite(self, x: Array) -> Array: """Element-wise test for finite values.""" v = x.value + if v.dtype.is_complex: + return Array(tf.math.is_finite(tf.math.real(v)) & tf.math.is_finite(tf.math.imag(v))) if not v.dtype.is_floating: return Array(tf.ones_like(v, dtype=tf.bool)) return Array(tf.math.is_finite(v)) @@ -342,6 +345,8 @@ def isfinite(self, x: Array) -> Array: def isinf(self, x: Array) -> Array: """Element-wise test for infinite values.""" v = x.value + if v.dtype.is_complex: + return Array(tf.math.is_inf(tf.math.real(v)) | tf.math.is_inf(tf.math.imag(v))) if not v.dtype.is_floating: return Array(tf.zeros_like(v, dtype=tf.bool)) return Array(tf.math.is_inf(v)) @@ -349,6 +354,8 @@ def isinf(self, x: Array) -> Array: def isnan(self, x: Array) -> Array: """Element-wise test for NaN values.""" v = x.value + if v.dtype.is_complex: + return Array(tf.math.is_nan(tf.math.real(v)) | tf.math.is_nan(tf.math.imag(v))) if not v.dtype.is_floating: return Array(tf.zeros_like(v, dtype=tf.bool)) return Array(tf.math.is_nan(v)) diff --git a/tests/test_iop_functions.py b/tests/test_iop_functions.py index 8c7e22c..c5ddad8 100644 --- a/tests/test_iop_functions.py +++ b/tests/test_iop_functions.py @@ -684,6 +684,18 @@ def test_finiteness_checks_non_float(backend: tuple, dtype: type[np.generic]) -> np.testing.assert_array_equal(_np(out), [expected, expected, expected]) +def test_finiteness_checks_complex(backend: tuple) -> None: + # Built with complex(re, im): ``np.nan * 1j`` would give nan+nanj and hide component-wise bugs. + vals = np.array( + [complex(1, 2), complex(np.inf, 0), complex(0, -np.inf), complex(np.nan, 0), complex(0, np.nan)], + dtype=np.complex64, + ) + arr = iop.from_numpy(vals) + np.testing.assert_array_equal(_np(iop.isfinite(arr)), np.isfinite(vals)) # [T, F, F, F, F] + np.testing.assert_array_equal(_np(iop.isinf(arr)), np.isinf(vals)) # [F, T, T, F, F] + np.testing.assert_array_equal(_np(iop.isnan(arr)), np.isnan(vals)) # [F, F, F, T, T] + + # Operators -------------------------------------------------------------- From a81b9b825e990757cef5b8b2bf0b622063f62e91 Mon Sep 17 00:00:00 2001 From: Lethephyr <59792666+Lethephyr@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:47:55 +0000 Subject: [PATCH 4/5] fix: avoid redundant cast in JaxBackend._next_key --- decent_array/interoperability/_jax/jax_backend.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/decent_array/interoperability/_jax/jax_backend.py b/decent_array/interoperability/_jax/jax_backend.py index 66e7d80..622f4e5 100644 --- a/decent_array/interoperability/_jax/jax_backend.py +++ b/decent_array/interoperability/_jax/jax_backend.py @@ -431,8 +431,9 @@ def choice(self, x: Array, size: int, replace: bool = True) -> Array: def _next_key(self) -> jax.Array: """Split the stored key, advance state, return a sub-key for one draw.""" - self._key, sub = jax.random.split(self._key) - return cast("jax.Array", sub) + self._key, raw_sub = jax.random.split(self._key) + sub: jax.Array = raw_sub + return sub # Dtypes From 969a3d9020d068af00997dddfa08fe12fe305824 Mon Sep 17 00:00:00 2001 From: Lethephyr <59792666+Lethephyr@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:37:32 +0000 Subject: [PATCH 5/5] meta: bump version to 0.2.8 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 7313e52..4cafebd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "decent-array" -version = "0.2.7" +version = "0.2.8" authors = [{name = "Simon Granström"}, {name = "Nicola Bastianello"}] maintainers = [{name = "Team Decent"}] description = "A library of array operations and linear algebra primitives for interoperability across ML frameworks."