BatchNorm accepts invalid feature count, epsilon, and momentum
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:
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.epsilonis finite and strictly positive.momentumis 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.
Source: tracel-ai/burn