Skip to content
Merged
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
8 changes: 4 additions & 4 deletions decent_array/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
18 changes: 18 additions & 0 deletions decent_array/_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import operator
from typing import Any

from decent_array._array import Array
Expand All @@ -14,6 +15,23 @@ 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 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 type(key) is tuple:
return tuple(unwrap_key(k) for k in key)
if not isinstance(key, int) and hasattr(key, "__index__") and not hasattr(key, "shape"):
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
11 changes: 6 additions & 5 deletions decent_array/interoperability/_jax/jax_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions decent_array/interoperability/_numpy/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions decent_array/interoperability/_pytorch/pytorch_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -234,9 +234,11 @@ 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))
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
Expand Down Expand Up @@ -426,10 +428,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

Expand Down
40 changes: 31 additions & 9 deletions decent_array/interoperability/_tensorflow/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -236,7 +236,11 @@ def sum(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: boo
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:
return Array(tf.reduce_mean(x.value, axis=axis, keepdims=keepdims))
# 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:
return Array(tf.reduce_min(x.value, axis=axis, keepdims=keepdims))
Expand Down Expand Up @@ -326,17 +330,35 @@ def where(
) -> Array:
return Array(tf.where(condition.value, unwrap(x1), unwrap(x2)))

# 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."""
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

Expand Down Expand Up @@ -442,12 +464,12 @@ 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))
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)

def get_item(self, x: Array, key: ArrayKey) -> Array:
return Array(x.value[key])
return Array(x.value[unwrap_key(key)])

# RNG

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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."
Expand Down
8 changes: 8 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 58 additions & 0 deletions tests/test_iop_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,15 @@ 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)


# Math elementwise -------------------------------------------------------


Expand Down Expand Up @@ -666,6 +675,27 @@ 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:
# 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 --------------------------------------------------------------


Expand Down Expand Up @@ -874,6 +904,34 @@ 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:
# 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

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])
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:
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 ------------------------------------------------------


Expand Down
Loading