ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Getting Started with JAX
Chapter 1: Introduction to JAX
What is JAX?
JAX vs NumPy
Core Design Philosophy: Function Transformations
Installation and Setup
Working with JAX Arrays
Device Management: CPU, GPU, TPU
Practice: Basic Array Operations
Quiz for Chapter 1
Chapter 2: Accelerating Functions with JIT Compilation
The Need for Speed: Why Compile?
Introducing jax.jit
How JIT Works: Tracing and Compilation
Python Control Flow and jit
Static vs Traced Values
Common Challenges with jit
Hands-on Practical: Applying jit
Quiz for Chapter 2
Chapter 3: Automatic Differentiation with grad
Understanding Gradients
Introducing jax.grad
How Autodiff Works: Reverse Mode
Differentiating with Respect to Arguments
Higher-Order Derivatives (grad of grad)
Value and Gradient (jax.value_and_grad)
Differentiation and Control Flow
Limitations and Considerations
Hands-on Practical: Computing Gradients
Quiz for Chapter 3
Chapter 4: Automatic Vectorization with vmap
The Concept of Vectorization
Introducing jax.vmap
Mapping over Specific Arguments (in_axes, out_axes)
Handling Multiple Batched Arguments
Nesting vmap
Combining vmap with jit and grad
Performance Considerations for vmap
Hands-on Practical: Vectorizing Functions
Quiz for Chapter 4
Chapter 5: Parallelization Across Devices with pmap
Introduction to Data Parallelism (SPMD)
Introducing jax.pmap
Mapping Data to Devices (in_axes, out_axes)
Device Meshes and Axis Names
Collective Operations (lax.psum, lax.pmean, etc.)
Combining pmap with other Transformations
Debugging pmapped Functions
Hands-on Practical: Parallel Computation
Quiz for Chapter 5
Chapter 6: Managing State in JAX
Functional Purity and Side Effects
The Challenge of State in Functional Code
Pattern: Explicit State Passing
Using PyTrees for Structured State
Example: Stateful Counter
Example: Simple Optimizer State
Combining State Management with Transformations
Practice: Implementing Stateful Functions
Quiz for Chapter 6

Introducing jax.jit

JAX achieves significant performance gains by compiling your Python functions. The primary tool for this is the jax.jit transformation. Think of jit (Just-In-Time compilation) as a way to take a standard Python function operating on JAX arrays and convert it into a highly optimized, fused sequence of operations specific to your hardware (CPU, GPU, or TPU).

Applying jax.jit

There are two main ways to apply the jit transformation:

  1. As a decorator: This is often the most convenient method for functions you define. You simply add @jax.jit on the line before the function definition.
  2. As a higher-order function: You can call jit directly, passing your function as an argument. This returns a new, compiled version of your function.

Let's look at a simple example. Suppose we have a function that performs a few numerical operations:

import jax
import jax.numpy as jnp
import time

# A function with some numerical computations
def complex_computation(x, weight, bias):
  y = jnp.dot(x, weight) + bias
  z = jnp.tanh(y)
  return jnp.mean(z)

# Create some random data
key = jax.random.PRNGKey(0)
x_data = jax.random.normal(key, (1000, 500))
weight_data = jax.random.normal(key, (500, 200))
bias_data = jax.random.normal(key, (200,))

# --- Method 1: Using jit as a decorator ---
@jax.jit
def compiled_computation_decorator(x, weight, bias):
  y = jnp.dot(x, weight) + bias
  z = jnp.tanh(y)
  return jnp.mean(z)

# --- Method 2: Using jit as a function ---
compiled_computation_functional = jax.jit(complex_computation)

# --- Timing the execution ---

# Time the original Python function
# Run once to avoid any initial overhead unrelated to JAX
_ = complex_computation(x_data, weight_data, bias_data).block_until_ready()

start_time = time.time()
result_original = complex_computation(x_data, weight_data, bias_data).block_until_ready()
end_time = time.time()
print(f"Original function time: {end_time - start_time:.6f} seconds")

# Time the JIT-compiled function (decorator version)
# First call includes compilation time
start_time_compile = time.time()
result_compiled_decorator = compiled_computation_decorator(x_data, weight_data, bias_data).block_until_ready()
end_time_compile = time.time()
print(f"Compiled function (decorator) first call (incl. compile): {end_time_compile - start_time_compile:.6f} seconds")

# Second call uses the cached compiled code
start_time_cached = time.time()
result_compiled_decorator_cached = compiled_computation_decorator(x_data, weight_data, bias_data).block_until_ready()
end_time_cached = time.time()
print(f"Compiled function (decorator) second call (cached): {end_time_cached - start_time_cached:.6f} seconds")

# Verify results are the same (within floating point tolerances)
print(f"Results match: {jnp.allclose(result_original, result_compiled_decorator)}")

# Timing the functional version (should be similar after first compile)
_ = compiled_computation_functional(x_data, weight_data, bias_data).block_until_ready() # Compile
start_time_func = time.time()
result_compiled_functional = compiled_computation_functional(x_data, weight_data, bias_data).block_until_ready()
end_time_func = time.time()
print(f"Compiled function (functional) subsequent call: {end_time_func - start_time_func:.6f} seconds")

Important Note on Timing: Notice the use of .block_until_ready() after each function call we want to time. JAX uses asynchronous dispatch by default, meaning operations are queued but might not complete immediately. block_until_ready() ensures the computation finishes before we record the end time, giving us accurate measurements.

You'll observe a pattern:

  1. The original function takes a certain amount of time, executing operation by operation in Python.
  2. The first call to the jit-compiled function (compiled_computation_decorator or compiled_computation_functional) is often slower than the original. This is because JAX needs to perform an important step called tracing (which we'll discuss in the next section) and then compile the traced operations using XLA (Accelerated Linear Algebra compiler).
  3. Subsequent calls to the same compiled function with inputs of the same shape and type are significantly faster. JAX caches the compiled code and reuses it, avoiding the overhead of Python interpretation and benefiting from fused, optimized operations executed directly on the accelerator.

The difference between the decorator (@jax.jit) and functional (jax.jit(fn)) approaches is primarily stylistic. The decorator is often preferred for its readability when defining functions. The functional form is useful when you want to compile a function obtained from elsewhere (e.g., a library function, though many JAX library functions are already JIT-compiled internally where appropriate).

In essence, jax.jit provides a straightforward way to request compilation for your performance-sensitive numerical functions. By understanding when and how to apply it, you can get substantial speedups for your JAX code. The next section will explain the tracing mechanism that makes this possible.

  • JIT compilation and tracing, The JAX team, 2024 (The JAX team) - Official documentation explaining the usage, behavior, and underlying mechanisms of jax.jit, including tracing and asynchronous execution.
  • XLA: Accelerated Linear Algebra, OpenXLA Project, 2024 (OpenXLA Project) - Overview of XLA (Accelerated Linear Algebra), the domain-specific compiler that JAX uses to optimize numerical computations for various hardware backends.
  • Learning JAX, Rostislav T. Konopliov and Alexander B. Konopliov, 2023 (O'Reilly Media) - A practical guide to JAX that covers core concepts including jax.jit for performance optimization, tracing, and interaction with hardware accelerators.

© 2026 ApX Machine Learning