Skip to main content

Write backend agnostic numeric code compatible with any numpy-ish array library.

Project description

A lightweight python AUTOmatic-arRAY library. Write numeric code that works for:

Travis-CI Code Coverage Code Quality

For example consider this function that orthogonalizes a matrix using the modified Gram-Schmidt algorithm:

from autoray import do

def modified_gram_schmidt(X):

    Q = []
    for j in range(0, X.shape[0]):

        q = X[j, :]
        for i in range(0, j):
            rij = do('tensordot', do('conj', Q[i]), q, 1)
            q = q - rij * Q[i]

        rjj = do('linalg.norm', q, 2)
        Q.append(q / rjj)

    return do('stack', Q, axis=0, like=X)

Which is now compatible with all of the above mentioned libraries! (N.B. this particular example is also probably slow). If you don’t like the explicit do syntax, then you can import the fake numpy object as a drop-in replacement instead:

>>> from autoray import numpy as np

>>> import tensorflow as tf
>>> x = tf.random.uniform(shape=(2, 3, 4))

>>> np.tensordot(x, x, [(2, 1), (2, 1)])
<tf.Tensor 'Tensordot:0' shape=(2, 2) dtype=float32>

>>> np.eye(3, like=x)  # many functions obviously can't dispatch without the `like` keyword
<tf.Tensor 'eye/MatrixDiag:0' shape=(3, 3) dtype=float32>

Of course complete compatibility is not going to be possible for all functions, operations and libraries, but autoray hopefully makes the job much easier.

How does it work?

autoray works using essentially a single dispatch mechanism on the first argument for do, or the like keyword argument if specified, fetching functions from the whichever module defined that supplied array. Additionally, it caches a few custom translations and lookups so as to handle libraries like tensorflow that don’t exactly replicate the numpy api (for example sum gets translated to tensorflow.reduce_sum).

Alternatives

  • The __array_function__ protocol has been suggested and now implemented in numpy. Hopefully this will eventually negate the need for autoray. On the other hand, third party libraries themselves need to implement the interface, which has not been done, for example, in tensorflow yet.

  • The uarray project aims to develop a generic array interface but comes with the warning “This is experimental and very early research code. Don’t use this.”.

Installation

You can install autoray as standard with pip. Alternatively, simply copy the monolithic autoray.py into your project internally (if dependencies aren’t your thing).

Contributing

Pull requests such as extra translations are very welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autoray-0.1.0.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

autoray-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file autoray-0.1.0.tar.gz.

File metadata

  • Download URL: autoray-0.1.0.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for autoray-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0f364715e2802b0bc0c17f0c873d167c2549ac0adbde545aa1ece493a1b87bc0
MD5 236950930bcb8444df84f11b4b514b3b
BLAKE2b-256 b3ab37fe12c451d0261a9083ae1869417d468eee1825fcb5ded0b162e6ca397d

See more details on using hashes here.

File details

Details for the file autoray-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: autoray-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for autoray-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fd4cf0a4280a39dcc5df310612d3f946a451f495e9356590eb4dfcfaa89ca68
MD5 45b7024601ea3d0357e85ddfb582a957
BLAKE2b-256 ab0ff38840fc7f1b945a864ec5b4a12b9888151ee8971bc15b50ba2c08395c85

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page