Skip to main content

EagerPy is a thin wrapper around PyTorch, TensorFlow Eager, JAX and NumPy that unifies their interface and thus allows writing code that works natively across all of them.

Project description

https://badge.fury.io/py/eagerpy.svg https://codecov.io/gh/jonasrauber/eagerpy/branch/master/graph/badge.svg https://img.shields.io/badge/code%20style-black-000000.svg

EagerPy: Writing Code That Works Natively with PyTorch, TensorFlow, JAX, and NumPy

EagerPy is a Python framework that lets you write code that automatically works natively with PyTorch, TensorFlow, JAX, and NumPy. EagerPy is also great when you work with just one framework but prefer a clean and consistent API that is fully chainable, provides extensive type annotions and lets you write beautiful code.

🔥 Design goals

  • Native Performance: EagerPy operations get directly translated into the corresponding native operations.

  • Fully Chainable: All functionality is available as methods on the tensor objects and as EagerPy functions.

  • Type Checking: Catch bugs before running your code thanks to EagerPy’s extensive type annotations.

📖 Documentation

Learn more about EagerPy in the documentation.

🚀 Quickstart

pip install eagerpy

EagerPy requires Python 3.6 or newer. Besides that, all essential dependencies are automatically installed. To use it with PyTorch, TensorFlow, JAX, or NumPy, the respective framework needs to be installed separately. These frameworks are not declared as dependencies because not everyone wants to use and thus install all of them and because some of these packages have different builds for different architectures and CUDA versions.

🎉 Example

import torch
x = torch.tensor([1., 2., 3., 4., 5., 6.])

import tensorflow as tf
x = tf.constant([1., 2., 3., 4., 5., 6.])

import jax.numpy as np
x = np.array([1., 2., 3., 4., 5., 6.])

import numpy as np
x = np.array([1., 2., 3., 4., 5., 6.])

# No matter which framwork you use, you can use the same code
import eagerpy as ep

# Just wrap a native tensor using EagerPy
x = ep.astensor(x)

# All of EagerPy's functionality is available as methods
x = x.reshape((2, 3))
x.flatten(start=1).square().sum(axis=-1).sqrt()
# or just: x.flatten(1).norms.l2()

# and as functions (yes, gradients are also supported!)
loss, grad = ep.value_and_grad(loss_fn, x)
ep.clip(x + eps * grad, 0, 1)

# You can even write functions that work transparently with
# Pytorch tensors, TensorFlow tensors, JAX arrays, NumPy arrays

def my_universal_function(a, b, c):
    # Convert all inputs to EagerPy tensors
    a, b, c = ep.astensors(a, b, c)

    # performs some computations
    result = (a + b * c).square()

    # and return a native tensor
    return result.raw

🗺 Use cases

Foolbox Native, the latest version of Foolbox, a popular adversarial attacks library, has been rewritten from scratch using EagerPy instead of NumPy to achieve native performance on models developed in PyTorch, TensorFlow and JAX, all with one code base.

EagerPy is also used by other frameworks to reduce code duplication (e.g. GUDHI) or to compare the performance of different frameworks.

📄 Citation

If you use EagerPy, please cite our paper using the this BibTex entry:

@article{rauber2020eagerpy,
  title={{EagerPy}: Writing Code That Works Natively with {PyTorch}, {TensorFlow}, {JAX}, and {NumPy}},
  author={Rauber, Jonas and Bethge, Matthias and Brendel, Wieland},
  journal={arXiv preprint arXiv:2008.04175},
  year={2020},
  url={https://eagerpy.jonasrauber.de},
}

🐍 Compatibility

We currently test with the following versions:

  • PyTorch 1.4.0

  • TensorFlow 2.1.0

  • JAX 0.1.57

  • NumPy 1.18.1

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

eagerpy-0.30.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

eagerpy-0.30.0-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

Details for the file eagerpy-0.30.0.tar.gz.

File metadata

  • Download URL: eagerpy-0.30.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.14

File hashes

Hashes for eagerpy-0.30.0.tar.gz
Algorithm Hash digest
SHA256 014c02b5a7f7e19f8471885cf8aa469f2e9cf518c88400f20b6b8db83d413106
MD5 00650aa2e445f30e97306399b2fa196a
BLAKE2b-256 03333bc665b3438fed5af8e95ec0ad04c7b12a228b36246ba4981031022f783d

See more details on using hashes here.

File details

Details for the file eagerpy-0.30.0-py3-none-any.whl.

File metadata

  • Download URL: eagerpy-0.30.0-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.14

File hashes

Hashes for eagerpy-0.30.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79c461b04577f02bf3b48191b2f911b55521204df99ec02288d96bfa34f13d80
MD5 65578ddca2aea48cd11c2bf10969cafd
BLAKE2b-256 e0b7445e74a70503630a9d3c58563da1f0c831532d45bd5987b861f562826ea4

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