tracel-ai/burn · error

BlackmanWindow size doesn't fit in i64 range.

Error message

BlackmanWindow size doesn't fit in i64 range.

What it means

blackman_window builds the window by materializing an arange(0..size) Int tensor whose backing values are i64; if the requested window size cannot be represented in i64, i64::try_from fails and the expect panics. This is a defensive overflow check for absurd sizes.

Source

Thrown at crates/burn-tensor/src/tensor/signal/blackman_window.rs:71

pub fn blackman_window(
    size: usize,
    periodic: bool,
    options: impl Into<TensorCreationOptions>,
) -> Tensor<1> {
    let opt = options.into();
    let dtype = opt.resolve_dtype::<Float>();
    let shape = [size];
    check!(TensorCheck::creation_ops::<1>("BlackmanWindow", &shape));

    if size == 0 {
        return Tensor::<1>::empty(shape, opt).cast(dtype);
    }

    if size == 1 {
        return Tensor::<1>::ones(shape, opt).cast(dtype);
    }

    let size_i64 = i64::try_from(size).expect("BlackmanWindow size doesn't fit in i64 range.");
    let denominator = if periodic { size } else { size - 1 };
    let angular_increment = (2.0 * core::f64::consts::PI) / denominator as f64;
    let cos_val = Tensor::<1, Int>::arange(0..size_i64, &opt.device)
        .float()
        .mul_scalar(angular_increment)
        .cos();

    // Using the double angle property of cosine: cos(2θ) = 2cos^2(θ) - 1
    // w[n] = 0.42 - 0.5cos(2πn / N) + 0.08cos(4πn / N)
    // w[n] = 0.42 - 0.5cos(2πn / N) + 0.08cos(2 * (2πn / N))
    // w[n] = 0.42 - 0.5cos(2πn / N) + 0.08(2cos^2(2πn / N) - 1)
    // w[n] = 0.34 - 0.5cos(2πn / N) + 0.16cos^2(2πn / N)
    let first_cos_term = cos_val.clone().mul_scalar(-0.5);
    let second_cos_term = cos_val.powi_scalar(2).mul_scalar(0.16);
    first_cos_term
        .add(second_cos_term)
        .add_scalar(0.34)
        .cast(dtype)

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Ensure the requested window size is a sane value (< i64::MAX) before calling
  2. Validate the size in caller code (e.g. cap at a practical limit like 2^32)
  3. Fix upstream arithmetic that computes the size to avoid overflow/wrapping
  4. Note the early-return path: size == 1 is handled separately, so sizes >= 2 within i64 are safe

Example fix

// before
let w = blackman_window(size, periodic, device);
// after
assert!(size <= i64::MAX as usize, "window size too large");
let w = blackman_window(size, periodic, device);
Defensive patterns

Strategy: validation

Validate before calling

if size > (i64::MAX as usize) {
    return Err("Blackman window size too large".into());
}
let w = blackman_window(size, periodic, device);

Prevention

When it happens

Trigger: Calling `blackman_window(size)` where size > i64::MAX; computing the size from an overflowed/incorrect arithmetic expression (e.g. usize arithmetic on 32-bit-units or a wrapped counter).

Common situations: Signal-processing configs where the window length is derived from sample counts that overflow; test/fuzz inputs passing usize::MAX; misconfigured FFT length parameters.

Related errors


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