Jax For Mac

JAX is NumPy on the CPU, GPU, and TPU, with great automatic differentiation for high-performance machine learning research.

With its updated version of Autograd, JAX can automatically differentiate native Python and NumPy code. It can differentiate through a large subset of Python’s features, including loops, ifs, recursion, and closures, and it can even take derivatives of derivatives of derivatives. It supports reverse-mode as well as forward-mode differentiation, and the two can be composed arbitrarily to any order.

What’s new is that JAX uses XLA to compile and run your NumPy code on accelerators, like GPUs and TPUs. Compilation happens under the hood by default, with library calls getting just-in-time compiled and executed. But JAX even lets you just-in-time compile your own Python functions into XLA-optimized kernels using a one-function API. Compilation and automatic differentiation can be composed arbitrarily, so you can express sophisticated algorithms and getmaximal performance without having to leave Python.

Jax for mac mini

Multiplying Matrices¶

We’ll be generating random data in the following examples. One big difference between NumPy and JAX is how you generate random numbers. For more details, see Common Gotchas in JAX.

Jaxx Liberty Blockchain Wallet is available for free on Android, iOS, Mac OS X, Windows, Linux, and Google Chrome extension. Start your journey. The leading blockchain wallet with built-in Exchange, Portfolio, Crypto News, Market Data and Block Explorer. Jax was born Jaclyn Cole Miskanic in Atlantic Beach, New York on May 5, 1996. Her mother was a schoolteacher and her father was a firefighter; after the September 11, 2001 terrorist attack on the World Trade Center, Jax's dad worked at the recovery site, where he sustained lung injuries.

Let’s dive right in and multiply two big matrices.

We added that block_until_ready because JAX uses asynchronous execution by default.

JAX NumPy functions work on regular NumPy arrays.

That’s slower because it has to transfer data to the GPU every time. You can ensure that an NDArray is backed by device memory using device_put.

The output of device_put still acts like an NDArray, but it only copies values back to the CPU when they’re needed for printing, plotting, saving to disk, branching, etc. The behavior of device_put is equivalent to the function jit(lambdax:x), but it’s faster.

If you have a GPU (or TPU!) these calls run on the accelerator and have the potential to be much faster than on CPU.

JAX is much more than just a GPU-backed NumPy. It also comes with a few program transformations that are useful when writing numerical code. For now, there’s three main ones:

  • jit, for speeding up your code

  • grad, for taking derivatives

  • vmap, for automatic vectorization or batching.

Let’s go over these, one-by-one. We’ll also end up composing these in interesting ways.

Using jit to speed up functions¶

JAX runs transparently on the GPU (or CPU, if you don’t have one, and TPU coming soon!). However, in the above example, JAX is dispatching kernels to the GPU one operation at a time. If we have a sequence of operations, we can use the @jit decorator to compile multiple operations together using XLA. Let’s try that.

We can speed it up with @jit, which will jit-compile the first time selu is called and will be cached thereafter.

Taking derivatives with grad

In addition to evaluating numerical functions, we also want to transform them. One transformation is automatic differentiation. In JAX, just like in Autograd, you can compute gradients with the grad function.

Let’s verify with finite differences that our result is correct.

Taking derivatives is as easy as calling grad. grad and jit compose and can be mixed arbitrarily. In the above example we jitted sum_logistic and then took its derivative. We can go further:

For more advanced autodiff, you can use jax.vjp for reverse-mode vector-Jacobian products and jax.jvp for forward-mode Jacobian-vector products. The two can be composed arbitrarily with one another, and with other JAX transformations. Here’s one way to compose them to make a function that efficiently computes full Hessian matrices:

Auto-vectorization with vmap

JAX has one more transformation in its API that you might find useful: vmap, the vectorizing map. It has the familiar semantics of mapping a function along array axes, but instead of keeping the loop on the outside, it pushes the loop down into a function’s primitive operations for better performance. When composed with jit, it can be just as fast as adding the batch dimensions by hand.

We’re going to work with a simple example, and promote matrix-vector products into matrix-matrix products using vmap. Although this is easy to do by hand in this specific case, the same technique can apply to more complicated functions.

Given a function such as apply_matrix, we can loop over a batch dimension in Python, but usually the performance of doing so is poor.

We know how to batch this operation manually. In this case, jnp.dot handles extra batch dimensions transparently.

However, suppose we had a more complicated function without batching support. We can use vmap to add batching support automatically.

Of course, vmap can be arbitrarily composed with jit, grad, and any other JAX transformation.

Jax For Mac Pro

This is just a taste of what JAX can do. We’re really excited to see what you do with it!