huggingface/candle · error
cannot use pad_with_same on an empty tensor
Error message
cannot use pad_with_same on an empty tensor
What it means
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.)
Source
Thrown at candle-core/src/tensor.rs:2716
Tensor::cat(&[&left, self], dim)
} else {
let dim = dim.to_index(self.shape(), "pad_with_zeros")?;
let mut dims = self.dims().to_vec();
dims[dim] = left;
let left = Tensor::zeros(dims.as_slice(), self.dtype, self.device())?;
dims[dim] = right;
let right = Tensor::zeros(dims.as_slice(), self.dtype, self.device())?;
Tensor::cat(&[&left, self, &right], dim)
}
}
/// Pad the input tensor using same values along dimension `dim`. This adds `left` elements before the
/// input tensor values and `right` elements after.
pub fn pad_with_same<D: Dim>(&self, dim: D, left: usize, right: usize) -> Result<Self> {
if left == 0 && right == 0 {
Ok(self.clone())
} else if self.elem_count() == 0 {
bail!("cannot use pad_with_same on an empty tensor")
} else if left == 0 {
let dim = dim.to_index(self.shape(), "pad_with_same")?;
let r = self.narrow(dim, self.dim(dim)? - 1, 1)?;
let mut v = vec![self];
for _ in 0..right {
v.push(&r)
}
Tensor::cat(&v, dim)
} else if right == 0 {
let dim = dim.to_index(self.shape(), "pad_with_same")?;
let l = self.narrow(dim, 0, 1)?;
let mut v = vec![];
for _ in 0..left {
v.push(&l)
}
v.push(self);
Tensor::cat(&v, dim)
} else {View on GitHub (pinned to d5fee525bf)
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
Example fix
// before
let t = t.pad_with_same(0, 1, 1)?;
// after
let t = if t.elem_count() == 0 { t } else { t.pad_with_same(0, 1, 1)? }; Defensive patterns
Strategy: validation
Validate before calling
if t.elem_count() == 0 {
// skip padding or substitute pad_with_zeros
}
assert!(t.elem_count() > 0); Try / catch
let padded = t.pad_with_same(dim, left, right)
.or_else(|_| t.pad_with_zeros(dim, left, right))?; Prevention
- 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
When it happens
Trigger: 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)).
Common situations: 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.
Related errors
- pad-mode 'reflect' is not supported
- {} is a dummy type and cannot be constructed
- {} is a dummy type and cannot be converted
- {} is a dummy type and cannot be converted to scalar
- {} is a dummy type and does not support storage
AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02).
Data as JSON: /api/errors/c1930a5cdfd0f18c.
Report an issue: GitHub.