liveBook · Manning

Structured control flow

As explained in Deep Learning with JAX

Control-flow constructs in JAX that can be compiled from traces, often using lower-level lax APIs rather than ordinary Python runtime branching.

What happens when a JAX loop or branch depends on an input value? Ordinary Python control flow cannot be traced reliably. Structured control flow replaces it with jax.lax primitives that JAX can compile and differentiate. section 5.3.3 section 3.6

Use the lower-level API when:

  • A small set of concrete values is acceptable; mark arguments static, understanding that new values trigger recompilation. section 5.5

A traceable loop and branch

f5 = lambda x: jax.lax.fori_loop(0, x, lambda i, v: v + i, 0) relu = lambda x: jax.lax.cond(x > 0, lambda x: x, lambda x: 0.0, x) Both functions accept input-dependent values while remaining compatible with make_jaxpr() and jit().

How Deep Learning with JAX covers it

How deep it goes
Descends from tracing errors to fori_loop and cond implementations, Jaxpr output, static arguments, and recompilation tradeoffs.
Examples
Uses an input-bounded summation loop and a ReLU-style conditional, each rewritten with a jax.lax primitive.
What you can do after
Apply it via jax.lax to write input-dependent loops and branches that tracing, JIT compilation, and differentiation can process.
How it connects
Connects JAX tracing with Jaxpr, JIT compilation, static arguments, differentiation, and XLA.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with JAX. The section links point to the parts of the book it draws on.