Python wrappers
Public Python APIs implemented by this engine symbols.Autograd node for training-mode Batch Normalization with N spatial axes.
Computes per-channel statistics over the (B, S_0, ..., S_{N-1})
axes of the input, normalises the input by them, and applies the
learnable per-channel affine . The
per-channel mean and reciprocal standard deviation
are saved in
saved_mean_ / saved_rstd_ for the backward
closed-form.
Inherits FuncOp<BatchNormNdBackward<N>, 3> with three saved-input
slots — x, gamma, and beta. The template is explicitly
instantiated for N = 1, 2, 3 in the .cpp file and exposed via
the aliases BatchNorm1dBackward, BatchNorm2dBackward,
and BatchNorm3dBackward.
Math
The Python-side running-stats update applies, in order,
where is the per-channel reduction count (the Bessel correction is applied only to the running variance buffer, never to the normalisation itself).
Shape
- Input
x:(B, C, S_0, ..., S_{N-1}) - Output : same shape as
x
References
Ioffe & Szegedy, "Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift" (ICML 2015).
Attributes
schema_v1OpSchemaname is "batch_norm1d", "batch_norm", or "batch_norm3d" depending on N; version=1; amp_policy=ForceFP32; produces_grad=true.saved_mean_Storage(C,), computed by the backend and saved for backward.saved_rstd_Storage(C,).B_, C_intx.shape() for the backward.S_int[N]x.shape()[2:] (length-1 dummy array for the N==0 corner case, which is never instantiated).eps_doubleeps; preserved so that the GPU MPSGraph backward can derive variance from .Notes
Thread safety: an instance is created once during forward and consumed once during backward — no concurrent access expected.
Static methods
1forward
→TensorImplPtrint forward(const int & x, const int & gamma, const int & beta, double eps, const int & running_mean, const int & running_var, double momentum)Run the training-mode forward pass.
Computes per-channel batch statistics, normalises x, applies
the affine, saves and the reciprocal std for backward, and
(when grad mode is on) registers this as the grad_fn of
the returned output. Dispatches to
IBackend::batch_norm_forward which returns [y, mean, rstd]
in a single fused call.
Parameters
xTensorImplPtr(B, C, S_0, ..., S_{N-1}).gammaTensorImplPtr(C,).betaTensorImplPtr(C,).epsdoubleReturns
TensorImplPtrNormalised output, same shape as x. Forward. When running_mean / running_var are non-null, the EMA update happens INSIDE this call using the same mean+rstd the backend already computed for the saved tensors — no second reduction over x. momentum is the EMA coefficient (must be finite; cumulative mode is handled in the Python wrapper).
Methods
1Compute gradients for the three saved inputs.
Returns {dx, d_gamma, d_beta} in the saved-input order using
IBackend::batch_norm_backward and the cached saved_mean_ /
saved_rstd_. Closed-form summary (per channel ):
and the input gradient follows the standard BN backward expansion.
Parameters
grad_outStorageReturns
std::vector<Storage>Three-element vector {dx, d_gamma, d_beta}.