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:
Beyond that, abstracting the array interface allows you to:
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):
U, s, VH = ar.do('linalg.svd', x)
sn = s + 0.1 * ar.do('random.normal', size=ar.shape(s), like=s)
return ar.do('einsum', 'ij,j,jk->ik', U, sn, VH)
x = ar.do('random.uniform', size=(100, 100), like="torch")
y = noised_svd(x)
ar.infer_backend(y)
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
with ar.backend_like('cupy'):
z = np.ones((3, 4), dtype='float32')
np.exp(z)
Custom backends and functions can be dynamically registered with:
The main documentation is available at autoray.readthedocs.io.