tracel-ai/burn · error

Either output_size or scale_factor must be provided

Error message

Either output_size or scale_factor must be provided

What it means

This panic is the fallback arm of the match in Interpolate2d::calculate_output_size: the Interpolate2d module can only compute its output spatial dimensions if exactly one of output_size or scale_factor was set in the config. When neither is provided (both None, matched by the `_` arm) the library cannot proceed and panics. It is a configuration-completeness check at forward time.

Source

Thrown at crates/burn-nn/src/modules/interpolate/interpolate2d.rs:159

        (None, Some(scale_factor)) => {
            // Calculate output size based on scale factor
            let [_, _, h, w] = input_dims;

            let new_dim_h = (h as f64) * (scale_factor[0] as f64);

            if new_dim_h > usize::MAX as f64 {
                panic!("Scale factor for height is too large");
            }

            let new_dim_w = (w as f64) * (scale_factor[1] as f64);

            if new_dim_w > usize::MAX as f64 {
                panic!("Scale factor for width is too large");
            }

            [new_dim_h as usize, new_dim_w as usize]
        }
        _ => panic!("Either output_size or scale_factor must be provided"),
    }
}

impl ModuleDisplay for Interpolate2d {
    fn custom_settings(&self) -> Option<DisplaySettings> {
        DisplaySettings::new()
            .with_new_line_after_attribute(false)
            .optional()
    }

    fn custom_content(&self, content: Content) -> Option<Content> {
        content
            .add_debug_attribute("mode", &self.mode)
            .add("output_size", &format!("{:?}", self.output_size))
            .add("scale_factor", &self.scale_factor)
            .optional()
    }
}

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Set the upsampling strategy explicitly: call .with_output_size([h, w]) or .with_scale_factor([sh, sw]) on the Interpolate2dConfig before init().
  2. If the config comes from a file/network, validate on load that exactly one of the two fields is present.
  3. Check that you are mutating the same config instance you pass to init (not a cloned/stale copy).

Example fix

// before
let cfg = Interpolate2dConfig::new();
let interpolate = Interpolate2d::new(&cfg); // panics on forward
// after
let cfg = Interpolate2dConfig::new().with_scale_factor([2.0, 2.0]);
let interpolate = Interpolate2d::new(&cfg);
Defensive patterns

Strategy: validation

Validate before calling

// call before init()/forward
assert!(
    config.output_size.is_some() ^ config.scale_factor.is_some(),
    "set exactly one of output_size or scale_factor"
);

Type guard

fn has_sizing(config: &Interpolate2dConfig) -> bool {
    config.output_size.is_some() || config.scale_factor.is_some()
}

Try / catch

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| module.forward(input)));
if result.is_err() {
    eprintln!("Interpolate2d config missing output_size/scale_factor");
}

Prevention

When it happens

Trigger: Constructing Interpolate2dConfig::new() without calling with_output_size(...) or with_scale_factor(...) and then calling forward on the resulting module. Also occurs when a config builder was created but the sizing option was set on the wrong config instance, or the value was lost during config (de)serialization.

Common situations: Boilerplate configs copied from examples where the sizing call was deleted, conditional code paths that forgot to set the option, migrating from another library (e.g. PyTorch's F.interpolate) where the mode/size arguments are optional at construction time, or deserializing a config from an older format that lacked these fields.

Understand the failure class

Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.

Related errors


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