{"record":{"id":"afc5a950c9f7c4cf","repo":"huggingface/candle","slug":"index-out-of-bounds-for-dimension-with-size","errorCode":null,"errorMessage":"Index {} out of bounds for dimension {} with size {}","messagePattern":"Index (.+?) out of bounds for dimension (.+?) with size (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":2516,"sourceCode":"                let mut strides: Vec<usize> = vec![1];\n                for i in (0..data_shape.len() - 1).rev() {\n                    strides.push(strides.last().unwrap() * data_shape[i + 1]);\n                }\n                strides.reverse();\n\n                // Process each update\n                for i in 0..num_updates {\n                    let index_slice = flat_indices.narrow(0, i, 1)?;\n                    let indices_vec = index_slice.squeeze(0)?.to_vec1::<i64>()?;\n\n                    // Convert multi-dimensional indices to flat index\n                    let mut flat_idx: usize = 0;\n                    for (dim, &idx) in indices_vec.iter().enumerate() {\n                        let dim_size = data_shape[dim] as i64;\n                        let norm_idx = if idx < 0 { dim_size + idx } else { idx };\n\n                        if norm_idx < 0 || norm_idx >= dim_size {\n                            bail!(\n                                \"Index {} out of bounds for dimension {} with size {}\",\n                                idx,\n                                dim,\n                                dim_size\n                            );\n                        }\n\n                        flat_idx += (norm_idx as usize) * strides[dim];\n                    }\n\n                    // Extract current update\n                    let update_slice = if update_element_shape.is_empty() {\n                        flat_updates.narrow(0, i, 1)?.squeeze(0)?\n                    } else {\n                        flat_updates.narrow(0, i, 1)?\n                    };\n\n                    match reduction {","sourceCodeStart":2498,"sourceCodeEnd":2534,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L2498-L2534","documentation":"During ScatterGD/GatherND-style indexed evaluation, candle-onnx converts each per-dimension index to a flat offset and validates it against the data shape. Negative indices are normalized (dim_size + idx), but if the resulting index is still outside [0, dim_size) this error is thrown, naming the offending index, dimension, and size.","triggerScenarios":"Evaluating a ScatterND node whose indices contain a value >= dim_size or < -dim_size for any dimension of the data tensor, e.g. index 10 into a dimension of size 8 or index -9 into size 8.","commonSituations":"Out-of-range indices generated by upstream ops (argmax on wrong axis, wrong padding values); integer overflow or bad position encodings in exported models.","solutions":["Clamp the indices before the op (e.g. via graph surgery or upstream clip node)","Fix upstream ops producing out-of-range indices","Validate indices against data shape in a preprocessing step before running the graph"],"exampleFix":"# before: idx = 10, dim size 8\n# after (preprocess)\nindices = np.clip(indices, -dim_size, dim_size - 1)","handlingStrategy":"validation","validationCode":"for (dim, &idx) in indices.iter().enumerate() {\n    let size = data_shape[dim] as i64;\n    let n = if idx < 0 { size + idx } else { idx };\n    if n < 0 || n >= size {\n        return Err(format!(\"index {} out of bounds for dim {} (size {})\", idx, dim, size));\n    }\n}","typeGuard":"fn indices_in_bounds(indices: &[i64], shape: &[usize]) -> bool {\n    indices.iter().zip(shape).all(|(&i, &s)| (i as i64) >= -(s as i64) && (i as i64) < s as i64)\n}","tryCatchPattern":"match eval(...) {\n    Err(e) if e.contains(\"out of bounds for dimension\") => clamp_indices_and_retry(),\n    other => other,\n}","preventionTips":["Clamp or validate upstream index-producing ops","Check for negative-index handling assumptions in exports","Test models with boundary indices before deployment"],"tags":["onnx","scatternd","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}