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
- 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:
from 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)
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
File details
Details for the file autoray-0.6.12.tar.gz
.
File metadata
- Download URL: autoray-0.6.12.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 721328aa06fc3577155d988052614a7b4bd6e4d01b340695344031ee4abd2a1e |
|
MD5 | 78f3c562cf131645d34751304f774632 |
|
BLAKE2b-256 | 0fdc5c70e7e8972b2618c45b4b8864765c36520907b03617d4ccfe5814ea265e |
File details
Details for the file autoray-0.6.12-py3-none-any.whl
.
File metadata
- Download URL: autoray-0.6.12-py3-none-any.whl
- Upload date:
- Size: 51.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ed7a4abcec052bcbb4f0447c426d0a0b9b9fa03ab71e76eaa77747ca43ac3e2 |
|
MD5 | 1545597d386bc4adf0f3b548dc40de06 |
|
BLAKE2b-256 | 1c4969aaa6e5a946b1965b7a42fc515449ae5a2bb7cd7e8a4df8b71496933a5a |