huggingface/candle · error

replication-pad with a size of {n} is not supported

Error message

replication-pad with a size of {n} is not supported

What it means

candle's replication_pad2d only implements pad sizes 0 (clone) and 1 (edge replication via narrow+cat on a 4-D NCHW tensor). Any other padding amount is explicitly rejected because the simple single-pixel replication construction does not generalize.

Source

Thrown at candle-nn/src/ops.rs:982

        w / downscale_factor,
        downscale_factor,
    ))?
    .permute((0, 1, 3, 5, 2, 4))?
    .reshape((b_size, out_c, h / downscale_factor, w / downscale_factor))
}

// https://pytorch.org/docs/stable/generated/torch.nn.ReplicationPad2d.html
pub fn replication_pad2d(xs: &Tensor, pad: usize) -> Result<Tensor> {
    match pad {
        0 => Ok(xs.clone()),
        1 => {
            let (_b_size, _c, h, w) = xs.dims4()?;
            let (first, last) = (xs.narrow(3, 0, 1)?, xs.narrow(3, w - 1, 1)?);
            let xs = Tensor::cat(&[&first, xs, &last], 3)?;
            let (first, last) = (xs.narrow(2, 0, 1)?, xs.narrow(2, h - 1, 1)?);
            Tensor::cat(&[&first, &xs, &last], 2)
        }
        n => candle::bail!("replication-pad with a size of {n} is not supported"),
    }
}

#[derive(Clone, Debug)]
pub struct Identity;

impl Identity {
    pub fn new() -> Identity {
        Self
    }
}

impl Default for Identity {
    fn default() -> Self {
        Self
    }
}

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Restructure the model to use pad=1 replication padding, or switch to zero padding (Tensor::pad / constant pad) which supports larger sizes
  2. Manually implement larger replication padding with repeated narrow + Tensor::cat per pixel
  3. Run the padding on a PyTorch side or pre-pad data before feeding to candle
  4. If 0 or 1 suffices, clamp/adjust the padding argument

Example fix

// before
let y = replication_pad2d(&x, 2)?; // unsupported
// after
let y = x.pad_with_zeros(2, 2, 2)?; // or custom narrow+cat replication loop
Defensive patterns

Strategy: fallback

Validate before calling

fn replication_pad2d_supported(pad: usize) -> bool { matches!(pad, 0 | 1) }

Try / catch

match replication_pad2d(&x, pad) {
    Ok(y) => y,
    Err(e) if e.to_string().contains("replication-pad") => x.pad_with_zeros(2, pad, pad)?,
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling candle_nn::ops::replication_pad2d(xs, n) with n >= 2 on any tensor; only pad=0 and pad=1 take the implemented branches.

Common situations: Porting PyTorch nn.ReplicationPad2d((2,2,2,2)) or larger padding configs to candle; translating ConvNet architectures that use multi-pixel replication padding; default padding values from reference implementations larger than 1.

Related errors


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