見出し画像

Jaxpr interpreters in JAX

Writing custom Jaxpr interpreters in JAX
Open in Colab Open in Kaggle

JAX offers several composable function transformations (jit, grad, vmap, etc.) that enable writing concise, accelerated code.

Here we show how to add your own function transformations to the system, by writing a custom Jaxpr interpreter. And we'll get composability with all the other transformations for free.

This example uses internal JAX APIs, which may break at any time. Anything not in the API Documentation should be assumed internal.

What is JAX doing?
JAX provides a NumPy-like API for numerical computing which can be used as is, but JAX's true power comes from composable function transformations. Take the jit function transformation, which takes in a function and returns a semantically identical function but is lazily compiled by XLA for accelerators.

When we call fast_f, what happens? JAX traces the function and constructs an XLA computation graph. The graph is then JIT-compiled and executed. Other transformations work similarly in that they first trace the function and handle the output trace in some way. To learn more about Jax's tracing machinery, you can refer to the "How it works" section in the README.

Jaxpr tracer
A tracer of special importance in Jax is the Jaxpr tracer, which records ops into a Jaxpr (Jax expression). A Jaxpr is a data structure that can be evaluated like a mini functional programming language and thus Jaxprs are a useful intermediate representation for function transformation.

To get a first look at Jaxprs, consider the make_jaxpr transformation. make_jaxpr is essentially a "pretty-printing" transformation: it transforms a function into one that, given example arguments, produces a Jaxpr representation of its computation. make_jaxpr is useful for debugging and introspection. Let's use it to look at how some example Jaxprs are structured.

jaxpr.invars - the invars of a Jaxpr are a list of the input variables to Jaxpr, analogous to arguments in Python functions.
jaxpr.outvars - the outvars of a Jaxpr are the variables that are returned by the Jaxpr. Every Jaxpr has multiple outputs.
jaxpr.constvars - the constvars are a list of variables that are also inputs to the Jaxpr, but correspond to constants from the trace (we'll go over these in more detail later).
jaxpr.eqns - a list of equations, which are essentially let-bindings. Each equation is a list of input variables, a list of output variables, and a primitive, which is used to evaluate inputs to produce outputs. Each equation also has a params, a dictionary of parameters.
Altogether, a Jaxpr encapsulates a simple program that can be evaluated with inputs to produce an output. We'll go over how exactly to do this later. The important thing to note now is that a Jaxpr is a data structure that can be manipulated and evaluated in whatever way we want.

Why are Jaxprs useful?
Jaxprs are simple program representations that are easy to transform. And because Jax lets us stage out Jaxprs from Python functions, it gives us a way to transform numerical programs written in Python.

Your first interpreter: invert
Let's try to implement a simple function "inverter", which takes in the output of the original function and returns the inputs that produced those outputs. For now, let's focus on simple, unary functions which are composed of other invertible unary functions.

  1. Evaluating a Jaxpr
    Before we write a custom Jaxpr interpreter, let's first implement the "default" interpreter, eval_jaxpr, which evaluates the Jaxpr as-is, computing the same values that the original, un-transformed Python function would.

To do this, we first create an environment to store the values for each of the variables, and update the environment with each equation we evaluate in the Jaxpr.

Notice that eval_jaxpr will always return a flat list even if the original function does not.

Furthermore, this interpreter does not handle higher-order primitives (like jit and pmap), which we will not cover in this guide. You can refer to core.eval_jaxpr (link) to see the edge cases that this interpreter does not cover.

Custom inverse Jaxpr interpreter
An inverse interpreter doesn't look too different from eval_jaxpr. We'll first set up the registry which will map primitives to their inverses. We'll then write a custom interpreter that looks up primitives in the registry.

It turns out that this interpreter will also look similar to the "transpose" interpreter used in reverse-mode autodifferentiation found here.

We'll now register inverses for some of the primitives. By convention, primitives in Jax end in _p and a lot of the popular ones live in lax.

inverse will first trace the function, then custom-interpret the Jaxpr. Let's set up a simple skeleton.

ref
https://github.com/google/jax/blob/main/docs/notebooks/Writing_custom_interpreters_in_Jax.ipynb

この記事が気に入ったらサポートをしてみませんか?