Skip to main content

LowRankGGN

LowRankGGN(rank=static(), oversample=10, fisher='exact', mc_samples=None)

Low-rank GGN over all fitted blocks jointly, from a Nyström sketch.

It keeps the top rank eigenpairs of the GGN \(\bar{C} = \frac{1}{W} \sum_n w_n J_n^\top H_n J_n\), by Tropp et al. 2017, Alg. 3.

Each batch adds \(Y = \sum_n w_n J_n^\top H_n J_n \Omega\) for a fixed \(P \times k\) test matrix \(\Omega\) with orthonormal columns, for \(P\) fitted parameters and \(k\) = rank + oversample. \(\Omega\) comes from the fit’s seed (FitContext.seed, drawn from the fit’s generator), so it is the same on every batch and every rank that passes the same generator, and the sketches add. A batch costs \(k\) JVPs (a VJP of a VJP each when the probe found no jvp) and \(k\) VJPs; the statistic holds \(P k\) entries. \(\Omega\) is kept in a process-wide cache of the last four \((\text{seed}, P, k)\), as float64 \(P \times k\) matrices that outlive the fit.

finalize divides \(Y\) by \(W\) and shifts it to \(Y + \nu \Omega\), with \(\nu = \sqrt{P} \, \epsilon \lVert Y \rVert_2\) for the machine epsilon \(\epsilon\), before its Cholesky and SVD. It clamps eigenvalues that round-off pushes below zero, so a rank-deficient \(\bar{C}\) stays finite, also in float32 and over many examples. Its null directions get eigenvalues of about zero.

The result is a Joint[LowRankPlusDiag] with a zero diagonal; a prior makes it invertible. The sketch needs a positive semi-definite \(\bar{C}\), so \(H_n\) is always the Fisher (the closed form, or the MC Fisher); the observed Hessian can be indefinite and is not offered. Gradients through finalize (D8) need distinct sketch singular values, so they can be NaN when \(\bar{C}\) has rank below \(k\). Models that only support plain autograd (checkpointing) need DiagGGN(fisher="mc") instead.

Parameters

Name Type Description Default
rank int Number of eigenpairs kept, at least 1. static()
oversample int Extra sketch columns, at least 0. 10
fisher Literal['exact', 'mc'] "exact" takes the likelihood’s closed-form output Hessian, "mc" the Monte Carlo Fisher. The empirical Fisher is not offered here. 'exact'
mc_samples int | None Draws for the MC Fisher, at least 1; only with fisher="mc" (1 when None). None

Raises

Name Type Description
ValueError rank is below 1, oversample is below 0, fisher is not "exact" or "mc", or mc_samples is given without fisher="mc" or below 1.

Attributes

Name Description
requires Needs vjp, an output Hessian, and jvp or vjp_twice.

Methods

Name Description
finalize The top rank eigenpairs of \(\bar{C}\), in a Joint[LowRankPlusDiag].
init A zero \(P \times k\) sketch over all blocks.
template A zero Joint[LowRankPlusDiag] of width rank.
update Add \(\sum_n w_n J_n^\top H_n J_n \Omega\) over the batch.

finalize

LowRankGGN.finalize(stat, /)

The top rank eigenpairs of \(\bar{C}\), in a Joint[LowRankPlusDiag].

The diagonal is zero. A zero sketch gives the first rank columns of \(\Omega\) with zero eigenvalues.

Parameters

Name Type Description Default
stat _JointStat The statistic over all the data, after all_reduce. required

Returns

Name Type Description
Joint[core.LowRankPlusDiag] The per-example average \(\bar{C} = \sum_n w_n C_n / \sum_n w_n\). The posterior multiplies it by scale.

init

LowRankGGN.init(params, ctx, /)

A zero \(P \times k\) sketch over all blocks.

Parameters

Name Type Description Default
params Params The params the curvature is taken at. required
ctx FitContext The fit’s context. required

Returns

Name Type Description
_JointStat A statistic \(e\) with \(e + s = s\) for every statistic \(s\) of this fit.

Raises

Name Type Description
ValueError rank + oversample exceeds the number of fitted parameters \(P\).
StructureError \(H_n\) is the dense MC Fisher and the probe’s event size exceeds MAX_DENSE_EVENT.

template

LowRankGGN.template(params, ctx, /)

A zero Joint[LowRankPlusDiag] of width rank.

Parameters

Name Type Description Default
params Params The params the curvature is taken at. required
ctx FitContext The fit’s context. required

Returns

Name Type Description
Joint[core.LowRankPlusDiag] A zero operator with the operator types, blocks, static fields, leaf shapes and dtypes that finalize returns.

update

LowRankGGN.update(stat, params, batch, generator, ctx, /)

Add \(\sum_n w_n J_n^\top H_n J_n \Omega\) over the batch.

Parameters

Name Type Description Default
stat _JointStat The statistic so far. required
params Params The params the curvature is taken at. required
batch Batch The batch to add. required
generator torch.Generator The batch’s generator, for sampled methods. required
ctx FitContext The fit’s context. required

Returns

Name Type Description
_JointStat stat with the batch added. Updating with batches \(a\), then \(b\), equals adding the statistics of \(a\) and of \(b\).

Raises

Name Type Description
ValueError batch.weights has a negative entry.
StructureError The likelihood refuses the outputs, or its mask is not bool or not shaped like the leading axes of \(f\), or \(H_n\) is the dense MC Fisher and the event size exceeds MAX_DENSE_EVENT (checked here when the probe did not record it).