Exact SCG Curvature for Distributed GP Training

Purpose

This note records the mathematical redesign needed to make scaled conjugate gradient (SCG) fit the ScaLAPACK backend cleanly. It is an internal implementation note, written at the level of detail of a derivation.

The core point is simple:

  • The present SCG implementation estimates curvature by probing the model at \(\theta + \sigma p\).

  • That is mathematically unnecessary for the GP objective.

  • For a Gaussian process, the directional curvature \(p^\top H p\) can be computed exactly from the current Cholesky factor, triangular solves, and directional kernel derivatives.

  • Those are exactly the operations that the ScaLAPACK backend already handles well.

This note therefore defines the exact curvature oracle that should replace the finite-difference probe in SCG.

Current SCG Bottleneck

The finite-difference probe used to live in gpr/ml/SCG.inl. That file is gone; SCG mode calls rgmin through gpr/ml/RgminScg.inl. The probe computes

which is a finite-difference Hessian-vector estimate of \(p^\top H p\).

For the dense Eigen backend this is merely extra work. For ScaLAPACK it is structurally bad:

  • Every probe point requires a fresh distributed covariance assembly.

  • Every probe point requires a fresh Cholesky factorization.

  • Probe failures are optimizer artifacts, not model requirements.

  • The retry loop amplifies MPI synchronization and non-PD failure handling.

This is why the current optimizer path is a poor match for distributed linear algebra even after the backend bugs were removed.

GP Objective Along a Search Direction

Let the negative log marginal likelihood be

Fix an optimizer search direction \(p\) in hyperparameter space and define the one-dimensional path

Write the covariance matrix expansion along that path as

where

Define

The exact directional first derivative is

The exact directional second derivative is

This scalar is exactly the SCG curvature quantity \(p^\top H p\).

Equivalent Inverse-Covariance Form

For some derivations it is cleaner to work with the inverse covariance

Then the same objective becomes

Define the directional inverse-covariance derivatives

The exact directional derivatives can then be written as

The covariance and inverse-covariance forms are equivalent through

This representation matters because some optimizer discussions are easier in inverse form, while the implementation is still best written using Cholesky solves against \(C\).

Symbolic Verification

The derivation is checked offline in python/tests/test_sympy_scg_exact_curvature.py.

That test proves three things with SymPy:

  • the exact directional first derivative formula,

  • the exact directional second derivative formula,

  • the special case \(E = 0\) that reduces to the first-order-only kernel path.

  • the exact equivalence between the covariance and inverse-covariance forms.

The dedicated environment and task are:

  • environment: symbolic

  • task: pixi run -e symbolic derive-scg

This keeps the symbolic machinery entirely offline. No SymPy code belongs in the production runtime.

Why This Matches ScaLAPACK

The formula above can be evaluated entirely with the operations that the distributed backend already supports well.

Terms that are already available

  • \(\alpha = A y\) from two triangular solves with the Cholesky factor

  • \(\log \det C\) from the Cholesky diagonal

  • \(\operatorname{tr}(A D)\) from a solve-based trace

Terms needed for exact curvature

The new exact-curvature oracle needs:

  • \(D = dC[p]\)

  • \(E = d^2 C[p,p]\)

  • \(\operatorname{tr}(A D A D)\)

  • \(\operatorname{tr}(A E)\)

  • \(\alpha^\top D A D \alpha\)

  • \(\alpha^\top E \alpha\)

All of these can be written using the current factor \(L\):

For example:

  • solve \(C X = D\) with matrix right-hand side, then \(\operatorname{tr}(A D A D) = \operatorname{tr}(X D)\)

  • let \(v = D \alpha\), solve \(C z = v\), then \(\alpha^\top D A D \alpha = v^\top z\)

  • solve \(C X_E = E\), then \(\operatorname{tr}(A E) = \operatorname{tr}(X_E)\)

This curvature path avoids:

  • explicit inverse materialization in the optimizer hot path

  • probe-point factorizations

  • optimizer-generated non-PD excursions made only to estimate curvature

The first-derivative training gradient has a different cost balance: it needs \(\operatorname{tr}(A\,dC_i)\) for every hyperparameter \(i\) at the same factorization. The ScaLAPACK implementation therefore has two equivalent trace routes:

  • the reference route applies the cached Cholesky factor to each materialized distributed derivative matrix;

  • the fast route computes one temporary distributed lower-triangle inverse, contracts it directly with owned lower-triangle derivative entries, and releases the temporary inverse if the gradient call created it.

