Skip to content

Distribution kernels (optix.distributions.functional)

Pure tensor functions behind the distribution classes. They take the canonical parameters of each family, validate nothing and never synchronise with the host, so they can be inlined in a compiled kernel.

Pure tensor functions of the distribution families (the maths).

Every function takes and returns tensors, broadcasts its inputs, keeps event dims last, never reads the configuration, never synchronises with the host and has no data-dependent Python control flow, so it can be inlined into a torch.compile kernel without graph breaks. Nothing is validated: invalid parameters give NaN or infinities.

Each family has one canonical signature:

  • Gamma: shape a and rate b;
  • Dirichlet: concentration alpha of shape (..., K);
  • Categorical: normalised logits l = log p of shape (..., K);
  • Normal: mean m and precision lam (plus normal_log_prob_var in variance form, for likelihood kernels).

KL divergences take all parameters of q, then all of p, and are not clamped at zero (in float32 nearly equal distributions can give a slightly negative value).

gamma_kl module-attribute

gamma_kl = kl_gamma

KL(Gamma(aq, bq) ‖ Gamma(ap, bp)), elementwise.

KL = (aq − ap) ψ(aq) − log Γ(aq) + log Γ(ap)
     + ap (log bq − log bp) + aq (bp − bq) / bq

The same function object as kl_gamma.

dirichlet_mean_log module-attribute

dirichlet_mean_log = dirichlet_elog

E[log πₖ] = ψ(αₖ) − ψ(α₀) under Dir(α).

The same function object as dirichlet_elog.

dirichlet_kl module-attribute

dirichlet_kl = kl_dirichlet

KL(Dir(a) ‖ Dir(b)), summed over the last dim.

KL = log Γ(a₀) − log Γ(b₀) − ∑ₖ (log Γ(aₖ) − log Γ(bₖ))
     + ∑ₖ (aₖ − bₖ) (ψ(aₖ) − ψ(a₀))

The same function object as kl_dirichlet.

gamma_mean

gamma_mean(a, b)

Mean of Gamma(a, b): E[x] = a / b.

Parameters:

Name Type Description Default
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

gamma_variance

gamma_variance(a, b)

Variance of Gamma(a, b): Var[x] = a / b².

Parameters:

Name Type Description Default
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

gamma_mean_log

gamma_mean_log(a, b)

E[log x] = ψ(a) − log b under Gamma(a, b).

Parameters:

Name Type Description Default
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs. Computed as log(a / b) − lmd(a), with lmd = log_minus_digamma (no cancellation for large a and b).

gamma_mean_log_ms

gamma_mean_log_ms(mean, shape)

E[log x] of a Gamma given its mean μ and shape ν.

E[log x] = log μ − (log ν − ψ(ν)), which avoids the cancellation between ψ(ν) and log(rate) for large ν (see log_minus_digamma).

Parameters:

Name Type Description Default
mean Tensor

Mean μ = a / b and shape ν = a.

required
shape Tensor

Mean μ = a / b and shape ν = a.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

gamma_mean_inv

gamma_mean_inv(a, b)

E[1 / x] = b / (a − 1) under Gamma(a, b); inf if a ≤ 1.

Parameters:

Name Type Description Default
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs. The gradient is finite (zero) where the value is infinite.

gamma_entropy

gamma_entropy(a, b)

Entropy of Gamma(a, b).

H = a − log b + log Γ(a) + (1 − a) ψ(a), computed as ½ log(2π a) + r(a) + (a − 1) lmd(a) − log b with stirling_residual r and log_minus_digamma lmd, so that the large log Γ and ψ terms do not cancel at large a.

Parameters:

Name Type Description Default
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

gamma_log_prob

gamma_log_prob(x, a, b)

Log-density of Gamma(a, b) at x > 0.

log p(x) = a log b − log Γ(a) + (a − 1) log x − b x, computed for x > 0 as −a g(b x / a) + ½ log a − log x − ½ log(2π) − r(a) with ratio_minus_log g and stirling_residual r (no cancellation between a log(b x), log Γ(a) and b x at large a); the direct formula is used at x = 0.

