huggingface/candle · error
index {index} is too low for tensor dimension {dim}
Error message
index {index} is too low for tensor dimension {dim} What it means
Thrown by candle-pyo3's `actual_index` when a negative index's absolute value exceeds the dimension size, so it cannot be wrapped to a valid position. This guards Tensor::get and Tensor::narrow against negative indices that fall before the start of the dimension. Candle refuses instead of panicking or wrapping unpredictably.
Source
Thrown at candle-pyo3/src/lib.rs:180
pydtype!(u8, |v| v);
pydtype!(u32, |v| v);
pydtype!(f16, f32::from);
pydtype!(bf16, f32::from);
pydtype!(f32, |v| v);
pydtype!(f64, |v| v);
pydtype!(F8E4M3, f32::from);
fn actual_index(t: &Tensor, dim: usize, index: i64) -> ::candle::Result<usize> {
let dim = t.dim(dim)?;
if 0 <= index {
let index = index as usize;
if dim <= index {
::candle::bail!("index {index} is too large for tensor dimension {dim}")
}
Ok(index)
} else {
if (dim as i64) < -index {
::candle::bail!("index {index} is too low for tensor dimension {dim}")
}
Ok((dim as i64 + index) as usize)
}
}
fn actual_dim(t: &Tensor, dim: i64) -> ::candle::Result<usize> {
let rank = t.rank();
if 0 <= dim {
let dim = dim as usize;
if rank <= dim {
::candle::bail!("dimension index {dim} is too large for tensor rank {rank}")
}
Ok(dim)
} else {
if (rank as i64) < -dim {
::candle::bail!("dimension index {dim} is too low for tensor rank {rank}")
}
Ok((rank as i64 + dim) as usize)View on GitHub (pinned to d5fee525bf)
Solutions
- Check the dimension size and ensure abs(index) <= dim_size before using a negative index
- Use min(index + dim_size, 0) normalization in your own code
- Handle empty tensors (dim 0) explicitly, where any index is invalid
Example fix
// before t.get(0, -11) # dim size 10 // after idx = -11 assert -t.dim(0) <= idx < t.dim(0) t.get(0, max(idx, -t.dim(0)))
Defensive patterns
Strategy: validation
Validate before calling
let dim_size = t.dim(0)? as i64;
if !(-dim_size..dim_size).contains(&index) { panic!("negative index {} invalid for dim size {}", index, dim_size); } Type guard
fn is_valid_neg_index(dim_size: usize, index: i64) -> bool {
(-(dim_size as i64)..(dim_size as i64)).contains(&index)
} Try / catch
match t.get(0, index) {
Ok(v) => v,
Err(e) if e.to_string().contains("too low") => t.get(0, 0)?,
Err(e) => return Err(e),
} Prevention
- Normalize negative indices yourself: idx + dim_size
- Handle empty tensors (size 0) explicitly before indexing
- Avoid negative indices when tensor rank/size is dynamic
When it happens
Trigger: Calling tensor.get(dim, index) or tensor.narrow(dim, start, len) with index < -(dim size), e.g. get(0, -11) on a dimension of size 10.
Common situations: Porting Python/NumPy negative-index habits to tensors whose dimensions are smaller than expected, or computing offsets that go negative due to empty tensors.
Related errors
- index {index} is too large for tensor dimension {dim}
- dimension index {dim} is too large for tensor rank {rank}
- dimension index {dim} is too low for tensor rank {rank}
- backward not supported for non uniform upscaling factors
- backward not supported for upsample_bilinear2d
AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02).
Data as JSON: /api/errors/3c7d8ed50415293d.
Report an issue: GitHub.