tracel-ai/burn · error

Affine is set to true, but gamma or beta is None

Error message

Affine is set to true, but gamma or beta is None

What it means

The internal group_norm helper in burn-nn's GroupNorm module panics when the affine flag is true but the learnable affine parameters (gamma weight and/or beta bias) were passed as None. Affine GroupNorm requires both tensors to apply the learned per-channel scale and shift; an inconsistent combination of the flag and the Option parameters indicates a mis-constructed module. The same helper also panics if the input rank is below 3, so check both conditions when debugging.

Source

Thrown at crates/burn-nn/src/modules/norm/group.rs:155

///
/// `Y = groupnorm(X) * γ + β`
///
/// Where:
/// - `X` is the input tensor
/// - `Y` is the output tensor
/// - `γ` is the learnable weight
/// - `β` is the learnable bias
///
pub(crate) fn group_norm<const D: usize>(
    input: Tensor<D>,
    gamma: Option<Tensor<1>>,
    beta: Option<Tensor<1>>,
    num_groups: usize,
    epsilon: f64,
    affine: bool,
) -> Tensor<D> {
    if (beta.is_none() || gamma.is_none()) && affine {
        panic!("Affine is set to true, but gamma or beta is None");
    }

    let shape = input.shape();
    if shape.num_elements() <= 2 {
        panic!(
            "input rank for GroupNorm should be at least 3, but got {}",
            shape.num_elements()
        );
    }

    let batch_size = shape[0];
    let num_channels = shape[1];

    let hidden_size = shape[2..].iter().product::<usize>() * num_channels / num_groups;
    let input = input.reshape([batch_size, num_groups, hidden_size]);

    // Widen before the reduction when the input dtype cannot hold a sum of
    // squares (see [`accumulation_dtype`]); `square()` below is what overflows.

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Align the config with the parameters: if the checkpoint/record has no gamma/beta, construct GroupNormConfig with affine = false; if affine is true, ensure the module was init()'d so gamma/beta exist and load the full record.
  2. Re-initialize the module from its config (GroupNormConfig::init()) so affine parameters are created, then load the state.
  3. Inspect the error path for the related panic: if input rank < 3, reshape/permute the input to [N, C, *] before forward.
  4. Verify checkpoint keys include the GroupNorm weight/bias entries when affine was used at training time.

Example fix

// before
let config = GroupNormConfig::new(32, 1e-5, true);
// params loaded from an affine=false checkpoint: gamma/beta are None -> panics in forward
// after
let config = GroupNormConfig::new(32, 1e-5, false); // matches the checkpoint
let norm = config.init();
norm = norm.load_record(checkpoint); // or re-init with affine=true and load full record
Defensive patterns

Strategy: validation

Validate before calling

// before calling forward / group_norm
if affine {
    assert!(gamma.is_some() && beta.is_some(), "affine GroupNorm requires gamma and beta");
}
assert!(input.shape().num_elements() > 2, "GroupNorm input rank must be at least 3");

Type guard

fn affine_params_ready(affine: bool, gamma: &Option<Tensor<1>>, beta: &Option<Tensor<1>>) -> bool {
    !affine || (gamma.is_some() && beta.is_some())
}

Try / catch

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| module.forward(input)));
match result {
    Ok(out) => out,
    Err(_) => rebuild_module_from_config_and_reload_record(),
}

Prevention

When it happens

Trigger: Calling GroupNorm::forward where the module was built with affine = true in GroupNormConfig but gamma or beta is None — e.g. the params were never initialized/loaded (checkpoint missing group_norm.gamma/beta keys), the tensors were set to None manually, or affine was flipped to true in the config after the params were created under affine = false.

Common situations: Loading a model state from a checkpoint trained with affine = false into a module built with affine = true (or vice versa), partially deserialized records where gamma/beta failed to load, copying a config between models with mismatched settings, or calling the low-level group_norm function directly without supplying the tensors.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/e9ec9a49d0984c91. Report an issue: GitHub.