A Comprehensive Guide to Accelerated Python Computing
Chapter 1: Why JAX — The Landscape of Python Numerical Computing
- The Problem with Existing Tools
- Enter JAX: A New Paradigm
- What Makes JAX Different
- Who Should Use JAX (and Who Should Not)
- How to Read This Book
Chapter 2: Getting Started — Installation, Setup, and Your First Program
- Python and Environment Prerequisites
- Installing JAX for Different Hardware
- Verifying Your Installation
- Your First JAX Array Operations
- Understanding the JAX REPL Experience
Chapter 3: JAX Arrays — The Foundation of Everything
- Creating and Inspecting Arrays
- Core Array Operations
- Broadcasting and Reshaping
- Indexing and Slicing
- JAX vs NumPy Arrays: Key Differences
- Memory Layout and Device Placement
Chapter 4: JIT Compilation — Making Python Code Fly
- What Is JIT Compilation
- Using @jit in Practice
- Static vs Dynamic Arguments
- Tracing and the XLA Backend
- Common JIT Pitfalls and Solutions
- Performance Measurement and Profiling
Chapter 5: Automatic Differentiation — The Engine of Modern ML
- How Automatic Differentiation Works
- Computing Gradients with jax.grad
- Higher-Order Derivatives
- Vector-Jacobian and Jacobian Products
- Value-and-Gradient Computation
- Common Autodiff Patterns in ML
Chapter 6: Functional Programming — Embracing JAX Philosophy
- Why Functional Programming Matters
- Immutability and No Side Effects
- Pure Functions and Determinism
- Closures and Function Composition
- Thinking Functionally About Data
- Anti-Patterns to Avoid
Chapter 7: Vectorization with vmap — Writing Loops Without Loops
- The Batch Processing Problem
- How vmap Works
- In-Axis and Out-Axis Specification
- Nested vmapping
- Combining vmap with jit and grad
- When Not to Use vmap
Chapter 8: Parallelism — pmap, pjit, and Distributed Computing
- Understanding Devices in JAX
- Device Arrays and Placement
- Using pmap for Single-Host Parallelism
- Introduction to pjit
- Shardings and Mesh Definition
- Multi-Host Distributed Training
Chapter 9: Control Flow — Loops and Conditionals That Compile
- Why Python Control Flow Fails Under JIT
- Conditional Execution with lax.cond
- While Loops with lax.while_loop
- For Loops with lax.fori_loop
- Scan for Sequential Operations
- Choosing the Right Primitive
Chapter 10: PyTrees — Structuring Complex Data
- What Is a PyTree
- Registering Custom Tree Types
- Flattening and Unflattening
- Operating on PyTrees
- PyTrees in Machine Learning Models
- Advanced PyTree Patterns
Chapter 11: Randomness — Reproducible Stochasticity
- Why JAX Handles Randomness Differently
- The PRNG Key System
- Splitting Keys for Parallelism
- fold_in for Labeling Keys
- Seeding and Reproducibility
- Common Randomness Pitfalls
Chapter 12: Custom Gradients — Beyond Automatic Differentiation
- When You Need Custom Gradients
- Using jax.custom_jvp
- Using jax.custom_vjp
- Gradient Checking Techniques
- Practical Examples from Research
Chapter 13: Neural Networks with Flax and Equinox
- Building Blocks of Neural Networks in JAX
- Introduction to Flax
- Introduction to Equinox
- Flax vs Equinox: Choosing a Library
- Common Architectures and Patterns
Chapter 14: Optimization — Training at Scale
- Optimizers in JAX Ecosystem
- Learning Rate Scheduling
- Gradient Clipping and Normalization
- Mixed Precision Training
- Checkpointing and Experiment Tracking
- Scaling to Large Models
Chapter 15: Advanced Topics — Scientific Computing, Probabilistic Programming, and RL
- JAX for Scientific Computing
- Introduction to Probabilistic Programming with NumPyro
- Reinforcement Learning with JAX
- Differentiable Physics Simulations
- Case Study: Training a Large Language Model
Chapter 16: Performance Engineering and Deployment
- Profiling JAX Programs
- Debugging Techniques and Tools
- Memory Optimization Strategies
- Interoperability with NumPy and Other Libraries
- Model Export and Serving
- Production Deployment Considerations