Skip to content

Newton-CG with Gauss–Newton Hessian actions (lowrankcg), and run_jvp for MiniRes - #208

Merged
KriFos1 merged 6 commits into
Python-Ensemble-Toolbox:mainfrom
KriFos1:feat/lowrankcg-newton-cg
Dec 3, 2026
Merged

KriFos1 merged 6 commits into
Python-Ensemble-Toolbox:mainfrom
KriFos1:feat/lowrankcg-newton-cg

Conversation

@KriFos1

@KriFos1 KriFos1 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Proposal

A Newton-CG sibling of lowrankgn, modelled on hIPPYlib's ReducedSpaceNewtonCG with its Gauss–Newton Hessian, and the simulator hook it needs. lowrankgn takes rows of the Jacobian and solves for the step in the space they span. lowrankcg takes 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_vjp behaves as before, run_jvp raises, and the tangent tests skip.

Entry points and public APIs

New:

  • pipt.LowRankCG, registered as ("lowrankcg", "laplace"); module pipt.update_schemes.lowrank_cg with LowRankCG and conjugate_gradients.
  • [dataassim.iteration] keys read by lowrankcg: gradient_tol (1e-6), cg_coarse_tol (0.5), cg_max_iter (100), cg_reorthogonalize (true), c_armijo (1e-4). It reads max_iter, data_misfit_tol, max_inner_iter, energy, max_rank and full_jacobian as lowrankgn does, and refuses lambda, lambda_min, lambda_factor.
  • Optional simulator hook run_jvp(state, directions), the transpose of run_vjp (documented in ensemble/protocols.py); implemented by simulator.minires.MiniRes.run_jvp on minires.tlm.tlm.
  • pipt.update_schemes.laplace_prior.apply_precision(blocks, dx).

Changed:

  • simulator.minires.MiniRes.run_vjp now 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, _commit and _give_up, which LowRankCG reuses. No change of behaviour.
  • benchmarks/pipt/minires: a Newton-CG row in METHODS; CountingMiniRes counts simulations at _simulate and adds forward sweeps, at one each like backward ones. The totals of the existing rows are unchanged.

What the scheme does

  • Gradient: one run_vjp sum plus the prior precision.
  • Step: preconditioned CG with the Eisenstat–Walker tolerance 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.
  • Globalization: halve the step until the Armijo condition holds, one simulation an attempt.
  • Limits: a state at a limit that the gradient pushes against is left out of the solve (projected Newton). Clipping a full Newton step, as hIPPYlib's line search does, stalled on a bounded test problem.
  • Stops on the gradient's norm in the metric of the prior covariance, or on the data misfit's change as lowrankgn.
  • The Laplace samples are inherited from lowrankgn unchanged.

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_jvp against central differences (also BHP control, well indices that follow the permeability, log_perm = false, Fortran ordering); the transpose identity with run_vjp to 1e-10, a fluid scalar included; one simulation per state; pickling; behaviour on a MiniRes without the tangent model.
  • test_statistical_consistency.py: lowrankcg is an expected deviation for the reason and by the amount lowrankgn is (the regularised prior fit they share).
  • Full suite on this branch: 919 passed, 6 xfailed, 1 xpassed with the MiniRes of Restore the tangent linear model (tlm_step, tlm), with BHP controls and shared tapes patnr/MiniRes#1; 910 passed, 9 skipped (the ones that need the tangent model), 6 xfailed, 1 xpassed with the released MiniRes 0.3.2 that CI installs. ruff check src tests is clean.

Benchmark

Official run on both cases (20 and 8 truths, 50 members), for the rows this PR touches.

Case Method Misfit logK RMSE Simulations
Coarse LowRank-GN, rank 12 10.9 ± 0.91 0.698 ± 0.021 185
Coarse Full-Jacobian GN 6.83 ± 0.42 0.693 ± 0.023 300
Coarse Newton-CG 11.1 ± 0.92 0.700 ± 0.026 151
Fine LowRank-GN, rank 12 11.7 ± 1.7 0.697 ± 0.027 186
Fine Full-Jacobian GN 8.35 ± 1.2 0.690 ± 0.020 300
Fine Newton-CG 13 ± 2.2 0.705 ± 0.022 149

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

  • On MiniRes the scheme needs a MiniRes with tlm.tlm, later than 0.3.2. uv.lock is not changed, so CI skips the tangent tests and the benchmark's Newton-CG smoke test.
  • With few data the full Jacobian is as cheap; the gain is for many data and few informed directions.
  • CG stores two state vectors per iteration when re-orthogonalizing.

KriFos1 and others added 5 commits December 3, 2026 10:10
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>
@KriFos1
KriFos1 requested a review from patnr December 3, 2026 09:51

@patnr patnr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@KriFos1
KriFos1 merged commit 2863a4f into Python-Ensemble-Toolbox:main Dec 3, 2026
4 checks passed
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.

2 participants