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

Jonas Rauber, Matthias Bethge, Wieland BrendelarXiv 2020

Abstract

EagerPy is a Python framework that lets you write code that automatically works natively with PyTorch, TensorFlow, JAX, and NumPy. Library developers no longer need to choose between supporting just one of these frameworks or reimplementing the library for each framework and dealing with code duplication. Users of such libraries can more easily switch frameworks without being locked in by a specific 3rd party library. Beyond multi-framework support, EagerPy also brings comprehensive type annotations and consistent support for method chaining to any framework. The latest documentation is available online at https://eagerpy.jonasrauber.de and the code can be found on GitHub at https://github.com/jonasrauber/eagerpy.

Introduction

The deep learning ecosystem is currently fragmented across several powerful but incompatible frameworks. While PyTorch, TensorFlow, and JAX share similar high-level goals, their APIs and underlying execution models are frustratingly inconsistent. For developers of specialized libraries—such as those focused on adversarial attacks, uncertainty estimation, or topological data analysis—this fragmentation presents a difficult choice: support only one framework, or manually reimplement the entire library for every backend. The latter leads to massive code duplication and significant maintenance overhead.

EagerPy is a lightweight Python framework designed to bridge these gaps. It allows developers to write deep learning code once and run it natively—with full GPU acceleration—on PyTorch, TensorFlow, JAX, and NumPy. By providing a unified, chainable API and functional automatic differentiation, EagerPy eliminates the need for framework-specific codebases without sacrificing performance.

The EagerPy Framework

EagerPy acts as a thin, transparent wrapper around native tensors. Its architecture is built on several key design choices that ensure both flexibility and performance.

Unified Tensor Abstraction

EagerPy defines an abstract Tensor class. When a native PyTorch or TensorFlow tensor is passed to EagerPy, it is wrapped in a framework-specific subclass. This subclass maps EagerPy methods directly to the underlying native functions. This abstraction allows developers to write code that is agnostic to the specific tensor type while still utilizing the backend's optimized operations.

Zero-Overhead Delegation

To maintain native performance, EagerPy avoids the common pitfall of converting data to intermediate formats like NumPy. Instead, it maintains references to the original native tensors (e.g., a PyTorch tensor residing on a GPU) and delegates operations directly to the backend. This ensures that computations stay on the hardware accelerator and benefit from framework-specific optimizations.

Functional Autograd Unification

One of the most significant hurdles in framework-agnostic development is the difference in automatic differentiation (autograd) models. PyTorch uses an object-oriented, in-place approach (.backward()), TensorFlow utilizes context managers (GradientTape), and JAX employs a functional approach (grad(f)). EagerPy unifies these by adopting JAX’s functional style, exposing a consistent value_and_grad API that works seamlessly across all supported backends.

Key Design Principles

Semantic vs. Syntactic Unification

True framework-agnosticism requires more than just renaming functions (e.g., standardizing dim vs. axis). EagerPy focuses on unifying the behavior of operations and gradient management. To ensure this consistency, the framework relies on an extensive automated test suite that verifies operations produce identical results across all backends.

Gradual Adoption and Transparency

EagerPy is designed for "transparent" integration. Developers can wrap a native tensor, perform specific operations using EagerPy’s unified API, and then immediately extract the native tensor using the .raw property. This allows for the gradual adoption of EagerPy within existing pipelines without requiring a full codebase rewrite.

Type Safety and Readability

EagerPy introduces "fully chainable" APIs (e.g., x.square().sum().sqrt()), which often improves readability compared to the deeply nested function calls common in standard NumPy or TensorFlow. Furthermore, EagerPy provides comprehensive PEP 484 type annotations, enabling developers to catch bugs early through static analysis tools like Mypy.

Real-World Impact and Results

The primary proof of concept for EagerPy is Foolbox 3.0, a widely used library for adversarial attacks. By migrating to EagerPy, the Foolbox developers replaced multiple framework-specific codebases with a single, unified codebase. This transition achieved native performance on all backends while significantly reducing the maintenance burden. Other specialized libraries, such as GUDHI (computational topology), have also adopted EagerPy to provide automatic differentiation support across frameworks.

Performance benchmarks demonstrate that because EagerPy delegates directly to native operations, the computational overhead is virtually zero compared to writing native framework code. This makes it a viable solution for performance-critical research and production environments.

Limitations and Future Work

As the name implies, EagerPy is primarily optimized for "eager" execution. While this covers the majority of research use cases, it may not be fully optimized for complex static-graph deployment scenarios where framework-specific compilers (like TorchScript or XLA) require more rigid structures. Additionally, as a wrapper, EagerPy faces a continuous engineering challenge to maintain feature parity as PyTorch, TensorFlow, and JAX evolve. The authors address this through their robust testing infrastructure, ensuring the library remains reliable as backends update.

Conclusion

EagerPy provides a necessary bridge in a fragmented deep learning landscape. By offering a unified API that preserves native performance and introduces functional autograd consistency, it empowers library developers to reach a broader audience with less effort. It moves the ecosystem toward a future where the choice of a deep learning framework does not dictate the availability of high-quality, specialized software tools.

Summary updated

BibTeX
@misc{rauber2020eagerpy,
  author = {Jonas Rauber and Matthias Bethge and Wieland Brendel},
  title = {{EagerPy: Writing Code That Works Natively with PyTorch, TensorFlow, JAX, and NumPy}},
  year = {2020},
  eprint = {2008.04175},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2008.04175},
}
Figure