Skip to content

Problem types: NumPy, Numba and JAX on CPU and GPU, to guide the choice of stack #5

Open

Description

@mmcky

A suite of the problem types QuantEcon's lectures and research use, each written idiomatically in NumPy, Numba and JAX, and timed as used on CPUs and GPUs across problem sizes. The aim is data-driven guidance on which stack to use for which kind of problem, at what size, and on what hardware. This is the experiment for meta#264.

Why

  • meta#264 asks for exactly this: crossover points between backends and devices, by problem type and size, to replace rules of thumb.
  • Precision and platform: float64 against float32 on CPU, CUDA and Apple Silicon #1 measures JAX only, by kernel type. Its precision matrix (results under results/) shows how differently kernel types behave on a GPU. On a TITAN RTX, float64 costs 1.3× on a latency-bound recursion, about 4× on a memory-bound step and 20× to 27× on compute or transcendental work. It doesn't say whether Numba on a CPU would beat JAX for the same problem.
  • The lecture runs (Lectures run as used: aiyagari_jax and ifp_egm_jax in float32 and float64 #3) found that how a problem is written matters as much as the hardware. A vectorised Bellman step and a looped one, or a stored reward array and a recomputed one, land in different regimes.

Problem types

Each type gets a NumPy version as the baseline, a Numba version (CPU, parallel=True where it fits) and a JAX version (CPU and GPU), each written the way a careful author in that stack would write it.

Problem type QuantEcon pattern Shape
P1 Value function iteration on a dense grid Savings and discrete-choice models Vectorised max over a choice grid; loops in Numba
P2 Policy iteration Howard improvement with a linear solve Dense or iterative solves
P3 Endogenous grid method Income fluctuation problems Interpolation
P4 Monte Carlo simulation Many paths of an AR(1) or Markov process Parallel across paths, sequential in time
P5 Markov chains Stationary distributions, simulation Sparse and dense linear algebra
P6 Root finding and optimisation per state Newton or bisection over a grid Many small independent solves
P7 Sequential recursions Kalman filter, lax.scan Latency-bound
P8 Gradient-based estimation Maximum likelihood with autodiff Where JAX's autodiff should matter

Dimensions

  • Problem size, swept to find the crossovers.
  • Precision: float64 throughout, plus float32 where the problem tolerates it.
  • Device: CPU and GPU, for JAX. Numba runs on CPU only; numba.cuda is out of scope unless a problem type calls for it.
  • Dask (in meta#264) is left out, unless a problem type needs data larger than memory or distributed scheduling.

Method

#1's house standard:

  • one fresh process per measurement, with compile time counted, and cold and warm times kept apart;
  • the result dtype asserted;
  • every implementation's answer checked against the NumPy baseline.

The harness extends precision/, with one module per problem type and one function per stack.

Platforms

As in #1: qe-mini1's M6 and qe-wharton1-gpu's Threadripper and TITAN RTX first. Then qe-wharton2's M5 Pro once it is onboarded, and the RunsOn T4 once RunsOn is configured for this repository.

Phases

  1. P1, P4, P3 and P7 first. They span the regimes Precision and platform: float64 against float32 on CPU, CUDA and Apple Silicon #1 found, and they shake out the harness across three stacks.
  2. Then P2, P5, P6 and P8.

Output

🤖 Generated with Claude Code

Activity

  1. mmcky commented on Nov 1, 2026

    @mmcky
    ContributorAuthor

    Phase 1's harness is designed (2026-11-01), and built in #24. These are the decisions, as agreed with @mmcky, that refine the plan above. The detail is in problems/README.md.

    Decision Choice
    Where A new problems/ folder beside precision/ and lectures/. It reuses precision/'s environment record and GPU telemetry. Results go to problems/results/<host>/<date>.jsonl, apart from results/, which the dashboard reads as the precision matrix
    Problems P1, P3, P4 and P7, each with a lecture's model and parameters: opt_savings_1, ifp_egm_jax, wealth_dynamics_jax, and precision/'s Kalman filter
    What a cell times The whole solve, to the lecture's tolerance, as used. Iterations are recorded, so a time per iteration follows, and float32's extra iterations show rather than hide
    Code Each stack written as a careful author in it would write it, not copied from the lecture: for example, P1 stores its rewards and forms the expectation with one matrix product an iteration, where opt_savings_1 uses a 4-D broadcast. lectures/ already measures the lectures as written
    Backends numpy, numba, numba-parallel, jax-cpu and jax-gpu. Both Numba backends compile the same source, so a small problem's parallel overhead shows
    P7's sweep The work in each step, not the series' length: 1 to 10,000 filters batched with vmap at 10 states, and 2 to 200 states for one filter. Every GPU measured pays about 90 to 160 µs a Kalman step, against about 1.4 µs on the M6, so this gives the "how much work a step needs before a GPU wins" rule of thumb
    Precision float64 throughout, and float32 for P3, P4 and P7. P1 stays in float64: its tolerance of 1e-5 on values near −50 is within a few units of float32's roundoff
    Checks Each answer against NumPy's in float64 at the same size: to a relative 1e-8 in float64 and 1e-3 in float32 for P1, P3 and P7, and for P4's shares of households above seven wealth levels, within five binomial standard errors. The result dtype is asserted, which catches Numba promoting float32 to float64
    Budget About 3 hours a machine. Sizes are capped from a pilot on the M6, and a backend that times out skips its larger sizes

    Platforms: the M6, the Threadripper and the TITAN RTX first, as above, once qe-wharton1-gpu is free of QuantEcon/project-econ-on-ai-hardware's runs, and the M6 run's plan is agreed. Then the M5 Pro and the T4.

  2. mmcky commented on Nov 7, 2026

    @mmcky
    ContributorAuthor

    Phase 1's M5 Pro step is done (2026-11-06, on qe-wharton2): the four CPU stacks at every size in both precisions, 200 cells, all ok, in problems/results/qe-wharton2/2026-11-06.jsonl (#48, 133da9b), with the run's notes.

    • Where the work spreads across cores, it's 1.45× to 1.90× as fast as the M6, warm, in float64, at the largest size of P1 in JAX and of P4 and P7's batch in parallel Numba.
    • Where most of the work falls on one core, the M6 is a little faster. P3 runs at 0.89× to 0.99× the M6's speed and a single filter at 0.90× to 0.93×, and serial Numba takes up to 12% longer.
    • Phase 1's guidance holds on it. Each cell's fastest stack is the M6's in 45 of 50 cells warm and 49 of 50 cold. The TITAN RTX overtakes it at the same sizes as the M6, or one measured size later in two places, P1 cold and P4 warm.
  3. mmcky commented on Nov 21, 2026

    @mmcky
    ContributorAuthor

    Phase 2's four problem types have had their first measured runs, every size in both precisions (P2 in float64 only), on 2026-11-21. They ran on qe-mini1's M6, qe-wharton2's M5 Pro, and qe-wharton1-gpu's Threadripper 2990WX and TITAN RTX: #83, merged as 5e838bf, with each host's run notes under problems/results/<host>/2026-11-21/. Every cell ran but one, and every answer agreed with NumPy's float64 answer but two, both float32 cells on qe-wharton1-gpu. The finding is QuantEcon/project-benchmarks-jax's findings/2026-11-21-phase-2-problem-types.md. In short:

    • Warm, the TITAN wins at each sweep's largest size, by 1.23× to 3.6× over the faster Mac in float64 (level with the M5 Pro on P6's shocks) and 1.15× to 20× in float32. The exception is P8 with 1,000 parameters in float64, where the M5 Pro is 1.17× to 2.2× faster: that work is mostly float64 matrix products, and Turing runs float64 at 1/32 of its float32 rate.
    • Cold, the Macs win almost everywhere. The card's start takes 0.9 s to 1.4 s at each sweep's smallest size, and on P8 in float64 XLA's autotuner adds up to 22 s of compiling. With --xla_gpu_autotune_level=0 that compile takes 0.3 s, but the warm solve is 3× to 3.7× slower.
    • On P8, the shape of the Numba pass matters more than the stack. One pass over the observations solves 10 million in float64 in 0.089 s on the M5 Pro, against 0.87 s on the card and 1.3 s for the same model with BLAS products over all the data. Blocks with BLAS products win from 30 to 300 parameters on the Macs. On the Threadripper, BLAS inside prange oversubscribes the cores: holding OpenBLAS to one thread makes the blocked form up to 11× faster in float64.
    • On the card, autodiff costs 1.7× to 1.95× the hand-written derivatives from 100 parameters up in float64, since XLA leaves autodiff's extra product inside the Newton loop there. On the CPUs it costs 0.99× to 1.15×, and P8: maximum likelihood with autodiff, by Newton–Raphson on the mle lectures' Poisson regression #81's development on the M6 found that the CPU backend computes it once.
    • float32 isn't free. The card's float32 dense solve at 5,000 assets is 14× faster than its float64 one, but 1.4e-2 off, outside P5's tolerance. On P6's grid, NumPy is 1.7× slower in float32 than in float64 on both Macs, since it interpolates in float64 on every call, and its cell at 50,000 on the Threadripper passed 1,800 s.

    Where these leave the guide, problem type by problem type, is in the finding's "For a JAX for Economists guide".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions