Abstract your array operations.
Project description
autoray is a lightweight python AUTOmatic-arRAY library for
abstracting your tensor operations. Primarily it provides an
automatic dispatch mechanism
that means you can write backend agnostic code that works for:
- numpy
- pytorch
- jax
- cupy
- dask
- autograd
- tensorflow
- sparse
- paddle
- mars
- ... and indeed any library that provides a numpy-ish api, even if it
knows nothing about
autoray.
Beyond that, abstracting the array interface allows you to:
- swap custom versions of functions for specific backends
- trace through computations lazily without actually running them
- automatically share intermediates and fold constants in computations
- compile functions with a unified interface for different backends
Basic usage
The main function of autoray is
do,
which takes a function
name followed by *args and **kwargs, and automatically looks up (and
caches) the correct function to match the equivalent numpy call:
import autoray as ar
def noised_svd(x):
# automatic dispatch based on supplied array
U, s, VH = ar.do('linalg.svd', x)
# automatic dispatch based on different array
sn = s + 0.1 * ar.do('random.normal', size=ar.shape(s), like=s)
# automatic dispatch for multiple arrays for certain functions
return ar.do('einsum', 'ij,j,jk->ik', U, sn, VH)
# explicit backend given by string
x = ar.do('random.uniform', size=(100, 100), like="torch")
# this function now works for any backend
y = noised_svd(x)
# explicit inference of backend from array
ar.infer_backend(y)
# 'torch'
If you don't like the explicit do syntax, or simply want a
drop-in replacement for existing code, you can also import the autoray.numpy
module:
from autoray import numpy as np
# set a temporary default backend
with ar.backend_like('cupy'):
z = np.ones((3, 4), dtype='float32')
np.exp(z)
# array([[2.7182817, 2.7182817, 2.7182817, 2.7182817],
# [2.7182817, 2.7182817, 2.7182817, 2.7182817],
# [2.7182817, 2.7182817, 2.7182817, 2.7182817]], dtype=float32)
Alternatively you can use
autoray.get_namespace
to get a backend specific (with optional default device and dtype) namespace
object, (c.f. the Python Array Api):
xp = ar.get_namespace(z)
xp.einsum("ii->i", z)
Custom backends and functions can be dynamically registered with:
The main documentation is available at autoray.readthedocs.io.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autoray-0.8.4.tar.gz.
File metadata
- Download URL: autoray-0.8.4.tar.gz
- Upload date:
- Size: 1.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4ce7066334279b216431a260bb5b5b84d87815e3295020a76d8701e43dc3432
|
|
| MD5 |
25b0e9257edbb122d06a80ec6b945c9c
|
|
| BLAKE2b-256 |
308fc951dc8acf51db7554b075f184488400401472ad9aa25edffa9ed9982621
|
Provenance
The following attestation bundles were made for autoray-0.8.4.tar.gz:
Publisher:
pypi-release.yml on jcmgray/autoray
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autoray-0.8.4.tar.gz -
Subject digest:
b4ce7066334279b216431a260bb5b5b84d87815e3295020a76d8701e43dc3432 - Sigstore transparency entry: 742273827
- Sigstore integration time:
-
Permalink:
jcmgray/autoray@5c74f7ea6ca8b975a147304afab5eb6897f1eb85 -
Branch / Tag:
refs/tags/v0.8.4 - Owner: https://github.com/jcmgray
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-release.yml@5c74f7ea6ca8b975a147304afab5eb6897f1eb85 -
Trigger Event:
release
-
Statement type:
File details
Details for the file autoray-0.8.4-py3-none-any.whl.
File metadata
- Download URL: autoray-0.8.4-py3-none-any.whl
- Upload date:
- Size: 937.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
287c366f2ba9d9c045ffa2b2421deea07439ed5a7f18b5d25b3a406d53b5c63c
|
|
| MD5 |
9af7492a0b9cad0322a24561c6638a68
|
|
| BLAKE2b-256 |
b249bc1c2e3ca121f6468e0cd8d78a96ab2dcb65d0f003917f5d4edcaea103a2
|
Provenance
The following attestation bundles were made for autoray-0.8.4-py3-none-any.whl:
Publisher:
pypi-release.yml on jcmgray/autoray
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autoray-0.8.4-py3-none-any.whl -
Subject digest:
287c366f2ba9d9c045ffa2b2421deea07439ed5a7f18b5d25b3a406d53b5c63c - Sigstore transparency entry: 742273852
- Sigstore integration time:
-
Permalink:
jcmgray/autoray@5c74f7ea6ca8b975a147304afab5eb6897f1eb85 -
Branch / Tag:
refs/tags/v0.8.4 - Owner: https://github.com/jcmgray
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-release.yml@5c74f7ea6ca8b975a147304afab5eb6897f1eb85 -
Trigger Event:
release
-
Statement type: