decent-array 0.1.0__tar.gz → 0.2.1__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.1/.github/dependabot.yml +13 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/.github/workflows/ci.yaml +18 -18
- decent_array-0.2.1/PKG-INFO +30 -0
- decent_array-0.2.1/decent_array/__init__.py +73 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/_array.py +24 -23
- decent_array-0.2.1/decent_array/_constants.py +10 -0
- decent_array-0.2.1/decent_array/_errors.py +37 -0
- decent_array-0.2.1/decent_array/_utils.py +19 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/__init__.py +1 -1
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_abstracts/backend.py +176 -12
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_backend_manager.py +47 -20
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/bit_operators.py +7 -6
- decent_array-0.1.0/decent_array/interoperability/_iop/comparasion.py → decent_array-0.2.1/decent_array/interoperability/_iop/comparison.py +7 -7
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/creation.py +6 -6
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/linalg.py +6 -6
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/manipulations.py +23 -22
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/math.py +17 -17
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/operators.py +5 -5
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/reductions.py +7 -7
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/rng.py +11 -11
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/utils.py +10 -10
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_jax/jax_backend.py +179 -80
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_numpy/numpy_backend.py +220 -96
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_pytorch/pytorch_backend.py +168 -81
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_tensorflow/tensorflow_backend.py +184 -86
- decent_array-0.2.1/decent_array/py.typed +0 -0
- decent_array-0.2.1/decent_array/types/__init__.py +18 -0
- decent_array-0.2.1/decent_array/types/_dtypes.py +174 -0
- decent_array-0.1.0/decent_array/types.py → decent_array-0.2.1/decent_array/types/_types.py +8 -29
- decent_array-0.2.1/docs/requirements.txt +3 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/api/decent_array.types.rst +4 -1
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/conf.py +4 -0
- decent_array-0.2.1/docs/source/user.rst +322 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/pyproject.toml +60 -41
- {decent_array-0.1.0 → decent_array-0.2.1}/readthedocs.yaml +1 -1
- {decent_array-0.1.0 → decent_array-0.2.1}/tests/conftest.py +23 -23
- {decent_array-0.1.0 → decent_array-0.2.1}/tests/test_array.py +40 -2
- {decent_array-0.1.0 → decent_array-0.2.1}/tests/test_backend_manager.py +39 -20
- decent_array-0.2.1/tests/test_dtypes.py +126 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/tests/test_iop_functions.py +5 -5
- decent_array-0.1.0/PKG-INFO +0 -36
- decent_array-0.1.0/decent_array/__init__.py +0 -8
- decent_array-0.1.0/docs/source/user.rst +0 -10
- decent_array-0.1.0/docs/sphinx_theme.txt +0 -2
- {decent_array-0.1.0 → decent_array-0.2.1}/.github/CODEOWNERS +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/.gitignore +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/LICENSE +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/README.md +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/benchmarks/bench_array.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/benchmarks/bench_common.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/benchmarks/bench_iop.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/benchmarks/profile_hotpath.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_abstracts/__init__.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_iop/__init__.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_jax/__init__.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_numpy/__init__.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_pytorch/__init__.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_tensorflow/__init__.py +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/Makefile +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/make.bat +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/_static/custom.css +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/_static/logo.png +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/_templates/module.rst.jinja +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/_templates/package.rst.jinja +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/api/decent_array.array.rst +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/api/decent_array.interoperability.rst +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/api/decent_array.rst +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/author.rst +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/developer.rst +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/docs/source/index.rst +0 -0
- {decent_array-0.1.0 → decent_array-0.2.1}/tests/test_iop_rng.py +0 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
# version update of dependencies (as specified in pyproject.toml)
|
|
4
|
+
- package-ecosystem: "pip"
|
|
5
|
+
directory: "/"
|
|
6
|
+
schedule:
|
|
7
|
+
interval: "weekly"
|
|
8
|
+
|
|
9
|
+
# version update of github action tools (as specified in workflows/ci.yaml)
|
|
10
|
+
- package-ecosystem: "github-actions"
|
|
11
|
+
directory: "/"
|
|
12
|
+
schedule:
|
|
13
|
+
interval: "weekly"
|
|
@@ -10,10 +10,10 @@ jobs:
|
|
|
10
10
|
|
|
11
11
|
steps:
|
|
12
12
|
- name: Check out code
|
|
13
|
-
uses: actions/checkout@
|
|
13
|
+
uses: actions/checkout@v7
|
|
14
14
|
|
|
15
15
|
- name: Set up Python
|
|
16
|
-
uses: actions/setup-python@
|
|
16
|
+
uses: actions/setup-python@v7
|
|
17
17
|
with:
|
|
18
18
|
python-version: "3.13"
|
|
19
19
|
|
|
@@ -28,10 +28,10 @@ jobs:
|
|
|
28
28
|
|
|
29
29
|
steps:
|
|
30
30
|
- name: Check out code
|
|
31
|
-
uses: actions/checkout@
|
|
31
|
+
uses: actions/checkout@v7
|
|
32
32
|
|
|
33
33
|
- name: Set up Python
|
|
34
|
-
uses: actions/setup-python@
|
|
34
|
+
uses: actions/setup-python@v7
|
|
35
35
|
with:
|
|
36
36
|
python-version: "3.13"
|
|
37
37
|
|
|
@@ -46,10 +46,10 @@ jobs:
|
|
|
46
46
|
|
|
47
47
|
steps:
|
|
48
48
|
- name: Check out code
|
|
49
|
-
uses: actions/checkout@
|
|
49
|
+
uses: actions/checkout@v7
|
|
50
50
|
|
|
51
51
|
- name: Set up Python
|
|
52
|
-
uses: actions/setup-python@
|
|
52
|
+
uses: actions/setup-python@v7
|
|
53
53
|
with:
|
|
54
54
|
python-version: "3.13"
|
|
55
55
|
|
|
@@ -64,10 +64,10 @@ jobs:
|
|
|
64
64
|
|
|
65
65
|
steps:
|
|
66
66
|
- name: Check out code
|
|
67
|
-
uses: actions/checkout@
|
|
67
|
+
uses: actions/checkout@v7
|
|
68
68
|
|
|
69
69
|
- name: Set up Python
|
|
70
|
-
uses: actions/setup-python@
|
|
70
|
+
uses: actions/setup-python@v7
|
|
71
71
|
with:
|
|
72
72
|
python-version: "3.13"
|
|
73
73
|
|
|
@@ -82,10 +82,10 @@ jobs:
|
|
|
82
82
|
|
|
83
83
|
steps:
|
|
84
84
|
- name: Check out code
|
|
85
|
-
uses: actions/checkout@
|
|
85
|
+
uses: actions/checkout@v7
|
|
86
86
|
|
|
87
87
|
- name: Set up Python
|
|
88
|
-
uses: actions/setup-python@
|
|
88
|
+
uses: actions/setup-python@v7
|
|
89
89
|
with:
|
|
90
90
|
python-version: "3.13"
|
|
91
91
|
|
|
@@ -100,10 +100,10 @@ jobs:
|
|
|
100
100
|
|
|
101
101
|
steps:
|
|
102
102
|
- name: Check out code
|
|
103
|
-
uses: actions/checkout@
|
|
103
|
+
uses: actions/checkout@v7
|
|
104
104
|
|
|
105
105
|
- name: Set up Python
|
|
106
|
-
uses: actions/setup-python@
|
|
106
|
+
uses: actions/setup-python@v7
|
|
107
107
|
with:
|
|
108
108
|
python-version: "3.13"
|
|
109
109
|
|
|
@@ -121,10 +121,10 @@ jobs:
|
|
|
121
121
|
|
|
122
122
|
steps:
|
|
123
123
|
- name: Check out code
|
|
124
|
-
uses: actions/checkout@
|
|
124
|
+
uses: actions/checkout@v7
|
|
125
125
|
|
|
126
126
|
- name: Set up Python
|
|
127
|
-
uses: actions/setup-python@
|
|
127
|
+
uses: actions/setup-python@v7
|
|
128
128
|
with:
|
|
129
129
|
python-version: "3.13"
|
|
130
130
|
|
|
@@ -139,10 +139,10 @@ jobs:
|
|
|
139
139
|
|
|
140
140
|
steps:
|
|
141
141
|
- name: Check out code
|
|
142
|
-
uses: actions/checkout@
|
|
142
|
+
uses: actions/checkout@v7
|
|
143
143
|
|
|
144
144
|
- name: Set up Python
|
|
145
|
-
uses: actions/setup-python@
|
|
145
|
+
uses: actions/setup-python@v7
|
|
146
146
|
with:
|
|
147
147
|
python-version: "3.13"
|
|
148
148
|
|
|
@@ -157,10 +157,10 @@ jobs:
|
|
|
157
157
|
|
|
158
158
|
steps:
|
|
159
159
|
- name: Check out code
|
|
160
|
-
uses: actions/checkout@
|
|
160
|
+
uses: actions/checkout@v7
|
|
161
161
|
|
|
162
162
|
- name: Set up Python
|
|
163
|
-
uses: actions/setup-python@
|
|
163
|
+
uses: actions/setup-python@v7
|
|
164
164
|
with:
|
|
165
165
|
python-version: "3.13"
|
|
166
166
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: decent-array
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: A library of array operations and linear algebra primitives for interoperability across ML frameworks.
|
|
5
|
+
Project-URL: Documentation, https://decent-array.readthedocs.io/en/latest/
|
|
6
|
+
Project-URL: Source, https://github.com/team-decent/decent-array
|
|
7
|
+
Project-URL: Issues, https://github.com/team-decent/decent-array/issues
|
|
8
|
+
Author: Simon Granström, Nicola Bastianello
|
|
9
|
+
Maintainer: Team Decent
|
|
10
|
+
License-Expression: AGPL-3.0-only
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Requires-Python: >=3.13
|
|
15
|
+
Requires-Dist: mypy-extensions<2,>=1.1.0
|
|
16
|
+
Requires-Dist: numpy<3,>=2
|
|
17
|
+
Provides-Extra: jax
|
|
18
|
+
Requires-Dist: jax<1,>=0.4; extra == 'jax'
|
|
19
|
+
Provides-Extra: jax-gpu
|
|
20
|
+
Requires-Dist: jax[cuda12]<1,>=0.4; extra == 'jax-gpu'
|
|
21
|
+
Provides-Extra: tf
|
|
22
|
+
Requires-Dist: tensorflow<3,>=2.18; extra == 'tf'
|
|
23
|
+
Provides-Extra: tf-gpu
|
|
24
|
+
Requires-Dist: tensorflow[and-cuda]<3,>=2.18; extra == 'tf-gpu'
|
|
25
|
+
Provides-Extra: torch
|
|
26
|
+
Requires-Dist: torch<3,>=2.5; extra == 'torch'
|
|
27
|
+
Requires-Dist: torchvision<1,>=0.20; extra == 'torch'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# decent-array
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from decent_array import interoperability, types
|
|
2
|
+
from decent_array._array import Array
|
|
3
|
+
from decent_array._constants import e, inf, nan, pi
|
|
4
|
+
from decent_array.types._dtypes import (
|
|
5
|
+
bfloat16,
|
|
6
|
+
bool_,
|
|
7
|
+
bytes_,
|
|
8
|
+
complex64,
|
|
9
|
+
complex128,
|
|
10
|
+
complex256,
|
|
11
|
+
float16,
|
|
12
|
+
float32,
|
|
13
|
+
float64,
|
|
14
|
+
float128,
|
|
15
|
+
int8,
|
|
16
|
+
int16,
|
|
17
|
+
int32,
|
|
18
|
+
int64,
|
|
19
|
+
object_,
|
|
20
|
+
qint8,
|
|
21
|
+
qint16,
|
|
22
|
+
qint32,
|
|
23
|
+
quint8,
|
|
24
|
+
quint16,
|
|
25
|
+
uint8,
|
|
26
|
+
uint16,
|
|
27
|
+
uint32,
|
|
28
|
+
uint64,
|
|
29
|
+
unicode_,
|
|
30
|
+
void,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
__all_docs__ = [
|
|
34
|
+
"Array",
|
|
35
|
+
"interoperability",
|
|
36
|
+
"types",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"Array",
|
|
41
|
+
"bfloat16",
|
|
42
|
+
"bool_",
|
|
43
|
+
"bytes_",
|
|
44
|
+
"complex64",
|
|
45
|
+
"complex128",
|
|
46
|
+
"complex256",
|
|
47
|
+
"e",
|
|
48
|
+
"float16",
|
|
49
|
+
"float32",
|
|
50
|
+
"float64",
|
|
51
|
+
"float128",
|
|
52
|
+
"inf",
|
|
53
|
+
"int8",
|
|
54
|
+
"int16",
|
|
55
|
+
"int32",
|
|
56
|
+
"int64",
|
|
57
|
+
"interoperability",
|
|
58
|
+
"nan",
|
|
59
|
+
"object_",
|
|
60
|
+
"pi",
|
|
61
|
+
"qint8",
|
|
62
|
+
"qint16",
|
|
63
|
+
"qint32",
|
|
64
|
+
"quint8",
|
|
65
|
+
"quint16",
|
|
66
|
+
"types",
|
|
67
|
+
"uint8",
|
|
68
|
+
"uint16",
|
|
69
|
+
"uint32",
|
|
70
|
+
"uint64",
|
|
71
|
+
"unicode_",
|
|
72
|
+
"void",
|
|
73
|
+
]
|
|
@@ -29,13 +29,13 @@ from __future__ import annotations
|
|
|
29
29
|
from typing import TYPE_CHECKING, Any, Self
|
|
30
30
|
|
|
31
31
|
from decent_array.interoperability._backend_manager import register_backend_listener
|
|
32
|
-
from decent_array.types import
|
|
32
|
+
from decent_array.types._dtypes import _BACKEND_DTYPE_TO_DTYPE
|
|
33
33
|
|
|
34
34
|
if TYPE_CHECKING:
|
|
35
35
|
from numpy.typing import NDArray
|
|
36
36
|
|
|
37
37
|
from decent_array.interoperability._abstracts import Backend
|
|
38
|
-
from decent_array.types import ArrayKey,
|
|
38
|
+
from decent_array.types import ArrayKey, ArrayTypes, Devices, dtype
|
|
39
39
|
|
|
40
40
|
|
|
41
41
|
_BACKEND_INSTANCE: Backend | None = None
|
|
@@ -49,7 +49,7 @@ def _update_backend(backend: Backend | None) -> None:
|
|
|
49
49
|
register_backend_listener(_update_backend)
|
|
50
50
|
|
|
51
51
|
|
|
52
|
-
class Array:
|
|
52
|
+
class Array:
|
|
53
53
|
"""
|
|
54
54
|
Wrapper around a single backend-native array.
|
|
55
55
|
|
|
@@ -60,7 +60,7 @@ class Array: # noqa: PLR0904
|
|
|
60
60
|
|
|
61
61
|
__slots__ = ("_backend", "value")
|
|
62
62
|
|
|
63
|
-
def __init__(self, value:
|
|
63
|
+
def __init__(self, value: ArrayTypes) -> None:
|
|
64
64
|
"""
|
|
65
65
|
Wrap ``value`` in an :class:`Array`.
|
|
66
66
|
|
|
@@ -355,23 +355,23 @@ class Array: # noqa: PLR0904
|
|
|
355
355
|
|
|
356
356
|
def __float__(self) -> float:
|
|
357
357
|
"""Coerce a scalar array to a Python float."""
|
|
358
|
-
return float(self.
|
|
358
|
+
return float(self.item())
|
|
359
359
|
|
|
360
360
|
def __bool__(self) -> bool:
|
|
361
361
|
"""Coerce a scalar array to a Python bool."""
|
|
362
|
-
return bool(self.
|
|
362
|
+
return bool(self.item())
|
|
363
363
|
|
|
364
364
|
def __int__(self) -> int:
|
|
365
365
|
"""Coerce a scalar array to a Python int."""
|
|
366
|
-
return int(self.
|
|
366
|
+
return int(self.item())
|
|
367
367
|
|
|
368
368
|
def __complex__(self) -> complex:
|
|
369
369
|
"""Coerce a scalar array to a Python complex."""
|
|
370
|
-
return complex(self.
|
|
370
|
+
return complex(self.item())
|
|
371
371
|
|
|
372
372
|
def __index__(self) -> int:
|
|
373
373
|
"""Coerce a scalar array to a Python int."""
|
|
374
|
-
return int(self.
|
|
374
|
+
return int(self.item())
|
|
375
375
|
|
|
376
376
|
# Repr -----------------------------------------------------------------
|
|
377
377
|
|
|
@@ -401,21 +401,12 @@ class Array: # noqa: PLR0904
|
|
|
401
401
|
return self._backend.ndim(self)
|
|
402
402
|
|
|
403
403
|
@property
|
|
404
|
-
def dtype(self) ->
|
|
405
|
-
"""
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
Raises:
|
|
409
|
-
ValueError: for dtypes that are not supported by all decent-array functions
|
|
404
|
+
def dtype(self) -> dtype:
|
|
405
|
+
"""Return dtype of the Array."""
|
|
406
|
+
dtype = _BACKEND_DTYPE_TO_DTYPE.get(self.value.dtype)
|
|
410
407
|
|
|
411
|
-
"""
|
|
412
|
-
# get framework-native dtype as string
|
|
413
|
-
# split takes care of types with names like "torch.float32"
|
|
414
|
-
dtype_name = str(self.value.dtype).split(".")[-1]
|
|
415
|
-
|
|
416
|
-
dtype = _STRING_TO_DTYPE.get(dtype_name)
|
|
417
408
|
if dtype is None:
|
|
418
|
-
raise ValueError(f"dtype {self.value.dtype} is not supported
|
|
409
|
+
raise ValueError(f"dtype {self.value.dtype} is not supported.")
|
|
419
410
|
|
|
420
411
|
return dtype
|
|
421
412
|
|
|
@@ -445,7 +436,7 @@ class Array: # noqa: PLR0904
|
|
|
445
436
|
return self._backend.all(self)
|
|
446
437
|
|
|
447
438
|
@property
|
|
448
|
-
def device(self) ->
|
|
439
|
+
def device(self) -> Devices:
|
|
449
440
|
"""Return the device of the array."""
|
|
450
441
|
return self._backend.device_of(self)
|
|
451
442
|
|
|
@@ -453,3 +444,13 @@ class Array: # noqa: PLR0904
|
|
|
453
444
|
def numpy(self) -> NDArray[Any]:
|
|
454
445
|
"""Return a NumPy array view of the array's data."""
|
|
455
446
|
return self._backend.to_numpy(self)
|
|
447
|
+
|
|
448
|
+
def item(self) -> Any: # noqa: ANN401
|
|
449
|
+
"""
|
|
450
|
+
Convert 0-dim array to Python scalar.
|
|
451
|
+
|
|
452
|
+
Raises:
|
|
453
|
+
TypeError: if ``x`` is not 0-dimensional.
|
|
454
|
+
|
|
455
|
+
"""
|
|
456
|
+
return self._backend.to_scalar(self)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Common errors used across backends."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from mypy_extensions import mypyc_attr
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@mypyc_attr(native_class=False)
|
|
9
|
+
class NotScalarError(TypeError):
|
|
10
|
+
def __init__(self, ndim: int):
|
|
11
|
+
super().__init__(f"Only 0-dim arrays can be converted to Python scalars, got {ndim}-dim array.")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class NDimError(ValueError):
|
|
15
|
+
def __init__(self, required_ndim: int, actual_ndim: int):
|
|
16
|
+
super().__init__(f"A {required_ndim}-dim array is required, got {actual_ndim}-dim array.")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MatrixTransposeError(ValueError):
|
|
20
|
+
def __init__(self, ndim: int):
|
|
21
|
+
super().__init__(f"An aray with at least 2 dimensions is required, got {ndim}-dim array.")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class UnsupportedDTypeCreationError(ValueError):
|
|
25
|
+
def __init__(self, dtype: Any, backend_name: str, device_name: str): # noqa: ANN401
|
|
26
|
+
super().__init__(f"Unsupported dtype '{dtype}' for {backend_name} on {device_name}.")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class UnsupportedDeviceError(ValueError):
|
|
30
|
+
def __init__(self, backend_name: str, device_name: str):
|
|
31
|
+
super().__init__(f"{backend_name} does not support device '{device_name}'.")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
stack_empty_error = ValueError("Cannot stack an empty sequence of arrays.")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
no_backend_error = RuntimeError("No backend active: call 'set_backend' with a supported framework to activate one.")
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
|
@@ -21,7 +21,7 @@ from ._iop.bit_operators import (
|
|
|
21
21
|
bitwise_right_shift,
|
|
22
22
|
bitwise_xor,
|
|
23
23
|
)
|
|
24
|
-
from ._iop.
|
|
24
|
+
from ._iop.comparison import equal, greater, greater_equal, less, less_equal, not_equal
|
|
25
25
|
from ._iop.creation import eye, ones, ones_like, zeros, zeros_like
|
|
26
26
|
from ._iop.linalg import dot, matmul, norm, vecdot, vector_norm
|
|
27
27
|
from ._iop.manipulations import (
|
{decent_array-0.1.0 → decent_array-0.2.1}/decent_array/interoperability/_abstracts/backend.py
RENAMED
|
@@ -18,25 +18,27 @@ from abc import ABC, abstractmethod
|
|
|
18
18
|
from collections.abc import Sequence
|
|
19
19
|
from typing import TYPE_CHECKING, Any
|
|
20
20
|
|
|
21
|
-
from decent_array.types import
|
|
21
|
+
from decent_array.types._types import Devices
|
|
22
22
|
|
|
23
23
|
if TYPE_CHECKING:
|
|
24
24
|
from numpy.typing import NDArray
|
|
25
25
|
|
|
26
26
|
from decent_array import Array
|
|
27
|
-
from decent_array.types import ArrayKey,
|
|
27
|
+
from decent_array.types import ArrayKey, ArrayTypes
|
|
28
|
+
from decent_array.types._dtypes import dtype
|
|
28
29
|
|
|
29
30
|
|
|
30
|
-
class Backend(ABC):
|
|
31
|
+
class Backend(ABC):
|
|
31
32
|
"""
|
|
32
33
|
Abstract base class for a backend.
|
|
33
34
|
|
|
34
|
-
Concrete backends are bound to a single :class:`
|
|
35
|
+
Concrete backends are bound to a single :class:`Devices` at construction
|
|
35
36
|
time; that device is the default for all new arrays produced by this backend.
|
|
36
37
|
"""
|
|
37
38
|
|
|
38
|
-
def __init__(self, device:
|
|
39
|
-
self.device:
|
|
39
|
+
def __init__(self, device: Devices = Devices.CPU, name: str = "") -> None:
|
|
40
|
+
self.device: Devices = device
|
|
41
|
+
self.name: str = name
|
|
40
42
|
|
|
41
43
|
# Array creation ------------------------------------------------------
|
|
42
44
|
|
|
@@ -61,12 +63,12 @@ class Backend(ABC): # noqa: PLR0904
|
|
|
61
63
|
"""Create an ``n x n`` identity matrix."""
|
|
62
64
|
|
|
63
65
|
@abstractmethod
|
|
64
|
-
def device_to_native(self, device:
|
|
65
|
-
"""Convert :class:`
|
|
66
|
+
def device_to_native(self, device: Devices) -> Any: # noqa: ANN401
|
|
67
|
+
"""Convert :class:`Devices` to the backend's native device representation."""
|
|
66
68
|
|
|
67
69
|
@abstractmethod
|
|
68
|
-
def device_of(self, x: Array) ->
|
|
69
|
-
"""Return the :class:`
|
|
70
|
+
def device_of(self, x: Array) -> Devices:
|
|
71
|
+
"""Return the :class:`Devices` of the given array."""
|
|
70
72
|
|
|
71
73
|
# Array manipulation --------------------------------------------------
|
|
72
74
|
|
|
@@ -75,7 +77,7 @@ class Backend(ABC): # noqa: PLR0904
|
|
|
75
77
|
"""Return a copy of ``x``."""
|
|
76
78
|
|
|
77
79
|
@abstractmethod
|
|
78
|
-
def to_numpy(self, x:
|
|
80
|
+
def to_numpy(self, x: ArrayTypes | Array) -> NDArray[Any]:
|
|
79
81
|
"""Convert ``x`` to a NumPy array on the CPU."""
|
|
80
82
|
|
|
81
83
|
@abstractmethod
|
|
@@ -90,6 +92,14 @@ class Backend(ABC): # noqa: PLR0904
|
|
|
90
92
|
def asarray(self, x: bool | int | float | complex) -> Array:
|
|
91
93
|
"""Convert a Python scalar to an :class:`Array` on this backend."""
|
|
92
94
|
|
|
95
|
+
@abstractmethod
|
|
96
|
+
def to_scalar(self, x: Array) -> Any: # noqa: ANN401
|
|
97
|
+
"""
|
|
98
|
+
Convert a 0-dim array to a scalar.
|
|
99
|
+
|
|
100
|
+
This method must use ``is_scalar(x)`` to raise when ``x`` is not 0-dim.
|
|
101
|
+
"""
|
|
102
|
+
|
|
93
103
|
@abstractmethod
|
|
94
104
|
def stack(self, arrays: Sequence[Array], axis: int = 0) -> Array:
|
|
95
105
|
"""Stack a sequence of arrays along a new dimension."""
|
|
@@ -135,7 +145,7 @@ class Backend(ABC): # noqa: PLR0904
|
|
|
135
145
|
"""Extract the diagonal entries from a 2-D matrix at the given ``offset``."""
|
|
136
146
|
|
|
137
147
|
@abstractmethod
|
|
138
|
-
def astype(self, x: Array, dtype:
|
|
148
|
+
def astype(self, x: Array, dtype: dtype) -> Array:
|
|
139
149
|
"""Cast ``x`` to a different dtype."""
|
|
140
150
|
|
|
141
151
|
# Linalg --------------------------------------------------------------
|
|
@@ -393,3 +403,157 @@ class Backend(ABC): # noqa: PLR0904
|
|
|
393
403
|
@abstractmethod
|
|
394
404
|
def choice(self, x: Array, size: int, replace: bool = True) -> Array:
|
|
395
405
|
"""Sample ``size`` elements from ``x``."""
|
|
406
|
+
|
|
407
|
+
# DTYPES --------------------------------------------------------------
|
|
408
|
+
|
|
409
|
+
@property
|
|
410
|
+
@abstractmethod
|
|
411
|
+
def bool_(self) -> Any: # noqa: ANN401
|
|
412
|
+
"""Bool dtype."""
|
|
413
|
+
|
|
414
|
+
@property
|
|
415
|
+
@abstractmethod
|
|
416
|
+
def uint8(self) -> Any: # noqa: ANN401
|
|
417
|
+
"""Unsigned 8-bit integer dtype."""
|
|
418
|
+
|
|
419
|
+
@property
|
|
420
|
+
def uint16(self) -> Any | None: # noqa: ANN401
|
|
421
|
+
"""Unsigned 16-bit integer dtype (optional)."""
|
|
422
|
+
return None
|
|
423
|
+
|
|
424
|
+
@property
|
|
425
|
+
def uint32(self) -> Any | None: # noqa: ANN401
|
|
426
|
+
"""Unsigned 32-bit integer dtype (optional)."""
|
|
427
|
+
return None
|
|
428
|
+
|
|
429
|
+
@property
|
|
430
|
+
def uint64(self) -> Any | None: # noqa: ANN401
|
|
431
|
+
"""Unsigned 64-bit integer dtype (optional)."""
|
|
432
|
+
return None
|
|
433
|
+
|
|
434
|
+
@property
|
|
435
|
+
@abstractmethod
|
|
436
|
+
def int8(self) -> Any: # noqa: ANN401
|
|
437
|
+
"""Signed 8-bit integer dtype."""
|
|
438
|
+
|
|
439
|
+
@property
|
|
440
|
+
@abstractmethod
|
|
441
|
+
def int16(self) -> Any: # noqa: ANN401
|
|
442
|
+
"""Signed 16-bit integer dtype."""
|
|
443
|
+
|
|
444
|
+
@property
|
|
445
|
+
@abstractmethod
|
|
446
|
+
def int32(self) -> Any: # noqa: ANN401
|
|
447
|
+
"""Signed 32-bit integer dtype."""
|
|
448
|
+
|
|
449
|
+
@property
|
|
450
|
+
def int64(self) -> Any: # noqa: ANN401
|
|
451
|
+
"""Signed 64-bit integer dtype."""
|
|
452
|
+
return None
|
|
453
|
+
|
|
454
|
+
@property
|
|
455
|
+
@abstractmethod
|
|
456
|
+
def float16(self) -> Any: # noqa: ANN401
|
|
457
|
+
"""16-bit floating-point dtype."""
|
|
458
|
+
|
|
459
|
+
@property
|
|
460
|
+
@abstractmethod
|
|
461
|
+
def float32(self) -> Any: # noqa: ANN401
|
|
462
|
+
"""32-bit floating-point dtype."""
|
|
463
|
+
|
|
464
|
+
@property
|
|
465
|
+
def float64(self) -> Any: # noqa: ANN401
|
|
466
|
+
"""64-bit floating-point dtype."""
|
|
467
|
+
return None
|
|
468
|
+
|
|
469
|
+
@property
|
|
470
|
+
def complex64(self) -> Any: # noqa: ANN401
|
|
471
|
+
"""64-bit complex dtype."""
|
|
472
|
+
return None
|
|
473
|
+
|
|
474
|
+
@property
|
|
475
|
+
def complex128(self) -> Any: # noqa: ANN401
|
|
476
|
+
"""128-bit complex dtype."""
|
|
477
|
+
return None
|
|
478
|
+
|
|
479
|
+
@property
|
|
480
|
+
def float128(self) -> Any | None: # noqa: ANN401
|
|
481
|
+
"""128-bit floating-point dtype (optional)."""
|
|
482
|
+
return None
|
|
483
|
+
|
|
484
|
+
@property
|
|
485
|
+
def complex256(self) -> Any | None: # noqa: ANN401
|
|
486
|
+
"""256-bit complex dtype (optional)."""
|
|
487
|
+
return None
|
|
488
|
+
|
|
489
|
+
@property
|
|
490
|
+
def qint8(self) -> Any | None: # noqa: ANN401
|
|
491
|
+
"""Quantized 8-bit integer dtype (optional)."""
|
|
492
|
+
return None
|
|
493
|
+
|
|
494
|
+
@property
|
|
495
|
+
def qint16(self) -> Any | None: # noqa: ANN401
|
|
496
|
+
"""Quantized 16-bit integer dtype (optional)."""
|
|
497
|
+
return None
|
|
498
|
+
|
|
499
|
+
@property
|
|
500
|
+
def qint32(self) -> Any | None: # noqa: ANN401
|
|
501
|
+
"""Quantized 32-bit integer dtype (optional)."""
|
|
502
|
+
return None
|
|
503
|
+
|
|
504
|
+
@property
|
|
505
|
+
def quint8(self) -> Any | None: # noqa: ANN401
|
|
506
|
+
"""Quantized unsigned 8-bit integer dtype (optional)."""
|
|
507
|
+
return None
|
|
508
|
+
|
|
509
|
+
@property
|
|
510
|
+
def quint16(self) -> Any | None: # noqa: ANN401
|
|
511
|
+
"""Quantized unsigned 16-bit integer dtype (optional)."""
|
|
512
|
+
return None
|
|
513
|
+
|
|
514
|
+
@property
|
|
515
|
+
def bfloat16(self) -> Any | None: # noqa: ANN401
|
|
516
|
+
"""Brain floating point 16-bit dtype (optional)."""
|
|
517
|
+
return None
|
|
518
|
+
|
|
519
|
+
@property
|
|
520
|
+
def unicode_(self) -> Any | None: # noqa: ANN401
|
|
521
|
+
"""Unicode string dtype (optional)."""
|
|
522
|
+
return None
|
|
523
|
+
|
|
524
|
+
@property
|
|
525
|
+
def bytes_(self) -> Any | None: # noqa: ANN401
|
|
526
|
+
"""Byte string dtype (optional)."""
|
|
527
|
+
return None
|
|
528
|
+
|
|
529
|
+
@property
|
|
530
|
+
def object_(self) -> Any | None: # noqa: ANN401
|
|
531
|
+
"""Python object dtype (optional)."""
|
|
532
|
+
return None
|
|
533
|
+
|
|
534
|
+
@property
|
|
535
|
+
def void(self) -> Any | None: # noqa: ANN401
|
|
536
|
+
"""Raw/void dtype (optional)."""
|
|
537
|
+
return None
|
|
538
|
+
|
|
539
|
+
# CONSTANTS -----------------------------------------------------------
|
|
540
|
+
|
|
541
|
+
@property
|
|
542
|
+
@abstractmethod
|
|
543
|
+
def e(self) -> Any: # noqa: ANN401
|
|
544
|
+
"""e = 2.71828...""" # noqa: D403
|
|
545
|
+
|
|
546
|
+
@property
|
|
547
|
+
@abstractmethod
|
|
548
|
+
def inf(self) -> Any: # noqa: ANN401
|
|
549
|
+
"""Infinity."""
|
|
550
|
+
|
|
551
|
+
@property
|
|
552
|
+
@abstractmethod
|
|
553
|
+
def nan(self) -> Any: # noqa: ANN401
|
|
554
|
+
"""Not-a-number."""
|
|
555
|
+
|
|
556
|
+
@property
|
|
557
|
+
@abstractmethod
|
|
558
|
+
def pi(self) -> Any: # noqa: ANN401
|
|
559
|
+
"""pi = 3.14159...""" # noqa: D403
|