# hessiankit

Hessian approximations for PyTorch models. Given a model, a batch, and the output factors of the loss Hessian, hessiankit computes the exact diagonal of the generalized Gauss-Newton (GGN) matrix and the Kronecker factors of KFAC, in both its expand and reduce forms. It supports masks and per-example weights. The model runs forward once with module hooks, and the gradients come from plain autograd. It depends on `torch` alone.

## Laplace first

We built hessiankit for the Laplace approximation. So whenever there is more than one way to do something, we pick the one that serves Laplace over the one that serves optimization or geometry research. We approximate the Hessian once, at the MAP estimate. Each call returns the exact sum over one batch, with the total weight of the examples that counted, and adding up the batches gives the sum over the whole dataset. It’s deterministic, and in float64 if you ask for it. The output-factor helpers give the GGN and the Fisher, which are positive semidefinite, so a posterior can invert them; an indefinite Hessian has to come through `hk.from_dense`. Scaling and inverting is left to the posterior.

## Install

``` bash
pip install hessiankit
```

## Next

- The [Quickstart](quickstart/basics.llms.md) walks through the main cases.
- [Why](why.llms.md) explains the choices behind the library.
- The [Reference](reference/index.llms.md) documents every public name.
- For the Laplace approximation itself, see [laplace**kit**](https://laplacekit.org).
