{"record":{"id":"3c7d8ed50415293d","repo":"huggingface/candle","slug":"index-index-is-too-low-for-tensor-dimension-dim","errorCode":null,"errorMessage":"index {index} is too low for tensor dimension {dim}","messagePattern":"index (.+?) is too low for tensor dimension (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-pyo3/src/lib.rs","lineNumber":180,"sourceCode":"pydtype!(u8, |v| v);\npydtype!(u32, |v| v);\npydtype!(f16, f32::from);\npydtype!(bf16, f32::from);\npydtype!(f32, |v| v);\npydtype!(f64, |v| v);\npydtype!(F8E4M3, f32::from);\n\nfn actual_index(t: &Tensor, dim: usize, index: i64) -> ::candle::Result<usize> {\n    let dim = t.dim(dim)?;\n    if 0 <= index {\n        let index = index as usize;\n        if dim <= index {\n            ::candle::bail!(\"index {index} is too large for tensor dimension {dim}\")\n        }\n        Ok(index)\n    } else {\n        if (dim as i64) < -index {\n            ::candle::bail!(\"index {index} is too low for tensor dimension {dim}\")\n        }\n        Ok((dim as i64 + index) as usize)\n    }\n}\n\nfn actual_dim(t: &Tensor, dim: i64) -> ::candle::Result<usize> {\n    let rank = t.rank();\n    if 0 <= dim {\n        let dim = dim as usize;\n        if rank <= dim {\n            ::candle::bail!(\"dimension index {dim} is too large for tensor rank {rank}\")\n        }\n        Ok(dim)\n    } else {\n        if (rank as i64) < -dim {\n            ::candle::bail!(\"dimension index {dim} is too low for tensor rank {rank}\")\n        }\n        Ok((rank as i64 + dim) as usize)","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-pyo3/src/lib.rs#L162-L198","documentation":"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.","triggerScenarios":"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.","commonSituations":"Porting Python/NumPy negative-index habits to tensors whose dimensions are smaller than expected, or computing offsets that go negative due to empty tensors.","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"],"exampleFix":"// before\nt.get(0, -11)  # dim size 10\n// after\nidx = -11\nassert -t.dim(0) <= idx < t.dim(0)\nt.get(0, max(idx, -t.dim(0)))","handlingStrategy":"validation","validationCode":"let dim_size = t.dim(0)? as i64;\nif !(-dim_size..dim_size).contains(&index) { panic!(\"negative index {} invalid for dim size {}\", index, dim_size); }","typeGuard":"fn is_valid_neg_index(dim_size: usize, index: i64) -> bool {\n    (-(dim_size as i64)..(dim_size as i64)).contains(&index)\n}","tryCatchPattern":"match t.get(0, index) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"too low\") => t.get(0, 0)?,\n    Err(e) => return Err(e),\n}","preventionTips":["Normalize negative indices yourself: idx + dim_size","Handle empty tensors (size 0) explicitly before indexing","Avoid negative indices when tensor rank/size is dynamic"],"tags":["rust","pyo3","tensor","negative-index"],"backgroundTag":"tensor-index-out-of-bounds","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}