{"record":{"id":"0d4c41613a50bc15","repo":"tracel-ai/burn","slug":"scale-factor-for-height-is-too-large","errorCode":null,"errorMessage":"Scale factor for height is too large","messagePattern":"Scale factor for height is too large","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-nn/src/modules/interpolate/interpolate2d.rs","lineNumber":148,"sourceCode":"/// or if the scale factor results in dimensions exceeding usize::MAX.\nfn calculate_output_size(\n    input_dims: [usize; 4],\n    output_size: Option<[usize; 2]>,\n    scale_factor: Option<[f32; 2]>,\n) -> [usize; 2] {\n    match (output_size, scale_factor) {\n        (Some(output_size), None) => {\n            // Use provided\n            output_size\n        }\n        (None, Some(scale_factor)) => {\n            // Calculate output size based on scale factor\n            let [_, _, h, w] = input_dims;\n\n            let new_dim_h = (h as f64) * (scale_factor[0] as f64);\n\n            if new_dim_h > usize::MAX as f64 {\n                panic!(\"Scale factor for height is too large\");\n            }\n\n            let new_dim_w = (w as f64) * (scale_factor[1] as f64);\n\n            if new_dim_w > usize::MAX as f64 {\n                panic!(\"Scale factor for width is too large\");\n            }\n\n            [new_dim_h as usize, new_dim_w as usize]\n        }\n        _ => panic!(\"Either output_size or scale_factor must be provided\"),\n    }\n}\n\nimpl ModuleDisplay for Interpolate2d {\n    fn custom_settings(&self) -> Option<DisplaySettings> {\n        DisplaySettings::new()\n            .with_new_line_after_attribute(false)","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-nn/src/modules/interpolate/interpolate2d.rs#L130-L166","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nlet cfg = Interpolate2dConfig::new().with_scale_factor([f64::INFINITY, 2.0]);\n// after\nlet scale_h = if scale_h.is_finite() { scale_h } else { 1.0 };\nlet cfg = Interpolate2dConfig::new().with_scale_factor([scale_h, 2.0]);\n// or, when the target size is known:\nlet cfg = Interpolate2dConfig::new().with_output_size([224, 224]);","handlingStrategy":"validation","validationCode":"let new_dim_h = (h as f64) * (scale_factor[0] as f64);\nif !new_dim_h.is_finite() || new_dim_h > usize::MAX as f64 {\n    // reject or clamp before calling forward\n    return Err(\"height scale out of range\");\n}","typeGuard":"fn valid_scale(s: f64) -> bool {\n    s.is_finite() && s > 0.0 && s <= 1e6\n}","tryCatchPattern":"let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| module.forward(input)));\nmatch result {\n    Ok(out) => out,\n    Err(_) => fallback_to_output_size_config(),\n}","preventionTips":["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."],"tags":["rust","burn-nn","panic","numeric-overflow","interpolate2d"],"backgroundTag":"integer-overflow","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}