# Conventions

The formulas behind `hk.compute`, in one place. Every result is a sum over the examples of a batch; the library never divides by $W$.

## Notation

For a requested layer and example $n$ of a batch of $N$:

| Symbol | Meaning |
|----|----|
| $t$ | A position of the layer: a token, or an output pixel of a convolution. $T$ positions per example. |
| $x_{n,t}$ | The layer’s input at $t$: the input vector, or the unfolded patch for a convolution, with a 1 appended when the layer has a bias. |
| $d_{n,t,e}$ | The gradient at the layer’s output at $t$, after backpropagating output factor $e$. |
| $v_{n,e}$, $s_{n,e}$ | Output factors and their signs: $\sum_e s_{n,e} v_{n,e} v_{n,e}^\top = H_n$, the output Hessian of the loss. |
| $m_{n,t}$ | The layer’s position mask, 1 to keep and 0 to drop. All ones without masks. |
| $w_n$ | The example weight, not negative. All ones without weights. |
| $T_n$ | $\sum_t m_{n,t}$, the unmasked positions of example $n$. |

An example with $T_n = 0$ is counted with $w_n = 0$. The total weight of a layer is

$$W = \sum_n w_n ,$$

so it is $N$ without masks and weights.

## Exact diagonal

For each parameter of the layer,

$$\operatorname{diag} = \sum_n w_n \sum_e s_{n,e} \Big( \sum_t m_{n,t}\, d_{n,t,e}\, x_{n,t}^\top \Big)^2 ,$$

squared entry by entry, the bias part being the last column. This is the diagonal of $\sum_n w_n J_n^\top H_n J_n$ when the factors are exact and `grouping="full"`. The diagonal of the per-example average GGN is $\operatorname{diag} / W$.

## KFAC

KFAC takes factors with positive signs only.

Expand:

$$A = \sum_n \frac{w_n}{T_n} \sum_t m_{n,t}\, x_{n,t} x_{n,t}^\top , \qquad
G = \sum_n w_n \sum_t m_{n,t} \sum_e d_{n,t,e}\, d_{n,t,e}^\top .$$

Reduce, with $\bar x_n = \frac{1}{T_n} \sum_t m_{n,t}\, x_{n,t}$:

$$A = \sum_n w_n\, \bar x_n \bar x_n^\top , \qquad
G = \sum_n w_n \sum_e \Big( \sum_t m_{n,t}\, d_{n,t,e} \Big) \Big( \sum_t m_{n,t}\, d_{n,t,e} \Big)^\top .$$

The per-example average of the layer’s GGN block is approximated by

$$\frac{G}{W} \otimes \frac{A}{W} ,$$

for the weight and bias as one matrix `[D_out, D_in + 1]`, flattened row by row. With one position per example, expand and reduce agree.

For a grouped convolution, $x_{n,t}$ is the patch averaged over the groups, so $A$ has `C_in / groups * kh * kw` rows and columns (plus one for the bias), as in Curvlinops 3.0.1.

## Masks and weights

Two rules, which the tests check:

- A weight $k$ (an integer) gives the same sums as the example repeated $k$ times.
- A masked position contributes nothing: not to $A$, $G$ or the diagonal, not to $T_n$, and not to $W$. An example with no unmasked position does not count.

`masks[layer]` is the mask $m$ of that layer, `[N, T]`. `output_mask` is a boolean mask over the output’s positions with one axis per output axis: the output’s size on the batch and position axes, and 1 on every event axis (`[N, 1, H, W]` for logits `[N, C, H, W]`, `[N, T, 1]` for `[N, T, C]`, `[N, 1]` for `[N, C]`). Its positions are the axes after $N$ in row-major order, $P$ of them. Any other shape raises `FactorError`. When a position axis has size 1, as in `[N, C, 1]`, shapes can’t tell it from the event axis: a mask `[N, C, 1]` passes the check and is read as if the class axis held positions. So the mask must have size 1 on the event axis, `[N, 1, 1]` here.

The output mask

1.  zeroes the output factors at masked positions;
2.  is the position mask of each layer with $T = P$ and no entry in `masks`, taking the layer’s positions to be the output’s, in the same row-major order (true when the layer keeps the output’s layout, such as a 1x1 convolution head or a token-wise linear layer);
3.  drops the examples with no unmasked position, for each layer with $T = 1$ and no entry in `masks`;
4.  raises `FactorError` for any other layer without an entry in `masks`.

An entry in `masks` always wins over rules 2 to 4.

The front door (`hk.kfac`, `hk.diagonal`) builds `masks` itself, so it never hits rule 4. `position_mask=` gives the mask of each layer with as many positions as it has, and when the output has that many positions too, it masks the output as well. A layer left for rule 4, such as a downsampled convolution in a segmentation net with void pixels, gets an entry that keeps all its positions of every example with an unmasked output. Its $G$ and diagonal are still exact, since the factors are zero at void outputs; only its $A$ counts positions that void outputs depend on. `hk.plan` shows the mask of each layer (“examples with a kept output” for this case), and `compute` with `masks={...}` takes a finer one.

## Grouping of exact factors

With several output positions, `grouping="full"` gives one factor per position and column, and `grouping="columns"` gives one per column, summed over the positions. The diagonal needs `"full"`. KFAC with `"columns"` matches Curvlinops; with `"full"`, reduce’s $G$ differs when positions share an example. Expand gives the same $G$ either way when the positions don’t interact.

## Curvlinops 3.0.1

`KFACLinearOperator` with `reduction="sum"`, `num_data=N` and `separate_weight_and_bias=False`, on one batch of $N$ examples, without masks or weights. `_input_covariances` and `_gradient_covariances` are private attributes of Curvlinops 3.0.1, so the correspondence holds for that version only.

| hessiankit | Curvlinops 3.0.1 |
|----|----|
| `a` | $N \times$ `_input_covariances[name]` |
| `g` | `_gradient_covariances[name]` |
| `categorical`, `bernoulli` | `CrossEntropyLoss`, `BCEWithLogitsLoss` |
| `gaussian(sigma=1)`, exact | `MSELoss`, with `g` half of Curvlinops’ |
| `gaussian(sigma=1)`, empirical | `MSELoss`, with `g` a quarter of Curvlinops’ |
| `kind="exact"`, `grouping="columns"` | `fisher_type="type-2"` |
| `kind="empirical"` | `fisher_type="empirical"` |

`MSELoss` with `reduction="sum"` is $\sum (f - y)^2$, whose Hessian is $2I$, while a Gaussian with $\sigma = 1$ has $I$. Curvlinops unfolds convolution inputs with zero padding whatever the padding mode; hessiankit pads as the layer does.
