# testing.check_curvature

``` python
testing.check_curvature(
    curv,
    model,
    likelihood,
    params,
    batches,
    *,
    generator=None,
)
```

Check a curvature (`Curvature` or `Staged`) against the core contracts.

`curv` must satisfy `Curvature` or `Staged`, and `requires` must be a `Requirements`. Write $\mathrm{fold}(a)$ for the statistic after `init` and one `update` per batch of $a$, and $a \mathbin{+\!\!+} b$ for the batches of $a$ followed by those of $b$. On every pass (one for a `Curvature`):

- `init` returns a `Stat` with a tensor `total_weight`, and it is the identity: $\mathrm{init} + \mathrm{init} = \mathrm{init}$ and $\mathrm{init} + \mathrm{fold}(a) = \mathrm{fold}(a)$.
- Monoid law: $\mathrm{fold}(a \mathbin{+\!\!+} b) = \mathrm{fold}(a) + \mathrm{fold}(b)$, with $a$ the first half of `batches` (rounded down) and $b$ the rest. Each batch gets a generator with the same seed in both folds.
- `scaled(2)` keeps the statistic’s structure and, when `requires.additive_stat` is True (the statistic is a sum), equals $s + s$ for the statistic $s$.

On the result of the last pass:

- It is an `Operator`. A per-block operator (`HasBlocks`, such as `BlockDiag` or `Joint`) is keyed by the layout’s blocks in layout order, each with the block’s size; any other operator covers all params.
- Template: when `curv` has one (`HasTemplate`), it has the result’s structure: the same operator types, blocks and static fields, and leaves of the same shapes and dtypes.
- Weight scaling: multiplying every example weight by 3 leaves the result unchanged (`finalize` returns averages), compared on the diagonal and on two random vectors. Skipped when `requires.weights` is False.
- Dense checks, only for at most `DENSE_MAX_PARAMS` parameters, with $A$ the matrix `mv` builds column by column: `diagonal()` is $\operatorname{diag}(A)$, `mv` is linear ($\mathrm{mv}(v) = A v$ for a random $v$), and `add_isotropic(1)` has the log-determinant $\log\det(A + I)$ and the inverse diagonal $\operatorname{diag}\big((A + I)^{-1}\big)$. The identity is added because curvatures are often singular.

Every equality holds up to `RTOL`. Errors that `curv` itself raises propagate.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| curv | HessianApprox\[Any\] | The approximation under test. | *required* |
| model | nn.Module \| ModelFn | The model, as a module or a `ModelFn`. It must be float64. | *required* |
| likelihood | Likelihood | The likelihood the model is fitted with. | *required* |
| params | Params | Float64 parameter mapping. The layout is `Layout.by_module(params)`. An operator that is not `HasBlocks` covers all params in mapping order, so such a result needs params grouped by module. | *required* |
| batches | Sequence\[Batch\] | At least two batches. The capabilities come from `probe` on the first. | *required* |
| generator | torch.Generator \| None | Seeds the passes. One seed is drawn from it, or from torch’s global RNG when `None` (`lk.core.spawn_generator`). Each batch’s update generator is `lk.core.generator_for_batch` of its pass’s generator and the examples before it, so both folds of the monoid law see the same draws for a batch. | `None` |

## Raises

| Name | Type | Description |
|----|----|----|
|  | AssertionError | A law above fails, or `curv`, a pass, `requires` or the result is not of the type it must be. The message names the law and `curv`. |
|  | ValueError | `params` are not float64, there are fewer than two batches, or the result is not `HasBlocks` and the params are not grouped by module. Also raised by `probe` when the likelihood’s `log_prob` refuses the first batch’s targets. |
|  | CapabilityError | The model or the likelihood lacks what `requires` asks for, as in a fit. The capabilities are checked once, on `curv.requires`; each pass’s likelihood needs are checked the same way. |
|  | StructureError | `probe` refuses the model or the likelihood on the first batch (see `lk.core.probe`). |
