{"record":{"id":"61c41f147535aced","repo":"tracel-ai/burn","slug":"unsupported-dimension-only-the-last-dimension-can","errorCode":null,"errorMessage":"Unsupported dimension, only the last dimension can differ: Tensor {:?} Index {:?}","messagePattern":"Unsupported dimension, only the last dimension can differ: Tensor (.+?) Index (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/base.rs","lineNumber":456,"sourceCode":"            let out_offset = n * slice_size;\n            output_vec[out_offset..(out_offset + slice_size)]\n                .copy_from_slice(&data_flat[base_offset..(base_offset + slice_size)]);\n        }\n\n        let out_shape = Shape::from(out_shape_vec);\n        let output = ArrayD::from_shape_vec(out_shape.as_slice(), output_vec)\n            .expect(\"gather_nd: shape mismatch\");\n\n        output.into_shared()\n    }\n\n    fn gather_batch_size(shape_tensor: &[usize], shape_indices: &[usize]) -> usize {\n        let ndims = shape_tensor.num_dims();\n        let mut batch_size = 1;\n\n        for i in 0..ndims - 1 {\n            if shape_tensor[i] != shape_indices[i] {\n                panic!(\n                    \"Unsupported dimension, only the last dimension can differ: Tensor {:?} Index \\\n                     {:?}\",\n                    shape_tensor, shape_indices\n                );\n            }\n            batch_size *= shape_indices[i];\n        }\n\n        batch_size\n    }\n\n    pub fn reshape(tensor: SharedArray<E>, shape: Shape) -> SharedArray<E> {\n        reshape!(\n            ty E,\n            shape shape,\n            array tensor,\n            d shape.num_dims()\n        )","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/base.rs#L438-L474","documentation":"gather (and scatter batch computation) allows only the last dimension of the index tensor to differ from the tensor's shape; all leading (batch) dimensions must match. gather_batch_size panics when any leading dimension of shape_tensor differs from the corresponding dimension of shape_indices.","triggerScenarios":"Calling Tensor::gather with an indices tensor whose leading dimensions don't match the input tensor, e.g. tensor of shape [2, 3, 4] gathered with indices of shape [5, 3, 2].","commonSituations":"Batch size mismatch between data and indices (wrong batch slice, dropped/added batch dimension); passing 2D indices for a 3D tensor; porting numpy fancy-indexing code that supports arbitrary shapes.","solutions":["Make all leading dimensions of the indices tensor equal to the input tensor's leading dimensions.","Reshape or slice the indices so only the last dimension differs from the tensor shape.","Adjust the batch handling code so indices are produced per-batch with the same batch size.","Print tensor.dims() and indices.dims() and align them before calling gather."],"exampleFix":"// before\nlet tensor = Tensor::zeros([2, 3, 4], &device);\nlet indices = Tensor::zeros([8, 3, 2], &device); // wrong leading dim\ntensor.gather(2, indices);\n// after\nlet indices = Tensor::zeros([2, 3, 2], &device); // leading dims match\ntensor.gather(2, indices);","handlingStrategy":"validation","validationCode":"let (td, id) = (tensor.dims(), indices.dims());\nassert_eq!(td.len(), id.len());\nassert!(td[..td.len()-1] == id[..id.len()-1], \"gather: leading dims must match\");","typeGuard":"fn gather_ok(t: &[usize], i: &[usize]) -> bool {\n    t.len() == i.len() && t[..t.len()-1] == i[..i.len()-1]\n}","tryCatchPattern":null,"preventionTips":["Keep batch dimensions identical between data and indices","Only vary the last (index) dimension","Print dims for both tensors when debugging gather"],"tags":["rust","burn","shape-mismatch","gather"],"backgroundTag":"shape-mismatch","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}