Skip to content

Float32 fidelity: which float64 lectures need float64, and can the heaviest step drop to float32? #11

Open

Description

@mmcky

Run the 13 float64 lectures in lecture-jax in float32 and record whether each still reaches its answer, then try a mixed-precision version of aiyagari_jax's greedy step. The aim is to know where QuantEcon's JAX code needs float64, and where it is a precaution.

Why

  • The lectures don't say what float64 protects. 13 of the 29 lectures in lecture-jax turn it on, each with the same sentence, "to increase the precision". None says what goes wrong without it.
  • Lectures run as used: aiyagari_jax and ifp_egm_jax in float32 and float64 #3 found that float32 fails aiyagari_jax in two ways, and neither is a slightly worse answer.
    • Two of its 50 solves stall at an error of about 2.5e-4, against the lecture's tolerance of 1e-4, and run to the 10,000-iteration cap. That makes the lecture about 12× slower as used.
    • On the TITAN RTX at a 5,000-point grid, its values went NaN in all three runs, from a bicgstab breakdown (QuantEcon/project-benchmarks-jax#5). The loop's test error > tol is false for NaN, so it stopped after two iterations with a policy wrong almost everywhere, and raised no error. Both CPUs stayed finite.
    • When float32 did converge, equilibrium capital matched float64 to 2e-5.
  • ifp_egm_jax converges in float32, to within 1.6e-4 of the float64 policy.
  • So float32 fails by not finishing, or by failing silently, and the failure can depend on the device. Two lectures don't say which of the other eleven share that.
  • Whole lectures, as used: every JAX lecture on an Apple CPU, a Turing GPU and an x86 CPU #4 runs these lectures in float32 for time. This issue asks whether the answer survives.
  • The step that float64 makes expensive may not need it. In dense discrete choice the costly step recomputes rewards and takes the maximum over choices: 26.8× slower in float64 than in float32 on the TITAN RTX (Phase 0 and Phase 1 results, and two diagnoses #6). That step only has to pick the best choice. The value update, the convergence test, policy evaluation and the equilibrium search could stay in float64.

Part 1: the 13 lectures in float32

Lecture Solver and tolerance, as written
aiyagari_jax Policy iteration, 1e-4; equilibrium capital by bisection, xtol 1e-4
arellano_jax Joint iteration on the repay and default value functions, 1e-8
hopenhayn Value function iteration, 1e-6; the entry price by bisection, 1e-4
ifp_egm_jax Endogenous grid method, 1e-5
inventory_ssd Value function iteration, 1e-6
jax_intro_jax None; it turns float64 on to show the difference
job_search Value function iteration, 1e-4
markov_asset_jax A linear solve for the price-dividend ratio, after a spectral-radius check
mle_jax Newton–Raphson, 1e-3 and 1e-5
newtons_method Newton's method, 1e-5; float64 is turned on in exercise 1's solution, for tolerances of 1e-7 and 1e-15
opt_invest Value function and optimistic policy iteration, 1e-5
opt_savings_1 Value function iteration, 1e-5
opt_savings_2 Value function and optimistic policy iteration, 1e-5

Also the two float32 lectures that solve to a tolerance, overborrowing (1e-5) and lucas_model_jax (1e-6): whether they meet it, or stop at their iteration cap.

Method:

  • lectures/run_lecture.py with --dtype float32 and --dtype float64, at the lecture-jax commit Whole lectures, as used: every JAX lecture on an Apple CPU, a Turing GPU and an x86 CPU #4 used. Each of the 13 has exactly one jax_enable_x64 line, as --dtype float32 requires.
  • --record each lecture's answer: the equilibrium values, prices, estimates or policies it reports. Choosing the expressions lecture by lecture is part of the work.
  • For each solve: whether it met its tolerance or stopped at its cap, and how many iterations it took. Where a solver doesn't return these, read them from its printed output.
  • Check every recorded value for NaN and Inf. Don't rely on a loop's exit: a NaN error ends while error > tol as if it had converged.
  • One run per configuration, and three where results vary between runs.

Platforms:

Device Why
M6 CPU, qe-mini1, with its agents paused Seconds per lecture; the CPU answer
TITAN RTX, qe-wharton1-gpu, card 0b:00.0 The silent NaN appeared only on the GPU
T4, RunsOn g4dn.2xlarge The lectures' CI device, if it is cheap to add to #4's job

Result: one table, lecture by device, giving whether float32 converges (yes, stops at its cap, NaN, or raises), the largest gap from float64 in the recorded answers, and whether that gap is within the lecture's own tolerance.

Part 2: a mixed-precision greedy step for aiyagari_jax

  • The variant: with float64 on, get_greedy casts to float32, computes rewards and the argmax over next-period assets, and returns the policy. The value function, get_value's bicgstab solve, the convergence test and the equilibrium bisection stay in float64.
  • Then one float64 greedy step at the converged solution, counting the policy points that change. That catches choices decided by a near-tie.
  • At the grids Lectures run as used: aiyagari_jax and ifp_egm_jax in float32 and float64 #3 used, 200 to 10,000 asset points, on the TITAN RTX and the M6.
  • Record: time per policy-iteration loop, against pure float64 and pure float32; the policy points that differ from float64, and by how many grid steps; the gap in equilibrium capital; iterations to converge.
  • If it is ready in time, add it to Precision and platform: float64 against float32 on CPU, CUDA and Apple Silicon #1's Phase 2 runs. A Blackwell workstation card does float64 at 1:64 of float32, so that is where the float32 step would matter most.

What the results would change

If Then
A float64 lecture reaches its answer in float32, within its tolerance, on every device Float64 is a precaution there. The lecture can say so, or say what it guards against
A lecture stalls, goes NaN, or moves its answer past its tolerance in float32 It needs float64 as written, and the lecture can say why
The mixed step gives float64's answer at a fraction of float64's cost Research-scale discrete-choice models can run their heavy step at a card's float32 rate. That changes which card suits them, which project-compute weighs
It doesn't Dense discrete choice needs float64 throughout, and a card's float64 rate decides

Deliverables

Related

🤖 Generated with Claude Code

Activity

  1. mmcky commented on Oct 26, 2026

    @mmcky
    ContributorAuthor

    Part 2 has moved (2026-10-26). The mixed-precision greedy step is now the first experiment of a new QuantEcon project, project-econ-on-ai-hardware, which asks whether economic models can be restructured to run at the speed of GPUs whose float32 is far faster than their float64, and still give float64's answers. That project's repository is private, so this comment records what changed.

    The experiment extends Part 2 in four ways:

    • a float64 check within a few grid points of each float32 choice, on every iteration, rather than one float64 greedy step at the end;
    • the continuation value centred for each state before the cast to float32, which doesn't change which choice is best;
    • consumption computed in float64 before the cast, for choices near the borrowing limit;
    • asset grids up to 60k on the TITAN RTX, beyond Part 2's 10k.

    It runs from the project's own repository. If the approach proves itself, it comes back here as a benchmark to re-run on each new card.

    Part 1, the 13 lectures in float32, stays in this issue.

  2. mmcky commented on Nov 1, 2026

    @mmcky
    ContributorAuthor

    Part 1: the 13 float64 lectures in float32, from what they print (2026-11-01). The float32 pass (#16) ran each lecture three times in each precision on the TITAN RTX, the Threadripper beside it, qe-mini1's M6 and an M5 Pro. Seven checks (#18) then swapped one ingredient at a time between the precisions (the random sample, the arithmetic, or the tolerance) to find out why each changed lecture changes. The times and what each lecture prints are in lectures/whole/README.md, and the checks' output in lectures/whole/checks/float32.txt.

    Only arellano_jax needs float64 for its answer.

    Lecture Does float32 reach float64's answer? float64 is
    arellano_jax No. The borrowing policy differs at 4,222 of 12,801 states, by up to 8 grid steps, and the default region at 34 Needed
    aiyagari_jax Yes: equilibrium capital within 0.03%. But it takes 7.5× to 12× as long, because two of its 50 solves run to their 10,000-loop cap (#3) Needed for time
    opt_invest, opt_savings_2 Yes: float64's policy at all but 1 and 70 of 15,000 states, one grid step away. Policy iteration runs to its 250-loop cap A precaution
    hopenhayn, mle_jax, markov_asset_jax Yes, on the same random sample (below) A precaution
    ifp_egm_jax Yes: within 1.6e-4 of float64's policy (#3). Its Numba comparison takes 2.5× to 3× as long, because Numba's np.interp is slower on float32 arrays A precaution
    job_search, opt_savings_1, inventory_ssd, newtons_method Yes A precaution
    jax_intro_jax It turns float64 on to show the difference The lesson
    • Three of the failures come from the random sample, not the arithmetic. jax.random draws a different sample in float32 from the same key. That alone moves hopenhayn's equilibrium price by 12%, sends mle_jax's Newton–Raphson to NaN, and fails markov_asset_jax's stability check. On float64's sample, float32 arithmetic gives float64's answer in all three. On float32's sample, float64 does no better: mle_jax runs to its 100-iteration cap, and markov_asset_jax's 15-step estimate comes out at 1.026 against an exact 0.994. Across seeds in float64 (Whole lectures: float64 sweeps over seeds for four float32 checks #20), hopenhayn's price has a standard deviation of 3.1% at its 200 draws, and markov_asset_jax's check fails from 5 of 20 start vectors, while mle_jax converges from all 20 seeds tried: float32's sample is an unusual one for it.
    • mle_jax's NaN is silent. The loop's test, error > tol, is false for NaN, so it stops after two iterations and prints an estimate of NaN without an error: the failure mode this issue describes for aiyagari_jax on the card.
    • arellano_jax uses no random numbers. In float32 it stops after 361 iterations, against 399, because its iterates stop changing at float32's resolution, not because it is near float64's solution. float64's solution is identical at tolerances from 1e-5 to 1e-10.
    • float32 is no speed-up. The TITAN RTX gains at most 1.21× on any of these lectures, and over the set float32 takes 3.3× as long on the card.

    Still open in Part 1:

    • a --record pass over each lecture's answers, as the method above asks: this used the cells' printed output and seven checks;
    • the card's float32 answers. The checks ran on a CPU, and on the TITAN RTX arellano_jax stops after 342 iterations, against 361 on the CPUs;
    • overborrowing and lucas_model_jax.

    Filed on lecture-jax: QuantEcon/lecture-jax#361 (arellano_jax could say what float64 protects), QuantEcon/lecture-jax#362 (hopenhayn's Monte Carlo error), QuantEcon/lecture-jax#363 (mle_jax's factorial overflows past 20!, so its printed log-likelihoods are wrong) and QuantEcon/lecture-jax#364 (markov_asset_jax's 15-step check).

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