predict_outputs
predict_outputs(
posterior,
inputs,
*,
strategy=None,
n_samples=None,
chunk_size=None,
generator=None,
)Linearized pushforward of a posterior, \(\mathcal{N}(f(x; \mu), J \Sigma J^\top)\).
With \(J\) the Jacobian of the outputs with respect to the parameters at the mean \(\mu\), the linearization \(f(x; \theta) \approx f(x; \mu) + J (\theta - \mu)\) maps \(\theta \sim \mathcal{N}(\mu, \Sigma)\) to this Gaussian over the outputs, per input.
n_samples="exact" is exact. With strategy="reverse-mode" it gives a PerInputCov and needs vjp and vmap. Its Jacobian is streamed in chunks of chunk_size output entries, and the inputs go in groups, so without autograd a chunk’s rows and their product with \(\Sigma\) hold at most \(2^{24}\) entries together, whatever the batch size, or \(2 \cdot \text{chunk} \cdot P\) when one input needs more (with autograd recording, the graph keeps more, and the covariance product may hold its own temporaries). Smaller chunks use less memory and take more VJPs. Each chunk’s rows are recomputed for every later chunk, so with \(E\) output entries the cost is \((E / \text{chunk} + 1) / 2\) times one Jacobian.
With strategy="forward-mode" it gives a FactorCov with factor \(J L\) ([N, *event, P], \(L L^\top = \Sigma\), from cov_sqrt_mv) and needs jvp and vmap. Here chunk_size counts parameter directions: one JVP on the whole batch per direction, chunk_size vmapped at a time, from a [chunk, P] block of the identity. The factor holds \(N E P\) entries in stat_dtype of the outputs. Each \(L e_p\) is cast to the model’s dtype before its JVP, so under bfloat16 the factor is a bfloat16 product, kept in float32 afterwards.
An int n_samples gives a SampleCov of that many draws \(J z_s\), with \(z_s\) the posterior’s weight noise (sample_centered, at least float32) centred and rescaled so the variance is unbiased. They are kept in at least float32. They are pushed one at a time, so no vmap: strategy="forward-mode" uses a JVP per draw (needs jvp), "reverse-mode" a VJP of a VJP (needs vjp and vjp_twice, which fused attention kernels lack).
strategy=None is resolve_strategy of the probe’s event size (caps.event_size), resolved again from the outputs’ event size if that differs. For exact outputs it is the cheaper path that fits (forward mode when \(P < E\)); models without jvp (attention) get the dense reverse-mode path when \(E \le\) MAX_DENSE_EVENT. n_samples=None is exact when the chosen strategy’s exact path fits (exact_fits), else 64 draws. n_samples="exact" picks the cheaper exact path the capabilities allow, whatever the budgets. Sampled outputs (an int n_samples, or None over budget) take reverse mode when the probe found vjp_twice, else forward mode. The VJP of a VJP records the forward once for all draws, so the batch’s forward and backward graph stays in memory until the last draw; strategy="forward-mode" runs the forward per draw instead.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| posterior | Posterior | A weight-space posterior. A function-space one gives posterior.at(inputs). |
required |
| inputs | Inputs | A batch of independent inputs (the model acts per example). A mapping that is not a torch pytree node (a tokenizer’s BatchEncoding) is passed on as a dict. 0-dim tensors and Python scalars in inputs are shared by every input (e.g. a diffusion timestep in a sampling loop); fitting data carries such values per example (prepare expands them). |
required |
| strategy | Strategy | None | How the Jacobian products run, "forward-mode" or "reverse-mode". None picks one, as above. |
None |
| n_samples | int | Literal['exact'] | None | "exact" for the exact covariance, an int (at least 2) for that many draws, or None to choose, as above. |
None |
| chunk_size | int | None | Output entries (reverse mode) or parameter directions (forward mode) per chunk, at least 1. None means 256. |
None |
| generator | torch.Generator | None | Seeds sampled outputs. One seed is drawn from it, or from torch’s global RNG when None, so torch.manual_seed makes the draws repeatable. Exact outputs draw nothing. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| OutputGaussian | The OutputGaussian with mean \(f(x; \mu)\) and a PerInputCov, FactorCov or SampleCov; for a function-space posterior, the output Gaussian of its at. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | An int n_samples below 2 or an int chunk_size below 1. For a function-space posterior, any explicit strategy, n_samples or chunk_size: it has no pushforward to choose. |
|
| RuntimeError | The posterior’s check_versions finds a parameter changed in place since the fit. |
|
| CapabilityError | The model lacks what the chosen path needs. Checked before the forward, and again once the outputs’ event size is known. | |
| NotImplementedError | The model does not return a single tensor. | |
| StructureError | The posterior’s covariance products fail, for example on a precision that is not positive definite. |