{"record":{"id":"7002c91f4a83a18c","repo":"huggingface/candle","slug":"replication-pad-with-a-size-of-n-is-not-supporte","errorCode":null,"errorMessage":"replication-pad with a size of {n} is not supported","messagePattern":"replication-pad with a size of (.+?) is not supported","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-nn/src/ops.rs","lineNumber":982,"sourceCode":"        w / downscale_factor,\n        downscale_factor,\n    ))?\n    .permute((0, 1, 3, 5, 2, 4))?\n    .reshape((b_size, out_c, h / downscale_factor, w / downscale_factor))\n}\n\n// https://pytorch.org/docs/stable/generated/torch.nn.ReplicationPad2d.html\npub fn replication_pad2d(xs: &Tensor, pad: usize) -> Result<Tensor> {\n    match pad {\n        0 => Ok(xs.clone()),\n        1 => {\n            let (_b_size, _c, h, w) = xs.dims4()?;\n            let (first, last) = (xs.narrow(3, 0, 1)?, xs.narrow(3, w - 1, 1)?);\n            let xs = Tensor::cat(&[&first, xs, &last], 3)?;\n            let (first, last) = (xs.narrow(2, 0, 1)?, xs.narrow(2, h - 1, 1)?);\n            Tensor::cat(&[&first, &xs, &last], 2)\n        }\n        n => candle::bail!(\"replication-pad with a size of {n} is not supported\"),\n    }\n}\n\n#[derive(Clone, Debug)]\npub struct Identity;\n\nimpl Identity {\n    pub fn new() -> Identity {\n        Self\n    }\n}\n\nimpl Default for Identity {\n    fn default() -> Self {\n        Self\n    }\n}\n","sourceCodeStart":964,"sourceCodeEnd":1000,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-nn/src/ops.rs#L964-L1000","documentation":"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.","triggerScenarios":"Calling candle_nn::ops::replication_pad2d(xs, n) with n >= 2 on any tensor; only pad=0 and pad=1 take the implemented branches.","commonSituations":"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.","solutions":["Restructure the model to use pad=1 replication padding, or switch to zero padding (Tensor::pad / constant pad) which supports larger sizes","Manually implement larger replication padding with repeated narrow + Tensor::cat per pixel","Run the padding on a PyTorch side or pre-pad data before feeding to candle","If 0 or 1 suffices, clamp/adjust the padding argument"],"exampleFix":"// before\nlet y = replication_pad2d(&x, 2)?; // unsupported\n// after\nlet y = x.pad_with_zeros(2, 2, 2)?; // or custom narrow+cat replication loop","handlingStrategy":"fallback","validationCode":"fn replication_pad2d_supported(pad: usize) -> bool { matches!(pad, 0 | 1) }","typeGuard":null,"tryCatchPattern":"match replication_pad2d(&x, pad) {\n    Ok(y) => y,\n    Err(e) if e.to_string().contains(\"replication-pad\") => x.pad_with_zeros(2, pad, pad)?,\n    Err(e) => return Err(e),\n}","preventionTips":["Only use pad values 0 or 1 with replication_pad2d","Implement a helper that generalizes replication padding via narrow+cat loops","Prefer zero padding for larger pads","Check candle version/changelog for extended pad support before using larger values"],"tags":["padding","unsupported-operation","candle"],"backgroundTag":"unsupported-pad-size","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}