huggingface/candle · error

in_channel mismatch between input ({c_in}) and kernel ({c_in

Error message

in_channel mismatch between input ({c_in}) and kernel ({c_in_k})

What it means

conv_transpose1d validates that the input tensor's channel count (c_in) equals the kernel's input-channel dimension (first dim of the 3D kernel). For transposed convolution the kernel is stored as (c_in, c_out, k_size), so a mismatch means the weight tensor does not correspond to the actual input channels.

Source

Thrown at candle-core/src/conv.rs:243

        });
        let out_dims = params.out_dims();
        Ok(crate::tensor::from_storage(storage, out_dims, op, false))
    }

    /// Applies a 1D transposed convolution over the input tensor.
    pub fn conv_transpose1d(
        &self,
        kernel: &Self,
        padding: usize,
        output_padding: usize,
        stride: usize,
        dilation: usize,
        groups: usize,
    ) -> Result<Self> {
        let (c_in_k, c_out, k_size) = kernel.dims3()?;
        let (b_size, c_in, l_in) = self.dims3()?;
        if c_in != c_in_k {
            crate::bail!("in_channel mismatch between input ({c_in}) and kernel ({c_in_k})")
        }
        if c_in % groups != 0 {
            crate::bail!("in_channel {c_in} is not divisible by the number of groups")
        }
        let params = ParamsConvTranspose1D {
            b_size,
            l_in,
            k_size,
            c_out,
            c_in: c_in / groups,
            padding,
            output_padding,
            stride,
            dilation,
        };
        if groups == 1 {
            self.conv_transpose1d_single_group(kernel, &params)
        } else {

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Swap the kernel to shape (c_in, c_out, k_size) — transposed conv expects input channels first.
  2. Update the ConvTranspose1d layer config (in_channels) to match the actual input tensor.
  3. Verify checkpoint weights match the model definition (channel counts) after editing the architecture.
  4. Print input.dims() and kernel.dims() and align dim(1) of input with dim(0) of kernel.

Example fix

// before: x (8, 16, 100), kernel (32, 16, 3)  -> c_in=16 vs c_in_k=32
x.conv_transpose1d(&k, 0, 0, 2, 1, 1)?;
// after: kernel must start with input channels
let k = Tensor::randn(0f32, 1f32, (16, 32, 3), &dev)?;
x.conv_transpose1d(&k, 0, 0, 2, 1, 1)?;
Defensive patterns

Strategy: validation

Validate before calling

let (_b, c_in, _l) = x.dims3()?;
let (c_in_k, _c_out, _k) = kernel.dims3()?;
if c_in != c_in_k { return Err(anyhow::anyhow!("conv_transpose1d: input channels {c_in} != kernel in-channels {c_in_k}")); }

Try / catch

match result { Err(e) if e.to_string().contains("in_channel mismatch") => { // permute kernel to (c_in, c_out, k) and retry
}, other => other?, }

Prevention

When it happens

Trigger: Calling Tensor::conv_transpose1d(&kernel, pad, output_padding, stride, dilation, groups) where kernel.dims3() yields c_in_k != self's dim(1), e.g. input (B, 3, L) with kernel of shape (64, 128, 3).

Common situations: Building ConvTranspose1d layers with wrong in_channels/out_channels ordering (transposed conv kernels are (in, out, k), the reverse of regular conv); loading weights from a checkpoint whose layer config changed; misusing a regular conv kernel for a transposed conv.

Related errors


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