Repository navigation
Newton-CG with Gauss–Newton Hessian actions (lowrankcg), and run_jvp for MiniRes - #208
Conversation
The optional simulator hook run_jvp(state, directions) is the transpose of run_vjp: the data's first-order change along directions of the state, at one forward (tangent) sweep each. With run_vjp it applies a Gauss-Newton Hessian to a vector without a row of the Jacobian. MiniRes implements it on its tangent linear model, minires.tlm.tlm, through the well model, the well indices that follow the permeability and the fluid's scalars that run_vjp differentiates; the two agree to round-off. That needs a MiniRes later than 0.3.2. On an older one run_jvp raises and run_vjp is as it was. A scheme asks for many products at one state, so the wrapper keeps the simulation, the tapes and the scalars' slopes of the last state linearized about. The products of run_vjp are unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The forecast of a trial, the move of the centre to an accepted one and the record of an iteration that kept none become _try, _commit and _give_up, for a scheme that takes another step to share. No change of behaviour. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A sibling of lowrankgn, after hIPPYlib's ReducedSpaceNewtonCG: the same
objective, prior, limits and Laplace samples, with the step from conjugate
gradients on Hessian actions (run_jvp, then run_vjp, plus the prior
precision) instead of rows of the Jacobian. Registered as
("lowrankcg", "laplace") and exported as pipt.LowRankCG.
The prior covariance preconditions the solve, whose tolerance follows the
gradient (Eisenstat-Walker); its residuals are re-orthogonalized by default,
so that sweeps a simulator solves iteratively do not delay or stall it. The
step is halved until the Armijo condition holds. A state at a limit that the
gradient pushes against is left out of the solve: clipping a full Newton
step stalled on a bounded problem. The run stops on the gradient's norm, or
on the data misfit's change as lowrankgn does.
laplace_prior gains apply_precision. The consistency suite lists the scheme
with the deviation of lowrankgn, whose prior fit it shares.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
lowrankcg at its defaults, 4 iterations, with the rank-12 samples of the LowRank-GN row. The count of simulations is taken where a member is simulated and adds forward sweeps, at one each like backward ones; the totals of the existing rows are unchanged. Official run on both cases for the rows this branch touches. The two Gauss-Newton rows reproduced (the same simulations for every truth, every score to a relative 1e-10), so their published scores and the case metadata are kept and only the new row is added; the README says which rows come from which run. The smoke test skips the row on a MiniRes without the tangent linear model. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review of the tangent linear model upstream (patnr/MiniRes#1) dropped the helper `linearize_trajectory` as a one-line comprehension over `tlm.linearize`, which the wrapper now writes out. `tlm` and `adjoint` keep their `tapes` argument. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
patnr
left a comment
There was a problem hiding this comment.
Approving. My review covers the MiniRes side (src/simulator/minires.py, the tests in tests/test_minires_sim.py) and the benchmark, not the CG scheme itself. run_jvp mirrors _state_gradient faithfully (the Peaceman WI change acting as a BHP one included), and the central-difference and transpose checks are the right ones.
MiniRes 0.3.3 is released with tlm.tlm and tapes. So this PR could bump the pin to minires>=0.3.3 and relock, and then drop the compatibility code: the two hasattr(tlm, "tlm") branches, the tapes=None splat in _state_gradient, and the skip markers. Then CI runs the tangent tests and the Newton-CG smoke test, which it currently skips. Fine as a follow-up if you'd rather merge as is.
Possible follow-up: denser data. As the PR says, with 36 well data the full Jacobian is as cheap, and the benchmark bears that out: Newton-CG is level with rank-12 LowRank-GN and behind the full-Jacobian GN. The scheme's case is many data and few informed directions, and neither the benchmark nor the wrapper can pose that yet: DIFFERENTIABLE is well quantities only. A datatype that observes the state directly, e.g. a saturation (or pressure) map at a few survey times as a stand-in for 4D seismic, would be cheap to add: its seeds are just entries of dSS/dPP, with no well model to differentiate. A benchmark case with it would show where lowrankcg overtakes the row-wise schemes, which is the point of having it.
Proposal
A Newton-CG sibling of
lowrankgn, modelled on hIPPYlib'sReducedSpaceNewtonCGwith its Gauss–Newton Hessian, and the simulator hook it needs.lowrankgntakes rows of the Jacobian and solves for the step in the space they span.lowrankcgtakes no row while iterating: conjugate gradients, preconditioned by the prior covariance, solve the same system from Hessian actions, each a forward (tangent) sweep followed by a backward one. That is the route for data sets too large for the full Jacobian.The tangent sweep for MiniRes is proposed upstream in patnr/MiniRes#1. This PR works without it: with the released MiniRes 0.3.2
run_vjpbehaves as before,run_jvpraises, and the tangent tests skip.Entry points and public APIs
New:
pipt.LowRankCG, registered as("lowrankcg", "laplace"); modulepipt.update_schemes.lowrank_cgwithLowRankCGandconjugate_gradients.[dataassim.iteration]keys read bylowrankcg:gradient_tol(1e-6),cg_coarse_tol(0.5),cg_max_iter(100),cg_reorthogonalize(true),c_armijo(1e-4). It readsmax_iter,data_misfit_tol,max_inner_iter,energy,max_rankandfull_jacobianaslowrankgndoes, and refuseslambda,lambda_min,lambda_factor.run_jvp(state, directions), the transpose ofrun_vjp(documented inensemble/protocols.py); implemented bysimulator.minires.MiniRes.run_jvponminires.tlm.tlm.pipt.update_schemes.laplace_prior.apply_precision(blocks, dx).Changed:
simulator.minires.MiniRes.run_vjpnow keeps the simulation, the tapes and the scalars' slopes of the last state linearized about, so repeated products at one state cost their sweeps only. Its products are unchanged.__getstate__drops that cache, so the class still pickles.LowRankGN.update_step: the bookkeeping of a trial is extracted into_try,_commitand_give_up, whichLowRankCGreuses. No change of behaviour.benchmarks/pipt/minires: a Newton-CG row inMETHODS;CountingMiniRescounts simulations at_simulateand adds forward sweeps, at one each like backward ones. The totals of the existing rows are unchanged.What the scheme does
run_vjpsum plus the prior precision.min(cg_coarse_tol, sqrt(|g| / |g_0|)), residuals re-orthogonalized by default (as hIPPyMFEM does), so that inexact sweeps do not delay or stall it.lowrankgn.lowrankgnunchanged.Tests
tests/assimilation/test_lowrank_cg.py: against the analytic posterior of a linear-Gaussian problem (one exact step reaches the mean; the samples have its covariance); the Hessian action against the dense Gauss–Newton Hessian; the mode under limits against SciPy's L-BFGS-B to 1e-6; step halving; re-orthogonalization on a stiff system and on an inexact action; refusals.tests/test_minires_sim.py:run_jvpagainst central differences (also BHP control, well indices that follow the permeability,log_perm = false, Fortran ordering); the transpose identity withrun_vjpto 1e-10, a fluid scalar included; one simulation per state; pickling; behaviour on a MiniRes without the tangent model.test_statistical_consistency.py:lowrankcgis an expected deviation for the reason and by the amountlowrankgnis (the regularised prior fit they share).ruff check src testsis clean.Benchmark
Official run on both cases (20 and 8 truths, 50 members), for the rows this PR touches.
The two Gauss–Newton rows were rerun because this PR touches their code: the same number of simulations for every truth, and every score to a relative 1e-10. Their published scores and the case metadata are kept; only the Newton-CG row is added, and the benchmark README says which rows come from which run.
Limitations
tlm.tlm, later than 0.3.2.uv.lockis not changed, so CI skips the tangent tests and the benchmark's Newton-CG smoke test.