tracel-ai/burn · error
Scale factor for height is too large
Error message
Scale factor for height is too large
What it means
This panic comes from Interpolate2d::calculate_output_size in burn-nn. When the module is configured with a scale_factor, the output height is computed as input_height * scale_factor[0] in f64; if the product exceeds usize::MAX (or is infinite/NaN-adjacent), the resulting usize cannot represent it, so the library panics instead of wrapping or saturating. It is a guard against silent numeric overflow when upsampling.
Source
Thrown at crates/burn-nn/src/modules/interpolate/interpolate2d.rs:148
/// or if the scale factor results in dimensions exceeding usize::MAX.
fn calculate_output_size(
input_dims: [usize; 4],
output_size: Option<[usize; 2]>,
scale_factor: Option<[f32; 2]>,
) -> [usize; 2] {
match (output_size, scale_factor) {
(Some(output_size), None) => {
// Use provided
output_size
}
(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)View on GitHub (pinned to d16f7ba2ed)
Solutions
- Reduce scale_factor[0] so that input_height * scale_factor[0] stays within usize range (keep it a small multiple like 2.0 or 4.0).
- Specify the exact target with with_output_size([h, w]) instead of a scale factor when you know the desired dimensions.
- Validate/parse the scale from external config: reject non-finite or > ~1e6 values before constructing Interpolate2dConfig.
- If you legitimately need extreme upsampling, downscale the input first or process it in tiles.
Example fix
// before
let cfg = Interpolate2dConfig::new().with_scale_factor([f64::INFINITY, 2.0]);
// after
let scale_h = if scale_h.is_finite() { scale_h } else { 1.0 };
let cfg = Interpolate2dConfig::new().with_scale_factor([scale_h, 2.0]);
// or, when the target size is known:
let cfg = Interpolate2dConfig::new().with_output_size([224, 224]); Defensive patterns
Strategy: validation
Validate before calling
let new_dim_h = (h as f64) * (scale_factor[0] as f64);
if !new_dim_h.is_finite() || new_dim_h > usize::MAX as f64 {
// reject or clamp before calling forward
return Err("height scale out of range");
} Type guard
fn valid_scale(s: f64) -> bool {
s.is_finite() && s > 0.0 && s <= 1e6
} Try / catch
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| module.forward(input)));
match result {
Ok(out) => out,
Err(_) => fallback_to_output_size_config(),
} Prevention
- Prefer with_output_size([h, w]) over scale_factor when target dimensions are known.
- Validate scale factors are finite, positive, and bounded (e.g. <= 1e6) at config load time.
- Remember usize::MAX is much smaller on 32-bit targets; bound-check with the target's usize.
When it happens
Trigger: Calling Interpolate2d::forward (directly or via a model) where the config used with_scale_factor([s_h, s_w]) and (input_height as f64) * s_h > usize::MAX as f64. Typical concrete triggers: scale_factor[0] = f64::INFINITY, an extremely large finite scale (e.g. 1e19), or a huge input height combined with a moderate scale.
Common situations: Typo in scale_factor (e.g. writing a target size like 4096 as a scale instead of an output_size), reading the scale from user input or a config file without bounds checks, accidentally passing f64::INFINITY or f64::MAX as a sentinel, or running on 32-bit targets where usize::MAX is much smaller.
Related errors
- Scale factor for width is too large
- Either output_size or scale_factor must be provided
- Standard deviation is required to be non-negative, but got {
- Affine is set to true, but gamma or beta is None
- capture tensor operations must run inside CaptureDevice::cap
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/0d4c41613a50bc15.
Report an issue: GitHub.