{"record":{"id":"c1930a5cdfd0f18c","repo":"huggingface/candle","slug":"cannot-use-pad-with-same-on-an-empty-tensor","errorCode":null,"errorMessage":"cannot use pad_with_same on an empty tensor","messagePattern":"cannot use pad_with_same on an empty tensor","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/tensor.rs","lineNumber":2716,"sourceCode":"            Tensor::cat(&[&left, self], dim)\n        } else {\n            let dim = dim.to_index(self.shape(), \"pad_with_zeros\")?;\n            let mut dims = self.dims().to_vec();\n            dims[dim] = left;\n            let left = Tensor::zeros(dims.as_slice(), self.dtype, self.device())?;\n            dims[dim] = right;\n            let right = Tensor::zeros(dims.as_slice(), self.dtype, self.device())?;\n            Tensor::cat(&[&left, self, &right], dim)\n        }\n    }\n\n    /// Pad the input tensor using same values along dimension `dim`. This adds `left` elements before the\n    /// input tensor values and `right` elements after.\n    pub fn pad_with_same<D: Dim>(&self, dim: D, left: usize, right: usize) -> Result<Self> {\n        if left == 0 && right == 0 {\n            Ok(self.clone())\n        } else if self.elem_count() == 0 {\n            bail!(\"cannot use pad_with_same on an empty tensor\")\n        } else if left == 0 {\n            let dim = dim.to_index(self.shape(), \"pad_with_same\")?;\n            let r = self.narrow(dim, self.dim(dim)? - 1, 1)?;\n            let mut v = vec![self];\n            for _ in 0..right {\n                v.push(&r)\n            }\n            Tensor::cat(&v, dim)\n        } else if right == 0 {\n            let dim = dim.to_index(self.shape(), \"pad_with_same\")?;\n            let l = self.narrow(dim, 0, 1)?;\n            let mut v = vec![];\n            for _ in 0..left {\n                v.push(&l)\n            }\n            v.push(self);\n            Tensor::cat(&v, dim)\n        } else {","sourceCodeStart":2698,"sourceCodeEnd":2734,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/tensor.rs#L2698-L2734","documentation":"pad_with_same pads a tensor by replicating its edge values, which requires at least one element to copy from. If the tensor has zero elements, there is no edge value to repeat, so the method bails instead of producing a meaningless result. (The zero-pad variant pad_with_zeros has no such restriction.)","triggerScenarios":"Calling tensor.pad_with_same(dim, left, right) with left>0 or right>0 on a tensor whose elem_count() == 0 (e.g. shape (0,) or (2,0,3)).","commonSituations":"Patching sequences after a filtering/slicing step produced empty tensors; batch pipelines where an empty input slips through; dynamic shapes in ONNX-style graphs yielding 0-size dims.","solutions":["Check elem_count() == 0 before calling pad_with_same and skip or use pad_with_zeros instead","Fix upstream logic so the tensor is never empty when padding is expected","Return an explicit error/early-exit in caller code for empty inputs"],"exampleFix":"// before\nlet t = t.pad_with_same(0, 1, 1)?;\n// after\nlet t = if t.elem_count() == 0 { t } else { t.pad_with_same(0, 1, 1)? };","handlingStrategy":"validation","validationCode":"if t.elem_count() == 0 {\n    // skip padding or substitute pad_with_zeros\n}\nassert!(t.elem_count() > 0);","typeGuard":null,"tryCatchPattern":"let padded = t.pad_with_same(dim, left, right)\n    .or_else(|_| t.pad_with_zeros(dim, left, right))?;","preventionTips":["Filter out empty tensors before padding stages","Log tensor shapes when a filter/squeeze could produce empty tensors","Prefer pad_with_zeros when edge replication isn't required"],"tags":["rust","candle","padding","empty-tensor"],"backgroundTag":"empty-tensor-padding","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}