Operators¶
CurvatureOperator
¶
Bases: ABC
Symmetric matrix-free linear operator over a flat parameter vector.
Source code in hessian_eigenthings/operators/base.py
size
abstractmethod
property
¶
Number of elements in the operator's input/output vector.
device
abstractmethod
property
¶
Device on which matvec produces its output.
dtype
abstractmethod
property
¶
Dtype of the output of matvec.
matvec(v)
abstractmethod
¶
Compute M @ v where M is this operator. v is a flat 1-D tensor of length self.size.
rmatvec(v)
¶
LambdaOperator
¶
Bases: CurvatureOperator
Wrap a callable as a CurvatureOperator. Useful for tests and ad-hoc operators.
Source code in hessian_eigenthings/operators/base.py
HessianOperator
¶
Bases: CurvatureOperator
Hessian of loss_fn(model, batch) averaged over batches in dataloader.
Two HVP methods are supported via method=:
-
"autograd"(default): exact double-backward viatorch.autograd.gradwithcreate_graph=True. Numerically exact (to rounding); ideal for single-device analysis up to ~7B parameters. -
"finite_difference": central-difference(∇L(θ+εv) − ∇L(θ−εv)) / 2εper Granziol & Juarev 2026. Two normal forward+backward passes per HVP, no second-backward graph anywhere — works with FSDP/HSDP/TP without any special handling. Trade-off: O(ε²) truncation bias plus precision-dependent roundoff (~1e-5 fp32, ~1e-2 bf16). Suitable for spectral analysis at scale.
Source code in hessian_eigenthings/operators/hessian.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
GGNOperator
¶
Bases: CurvatureOperator
Generalized Gauss-Newton matrix G = J^T H_loss J.
For convex per-sample losses (cross-entropy + softmax, MSE) H_loss is PSD so
G is PSD by construction. For cross-entropy + softmax classification, G
equals the Fisher information matrix.
The two-function API (forward_fn returns the model output, loss_of_output_fn
converts that output + batch into a scalar loss) lets us compute J v, the
loss-Hessian-vector product H_loss · (Jv), and J^T · (H_loss · Jv) without
coupling to the loss internals.
Two implementations of the matvec are available via loss_hvp=:
-
"analytical"(default): finite-difference JVP + analytical loss-Hessian-vec product (read fromloss_of_output_fn.hvp, which must be present) + a single normal backward to applyJ^T. Memory footprint matches one normal training step. Required for LM-scale use; see the OOM diagnostic inscripts/repro_ggn_oom.py. -
"autograd": the originaltorch.func.jvp+ autograd double-backward +torch.func.vjppath. Numerically exact and supports any loss, but memory scales badly with output size — for cross-entropy heads with large vocab thecreate_graph=Truestep alone can dominate. Kept as a fallback for losses without an analytical.hvp.
Source code in hessian_eigenthings/operators/ggn.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
EmpiricalFisherOperator
¶
Bases: CurvatureOperator
Empirical Fisher F = (1/N) Σ_i g_i g_i^T where g_i = ∂loss_i/∂θ are per-sample grads.
Empirical Fisher uses the true labels in the loss (unlike the MC Fisher which samples labels from the model's predictive distribution), and is therefore a biased estimator of the actual Fisher information. Conflating the two is the classic GGN-vs-Fisher-vs-empirical-Fisher pitfall — see Martens 2014.
Per-sample gradients are computed in one pass via torch.func.vmap(grad(...)),
so the cost is one forward+backward per batch, not per sample.
The per_sample_loss_fn(model, sample) -> Tensor takes a single (un-batched)
sample. The sample_dim argument tells the operator which axis to vmap over
when receiving a batch from the dataloader.
Source code in hessian_eigenthings/operators/fisher.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
DDPHessianOperator
¶
Bases: HessianOperator
HessianOperator that all-reduces the HVP across torch.distributed ranks.
The model passed in may already be wrapped with
torch.nn.parallel.DistributedDataParallel; we read params from it directly.
Each rank should be receiving its own shard of the dataset (typical pattern: a
torch.utils.data.distributed.DistributedSampler).