batches can be any iterable of (inputs, targets), a DataLoader say, and the result is the sum over all of them. total_weight is the number of examples that went in, \(W\), and the per-example average GGN of the last layer is approximated by \((G / W) \otimes (A / W)\).
We didn’t name any layer, nor say which Fisher or which KFAC form to use. hk.plan tells us what was picked, and why:
print(hk.plan(model, batches[0], likelihood))
hk.plan: kfac on output [32, 3], event axes (1,), 1 position per example
layer type T params A G mask
0 Linear 1 112 7 16 none
2 Linear 1 51 17 3 none
fisher: exact (3 backward passes per batch, at most EXACT_MAX_PASSES = 64)
form: reduce (the output has one position per example)
backward passes per batch: 3
memory: 4.7 KiB in float64
Both nn.Linear layers are in, and each row gives its positions per example T, its number of parameters, and the sizes of its two factors. With three classes, the exact Fisher takes three backward passes per batch, which is cheap, so we get it exactly. With a vocabulary of 50,000 tokens it wouldn’t be, and the plan would switch to Monte Carlo. The output has one position per example, so KFAC takes its reduce form. plan runs the model forward once and computes nothing else, so we can call it before an expensive run.
Every choice can be overridden:
result = hk.kfac( model, batches, likelihood, layers=["2"], fisher="mc", mc_samples=4)print(list(result.kron), result.uncovered)
['2'] {'0.weight': 'not in layers', '0.bias': 'not in layers'}
uncovered names each parameter we left out, with the reason. The defaults leave parameters out too, and list them the same way: a LayerNorm has no rule, say, and the base layer under a LoRA adapter is frozen.
A diagonal Laplace posterior
hk.diagonal takes the same arguments and gives the exact diagonal of the GGN. With a Gaussian prior of precision \(\tau\), the diagonal posterior precision is that sum plus \(\tau\), and the posterior variance is one over it:
diag = hk.diagonal(model, batches, likelihood)tau =1.0variance = { name: (d + tau).reciprocal() for name, d in diag.by_parameter().items()}print(variance["2.weight"][0])
For example \(n\) with output \(f_n\), let \(J_n = \partial f_n / \partial \theta\) and \(H_n\) the Hessian of the loss in \(f_n\). The GGN of the loss summed over the data is \(G = \sum_n J_n^\top H_n J_n\). hessiankit writes \(H_n = \sum_e v_{n,e} v_{n,e}^\top\) with output factors \(v_{n,e}\) and runs one backward pass per factor. There are as many factors as entries of \(f_n\) that interact, the classes here, so the exact \(H_n\) gets expensive for large outputs. Monte Carlo replaces it by \(g_n g_n^\top\) with \(g_n\) the gradient of the loss at a label sampled from the model, which takes one backward pass per sample and has expectation \(H_n\).
KFAC approximates the block of \(G\) of one layer by \(G_\ell \otimes A_\ell\), from the layer’s inputs and output gradients. When the output has several positions (a loss per token or pixel), the expand form treats the positions as examples; when it has one, the reduce form sums the layer’s positions first (Eschenhagen et al., 2023). With a prior \(\mathcal{N}(0, \tau^{-1} I)\), the Laplace posterior is \(\mathcal{N}(\theta_\text{MAP}, (G + \tau I)^{-1})\).
The defaults look only at the likelihood and the shape of the output. Layers with several positions per example (tokens) take position_mask=, and models that don’t return their output as a tensor take forward=; By hand shows the layer underneath, where we make every choice ourselves.