{"record":{"id":"69cb286dd5222c4c","repo":"huggingface/candle","slug":"scatternd-expects-k-indices-shape-1-to-be-at-m","errorCode":null,"errorMessage":"ScatterND expects k (indices.shape[-1]) to be at most the rank of data","messagePattern":"ScatterND expects k \\(indices\\.shape\\[-1\\]\\) to be at most the rank of data","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":2441,"sourceCode":"            \"ScatterND\" => {\n                let data = get(&node.input[0])?;\n\n                let indices = get(&node.input[1])?;\n                let indices = indices.to_dtype(DType::I64)?;\n\n                let updates = get(&node.input[2])?;\n\n                let reduction = get_attr_opt::<str>(node, \"reduction\")?.unwrap_or(\"none\");\n\n                let indices_shape = indices.dims();\n                let data_shape = data.dims();\n                let _updates_shape = updates.dims();\n\n                // Last dimension of indices represents the depth of indexing\n                let k = indices_shape.last().unwrap().clone();\n\n                if k > data.rank() {\n                    bail!(\"ScatterND expects k (indices.shape[-1]) to be at most the rank of data\");\n                }\n\n                let num_updates = indices_shape[..indices_shape.len() - 1]\n                    .iter()\n                    .product::<usize>();\n\n                let flat_indices = if indices.rank() == 1 && k == 1 {\n                    indices.unsqueeze(0)?\n                } else {\n                    indices.reshape((num_updates, k))?\n                };\n\n                // Calculate the shape of each update element\n                let update_element_shape = if k < data_shape.len() {\n                    data_shape[k..].to_vec()\n                } else {\n                    vec![]\n                };","sourceCodeStart":2423,"sourceCodeEnd":2459,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L2423-L2459","documentation":"In ScatterND, the last dimension k of the `indices` tensor determines the depth of indexing (number of data dimensions being sliced). ONNX requires k <= rank(data). candle-onnx checks this upfront because indices deeper than the data rank are meaningless and cannot be processed.","triggerScenarios":"Evaluating a ScatterND node where indices.shape[-1] exceeds data.rank(), e.g. 3-D indices into a 2-D tensor, often caused by mismatched indices/data produced upstream in the graph.","commonSituations":"Models where indices were built for a different data rank than actually flows into ScatterND; exporter bugs; hand-edited graphs after removing a dimension.","solutions":["Correct the indices tensor so its last dim equals (or is below) the data rank","Fix upstream reshape/expand ops producing the wrong indices shape","Re-export the model and compare ScatterND node inputs with netron"],"exampleFix":"# before: data rank 2, indices shape [4, 3]\n# after\nindices = indices[..., :2]  # slice trailing dim to match data rank","handlingStrategy":"validation","validationCode":"let k = indices.dims()[indices.rank() - 1];\nif k > data.rank() {\n    return Err(format!(\"ScatterND k={} > data rank {}\", k, data.rank()));\n}","typeGuard":"fn scatternd_indices_valid(indices: &Tensor, data: &Tensor) -> bool {\n    indices.rank() > 0 && indices.dims()[indices.rank() - 1] <= data.rank()\n}","tryCatchPattern":"match eval(...) {\n    Err(e) if e.contains(\"ScatterND expects k\") => eprintln!(\"indices depth exceeds data rank\"),\n    other => other,\n}","preventionTips":["Verify indices.shape[-1] equals the intended indexing depth","Run onnx.shape_inference on the model before inference","Compare ScatterND inputs in netron after graph edits"],"tags":["onnx","scatternd","shape"],"backgroundTag":"invalid-tensor-shape","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}