Repository navigation
Problems: P8, maximum likelihood with autodiff, and three companions (#81) - #82
Merged
Merged
Conversation
- 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
Contributor
Author
|
An independent review of First round,
Second round,
Third round,
|
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
mlelectures, 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.mlehad before it moved to JAX. Numba computes μ and y − μ in oneprangepass and G and H through BLAS. JAX takesjax.gradof the lectures' log-likelihood andjax.jacfwdof that, as both lectures do now, insidelax.while_loop.obs(10,000 to 10 million observations, with the exercise's 3 parameters) andparams(3 to 1,000 parameters, with 100,000 observations), in both precisions.last_step, a measure of its own.verifyfails 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 runsmle's JAX loop with the hand-written G and H, somleagainstmle_handmeasures what autodiff costs JAX.problems/mle_onepass.pyandproblems/mle_blocked.py: two more companions, Numba only, which read X once an iteration in oneprangepass.mle_onepassadds each observation's terms to G and H, in blocks of 4,096 observations, and runsobsandparamsto 300 parameters.mle_blockedhas 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. Withmle'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.run_suite.py: a problem may list, asbackends, the only backends it runs on:mle_handJAX's,mle_onepassandmle_blockedNumba's.summarise.py: a problem may name areferenceproblem, whose NumPy rows its answers and iterations are checked against, and whose iteration cap it shares. Each companion's ismle.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.prangepass, and G and H with@, which Numba hands to BLASnjitloopmle_onepassprangepass, one observation at a timenjitloopmle_blockedprangepass, by BLAS a block of observations at a timenjitloopmlejax.gradandjax.jacfwdof the lectures' log-likelihoodlax.while_loopmle_handjax.numpylax.while_loopAutodiff'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_onepassandmle_blockedare 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 keptmle'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.obsgains 3 million observations (Q22), between 1 million and 10 million, for every stack.obsand −2 inparams, 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
faaef81against #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
8e72df9merge-ready, with one should-fix and four nits, all taken inbbc0ba2:mle_blockedadded its partials on one thread. With one k × k partial for each block of 256 rows,newtonadded 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.mle_hand, is checked againstmle's NumPy rows.mle_jax, with lecture-python'smle, inmle.py,spec.pyand the README.mle_onepassforms 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.
--quick, 112 cells onnumpy,numba,numba-parallelandjax-cpu, at8e72df9: 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 foundsummarise.py's output over every committed result identical before and after the change.bbc0ba2: everymle_onepassandmle_blockedcell again, 92 in all, ok, and P8's 36 quick cells.mleon the four CPU backends, 24 ofmle_handonjax-cpu, and 44 ofmle_onepassand 48 ofmle_blockedon 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.mle_onepass, parallelmle_blocked, parallelobs, 10 million, float64obs, 10 million, float32params, 30, float64params, 300, float64params, 1,000, float64params, 1,000, float32Each Numba form is the fastest somewhere:
mle_onepasswith 3 parameters,mle_blockedfrom 30 to 300, and BLAS over all of X at 1,000. Againstmle_hand, autodiff cost JAX 6 to 19% inobs. Inparamsit ran from 8% faster to 54% slower, the most at 1,000 parameters: 54% in float64 and 27% in float32.jax-gpuhasn'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