huggingface/candle · error
index {index} is too large for tensor dimension {dim}
Error message
index {index} is too large for tensor dimension {dim} What it means
This error comes from candle-pyo3's Python binding helper `actual_index`, which normalizes a user-supplied index for a tensor dimension before calling Tensor::get or Tensor::narrow. It is thrown when a non-negative index is greater than or equal to the size of the selected dimension, i.e. out of bounds. Candle uses bail! to fail fast rather than silently clamping or wrapping.
Source
Thrown at candle-pyo3/src/lib.rs:175
}
};
}
pydtype!(i64, |v| v);
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)View on GitHub (pinned to d5fee525bf)
Solutions
- Print the tensor shape (t.dims()) and check the index against the actual dimension size before indexing
- Clamp or modulo the index: index = min(index, dim_size - 1)
- Use negative indexing (e.g. -1 for the last element) which actual_index supports
- Fix upstream data pipeline so tensors have the expected shape
Example fix
// before elem = t.get(0, 10) # dim size is 10 // after assert 0 <= 10 < t.dim(0) elem = t.get(0, 9) # or t.get(0, -1) for the last element
Defensive patterns
Strategy: validation
Validate before calling
let dim_size = t.dim(0)?;
if !(0..dim_size).contains(&index) { panic!("index {} out of bounds for dim size {}", index, dim_size); } Type guard
fn is_valid_index(dim_size: usize, index: i64) -> bool {
index >= 0 && (index as usize) < dim_size
} Try / catch
match t.get(0, index) {
Ok(v) => v,
Err(e) if e.to_string().contains("too large") || e.to_string().contains("too low") => fallback_value(),
Err(e) => return Err(e),
} Prevention
- Always check tensor dims before indexing
- Prefer negative indexing (-1) for last-element access
- Log tensor shapes when debugging indexing errors
- Guard loops with `for i in 0..t.dim(0)?` instead of hard-coded sizes
When it happens
Trigger: Calling tensor.get(dim, index) or tensor.narrow(dim, start, len) from Python with index >= t.dim(dim), e.g. indexing element 10 of a dimension of size 10.
Common situations: Off-by-one loops over tensor sizes, assuming 0-based vs 1-based indexing, using a shape computed from a different tensor, or batches smaller than expected after preprocessing.
Related errors
- index {index} is too low 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/024c68049936ad10.
Report an issue: GitHub.