Parameters:

Name Type Description Default
x Tensor

Values.

required
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

gamma_log_normalizer

gamma_log_normalizer(a, b)

Log-normalizer A = log Γ(a) − a log b of Gamma(a, b).

With sufficient statistics T(x) = (log x, x) and natural parameters η = (a − 1, −b), ∂A/∂η = E[T].

Parameters:

Name Type Description Default
a Tensor

Shape and rate.

required
b Tensor

Shape and rate.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

dirichlet_mean

dirichlet_mean(alpha)

Mean E[π] = α / α₀ of Dir(α), α₀ = ∑ₖ αₖ.

Parameters:

Name Type Description Default
alpha (..., K) Tensor

Concentration.

required

Returns:

Type Description
(..., K) Tensor

dirichlet_variance

dirichlet_variance(alpha)

Marginal variances of Dir(α).

Var[πₖ] = αₖ (α₀ − αₖ) / (α₀² (α₀ + 1)).

Parameters:

Name Type Description Default
alpha (..., K) Tensor

Concentration.

required

Returns:

Type Description
(..., K) Tensor

dirichlet_entropy

dirichlet_entropy(alpha)

Entropy of Dir(α).

H = ∑ₖ log Γ(αₖ) − log Γ(α₀) + (α₀ − K) ψ(α₀) − ∑ₖ (αₖ − 1) ψ(αₖ), computed with stirling_residual r and log_minus_digamma lmd as

½ ∑ₖ log(αₖ/α₀) − ½ (K − 1) log(α₀/(2π)) + ∑ₖ r(αₖ) − r(α₀)
+ ∑ₖ (αₖ − 1) lmd(αₖ) − (α₀ − K) lmd(α₀)

(no cancellation at large concentrations).

Parameters:

Name Type Description Default
alpha (..., K) Tensor

Concentration.

required

Returns:

Type Description
(...) Tensor

dirichlet_log_prob

dirichlet_log_prob(x, alpha)

Log-density of Dir(α) at a point x of the simplex.

log p(x) = log Γ(α₀) − ∑ₖ log Γ(αₖ) + ∑ₖ (αₖ − 1) log xₖ.

Parameters:

Name Type Description Default
x (..., K) Tensor

Values.

required
alpha (..., K) Tensor

Concentration.

required

Returns:

Type Description
(...) Tensor

Computed with the mean m = α / α₀ and stirling_residual r as ∑ₖ (αₖ − 1) log(xₖ/mₖ) − ½ ∑ₖ log mₖ + ½ (K − 1) log(α₀/(2π)) + r(α₀) − ∑ₖ r(αₖ): the log Γ terms and α log x do not cancel at large concentrations.

dirichlet_log_normalizer

dirichlet_log_normalizer(alpha)

Log-normalizer A = ∑ₖ log Γ(αₖ) − log Γ(α₀).

With T(x) = log x and η = α − 1, ∂A/∂η = E[T].

Parameters:

Name Type Description Default
alpha (..., K) Tensor

Concentration.

required

Returns:

Type Description
(...) Tensor

Computed without the cancellation of the log Γ terms at large concentrations (Stirling residuals, as in dirichlet_log_prob).

categorical_normalize

categorical_normalize(logits)

Normalised logits log_softmax(logits) (last dim = categories).

Parameters:

Name Type Description Default
logits (..., K) Tensor

Unnormalised log-probabilities.

required

Returns:

Type Description
(..., K) Tensor

l with logsumexp(l, -1) == 0.

categorical_logits_from_probs

categorical_logits_from_probs(probs)

Normalised logits log(p / ∑ₖ pₖ) of non-negative weights.

Zero weights give -inf with a zero (finite) gradient, thanks to a double where.

Parameters:

Name Type Description Default
probs (..., K) Tensor

Non-negative, unnormalised probabilities.

required

Returns:

Type Description
(..., K) Tensor

