huggingface/candle · error

kernel-size {kernel_size:?} is larger than the input size {h

Error message

kernel-size {kernel_size:?} is larger than the input size {h},{w}

What it means

avg_pool2d requires the input's spatial dims to be at least as large as the pooling kernel; otherwise the output H/W formula would be negative. The check compares the 4-D input's h,w against kernel_size before computing output sizes.

Source

Thrown at candle-core/src/tensor.rs:1360

    /// the two last dimensions using a kernel of size `sz`. The returned element is the average
    /// value over the kernel window.
    pub fn avg_pool2d<T: crate::ToUsize2>(&self, sz: T) -> Result<Self> {
        let sz = sz.to_usize2();
        self.avg_pool2d_with_stride(sz, sz)
    }

    /// Same as `avg_pool2d` but with a `stride` that can be set to a value different from the
    /// kernel size.
    pub fn avg_pool2d_with_stride<T: crate::ToUsize2>(
        &self,
        kernel_size: T,
        stride: T,
    ) -> Result<Self> {
        let kernel_size = kernel_size.to_usize2();
        let stride = stride.to_usize2();
        let (n, c, h, w) = self.dims4()?;
        if h < kernel_size.0 || w < kernel_size.1 {
            bail!("kernel-size {kernel_size:?} is larger than the input size {h},{w}")
        }
        // https://pytorch.org/docs/stable/generated/torch.nn.AvgPool2d.html#torch.nn.AvgPool2d
        let h_out = (h - kernel_size.0) / stride.0 + 1;
        let w_out = (w - kernel_size.1) / stride.1 + 1;
        let op = BackpropOp::new1(self, |arg| Op::AvgPool2D {
            arg,
            kernel_size,
            stride,
        });
        let storage = self
            .storage()
            .avg_pool2d(self.layout(), kernel_size, stride)?;
        Ok(from_storage(storage, (n, c, h_out, w_out), op, false))
    }

    /// 2D max pooling over an input tensor with multiple channels.
    ///
    /// The input tensor should have four dimensions, `(batch, channels, h, w)`, the returned

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Reduce the pooling kernel size (and stride) so kernel_size <= (h, w) at that layer.
  2. Keep spatial resolution larger by removing an earlier downsample/stride or using padding.
  3. Reconfigure the model for your input resolution, or resize inputs up before the pool.
  4. Guard at runtime: check h/w via t.dims4()? before calling avg_pool2d.

Example fix

// before
let pooled = feat.avg_pool2d((2, 2), (2, 2))?; // feat is [1,8,1,1]
// after
let (_, _, h, w) = feat.dims4()?;
let k = (2.min(h), 2.min(w));
let pooled = feat.avg_pool2d(k, k)?;
Defensive patterns

Strategy: validation

Validate before calling

let (_, _, h, w) = feat.dims4()?;
let (kh, kw) = kernel_size;
if h < kh || w < kw {
    return Err(anyhow!("input {}x{} smaller than kernel {}x{}", h, w, kh, kw));
}
let pooled = feat.avg_pool2d(kernel_size, stride)?;

Try / catch

match feat.avg_pool2d(kernel_size, stride) {
    Ok(p) => p,
    Err(e) if e.to_string().contains("larger than the input size") =>
        // fall back to a smaller kernel or adaptive handling
        feat.avg_pool2d((1, 1), stride)?,
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Calling tensor.avg_pool2d(kernel_size, stride) where kernel_size.0 > h or kernel_size.1 > w on an [N,C,H,W] tensor — e.g. kernel (2,2) on a 1x1 or (h=1) feature map.

Common situations: Very small intermediate feature maps after aggressive downsampling in a CNN; wrong kernel-size config; feeding a low-resolution image into a network designed for larger inputs.

Related errors


AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02). Data as JSON: /api/errors/737d8c3fee31ac90. Report an issue: GitHub.