C.W.K.
Stream
Lesson 05 of 08 · published

JAX Backend

~9 min · backend

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

The speed-and-scale backend

The JAX backend (JAX 0.4.20+) is frequently the fastest option for GPU and TPU training, because JAX compiles your computation through XLA into one fused, hardware-tuned kernel instead of dispatching op-by-op. It's the engine behind much of Google's large-scale training, and it carries three things into Keras that matter once your model gets serious:

  • JIT compilation via XLA for maximum throughput
  • Functional transforms — jax.vmap, jax.grad, jax.pmap — composable on your own math
  • The strongest support for keras.distribution (multi-GPU/TPU data and model parallelism)

The functional-purity tax

JAX's power comes from a constraint: transformed functions must be pure — no hidden mutable state. Keras model objects do hold state (weights, optimizer slots, metric tallies), so to run them inside a JIT/grad transform you pass that state in and get the updated state back, rather than mutating in place. That's the stateless_call pattern in the Code block. If you only ever call fit() you may never touch it; the moment you write a custom JAX training step, it becomes the core idiom.

Code

JAX stateless call — pass state in, get it back·python
os.environ["KERAS_BACKEND"] = "jax"
import keras

model = keras.Sequential([...])
model.compile(optimizer="adam", loss="mse")

# JAX stateless API for functional purity
variables = model.variables
outputs = model.stateless_call(variables, inputs)

External links

Exercise

With KERAS_BACKEND=jax, write a small keras.ops.matmul snippet inside a @jax.jit-decorated function. Time it with vs without jit on a 1000×1000 matmul.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.