Skip to content
class

BatchNormEvalBackward

BatchNormEvalBackward

Autograd node for inference-mode Batch Normalization.

Uses caller-supplied running mean and variance — not computed from the current batch — to normalise the input. This is the path the Python BatchNormNd module dispatches to once model.eval() has been called (and track_running_stats=True).

Computes x^b,c,s=(xb,c,s−μc) / σc2+ϵ\hat{x}_{b,c,\mathbf{s}} = (x_{b,c,\mathbf{s}} - \mu_c)\,/\, \sqrt{\sigma_c^2 + \epsilon} per channel using the supplied mean and var tensors, then applies the per-channel affine y=γ x^+βy = \gamma\,\hat{x} + \beta. The reciprocal standard deviation rstd_ is cached for the backward closed-form.

Inherits FuncOp<BatchNormEvalBackward, 5> with five saved-input slots — x, mean, var, gamma, beta — because during training-time fine-tuning users may want gradients with respect to all five operands. In typical inference usage only x requires grad, in which case the four remaining gradient slots are trivially zero.

Math

x^b,c,s=xb,c,s−μcσc2+ϵ,y=γc x^+βc \hat{x}_{b,c,\mathbf{s}} = \frac{x_{b,c,\mathbf{s}} - \mu_c} {\sqrt{\sigma_c^2 + \epsilon}}, \qquad y = \gamma_c\,\hat{x} + \beta_c

See Also

BatchNorm2dBackward : Training-mode counterpart that computes statistics from the current batch.

Attributes

schema_v1OpSchema
Registered schema (name="batch_norm_eval", version=1, amp_policy=ForceFP32, produces_grad=true).
eps_double
Forward's eps; retained for the backward.
rstd_Storage
Pre-computed reciprocal standard deviation 1/σc2+ϵ1 / \sqrt{\sigma_c^2 + \epsilon}, shape (C,).

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & x, const int & mean, const int & var, const int & gamma, const int & beta, double eps)

Run the inference-mode forward pass.

Normalises x channel-wise using the supplied mean and var (typically the buffers running_mean / running_var accumulated during training), then applies the affine. Caches rstd_ for backward and (when grad mode is on) registers this as the grad_fn of the returned output.

Parameters

xTensorImplPtr
Input tensor of shape (B, C, ...).
meanTensorImplPtr
Running per-channel mean of shape (C,).
varTensorImplPtr
Running per-channel variance of shape (C,).
gammaTensorImplPtr
Per-channel scale of shape (C,).
betaTensorImplPtr
Per-channel shift of shape (C,).
epsdouble
Numerical-stability constant.

Returns

TensorImplPtr

Normalised output, same shape as x.

Methods

3
fn

apply

→std::vector<Storage>
int apply(int grad_out)

Compute gradients for the five saved inputs.

Returns {dx, d_mean, d_var, d_gamma, d_beta} in the saved-input order. In standard eval usage the gradients with respect to mean, var, gamma, and beta are zero (running stats and the affine are treated as frozen constants), but the kernel computes them anyway so that fine-tuning use cases work.

Parameters

grad_outStorage
Incoming gradient, same shape as the forward output.

Returns

std::vector<Storage>

Five-element vector {dx, d_mean, d_var, d_gamma, d_beta}.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same gradients from tracked ops, for create_graph=True. var reaches the node only as rstd_ — it is not saved — so a running variance that itself requires grad is refused here, as the reference refuses differentiating running statistics at all.

fn

release_own_saved

void release_own_saved()