Skip to content

Problems: P8, maximum likelihood with autodiff, and three companions (#81) - #82

Merged
mmcky merged 5 commits into
mainfrom
2026-11-21-p8-mle
Nov 21, 2026
Merged

mmcky merged 5 commits into
mainfrom
2026-11-21-p8-mle

Conversation

@mmcky

@mmcky mmcky commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Phase 2's fourth and last problem type, for #5: P8, maximum likelihood with autodiff, by Newton–Raphson on the Poisson regression of QuantEcon's mle lectures, as designed in #81 and approved on 2026-11-20.

What it adds

  • problems/mle.py: the lectures' Poisson regression, fitted by Newton–Raphson in NumPy, Numba and JAX.
    • From β = 0.1 in every coordinate, the lectures' start, each iteration takes β ← β − H⁻¹G by a linear solve, until no coordinate changes by more than the exercise's 1e-5, or the lectures' 100 iterations.
    • NumPy and Numba use the hand-written G = X′(y − μ) and H = −X′ diag(μ) X that lecture-python's mle had before it moved to JAX. Numba computes μ and y − μ in one prange pass and G and H through BLAS. JAX takes jax.grad of the lectures' log-likelihood and jax.jacfwd of that, as both lectures do now, inside lax.while_loop.
    • The data are drawn once by NumPy from a fixed seed, and shared. Two sweeps: obs (10,000 to 10 million observations, with the exercise's 3 parameters) and params (3 to 1,000 parameters, with 100,000 observations), in both precisions.
    • Each cell records its last step's largest change as last_step, a measure of its own. verify fails a cell that reached 100 iterations or whose estimate isn't finite, since a NaN stops the loop as if it had converged.
  • problems/mle_hand.py: a companion, JAX only. It runs mle's JAX loop with the hand-written G and H, so mle against mle_hand measures what autodiff costs JAX.
  • problems/mle_onepass.py and problems/mle_blocked.py: two more companions, Numba only, which read X once an iteration in one prange pass. mle_onepass adds each observation's terms to G and H, in blocks of 4,096 observations, and runs obs and params to 300 parameters. mle_blocked has each block of observations, 256 rows in float64 and 1,024 in float32, form its own μ, G and H with BLAS while it is in cache, and sums them in 64 fixed runs of blocks. It runs both sweeps. With mle's Numba, which forms G and H through BLAS over all of X, the three show how to structure the pass as the parameters grow.
  • Two harness changes, as P8: maximum likelihood with autodiff, by Newton–Raphson on the mle lectures' Poisson regression #81 proposed:
    • run_suite.py: a problem may list, as backends, the only backends it runs on: mle_hand JAX's, mle_onepass and mle_blocked Numba's.
    • summarise.py: a problem may name a reference problem, whose NumPy rows its answers and iterations are checked against, and whose iteration cap it shares. Each companion's is mle.
  • problems/spec.py: P8's four entries, after P7, with a memory estimate of 5 arrays of X's shape. XLA's temporaries measured up to 3, with X and y as arguments another 1.33.
  • problems/README.md: P8's row and two notes, one on autodiff and one on the three Numba forms, P8 in the lists of problems that solve to the lecture's tolerance and are checked at fixed positions, and NumPy's float32 P8 cells under "NumPy, SciPy and float32": float32 products with a float64 k × k solve.
The derivatives The loop
NumPy By hand, with NumPy's matrix products A Python loop
Numba By hand: μ and y − μ in one prange pass, and G and H with @, which Numba hands to BLAS An njit loop
Numba, mle_onepass By hand: μ, G and H in one prange pass, one observation at a time An njit loop
Numba, mle_blocked By hand: μ, G and H in one prange pass, by BLAS a block of observations at a time An njit loop
JAX, mle jax.grad and jax.jacfwd of the lectures' log-likelihood lax.while_loop
JAX, mle_hand By hand, in jax.numpy lax.while_loop

Autodiff's extra product. Forward-mode autodiff forms the Hessian through a product of X with a k × k identity, as much work as the Hessian's own product. It doesn't depend on β, so on the CPU, inside lax.while_loop, XLA computes it once per solve. #81 has the check. Whether XLA does the same on a GPU is to be checked on the first GPU run.

Departures from #81

Each was Matt's answer to a question from the independent review, on 2026-11-20.

  • mle_onepass and mle_blocked are new (Q22 to Q24). The review found that a loop accumulating G and H in Numba's pass was much faster with few parameters, and then that one with BLAS inside each block was faster with more. Matt kept mle's Numba column as P8: maximum likelihood with autodiff, by Newton–Raphson on the mle lectures' Poisson regression #81 has it (Q20), and asked for both forms' numbers across sizes on every machine, to show which structure pays where.
  • obs gains 3 million observations (Q22), between 1 million and 10 million, for every stack.
  • P8's float32 check is a relative 1e-5, not the suite's 1e-3 (Q21). The check divides by the largest value, the intercept, −2.5 in obs and −2 in params, where most slopes are far smaller: the median at 300 parameters is 0.03. At 1e-3, a cell that stopped two iterations early would pass, at 3e-5 to 6e-4, and so would TF32's rounding of G and H, at about 3e-5. Float32 cells differed from NumPy's float64 by 3.3e-7 at most.

The independent review

A fresh subagent reviewed faaef81 against #81 and the lectures, then each follow-up commit, three rounds in all. It found no correctness bug. Its findings, and what became of each, are in the comment below. Three of them led to Matt's questions Q20 to Q24, and to the departures above.

The organiser's review

The organiser's reviewer found 8e72df9 merge-ready, with one should-fix and four nits, all taken in bbc0ba2:

  1. mle_blocked added its partials on one thread. With one k × k partial for each block of 256 rows, newton added 391 of them serially every iteration at 100,000 observations, about 0.84 s of a 7.2 s solve at 1,000 parameters. The partials also took 3.1 GB. Now each of 64 fixed runs of blocks sums into its own. That took 0.32 s against 0.37 s at 300 parameters, and 6.6–6.9 s against 7.2 s at 1,000, in fresh processes. The partials now take 0.5 GB, and parallel answers stay serial ones.
  2. The float32 check's reason now gives the intercepts and slopes as they are: −2.5 or −2, against a median slope of 0.03 at 300 parameters.
  3. The README says every companion, not only mle_hand, is checked against mle's NumPy rows.
  4. The lecture is lecture-jax's mle_jax, with lecture-python's mle, in mle.py, spec.py and the README.
  5. The README says mle_onepass forms only H's lower triangle, half the H work of the BLAS forms.

Checked on qe-mini1, CPU backends only

These were development runs, not measurements, and their rows aren't committed.

  • The whole suite's --quick, 112 cells on numpy, numba, numba-parallel and jax-cpu, at 8e72df9: every cell ran. summarise.py's only notes were NumPy's float32 iteration counts for P3 and P5, which differ from float64's by design. The review also found summarise.py's output over every committed result identical before and after the change.
  • After the organiser's review, at bbc0ba2: every mle_onepass and mle_blocked cell again, 92 in all, ok, and P8's 36 quick cells.
  • Every P8 cell, 212 in all, none skipped for memory in a dry run: 96 of mle on the four CPU backends, 24 of mle_hand on jax-cpu, and 44 of mle_onepass and 48 of mle_blocked on the two Numba backends, in both precisions, from 22:02 to 23:31 UTC on 2026-11-20. Every cell ran, and every answer was within the checks: a relative 1e-8 of NumPy's float64 in float64, and 1e-5 in float32.
  • Iterations: every stack took NumPy's count in every cell, except from 3 million observations in float32. There Newton's first steps, far from the estimate, depend on rounding in sums of millions of float32 terms. At 3 million, NumPy, Numba and JAX took 18 iterations and the one-pass forms 17, where float64 took 17; at 10 million, NumPy and Numba took 17, JAX 19 and the one-pass forms 16, where float64 took 16. The steps differ from the first iterations on; once converged, the rounding noise is 3e-8 or less, well below the tolerance. The estimates agreed to 3.3e-7. The README says so.
  • Some times, warm, in seconds:
Cell NumPy Numba, parallel mle_onepass, parallel mle_blocked, parallel JAX, autodiff JAX, by hand
obs, 10 million, float64 1.83 1.27 0.200 0.222 1.20 1.03
obs, 10 million, float32 1.49 1.11 0.155 0.183 1.18 1.08
params, 30, float64 0.0162 0.0122 0.0094 0.0059 0.0155 0.0159
params, 300, float64 0.404 0.380 1.07 0.312 0.498 0.459
params, 1,000, float64 5.77 5.66 — 6.97 10.2 6.58
params, 1,000, float32 1.69 1.69 — 2.16 4.24 3.33

Each Numba form is the fastest somewhere: mle_onepass with 3 parameters, mle_blocked from 30 to 300, and BLAS over all of X at 1,000. Against mle_hand, autodiff cost JAX 6 to 19% in obs. In params it ran from 8% faster to 54% slower, the most at 1,000 parameters: 54% in float64 and 27% in float32.

jax-gpu hasn't run, since qe-mini1 has no NVIDIA card. It first runs with Phase 2's measured runs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG

mmcky and others added 4 commits November 21, 2026 09:13
- mle.py: the Poisson regression of QuantEcon's mle lectures, fitted by
  Newton-Raphson from beta = 0.1 to the exercise's 1e-5 or 100 iterations,
  with a linear solve. NumPy and Numba use the hand-written G and H that
  lecture-python's mle had; JAX takes jax.grad and jax.jacfwd of the
  lectures' log-likelihood, inside lax.while_loop. Sweeps: obs (10,000 to
  10 million observations, 3 parameters) and params (3 to 1,000
  parameters, 100,000 observations), in both precisions. Each cell records
  last_step, and verify fails one at the cap or with a non-finite estimate
- mle_hand.py: mle's JAX loop with the hand-written G and H, JAX only, so
  mle against mle_hand measures what autodiff costs JAX
- run_suite.py: a problem may list the only backends it runs on
- summarise.py: a problem may name a reference problem, whose NumPy rows
  its answers and iterations are checked against and whose cap it shares
- spec.py: P8's two entries; README: P8's row and note

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG
- mle_hand's G is (y - mu) @ X: X.T @ (y - mu) made XLA's CPU backend copy
  X transposed every iteration, so the hand-written loop was the slower
- mle_onepass: Numba with mu, G and H in one prange pass, each block of
  4,096 observations into its own, on the obs sweep and params to 300, to
  show where BLAS overtakes it (Matt, Q22 and Q23)
- obs gains 3 million observations, between 1 and 10 million (Q22)
- P8's float32 check is 1e-5: the largest value is the intercept, about 2,
  where the slopes are as small as 0.06 (Q21)
- README: the hoisting is shown on the CPU, TF32 cards untested, the
  one-pass loop's numbers, the float32 iteration counts from 3 million;
  summarise.py's docstring names reference

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG
- mle_blocked: Numba with one prange pass in which each block of
  observations forms its own mu, G and H with BLAS while it is in cache,
  256 rows in float64 and 1,024 in float32, on both sweeps (Matt, Q24).
  mle_onepass and mle_blocked share their Newton loop over block sums
- README: P8's three Numba forms in a note of their own, with what each was
  fastest at on the M6; the old G slowed mle_hand in params only; the
  largest float32 difference is 3.3e-7; mle_onepass's docstring says what
  it shows

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG
- mle_blocked's memory estimate adds each block's G and H: at 1,000
  parameters in float64 the blocks' H take 3.1 GB, and the cell peaked at
  5.6 GiB against mle's estimate of 3.7
- README: at 1,000 parameters the M6's matrix units limit mle_blocked, so
  12 threads were no faster than 2; OpenBLAS inside prange to be checked on
  the first Linux run; which TF32 rounding P8's float32 check flags

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG
@mmcky

mmcky commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

An independent review of faaef81, and of each follow-up commit to 8e72df9. Copilot couldn't review this PR. A fresh reviewer read the diff against #81 and the lectures, without my reasoning, and ran its own checks on qe-mini1's CPU, with scripts kept outside the repository. It found no correctness bug in any of three rounds. Every cell it ran agreed with NumPy's float64. The harness changes left summarise.py's output over every committed result identical, and a dry run added only P8's cells.

First round, faaef81:

  1. mle_hand's G made XLA copy X transposed every iteration. Written as X.T @ (y − μ), it made the hand-written loop the slower one in params. Fixed in 7a2aac3 as (y − μ) @ X, which XLA's CPU backend reads in place, as it does jax.grad's G.
  2. A one-pass Numba loop was much faster with few parameters. Matt kept mle's Numba as P8: maximum likelihood with autodiff, by Newton–Raphson on the mle lectures' Poisson regression #81 has it (Q20). The one-pass loop became mle_onepass (Q22, Q23), and obs gained 3 million observations.
  3. TF32 cards might cap every float32 cell. A NumPy emulation of TF32 rounding left Newton's step above the tolerance. This can't be tested here, and the README says so.
  4. The hoisting was shown on the CPU only. The README says so.
  5. The float32 check was too loose for P8, since the intercept sets its scale. Matt chose 1e-5 (Q21).
  6. Nits: summarise.py's docstring now names reference. Left as they are: run_suite.py's name column was already narrower than several problem names, and the companions' rows carry problem_type P8, as mle's do, which nothing keys on.

Second round, 7a2aac3:

  • numba_onepass held up. Its block reduction and symmetric fill matched NumPy's Newton step to 1.6e-14, at every edge size tried: n from 1, and sizes that aren't a multiple of a block. Parallel results were bitwise equal to serial ones, and there was no false sharing.
  • What loses to BLAS from 100 parameters is adding one observation at a time, not reading X once. A form with BLAS inside each block beat BLAS over all of X up to 300 parameters. Matt asked for that form too, as mle_blocked (Q24), in 765044a.
  • Also fixed: the README said the old G slowed mle_hand, which held in params only; and the largest float32 difference is 3.3e-7, not 1.3e-7.

Third round, 765044a:

  • The refactor left mle_onepass bitwise unchanged in 28 cells, and numba_blocked was right at every edge size.
  • Fixed in 8e72df9:
    • mle_blocked's memory estimate now adds each block's G and H: at 1,000 parameters in float64 its cell peaked at 5.6 GiB, against 3.7 GiB estimated.
    • The README explains its 1,000-parameter result: the M6's matrix units are the limit, so 12 threads were no faster than 2.
    • The README says which TF32 rounding the float32 check flags.
  • To check on the first Linux run: whether OpenBLAS's own threads, called inside prange, oversubscribe the cores.

- mle_blocked sums its blocks' G and H in 64 fixed runs, each into its
  own, where one partial a block left hundreds of k x k sums to add on one
  thread every iteration: at 300 parameters 0.32 s against 0.37, and the
  partials take 0.5 GB at 1,000 parameters rather than 3.1. Parallel
  answers stay serial ones. The memory estimate follows
- spec.py: the float32 check's reason gives the intercepts and slopes as
  they are; the lecture is lecture-jax's mle_jax, and lecture-python's mle
- README: every companion is checked against mle's NumPy rows;
  mle_onepass forms only H's lower triangle; the chunked sum's numbers

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMZbJB4j8pmPEYyJ8SG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant