ainmax 0.0.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.
Potentially problematic release.
This version of ainmax might be problematic. Click here for more details.
- ainmax-0.0.1/.gitignore +174 -0
- ainmax-0.0.1/LICENSE +21 -0
- ainmax-0.0.1/PKG-INFO +62 -0
- ainmax-0.0.1/README.md +47 -0
- ainmax-0.0.1/ainmax/__init__.py +43 -0
- ainmax-0.0.1/ainmax/_src/__init__.py +0 -0
- ainmax-0.0.1/ainmax/_src/array.py +158 -0
- ainmax-0.0.1/ainmax/_src/axis.py +57 -0
- ainmax-0.0.1/ainmax/_src/axis_util.py +561 -0
- ainmax-0.0.1/ainmax/_src/basearray.py +13 -0
- ainmax-0.0.1/ainmax/_src/basearray.pyi +252 -0
- ainmax-0.0.1/ainmax/_src/config.py +203 -0
- ainmax-0.0.1/ainmax/_src/feidhm/__init__.py +18 -0
- ainmax-0.0.1/ainmax/_src/feidhm/constructors.py +82 -0
- ainmax-0.0.1/ainmax/_src/feidhm/creation.py +916 -0
- ainmax-0.0.1/ainmax/_src/feidhm/indexing.py +736 -0
- ainmax-0.0.1/ainmax/_src/feidhm/integrate.py +85 -0
- ainmax-0.0.1/ainmax/_src/feidhm/linalg.py +1576 -0
- ainmax-0.0.1/ainmax/_src/feidhm/methods.py +616 -0
- ainmax-0.0.1/ainmax/_src/feidhm/operations.py +2252 -0
- ainmax-0.0.1/ainmax/_src/feidhm/polynomials.py +478 -0
- ainmax-0.0.1/ainmax/_src/feidhm/reductions.py +1122 -0
- ainmax-0.0.1/ainmax/_src/feidhm/signal.py +594 -0
- ainmax-0.0.1/ainmax/_src/feidhm/special.py +1149 -0
- ainmax-0.0.1/ainmax/_src/feidhm/tensor_contractions.py +499 -0
- ainmax-0.0.1/ainmax/_src/feidhm/ufuncs.py +1558 -0
- ainmax-0.0.1/ainmax/_src/feidhm/util.py +202 -0
- ainmax-0.0.1/ainmax/_src/filters.py +70 -0
- ainmax-0.0.1/ainmax/_src/hof.py +340 -0
- ainmax-0.0.1/ainmax/_src/nn/__init__.py +0 -0
- ainmax-0.0.1/ainmax/_src/nn/activations.py +128 -0
- ainmax-0.0.1/ainmax/_src/nn/conv.py +349 -0
- ainmax-0.0.1/ainmax/_src/nn/linear.py +0 -0
- ainmax-0.0.1/ainmax/_src/nn/misc.py +62 -0
- ainmax-0.0.1/ainmax/_src/nn/normalization.py +61 -0
- ainmax-0.0.1/ainmax/_src/nn/recurrent.py +7 -0
- ainmax-0.0.1/ainmax/_src/partition.py +82 -0
- ainmax-0.0.1/ainmax/_src/random.py +889 -0
- ainmax-0.0.1/ainmax/_src/shard_map.py +172 -0
- ainmax-0.0.1/ainmax/_src/sharding.py +154 -0
- ainmax-0.0.1/ainmax/_src/typing.py +498 -0
- ainmax-0.0.1/ainmax/_src/util.py +38 -0
- ainmax-0.0.1/ainmax/feidhm/__init__.py +289 -0
- ainmax-0.0.1/ainmax/feidhm/integrate.py +1 -0
- ainmax-0.0.1/ainmax/feidhm/linalg.py +44 -0
- ainmax-0.0.1/ainmax/feidhm/signal.py +16 -0
- ainmax-0.0.1/ainmax/feidhm/special.py +57 -0
- ainmax-0.0.1/ainmax/py.typed +0 -0
- ainmax-0.0.1/ainmax/random.py +48 -0
- ainmax-0.0.1/ainmax/sharding.py +5 -0
- ainmax-0.0.1/ainmax/typing.py +38 -0
- ainmax-0.0.1/pyproject.toml +73 -0
ainmax-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
### Python template
|
|
2
|
+
# Byte-compiled / optimized / DLL files
|
|
3
|
+
__pycache__/
|
|
4
|
+
*.py[cod]
|
|
5
|
+
*$py.class
|
|
6
|
+
|
|
7
|
+
# C extensions
|
|
8
|
+
*.so
|
|
9
|
+
|
|
10
|
+
# Distribution / packaging
|
|
11
|
+
.Python
|
|
12
|
+
build/
|
|
13
|
+
develop-eggs/
|
|
14
|
+
dist/
|
|
15
|
+
downloads/
|
|
16
|
+
eggs/
|
|
17
|
+
.eggs/
|
|
18
|
+
lib/
|
|
19
|
+
lib64/
|
|
20
|
+
parts/
|
|
21
|
+
sdist/
|
|
22
|
+
var/
|
|
23
|
+
wheels/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py,cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
.testmondata
|
|
56
|
+
.testmondata*
|
|
57
|
+
|
|
58
|
+
# Translations
|
|
59
|
+
*.mo
|
|
60
|
+
*.pot
|
|
61
|
+
|
|
62
|
+
# Django stuff:
|
|
63
|
+
*.log
|
|
64
|
+
local_settings.py
|
|
65
|
+
db.sqlite3
|
|
66
|
+
db.sqlite3-journal
|
|
67
|
+
|
|
68
|
+
# Flask stuff:
|
|
69
|
+
instance/
|
|
70
|
+
.webassets-cache
|
|
71
|
+
|
|
72
|
+
# Scrapy stuff:
|
|
73
|
+
.scrapy
|
|
74
|
+
|
|
75
|
+
# Sphinx documentation
|
|
76
|
+
docs/_build/
|
|
77
|
+
|
|
78
|
+
# PyBuilder
|
|
79
|
+
.pybuilder/
|
|
80
|
+
target/
|
|
81
|
+
|
|
82
|
+
# Jupyter Notebook
|
|
83
|
+
.ipynb_checkpoints
|
|
84
|
+
|
|
85
|
+
# IPython
|
|
86
|
+
profile_default/
|
|
87
|
+
ipython_config.py
|
|
88
|
+
|
|
89
|
+
# pyenv
|
|
90
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
91
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
92
|
+
# .python-version
|
|
93
|
+
|
|
94
|
+
# pipenv
|
|
95
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
96
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
97
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
98
|
+
# install all needed dependencies.
|
|
99
|
+
#Pipfile.lock
|
|
100
|
+
|
|
101
|
+
# poetry
|
|
102
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
103
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
104
|
+
# commonly ignored for libraries.
|
|
105
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
106
|
+
#poetry.lock
|
|
107
|
+
|
|
108
|
+
# pdm
|
|
109
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
110
|
+
#pdm.lock
|
|
111
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
112
|
+
# in version control.
|
|
113
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
114
|
+
.pdm.toml
|
|
115
|
+
.pdm-python
|
|
116
|
+
.pdm-build/
|
|
117
|
+
|
|
118
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
119
|
+
__pypackages__/
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# Celery stuff
|
|
123
|
+
celerybeat-schedule
|
|
124
|
+
celerybeat.pid
|
|
125
|
+
|
|
126
|
+
# SageMath parsed files
|
|
127
|
+
*.sage.py
|
|
128
|
+
|
|
129
|
+
# Environments
|
|
130
|
+
.env
|
|
131
|
+
.venv
|
|
132
|
+
env/
|
|
133
|
+
venv/
|
|
134
|
+
ENV/
|
|
135
|
+
env.bak/
|
|
136
|
+
venv.bak/
|
|
137
|
+
|
|
138
|
+
# Spyder project settings
|
|
139
|
+
.spyderproject
|
|
140
|
+
.spyproject
|
|
141
|
+
|
|
142
|
+
# Rope project settings
|
|
143
|
+
.ropeproject
|
|
144
|
+
|
|
145
|
+
# mkdocs documentation
|
|
146
|
+
/site
|
|
147
|
+
|
|
148
|
+
# mypy
|
|
149
|
+
.mypy_cache/
|
|
150
|
+
.dmypy.json
|
|
151
|
+
dmypy.json
|
|
152
|
+
|
|
153
|
+
#ruff
|
|
154
|
+
|
|
155
|
+
# Pyre type checker
|
|
156
|
+
.pyre/
|
|
157
|
+
|
|
158
|
+
# pytype static type analyzer
|
|
159
|
+
.pytype/
|
|
160
|
+
|
|
161
|
+
# Cython debug symbols
|
|
162
|
+
cython_debug/
|
|
163
|
+
|
|
164
|
+
# PyCharm
|
|
165
|
+
.idea/
|
|
166
|
+
|
|
167
|
+
# Visual Studio Code
|
|
168
|
+
.vscode/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# project specific
|
|
174
|
+
.scratch/
|
ainmax-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2026 Christopher Kirwan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
|
|
20
|
+
OUT OF OR IN CONNECTION WITH WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
ainmax-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ainmax
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Scientific computing in JAX with named arrays
|
|
5
|
+
Author-email: Christopher Kirwan <christopher.kirwan@ibm.com>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: Intended Audience :: Science/Research
|
|
8
|
+
Classifier: Programming Language :: Python
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Requires-Dist: equinox>=0.13.8
|
|
11
|
+
Requires-Dist: jax>=0.12.0.1
|
|
12
|
+
Provides-Extra: gpu
|
|
13
|
+
Requires-Dist: jax[cuda13]; extra == 'gpu'
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
<div align="center">
|
|
17
|
+
<picture>
|
|
18
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/assets/icons/logo-horizontal-dark.png">
|
|
19
|
+
<source media="(prefers-color-scheme: light)" srcset="docs/assets/icons/logo-horizontal-light.png">
|
|
20
|
+
<img alt="ainmax logo" width="50%" src="docs/assets/icons/logo-horizontal-dark.png">
|
|
21
|
+
</picture>
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
<br>
|
|
27
|
+
|
|
28
|
+
<div align="center">
|
|
29
|
+
|
|
30
|
+
anm /anm/ n.
|
|
31
|
+
|
|
32
|
+
Primitive Irish · From Proto-Celtic anman, meaning "name"; ancestor of Old Irish ainmm and Modern Irish ainm.
|
|
33
|
+
|
|
34
|
+
</div>
|
|
35
|
+
|
|
36
|
+
<div align="center">
|
|
37
|
+
|
|
38
|
+
[](https://www.python.org/)
|
|
39
|
+
[](https://github.com/jax-ml/jax)
|
|
40
|
+
<!-- [](DOCUMENTATION_URL) -->
|
|
41
|
+
[](LICENSE)
|
|
42
|
+
|
|
43
|
+
</div>
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
**ainmax** is a JAX-powered Python library for numerical computing, built around named arrays and composable mathematical abstractions.
|
|
47
|
+
|
|
48
|
+
By combining explicit array semantics with JAX's compilation, automatic differentiation, and accelerator execution, ainmax provides a foundation for building expressive, efficient, and scalable scientific computations.
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
- **Named arrays** — Multidimensional arrays with explicit semantic axes, making complex tensor operations easier to express and reason about.
|
|
53
|
+
- **Numerical computing** — A collection of composable array operations, linear algebra, and scientific computing utilities.
|
|
54
|
+
- **JAX integration** — Automatic differentiation, compilation, vectorisation, and accelerator execution.
|
|
55
|
+
- **Scalable computation** — Foundations for distributed and multi-device numerical workloads.
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
Requires Python 3.12 or later.
|
|
60
|
+
|
|
61
|
+
## Acknowledgements
|
|
62
|
+
Significantly inspired by `haliax`, development of which is now moved to [marin](https://github.com/marin-community/marin)
|
ainmax-0.0.1/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/assets/icons/logo-horizontal-dark.png">
|
|
4
|
+
<source media="(prefers-color-scheme: light)" srcset="docs/assets/icons/logo-horizontal-light.png">
|
|
5
|
+
<img alt="ainmax logo" width="50%" src="docs/assets/icons/logo-horizontal-dark.png">
|
|
6
|
+
</picture>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<br>
|
|
12
|
+
|
|
13
|
+
<div align="center">
|
|
14
|
+
|
|
15
|
+
anm /anm/ n.
|
|
16
|
+
|
|
17
|
+
Primitive Irish · From Proto-Celtic anman, meaning "name"; ancestor of Old Irish ainmm and Modern Irish ainm.
|
|
18
|
+
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
<div align="center">
|
|
22
|
+
|
|
23
|
+
[](https://www.python.org/)
|
|
24
|
+
[](https://github.com/jax-ml/jax)
|
|
25
|
+
<!-- [](DOCUMENTATION_URL) -->
|
|
26
|
+
[](LICENSE)
|
|
27
|
+
|
|
28
|
+
</div>
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
**ainmax** is a JAX-powered Python library for numerical computing, built around named arrays and composable mathematical abstractions.
|
|
32
|
+
|
|
33
|
+
By combining explicit array semantics with JAX's compilation, automatic differentiation, and accelerator execution, ainmax provides a foundation for building expressive, efficient, and scalable scientific computations.
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Named arrays** — Multidimensional arrays with explicit semantic axes, making complex tensor operations easier to express and reason about.
|
|
38
|
+
- **Numerical computing** — A collection of composable array operations, linear algebra, and scientific computing utilities.
|
|
39
|
+
- **JAX integration** — Automatic differentiation, compilation, vectorisation, and accelerator execution.
|
|
40
|
+
- **Scalable computation** — Foundations for distributed and multi-device numerical workloads.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
Requires Python 3.12 or later.
|
|
45
|
+
|
|
46
|
+
## Acknowledgements
|
|
47
|
+
Significantly inspired by `haliax`, development of which is now moved to [marin](https://github.com/marin-community/marin)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from ainmax import feidhm as feidhm, random as random, typing as typing
|
|
2
|
+
from ainmax._src.axis import Axis as Axis
|
|
3
|
+
from ainmax._src.axis_util import (
|
|
4
|
+
axis_index as axis_index,
|
|
5
|
+
axis_indices as axis_indices,
|
|
6
|
+
axis_names as axis_names,
|
|
7
|
+
axis_selection_to_tuple as axis_selection_to_tuple,
|
|
8
|
+
axis_shape_to_tuple as axis_shape_to_tuple,
|
|
9
|
+
axis_sizes as axis_sizes,
|
|
10
|
+
axis_spec_to_tuple as axis_spec_to_tuple,
|
|
11
|
+
concatenate_axes as concatenate_axes,
|
|
12
|
+
intersect_axes as intersect_axes,
|
|
13
|
+
is_anonymous_axis as is_anonymous_axis,
|
|
14
|
+
is_axis_compatible as is_axis_compatible,
|
|
15
|
+
is_named_axis as is_named_axis,
|
|
16
|
+
make_axes as make_axes,
|
|
17
|
+
match_axes as match_axes,
|
|
18
|
+
remove_axes as remove_axes,
|
|
19
|
+
replace_axes as replace_axes,
|
|
20
|
+
resolve_axes as resolve_axes,
|
|
21
|
+
union_axes as union_axes,
|
|
22
|
+
)
|
|
23
|
+
from ainmax._src.config import (
|
|
24
|
+
config as config,
|
|
25
|
+
default_matmul_precision as default_matmul_precision,
|
|
26
|
+
enable_x64 as enable_x64,
|
|
27
|
+
numpy_dtype_promotion as numpy_dtype_promotion,
|
|
28
|
+
short_arrays as short_arrays,
|
|
29
|
+
)
|
|
30
|
+
from ainmax._src.filters import (
|
|
31
|
+
is_arrayish as is_arrayish,
|
|
32
|
+
is_inexact_arrayish as is_inexact_arrayish,
|
|
33
|
+
is_inexact_jax_array as is_inexact_jax_array,
|
|
34
|
+
is_inexact_jax_array_like as is_inexact_jax_array_like,
|
|
35
|
+
is_inexact_named_array as is_inexact_named_array,
|
|
36
|
+
is_jax_array as is_jax_array,
|
|
37
|
+
is_jax_array_like as is_jax_array_like,
|
|
38
|
+
is_named_array as is_named_array,
|
|
39
|
+
is_scalar as is_scalar,
|
|
40
|
+
)
|
|
41
|
+
from ainmax._src.hof import fold as fold, scan as scan, vmap as vmap
|
|
42
|
+
from ainmax._src.partition import PM as PM
|
|
43
|
+
from ainmax._src.typing import NamedArray as NamedArray, NamedArrayLike as NamedArrayLike
|
|
File without changes
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import operator as op
|
|
2
|
+
|
|
3
|
+
import equinox as eqx
|
|
4
|
+
import jax.numpy as jnp
|
|
5
|
+
import numpy as np
|
|
6
|
+
import wadler_lindig as wl
|
|
7
|
+
from jax import Array
|
|
8
|
+
from jax.sharding import Sharding
|
|
9
|
+
|
|
10
|
+
from ainmax._src.axis_util import check_unique_axis_names
|
|
11
|
+
from ainmax._src.config import config
|
|
12
|
+
from ainmax._src.typing import Axis, NamedArray
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _validate_shape(array: Array, axes: tuple[Axis, ...]) -> None:
|
|
16
|
+
if array.ndim != len(axes):
|
|
17
|
+
raise ValueError(
|
|
18
|
+
f"Array has {array.ndim} dimensions but {len(axes)} axes were provided: {[ax.label for ax in axes]}"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
for i, (size, ax) in enumerate(zip(array.shape, axes)):
|
|
22
|
+
if size != ax.size:
|
|
23
|
+
raise ValueError(
|
|
24
|
+
f"Axis {ax.label!r} at position {i} has declared size {ax.size} but array has size {size}"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
check_unique_axis_names(axes)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class NamedArrayImpl(NamedArray, eqx.Module):
|
|
31
|
+
array: Array
|
|
32
|
+
axes: tuple[Axis, ...] = eqx.field(static=True)
|
|
33
|
+
|
|
34
|
+
def __check_init__(self):
|
|
35
|
+
_validate_shape(self.array, self.axes)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def shape(self) -> tuple[int, ...]:
|
|
39
|
+
return self.array.shape
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def dtype(self) -> jnp.dtype:
|
|
43
|
+
return self.array.dtype
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def itemsize(self) -> int:
|
|
47
|
+
return self.dtype.itemsize
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def nbytes(self) -> int:
|
|
51
|
+
return self.array.nbytes
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def ndim(self) -> int:
|
|
55
|
+
return self.array.ndim
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def size(self) -> int:
|
|
59
|
+
return self.array.size
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def sharding(self) -> Sharding:
|
|
63
|
+
return self.array.sharding
|
|
64
|
+
|
|
65
|
+
def __pdoc__(self, **kwargs) -> wl.AbstractDoc:
|
|
66
|
+
"""Wadler-Lindig pretty-printer for NamedArrayImpl.
|
|
67
|
+
|
|
68
|
+
Default (compact):
|
|
69
|
+
f32[x:32, y:32]
|
|
70
|
+
|
|
71
|
+
Verbose (pass ``verbose=True`` through ``wl.pdoc``/``wl.pformat`` kwargs):
|
|
72
|
+
NamedArray(
|
|
73
|
+
array=f32[32,32], axes=(Axis(name='x', size=32), Axis(name='y', size=32))
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
Handles ``array=None`` gracefully (equinox filtered/partitioned pytrees).
|
|
77
|
+
"""
|
|
78
|
+
indent: int = kwargs.get("indent", 2)
|
|
79
|
+
short_arrays: bool = kwargs.get("short_arrays", True) and config.short_arrays
|
|
80
|
+
pdoc_kwargs = {"short_arrays": short_arrays, "indent": indent}
|
|
81
|
+
|
|
82
|
+
array = self.array
|
|
83
|
+
axes = self.axes
|
|
84
|
+
|
|
85
|
+
if not short_arrays or array is None:
|
|
86
|
+
return wl.bracketed(
|
|
87
|
+
begin=wl.TextDoc("NamedArray("),
|
|
88
|
+
docs=[
|
|
89
|
+
wl.TextDoc("array=") + wl.pdoc(array, **pdoc_kwargs),
|
|
90
|
+
wl.TextDoc("axes=") + wl.pdoc(axes, **pdoc_kwargs),
|
|
91
|
+
],
|
|
92
|
+
sep=wl.comma,
|
|
93
|
+
end=wl.TextDoc(")"),
|
|
94
|
+
indent=indent,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# compact form: dtype[ax0_name:ax0_size, ax1_name:ax1_size, …]
|
|
98
|
+
dtype_str = (
|
|
99
|
+
str(array.dtype).replace("float", "f").replace("uint", "u").replace("int", "i").replace("complex", "c")
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
axis_docs = [wl.TextDoc(f"{ax.label}:{ax.size}") for ax in axes]
|
|
103
|
+
|
|
104
|
+
return wl.TextDoc(dtype_str) + wl.bracketed(
|
|
105
|
+
begin=wl.TextDoc("["),
|
|
106
|
+
docs=axis_docs,
|
|
107
|
+
sep=wl.comma,
|
|
108
|
+
end=wl.TextDoc("]"),
|
|
109
|
+
indent=indent,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
def __len__(self):
|
|
113
|
+
try:
|
|
114
|
+
return self.shape[0]
|
|
115
|
+
except IndexError as err:
|
|
116
|
+
raise TypeError("len() of unsized object") from err # same as numpy error
|
|
117
|
+
|
|
118
|
+
def __bool__(self):
|
|
119
|
+
return bool(self.array)
|
|
120
|
+
|
|
121
|
+
def __float__(self):
|
|
122
|
+
return float(self.array)
|
|
123
|
+
|
|
124
|
+
def __int__(self):
|
|
125
|
+
return int(self.array)
|
|
126
|
+
|
|
127
|
+
def __complex__(self):
|
|
128
|
+
return complex(self.array)
|
|
129
|
+
|
|
130
|
+
def __hex__(self):
|
|
131
|
+
return hex(self.array)
|
|
132
|
+
|
|
133
|
+
def __oct__(self):
|
|
134
|
+
return oct(self.array)
|
|
135
|
+
|
|
136
|
+
def __index__(self):
|
|
137
|
+
return op.index(self.array)
|
|
138
|
+
|
|
139
|
+
def __repr__(self) -> str:
|
|
140
|
+
return wl.pformat(self)
|
|
141
|
+
|
|
142
|
+
def __str__(self) -> str:
|
|
143
|
+
return wl.pformat(self)
|
|
144
|
+
|
|
145
|
+
def __iter__(self):
|
|
146
|
+
return self.array.__iter__()
|
|
147
|
+
|
|
148
|
+
def __array__(self, dtype: np.dtype | None = None, context: None = None, copy: bool | None = None):
|
|
149
|
+
del context # unused
|
|
150
|
+
|
|
151
|
+
kwds = {} if copy is None else {"copy": copy}
|
|
152
|
+
return np.asarray(self.array, dtype=dtype, **kwds) # pyrefly: ignore[no-matching-overload]
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _short_dtype(array) -> str:
|
|
156
|
+
"""Contract dtype the same way wl.array_summary does internally."""
|
|
157
|
+
s = str(array.dtype)
|
|
158
|
+
return s.replace("float", "f").replace("uint", "u").replace("int", "i").replace("complex", "c")
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import keyword
|
|
2
|
+
import re
|
|
3
|
+
from collections.abc import Mapping, Sequence
|
|
4
|
+
from dataclasses import dataclass, replace
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
_AXIS_NAME = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def validate_axis_name(name) -> str:
|
|
11
|
+
if not isinstance(name, str):
|
|
12
|
+
raise TypeError(f"Axis name must be a string, got {type(name).__name__}")
|
|
13
|
+
|
|
14
|
+
if not _AXIS_NAME.fullmatch(name):
|
|
15
|
+
raise ValueError(f"Invalid axis name: {name!r}")
|
|
16
|
+
|
|
17
|
+
if keyword.iskeyword(name) or keyword.issoftkeyword(name):
|
|
18
|
+
raise ValueError(f"Axis name is a Python keyword: {name!r}")
|
|
19
|
+
|
|
20
|
+
return name
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class Axis:
|
|
25
|
+
size: int
|
|
26
|
+
name: str | None = None
|
|
27
|
+
|
|
28
|
+
def __post_init__(self):
|
|
29
|
+
if isinstance(self.size, bool) or not isinstance(self.size, int):
|
|
30
|
+
raise TypeError(f"Axis size must be an integer, got {type(self.size).__name__}")
|
|
31
|
+
if self.size <= 0:
|
|
32
|
+
raise ValueError(f"Axis size must be positive, got {self.size}")
|
|
33
|
+
|
|
34
|
+
if self.name is not None:
|
|
35
|
+
validate_axis_name(self.name)
|
|
36
|
+
|
|
37
|
+
def rename(self, name: str):
|
|
38
|
+
return replace(self, name=name)
|
|
39
|
+
|
|
40
|
+
def resize(self, size: int):
|
|
41
|
+
return replace(self, size=size)
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def label(self) -> str:
|
|
45
|
+
return self.name if self.name is not None else "?"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
AxisLike = str | Axis
|
|
49
|
+
|
|
50
|
+
AxisDict = Mapping[str, int]
|
|
51
|
+
AxisDim = int | Axis
|
|
52
|
+
AxisShape = AxisDim | Sequence[AxisDim] | AxisDict
|
|
53
|
+
|
|
54
|
+
AxisSpec = AxisLike | Sequence[AxisLike]
|
|
55
|
+
|
|
56
|
+
AxisSelector = int | AxisLike
|
|
57
|
+
AxisSelection = AxisSelector | Sequence[int | AxisLike]
|