Skip to main content
QUICK REVIEW

[Paper Review] Equinox: neural networks in JAX via callable PyTrees and filtered transformations

Patrick Kidger, Cristian García|arXiv (Cornell University)|Oct 30, 2021
Software Engineering ResearchComputer Science21 citations
TL;DR

Equinox introduces a neural network library for JAX that enables PyTorch-style class-based model definition without sacrificing JAX's functional programming paradigm. It represents models as callable PyTrees and uses filtered transformations to selectively apply JAX operations like jit, grad, and vmap to only the relevant parameters, achieving full compatibility with native JAX while avoiding new abstractions or special-purpose wrappers.

ABSTRACT

JAX and PyTorch are two popular Python autodifferentiation frameworks. JAX is based around pure functions and functional programming. PyTorch has popularised the use of an object-oriented (OO) class-based syntax for defining parameterised functions, such as neural networks. That this seems like a fundamental difference means current libraries for building parameterised functions in JAX have either rejected the OO approach entirely (Stax) or have introduced OO-to-functional transformations, multiple new abstractions, and been limited in the extent to which they integrate with JAX (Flax, Haiku, Objax). Either way this OO/functional difference has been a source of tension. Here, we introduce `Equinox', a small neural network library showing how a PyTorch-like class-based approach may be admitted without sacrificing JAX-like functional programming. We provide two main ideas. One: parameterised functions are themselves represented as `PyTrees', which means that the parameterisation of a function is transparent to the JAX framework. Two: we filter a PyTree to isolate just those components that should be treated when transforming (`jit', `grad' or `vmap'-ing) a higher-order function of a parameterised function -- such as a loss function applied to a model. Overall Equinox resolves the above tension without introducing any new programmatic abstractions: only PyTrees and transformations, just as with regular JAX. Equinox is available at \\url{https://github.com/patrick-kidger/equinox}.

Motivation & Objective

  • To resolve the tension between object-oriented model building (as in PyTorch) and functional programming (as in JAX) in deep learning frameworks.
  • To enable class-based neural network definitions in JAX without introducing new abstractions or coupling to custom transformations.
  • To allow selective application of JAX transformations (jit, grad, vmap) to only specific components of a model, such as learnable parameters, while preserving compatibility with arbitrary JAX code.
  • To maintain full interoperability with existing JAX tools and transformations by avoiding special-purpose wrappers or modified versions of jit/grad/vmap.
  • To demonstrate that a PyTorch-like syntax can be natively supported in JAX using only PyTrees and standard JAX transformations.

Proposed method

  • Models are defined as Python classes whose instance attributes are stored as a PyTree, with the __call__ method serving as a pure, parameterized function.
  • The model instance is treated as a PyTree, allowing JAX operations like jit, grad, and vmap to be applied directly to the callable instance.
  • Filtering is performed via the partition function, which splits a PyTree into two parts based on a predicate (e.g., is_array), isolating parameters from non-learnable components like activation functions or static metadata.
  • Filtered transformations such as filter_jit and filter_grad are provided as convenience wrappers that apply JAX transformations only to the specified subset of parameters, without requiring special handling.
  • The filtering process occurs at the API boundary, not within the model structure, ensuring compatibility with future JAX transformations and avoiding stateful metadata like requires_grad flags.
  • Bound methods are treated as PyTrees via Python’s descriptor protocol, enabling full PyTree compatibility for method calls.

Experimental results

Research questions

  • RQ1Can a PyTorch-style class-based API for neural networks be natively supported in JAX without introducing new abstractions or breaking functional programming principles?
  • RQ2How can JAX’s transformations (jit, grad, vmap) be selectively applied to only the learnable parameters of a model while preserving compatibility with standard JAX operations?
  • RQ3Is it possible to represent parameterized functions as callable PyTrees that are fully compatible with JAX’s functional programming model?
  • RQ4Can filtering be used to isolate differentiable parameters from non-differentiable components (e.g., activation functions) without embedding metadata into the model structure?
  • RQ5Does this approach eliminate the need for library-specific wrappers or transformations, such as those required in Flax, Haiku, or Objax?

Key findings

  • Equinox successfully enables a PyTorch-like class-based syntax for defining neural networks in JAX without introducing new abstractions or coupling to custom transformations.
  • The use of PyTrees to represent models allows JAX’s native jit, grad, and vmap to be applied directly to model instances, preserving full functional programming compatibility.
  • Filtered transformations such as filter_jit and filter_grad operate on arbitrary PyTrees and are not tied to model-specific representations, ensuring broad compatibility with existing JAX code.
  • The filtering mechanism, based on partitioning PyTrees using a predicate function, allows selective differentiation and compilation to only the relevant parameters, such as JAX arrays, while leaving non-learnable components (e.g., Python callables) untouched.
  • The approach avoids the use of stateful metadata like requires_grad, instead placing filtering logic at the API boundary, which enhances extensibility and future-proofing.
  • Equinox demonstrates that neural network libraries can be built using only standard JAX primitives—PyTrees and transformations—without requiring special-purpose wrappers or runtime abstractions.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.