decent-array 0.2.7__tar.gz → 0.2.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {decent_array-0.2.7 → decent_array-0.2.8}/PKG-INFO +1 -1
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/_array.py +4 -4
- decent_array-0.2.8/decent_array/_utils.py +37 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_jax/jax_backend.py +6 -5
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_numpy/numpy_backend.py +3 -3
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_pytorch/pytorch_backend.py +7 -5
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_tensorflow/tensorflow_backend.py +31 -9
- {decent_array-0.2.7 → decent_array-0.2.8}/pyproject.toml +1 -1
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/test_array.py +8 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/test_iop_functions.py +58 -0
- decent_array-0.2.7/decent_array/_utils.py +0 -19
- {decent_array-0.2.7 → decent_array-0.2.8}/.github/CODEOWNERS +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/.github/dependabot.yml +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/.github/workflows/ci.yaml +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/.gitignore +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/LICENSE +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/README.md +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/benchmarks/bench_array.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/benchmarks/bench_common.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/benchmarks/bench_iop.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/benchmarks/profile_hotpath.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/_constants.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/_errors.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_abstracts/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_abstracts/backend.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_backend_manager.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/bit_operators.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/comparison.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/creation.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/linalg.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/manipulations.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/math.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/operators.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/reductions.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/rng.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/utils.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_jax/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_numpy/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_pytorch/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_tensorflow/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/py.typed +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/types/__init__.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/types/_dtypes.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/decent_array/types/_types.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/Makefile +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/make.bat +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/requirements.txt +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/_static/custom.css +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/_static/logo.png +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/_templates/module.rst.jinja +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/_templates/package.rst.jinja +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/api/decent_array.array.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/api/decent_array.interoperability.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/api/decent_array.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/api/decent_array.types.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/author.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/conf.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/developer.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/index.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/docs/source/user.rst +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/readthedocs.yaml +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/conftest.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/test_backend_manager.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/test_bindings.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/test_dtypes.py +0 -0
- {decent_array-0.2.7 → decent_array-0.2.8}/tests/test_iop_rng.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: decent-array
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.8
|
|
4
4
|
Summary: A library of array operations and linear algebra primitives for interoperability across ML frameworks.
|
|
5
5
|
Project-URL: Documentation, https://decent-array.readthedocs.io/en/latest/
|
|
6
6
|
Project-URL: Source, https://github.com/team-decent/decent-array
|
|
@@ -11,13 +11,13 @@ Operator contract is *strict*: binary arithmetic and indexing accept either anot
|
|
|
11
11
|
|
|
12
12
|
Hot-path notes:
|
|
13
13
|
|
|
14
|
-
* ``__add__``/``__sub__``/``__mul__``/``__truediv__
|
|
14
|
+
* ``__add__``/``__sub__``/``__mul__``/``__truediv__``, ``__floordiv__``, ``__mod__``,
|
|
15
15
|
the unary ``__neg__``/``__abs__``/``__pow__``, the comparisons ``__eq__``/``__ne__``/``__lt__``/
|
|
16
16
|
``__le__``/``__gt__``/``__ge__`` and the bitwise ``__and__``/``__rand__`` are
|
|
17
17
|
inlined: every supported framework's tensor implements the equivalent operator
|
|
18
18
|
natively with numpy-equivalent semantics, so routing through the interoperability layer
|
|
19
19
|
introduces unnecessary overhead.
|
|
20
|
-
* Operators that *do* go through the backend (in-place math, indexing, properties
|
|
20
|
+
* Operators that *do* go through the backend (``__matmul__``, shifts, in-place math, indexing, properties
|
|
21
21
|
like ``shape``/``transpose``) read the cached ``_backend`` slot.
|
|
22
22
|
* Overriding ``__eq__`` makes :class:`Array` unhashable (Python clears ``__hash__``
|
|
23
23
|
automatically). This matches numpy/torch/jax/tf, where element-wise equality is
|
|
@@ -144,11 +144,11 @@ class Array:
|
|
|
144
144
|
|
|
145
145
|
def __matmul__(self, other: Array, /) -> Array:
|
|
146
146
|
"""Return the matrix multiplication of the array with ``other``."""
|
|
147
|
-
return
|
|
147
|
+
return self._backend.matmul(self, other)
|
|
148
148
|
|
|
149
149
|
def __rmatmul__(self, other: Array, /) -> Array:
|
|
150
150
|
"""Return the matrix multiplication of ``other`` with the array."""
|
|
151
|
-
return
|
|
151
|
+
return self._backend.matmul(other, self)
|
|
152
152
|
|
|
153
153
|
def __pow__(self, other: int | float | complex | Array, /) -> Array:
|
|
154
154
|
"""Exponentiate the array element-wise."""
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import operator
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from decent_array._array import Array
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def unwrap(x: Any) -> Any: # noqa: ANN401
|
|
8
|
+
"""
|
|
9
|
+
Return the underlying value of an :class:`Array`, or pass ``x`` through.
|
|
10
|
+
|
|
11
|
+
Typed as ``Any`` because operator dunders may pass either an :class:`Array` or a
|
|
12
|
+
Python scalar; the strict abstract signature would force a ``cast`` at every call
|
|
13
|
+
site without runtime benefit.
|
|
14
|
+
"""
|
|
15
|
+
return x.value if type(x) is Array else x
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def unwrap_key(key: Any) -> Any: # noqa: ANN401
|
|
19
|
+
"""
|
|
20
|
+
Normalize an indexing key for the native framework.
|
|
21
|
+
|
|
22
|
+
Unwraps :class:`Array` items and converts custom ``SupportsIndex`` objects (e.g. ``W[agent_i, agent_j]``)
|
|
23
|
+
to ``int``, since TensorFlow and JAX reject them. Native arrays/scalars (which expose ``shape``) and
|
|
24
|
+
``bool`` (which numpy treats as a new axis) are passed through unchanged.
|
|
25
|
+
"""
|
|
26
|
+
if type(key) is Array:
|
|
27
|
+
return key.value
|
|
28
|
+
if type(key) is tuple:
|
|
29
|
+
return tuple(unwrap_key(k) for k in key)
|
|
30
|
+
if not isinstance(key, int) and hasattr(key, "__index__") and not hasattr(key, "shape"):
|
|
31
|
+
return operator.index(key)
|
|
32
|
+
return key
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def is_scalar(x: Array) -> bool:
|
|
36
|
+
"""Return True if ``x`` is a 0-dim Array."""
|
|
37
|
+
return x.ndim == 0
|
|
@@ -27,7 +27,7 @@ from decent_array._errors import (
|
|
|
27
27
|
UnsupportedDTypeCreationError,
|
|
28
28
|
stack_empty_error,
|
|
29
29
|
)
|
|
30
|
-
from decent_array._utils import is_scalar, unwrap
|
|
30
|
+
from decent_array._utils import is_scalar, unwrap, unwrap_key
|
|
31
31
|
from decent_array.interoperability._abstracts import Backend
|
|
32
32
|
from decent_array.interoperability._backend_manager import register_backend
|
|
33
33
|
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
|
|
@@ -384,10 +384,10 @@ class JaxBackend(Backend):
|
|
|
384
384
|
|
|
385
385
|
def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex | Array) -> None:
|
|
386
386
|
# JAX arrays are immutable; rebind the wrapper to a new array with `key` updated.
|
|
387
|
-
x.value = x.value.at[key].set(unwrap(value))
|
|
387
|
+
x.value = x.value.at[unwrap_key(key)].set(unwrap(value))
|
|
388
388
|
|
|
389
389
|
def get_item(self, x: Array, key: ArrayKey) -> Array:
|
|
390
|
-
return Array(x.value[key])
|
|
390
|
+
return Array(x.value[unwrap_key(key)])
|
|
391
391
|
|
|
392
392
|
# RNG
|
|
393
393
|
|
|
@@ -431,8 +431,9 @@ class JaxBackend(Backend):
|
|
|
431
431
|
|
|
432
432
|
def _next_key(self) -> jax.Array:
|
|
433
433
|
"""Split the stored key, advance state, return a sub-key for one draw."""
|
|
434
|
-
self._key,
|
|
435
|
-
|
|
434
|
+
self._key, raw_sub = jax.random.split(self._key)
|
|
435
|
+
sub: jax.Array = raw_sub
|
|
436
|
+
return sub
|
|
436
437
|
|
|
437
438
|
# Dtypes
|
|
438
439
|
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_numpy/numpy_backend.py
RENAMED
|
@@ -22,7 +22,7 @@ from decent_array._errors import (
|
|
|
22
22
|
UnsupportedDTypeCreationError,
|
|
23
23
|
stack_empty_error,
|
|
24
24
|
)
|
|
25
|
-
from decent_array._utils import is_scalar, unwrap
|
|
25
|
+
from decent_array._utils import is_scalar, unwrap, unwrap_key
|
|
26
26
|
from decent_array.interoperability._abstracts import Backend
|
|
27
27
|
from decent_array.interoperability._backend_manager import register_backend
|
|
28
28
|
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
|
|
@@ -401,10 +401,10 @@ class NumpyBackend(Backend):
|
|
|
401
401
|
return Array(np.argmin(v, axis=axis))
|
|
402
402
|
|
|
403
403
|
def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex | Array) -> None:
|
|
404
|
-
x.value[key] = unwrap(value)
|
|
404
|
+
x.value[unwrap_key(key)] = unwrap(value)
|
|
405
405
|
|
|
406
406
|
def get_item(self, x: Array, key: ArrayKey) -> Array:
|
|
407
|
-
return Array(x.value[key])
|
|
407
|
+
return Array(x.value[unwrap_key(key)])
|
|
408
408
|
|
|
409
409
|
# RNG
|
|
410
410
|
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_pytorch/pytorch_backend.py
RENAMED
|
@@ -16,7 +16,7 @@ from numpy.typing import NDArray
|
|
|
16
16
|
|
|
17
17
|
from decent_array import Array
|
|
18
18
|
from decent_array._errors import MatrixTransposeError, NDimError, UnsupportedDTypeCreationError, stack_empty_error
|
|
19
|
-
from decent_array._utils import is_scalar, unwrap
|
|
19
|
+
from decent_array._utils import is_scalar, unwrap, unwrap_key
|
|
20
20
|
from decent_array.interoperability._abstracts import Backend
|
|
21
21
|
from decent_array.interoperability._backend_manager import register_backend
|
|
22
22
|
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
|
|
@@ -234,9 +234,11 @@ class PyTorchBackend(Backend):
|
|
|
234
234
|
|
|
235
235
|
def mean(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
236
236
|
v = x.value
|
|
237
|
+
# torch.mean refuses bool/int inputs ("could not infer output dtype"); promote to float like numpy.
|
|
238
|
+
out_dtype = None if v.is_floating_point() or v.is_complex() else torch.get_default_dtype()
|
|
237
239
|
if axis is None:
|
|
238
|
-
return Array(torch.mean(v))
|
|
239
|
-
return Array(torch.mean(v, dim=axis, keepdim=keepdims))
|
|
240
|
+
return Array(torch.mean(v, dtype=out_dtype))
|
|
241
|
+
return Array(torch.mean(v, dim=axis, keepdim=keepdims, dtype=out_dtype))
|
|
240
242
|
|
|
241
243
|
def min(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
242
244
|
v = x.value
|
|
@@ -426,10 +428,10 @@ class PyTorchBackend(Backend):
|
|
|
426
428
|
return Array(torch.argmin(x.value, dim=axis, keepdim=keepdims))
|
|
427
429
|
|
|
428
430
|
def set_item(self, x: Array, key: ArrayKey, value: bool | int | float | complex | Array) -> None:
|
|
429
|
-
x.value[key] = unwrap(value)
|
|
431
|
+
x.value[unwrap_key(key)] = unwrap(value)
|
|
430
432
|
|
|
431
433
|
def get_item(self, x: Array, key: ArrayKey) -> Array:
|
|
432
|
-
return Array(x.value[key])
|
|
434
|
+
return Array(x.value[unwrap_key(key)])
|
|
433
435
|
|
|
434
436
|
# RNG
|
|
435
437
|
|
|
@@ -25,7 +25,7 @@ from decent_array._errors import (
|
|
|
25
25
|
UnsupportedDTypeCreationError,
|
|
26
26
|
stack_empty_error,
|
|
27
27
|
)
|
|
28
|
-
from decent_array._utils import is_scalar, unwrap
|
|
28
|
+
from decent_array._utils import is_scalar, unwrap, unwrap_key
|
|
29
29
|
from decent_array.interoperability._abstracts import Backend
|
|
30
30
|
from decent_array.interoperability._backend_manager import register_backend
|
|
31
31
|
from decent_array.types import ArrayKey, ArrayLike, ArrayTypes, Devices, Frameworks
|
|
@@ -46,7 +46,7 @@ class TensorflowBackend(Backend):
|
|
|
46
46
|
|
|
47
47
|
def empty(self, shape: int | tuple[int, ...]) -> Array:
|
|
48
48
|
with tf.device(self._native_device):
|
|
49
|
-
return Array(tf.experimental.numpy.empty(shape))
|
|
49
|
+
return Array(tf.experimental.numpy.empty(shape, dtype=tf.float32))
|
|
50
50
|
|
|
51
51
|
def empty_like(self, x: Array, /) -> Array:
|
|
52
52
|
with tf.device(self._native_device):
|
|
@@ -68,7 +68,7 @@ class TensorflowBackend(Backend):
|
|
|
68
68
|
|
|
69
69
|
def eye(self, n_rows: int, n_cols: int | None = None, /, *, k: int = 0) -> Array:
|
|
70
70
|
with tf.device(self._native_device):
|
|
71
|
-
return Array(tf.experimental.numpy.eye(n_rows, n_cols, k=k))
|
|
71
|
+
return Array(tf.experimental.numpy.eye(n_rows, n_cols, k=k, dtype=tf.float32))
|
|
72
72
|
|
|
73
73
|
def arange(self, start: int | float, /, stop: int | float | None = None, step: int | float = 1) -> Array:
|
|
74
74
|
with tf.device(self._native_device):
|
|
@@ -236,7 +236,11 @@ class TensorflowBackend(Backend):
|
|
|
236
236
|
return Array(tf.reduce_sum(x.value, axis=axis, keepdims=keepdims))
|
|
237
237
|
|
|
238
238
|
def mean(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
239
|
-
|
|
239
|
+
# tf.reduce_mean rejects bool and truncates integer means; promote to float like numpy.
|
|
240
|
+
v = x.value
|
|
241
|
+
if not v.dtype.is_floating and not v.dtype.is_complex:
|
|
242
|
+
v = tf.cast(v, tf.float32)
|
|
243
|
+
return Array(tf.reduce_mean(v, axis=axis, keepdims=keepdims))
|
|
240
244
|
|
|
241
245
|
def min(self, x: Array, axis: int | tuple[int, ...] | None = None, keepdims: bool = False) -> Array:
|
|
242
246
|
return Array(tf.reduce_min(x.value, axis=axis, keepdims=keepdims))
|
|
@@ -326,17 +330,35 @@ class TensorflowBackend(Backend):
|
|
|
326
330
|
) -> Array:
|
|
327
331
|
return Array(tf.where(condition.value, unwrap(x1), unwrap(x2)))
|
|
328
332
|
|
|
333
|
+
# tf.math.is_* only accept real floats: complex values are checked component-wise (numpy semantics), and
|
|
334
|
+
# integer/bool values are always finite and never inf/nan.
|
|
335
|
+
|
|
329
336
|
def isfinite(self, x: Array) -> Array:
|
|
330
337
|
"""Element-wise test for finite values."""
|
|
331
|
-
|
|
338
|
+
v = x.value
|
|
339
|
+
if v.dtype.is_complex:
|
|
340
|
+
return Array(tf.math.is_finite(tf.math.real(v)) & tf.math.is_finite(tf.math.imag(v)))
|
|
341
|
+
if not v.dtype.is_floating:
|
|
342
|
+
return Array(tf.ones_like(v, dtype=tf.bool))
|
|
343
|
+
return Array(tf.math.is_finite(v))
|
|
332
344
|
|
|
333
345
|
def isinf(self, x: Array) -> Array:
|
|
334
346
|
"""Element-wise test for infinite values."""
|
|
335
|
-
|
|
347
|
+
v = x.value
|
|
348
|
+
if v.dtype.is_complex:
|
|
349
|
+
return Array(tf.math.is_inf(tf.math.real(v)) | tf.math.is_inf(tf.math.imag(v)))
|
|
350
|
+
if not v.dtype.is_floating:
|
|
351
|
+
return Array(tf.zeros_like(v, dtype=tf.bool))
|
|
352
|
+
return Array(tf.math.is_inf(v))
|
|
336
353
|
|
|
337
354
|
def isnan(self, x: Array) -> Array:
|
|
338
355
|
"""Element-wise test for NaN values."""
|
|
339
|
-
|
|
356
|
+
v = x.value
|
|
357
|
+
if v.dtype.is_complex:
|
|
358
|
+
return Array(tf.math.is_nan(tf.math.real(v)) | tf.math.is_nan(tf.math.imag(v)))
|
|
359
|
+
if not v.dtype.is_floating:
|
|
360
|
+
return Array(tf.zeros_like(v, dtype=tf.bool))
|
|
361
|
+
return Array(tf.math.is_nan(v))
|
|
340
362
|
|
|
341
363
|
# Comparisons
|
|
342
364
|
|
|
@@ -442,12 +464,12 @@ class TensorflowBackend(Backend):
|
|
|
442
464
|
# that hammer set_item in tight loops should consider numpy or pytorch.
|
|
443
465
|
original = x.value
|
|
444
466
|
np_array = original.numpy().copy()
|
|
445
|
-
np_array[key] = np.asarray(unwrap(value))
|
|
467
|
+
np_array[unwrap_key(key)] = np.asarray(unwrap(value))
|
|
446
468
|
with tf.device(self._native_device):
|
|
447
469
|
x.value = tf.convert_to_tensor(np_array, dtype=original.dtype)
|
|
448
470
|
|
|
449
471
|
def get_item(self, x: Array, key: ArrayKey) -> Array:
|
|
450
|
-
return Array(x.value[key])
|
|
472
|
+
return Array(x.value[unwrap_key(key)])
|
|
451
473
|
|
|
452
474
|
# RNG
|
|
453
475
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "decent-array"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.8"
|
|
4
4
|
authors = [{name = "Simon Granström"}, {name = "Nicola Bastianello"}]
|
|
5
5
|
maintainers = [{name = "Team Decent"}]
|
|
6
6
|
description = "A library of array operations and linear algebra primitives for interoperability across ML frameworks."
|
|
@@ -170,6 +170,14 @@ def test_matmul_array(backend: tuple) -> None:
|
|
|
170
170
|
np.testing.assert_allclose(_np(a @ b), expected)
|
|
171
171
|
|
|
172
172
|
|
|
173
|
+
def test_matmul_vector(backend: tuple) -> None:
|
|
174
|
+
mat = _create_array([[1.0, 2.0], [3.0, 4.0]])
|
|
175
|
+
vec = _create_array([5.0, 6.0])
|
|
176
|
+
np.testing.assert_allclose(_np(mat @ vec), [17.0, 39.0])
|
|
177
|
+
np.testing.assert_allclose(_np(vec @ mat), [23.0, 34.0])
|
|
178
|
+
np.testing.assert_allclose(_np(vec @ vec), 61.0)
|
|
179
|
+
|
|
180
|
+
|
|
173
181
|
def test_rmatmul_explicit_call(backend: tuple) -> None:
|
|
174
182
|
# Python dispatch never picks Array.__rmatmul__ when both operands are Array,
|
|
175
183
|
# so call it directly to exercise the code path.
|
|
@@ -522,6 +522,15 @@ def test_all_dim_keepdims(backend: tuple) -> None:
|
|
|
522
522
|
np.testing.assert_array_equal(_np(iop.all(arr, axis=0, keepdims=True)), [[True, False]])
|
|
523
523
|
|
|
524
524
|
|
|
525
|
+
def test_mean_bool_and_int(backend: tuple) -> None:
|
|
526
|
+
bool_arr = iop.from_numpy(np.array([[True, False], [True, True]], dtype=np.bool_))
|
|
527
|
+
np.testing.assert_allclose(_np(iop.mean(bool_arr)), 0.75)
|
|
528
|
+
np.testing.assert_allclose(_np(iop.mean(bool_arr, axis=1)), [0.5, 1.0])
|
|
529
|
+
|
|
530
|
+
int_arr = iop.from_numpy(np.array([1, 2, 3, 4], dtype=np.int32))
|
|
531
|
+
np.testing.assert_allclose(_np(iop.mean(int_arr)), 2.5)
|
|
532
|
+
|
|
533
|
+
|
|
525
534
|
# Math elementwise -------------------------------------------------------
|
|
526
535
|
|
|
527
536
|
|
|
@@ -666,6 +675,27 @@ def test_isnan(backend: tuple) -> None:
|
|
|
666
675
|
np.testing.assert_array_equal(_np(iop.isnan(arr)), [False, False, False, True])
|
|
667
676
|
|
|
668
677
|
|
|
678
|
+
@pytest.mark.parametrize("dtype", [np.bool_, np.int32, np.uint8])
|
|
679
|
+
def test_finiteness_checks_non_float(backend: tuple, dtype: type[np.generic]) -> None:
|
|
680
|
+
arr = iop.from_numpy(np.array([0, 1, 1], dtype=dtype))
|
|
681
|
+
for fn, expected in ((iop.isfinite, True), (iop.isinf, False), (iop.isnan, False)):
|
|
682
|
+
out = fn(arr)
|
|
683
|
+
assert out.dtype == da.bool_
|
|
684
|
+
np.testing.assert_array_equal(_np(out), [expected, expected, expected])
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
def test_finiteness_checks_complex(backend: tuple) -> None:
|
|
688
|
+
# Built with complex(re, im): ``np.nan * 1j`` would give nan+nanj and hide component-wise bugs.
|
|
689
|
+
vals = np.array(
|
|
690
|
+
[complex(1, 2), complex(np.inf, 0), complex(0, -np.inf), complex(np.nan, 0), complex(0, np.nan)],
|
|
691
|
+
dtype=np.complex64,
|
|
692
|
+
)
|
|
693
|
+
arr = iop.from_numpy(vals)
|
|
694
|
+
np.testing.assert_array_equal(_np(iop.isfinite(arr)), np.isfinite(vals)) # [T, F, F, F, F]
|
|
695
|
+
np.testing.assert_array_equal(_np(iop.isinf(arr)), np.isinf(vals)) # [F, T, T, F, F]
|
|
696
|
+
np.testing.assert_array_equal(_np(iop.isnan(arr)), np.isnan(vals)) # [F, F, F, T, T]
|
|
697
|
+
|
|
698
|
+
|
|
669
699
|
# Operators --------------------------------------------------------------
|
|
670
700
|
|
|
671
701
|
|
|
@@ -874,6 +904,34 @@ def test_get_item_function(backend: tuple) -> None:
|
|
|
874
904
|
np.testing.assert_allclose(_np(get_item(arr, 1)), 2.0)
|
|
875
905
|
|
|
876
906
|
|
|
907
|
+
def test_supports_index_and_array_keys(backend: tuple) -> None:
|
|
908
|
+
# Mirrors decent-bench agents, which implement __index__ so that ``W[agent_i, agent_j]`` works.
|
|
909
|
+
class CustomIndex:
|
|
910
|
+
def __init__(self, idx: int) -> None:
|
|
911
|
+
self._idx = idx
|
|
912
|
+
|
|
913
|
+
def __index__(self) -> int:
|
|
914
|
+
return self._idx
|
|
915
|
+
|
|
916
|
+
mat = iop.from_numpy(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))
|
|
917
|
+
i0, i1 = CustomIndex(0), CustomIndex(1)
|
|
918
|
+
np.testing.assert_allclose(_np(mat[i0, i1]), 2.0)
|
|
919
|
+
mat[i1, i0] = 9.0
|
|
920
|
+
np.testing.assert_allclose(_np(mat[i1, i0]), 9.0)
|
|
921
|
+
|
|
922
|
+
mask = mat > 2.5
|
|
923
|
+
np.testing.assert_allclose(_np(mat[mask]), [9.0, 4.0])
|
|
924
|
+
mat[mask] = 0.0
|
|
925
|
+
np.testing.assert_allclose(_np(mat), [[1.0, 2.0], [0.0, 0.0]])
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
def test_creation_default_float_dtype_consistent(backend: tuple) -> None:
|
|
929
|
+
expected = iop.zeros((2, 2)).dtype
|
|
930
|
+
assert iop.ones((2, 2)).dtype == expected
|
|
931
|
+
assert iop.empty((2, 2)).dtype == expected
|
|
932
|
+
assert iop.eye(2).dtype == expected
|
|
933
|
+
|
|
934
|
+
|
|
877
935
|
# No-backend errors ------------------------------------------------------
|
|
878
936
|
|
|
879
937
|
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
from typing import Any
|
|
2
|
-
|
|
3
|
-
from decent_array._array import Array
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def unwrap(x: Any) -> Any: # noqa: ANN401
|
|
7
|
-
"""
|
|
8
|
-
Return the underlying value of an :class:`Array`, or pass ``x`` through.
|
|
9
|
-
|
|
10
|
-
Typed as ``Any`` because operator dunders may pass either an :class:`Array` or a
|
|
11
|
-
Python scalar; the strict abstract signature would force a ``cast`` at every call
|
|
12
|
-
site without runtime benefit.
|
|
13
|
-
"""
|
|
14
|
-
return x.value if type(x) is Array else x
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def is_scalar(x: Array) -> bool:
|
|
18
|
-
"""Return True if ``x`` is a 0-dim Array."""
|
|
19
|
-
return x.ndim == 0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_abstracts/__init__.py
RENAMED
|
File without changes
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_abstracts/backend.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/bit_operators.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_iop/manipulations.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_pytorch/__init__.py
RENAMED
|
File without changes
|
{decent_array-0.2.7 → decent_array-0.2.8}/decent_array/interoperability/_tensorflow/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|