categorical_entropy

categorical_entropy(logits)

Entropy H = −∑ₖ pₖ log pₖ, with p = exp(l), 0 log 0 = 0.

Parameters:

Name Type Description Default
logits (..., K) Tensor

Normalised logits l = log p.

required

Returns:

Type Description
(...) Tensor

Entropy; the gradient stays finite where p = 0 (double where).

categorical_log_prob

categorical_log_prob(index, logits)

Log-probability l[index] of integer labels.

Parameters:

Name Type Description Default
index Tensor

Integer labels, broadcastable against logits.shape[:-1].

required
logits (..., K) Tensor

Normalised logits.

required

Returns:

Type Description
Tensor

Broadcast shape of index and logits.shape[:-1].

categorical_log_normalizer

categorical_log_normalizer(eta)

Log-normalizer A = logsumexp(η) (zero for normalised logits).

With T(z) = onehot(z), ∂A/∂η = E[T] = p.

Parameters:

Name Type Description Default
eta (..., K) Tensor

Natural parameters (logits up to an additive constant).

required

Returns:

Type Description
(...) Tensor

categorical_kl

categorical_kl(lq, lp)

KL(Cat(q) ‖ Cat(p)) = ∑ₖ qₖ (log qₖ − log pₖ), 0 log 0 = 0.

Parameters:

Name Type Description Default
lq (..., K) Tensor

Normalised logits of q and p.

required
lp (..., K) Tensor

Normalised logits of q and p.

required

Returns:

Type Description
(...) Tensor

Divergence; +inf where q > 0 and p = 0. Gradients are finite where q = 0 (double where).

normal_variance

normal_variance(lam)

Variance 1 / λ of a Normal with precision lam (λ).

Parameters:

Name Type Description Default
lam Tensor

Precision.

required

Returns:

Type Description
Tensor

normal_mean_sq

normal_mean_sq(m, lam)

Second moment E[x²] = μ² + 1 / λ.

Parameters:

Name Type Description Default
m Tensor

Mean and precision.

required
lam Tensor

Mean and precision.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

normal_entropy

normal_entropy(lam)

Entropy H = (1 + log(2π) − log λ) / 2.

Parameters:

Name Type Description Default
lam Tensor

Precision.

required

Returns:

Type Description
Tensor

normal_log_prob

normal_log_prob(x, m, lam)

Log-density (log λ − log(2π) − λ (x − μ)²) / 2.

Parameters:

Name Type Description Default
x Tensor

Values.

required
m Tensor

Mean and precision.

required
lam Tensor

Mean and precision.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

normal_log_prob_var

normal_log_prob_var(x, m, var)

Log-density in variance form.

log p(x) = −(log σ² + log(2π) + (x − μ)² / σ²) / 2.

The form to call inside likelihood kernels whose variance is computed per sample (it saves a reciprocal).

Parameters:

Name Type Description Default
x Tensor

Values.

required
m Tensor

Mean and variance.

required
var Tensor

Mean and variance.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

normal_log_normalizer

normal_log_normalizer(m, lam)

Log-normalizer A = λμ² / 2 − ½ log λ.

With T(x) = (x, x²), η = (λμ, −λ/2) and base measure log h = −½ log(2π), ∂A/∂η = E[T] = (μ, μ² + 1/λ).

Parameters:

Name Type Description Default
m Tensor

Mean and precision.

required
lam Tensor

Mean and precision.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.

normal_kl

normal_kl(mq, lq, mp, lp)

KL(N(mq, 1/lq) ‖ N(mp, 1/lp)), elementwise, in precision form.

KL = (log lq − log lp + lp / lq + lp (mq − mp)² − 1) / 2.

Note

kl_normal takes variances.

Parameters:

Name Type Description Default
mq Tensor

Mean and precision of q.

required
lq Tensor

Mean and precision of q.

required
mp Tensor

Mean and precision of p.

required
lp Tensor

Mean and precision of p.

required

Returns:

Type Description
Tensor

Broadcast shape of the inputs.