#5929·burn

BatchNorm accepts invalid feature count, epsilon, and momentum

Author: Mikyx-1Created Oct 1, 2026Updated Oct 1, 2026

Describe the bug

BatchNormConfig::init initializes tensors and copies epsilon and momentum without validating their domains. Zero features can fail later in tensor operations, while invalid numerical values can produce NaN outputs or invalid running statistics.

To reproduce

With the Flex backend enabled, initialize any of these configurations:

rust
use burn::nn::BatchNormConfig;
use burn::tensor::Device;

let device = Device::flex();

BatchNormConfig::new(0).init(&device);
BatchNormConfig::new(1).with_epsilon(0.0).init(&device);
BatchNormConfig::new(1).with_epsilon(f64::NAN).init(&device);
BatchNormConfig::new(1).with_epsilon(f64::INFINITY).init(&device);
BatchNormConfig::new(1).with_momentum(-0.1).init(&device);
BatchNormConfig::new(1).with_momentum(1.1).init(&device);
BatchNormConfig::new(1).with_momentum(f64::NAN).init(&device);

The initializer does not reject these configurations with clear parameter validation. In particular, zero epsilon permits division by zero for a constant training batch; NaN epsilon propagates NaNs, and out-of-range momentum can make running variance invalid.

Expected behavior

Validate the configuration before allocating tensors:

  • num_features > 0.
  • epsilon is finite and strictly positive.
  • momentum is finite and in the inclusive range [0, 1].

Reject invalid values with clear initialization panic messages. Document and preserve the valid momentum boundaries: 0 retains the running statistics, and 1 replaces them with the current batch statistics. Both values should still normalize training inputs using batch statistics.

Additional context

The missing validation is present in crates/burn-nn/src/modules/norm/batch.rs on upstream commit 92661d694. This report originates from https://github.com/Mikyx-1/burn/issues/259.

Regression coverage should include zero features, nonpositive epsilon, NaN and both infinities, out-of-range momentum, and valid one-feature layers at both momentum endpoints.