The fast route is algebraically the same trace because both \(A\) and \(dC_i\) are symmetric:

python/tests/test_sympy_scalapack_gradient_blocks.py proves this lower-triangle contraction with SymPy, proves the Cholesky-solve trace identity, and checks the distributed block/quadratic contractions used by the MPI gradient path. The C++ backend regression compares the temporary lower-triangle inverse contraction against the solve-based trace.

Required C++ Design

The runtime implementation should stay pure C++ and split into three layers.

1. Covariance directional derivatives

The kernel layer needs backend-independent directional APIs:

  • first directional derivative: \(D = dC[p]\)

  • second directional derivative: \(E = d^2 C[p,p]\)

This should be exposed as directional matrix builders rather than forcing the optimizer to assemble them from many parameterwise matrices.

For the current kernels:

  • ConstantCF: trivial in value space; in log-parameter space the first and second directional derivatives are both proportional to the constant kernel matrix.

  • SexpatCF: requires mixed second derivatives with respect to \(\log \sigma^2\) and the log-lengthscale parameters. This is the main implementation work.

  • LikGaussian: diagonal contribution; simple in both first and second directional form.

2. GP model exact-curvature hook

The GP model should expose something structurally like

struct DirectionalCurvatureData {
    double first_directional;
    double second_directional;
};

DirectionalCurvatureData evaluateDirectionalDerivatives(
    const Eigen::VectorXd& theta,
    const Eigen::VectorXd& direction,
    const EigenMatrix& x,
    const Eigen::VectorXd& x_ind,
    const Eigen::VectorXd& y);

The implementation must:

  • reuse the current Cholesky factorization at \(\theta\)

  • compute \(\alpha\)

  • build \(D\) and \(E\)

  • evaluate the exact scalar derivatives from solve-based formulas

3. SCG curvature oracle replacement

The SCG step, now the rgmin loop called from gpr/ml/RgminScg.inl, should replace the probe-point block

  • build \(x_+ = \theta + \sigma p\)

  • evaluate \(g(\theta + \sigma p)\)

  • form finite-difference \(\gamma\)

with

  • request exact directional curvature from the model

  • set \(\gamma = p^\top H p\) directly

Then the surrounding SCG trust-parameter logic can remain recognizable.

Why This Is Mathematically Equivalent

This is not a new optimizer. It is a new curvature oracle for the same second-order model.

The present SCG uses a finite-difference estimate of \(p^\top H p\). The proposed version computes the same scalar exactly from the current factorization and directional derivatives.

Therefore:

  • the local quadratic model is the same object,

  • the implementation is more numerically stable,

  • the distributed backend stops paying for probe-point factorizations.

Paper Integration Notes

The current manuscript draft in src/rg_main.org of the manuscript repository describes SCG as estimating curvature by finite differences. The relevant implementation discussion is around the lines that say:

  • SCG computes a search direction,

  • estimates curvature via a finite-difference Hessian-vector product,

  • retries when the Cholesky factorization fails.

If the exact-curvature redesign is implemented, the paper section should be updated to say:

  • the optimizer uses exact directional curvature of the GP negative log marginal likelihood,

  • the curvature is evaluated from the current Cholesky factor and directional kernel derivatives,

  • the distributed implementation therefore uses only factorization, solves, and trace reductions on the critical path.

That is a stronger and cleaner algorithmic statement than the current finite-difference description.

The manuscript performance section also records the first-derivative ScaLAPACK gradient trace optimization separately from exact curvature: matrix-free lower-triangle trace accumulation improves the release training step at np=4, =np=8, and np=16 on the bench_scalapack_scaling.sh fixture, while prediction and variance timings remain outside that path.

Verification Commands

  • pixi run -e symbolic pytest --noconftest python/tests/test_sympy_scg_exact_curvature.py -v

  • pixi run -e symbolic pytest --noconftest python/tests/test_sympy_scalapack_gradient_blocks.py -v

  • pixi run -e symbolic derive-nlml-hvp (the exact NLML Hessian-vector product; the meson suite runs the same file as test_sympy_nlml_hvp)

  • pixi run -e scalapack bash scripts/bench_scalapack_scaling.sh compare <baseline-ref>