{"record":{"id":"b5d8b2f3e58f50cd","repo":"tracel-ai/burn","slug":"index-raw-out-of-bounds-for-dimension-of-size-d","errorCode":null,"errorMessage":"index {raw} out of bounds for dimension of size {dim_size}","messagePattern":"index (.+?) out of bounds for dimension of size (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/gather_scatter.rs","lineNumber":141,"sourceCode":"                })\n                .collect(),\n        ),\n        DType::U16 => Cow::Owned(\n            tensor\n                .storage::<u16>()\n                .iter()\n                .map(|&v| v as isize)\n                .collect(),\n        ),\n        DType::U8 => Cow::Owned(tensor.storage::<u8>().iter().map(|&v| v as isize).collect()),\n        other => panic!(\"read_indices: unsupported index dtype {:?}\", other),\n    }\n}\n\n#[cold]\n#[inline(never)]\nfn index_oob(raw: isize, dim_size: usize) -> ! {\n    panic!(\"index {raw} out of bounds for dimension of size {dim_size}\");\n}\n\n/// Validate an index is non-negative and within bounds, panicking with a clear message otherwise.\n#[inline(always)]\nfn checked_index(raw: isize, dim_size: usize) -> usize {\n    if raw < 0 || raw as usize >= dim_size {\n        index_oob(raw, dim_size);\n    }\n    raw as usize\n}\n\n/// Gather values from tensor along a dimension using indices.\n///\n/// For a 2D tensor with dim=1:\n/// output[i, j] = tensor[i, indices[i, j]]\n///\n/// The output has the same shape as indices.\npub fn gather<E: Element + Pod + Default + Copy + Send + Sync>(","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/gather_scatter.rs#L123-L159","documentation":"index_oob panics when an index read from the indices tensor is negative or >= the size of the dimension being indexed. checked_index routes every invalid index here so gather/scatter/select ops fail with a clear message instead of reading out-of-bounds memory.","triggerScenarios":"Any of gather, scatter_update, select, select_update, scatter_nd, gather_nd receiving an indices tensor containing a negative value or a value >= dim_size for the target dimension.","commonSituations":"Off-by-one errors when building index tensors, negative padding values used as indices, indices computed for a differently-shaped tensor, model weights/exported graphs referencing stale dimensions, or clamping forgotten after arithmetic.","solutions":["Validate/clamp indices to [0, dim_size) before the op (tensor.clamp or explicit bounds check)","Check that the indices tensor was built for the current tensor shape, not an older one","Replace padding sentinel values (-1) with a valid index or use a masked scatter","Log the failing raw index and dim_size from the panic message to find the producer of the bad index"],"exampleFix":"// before: -1 used as 'none' sentinel\nlet out = tensor.gather(0, idx); // panic: index -1 out of bounds for dimension of size 10\n// after: clamp sentinels into range and mask afterwards\nlet safe_idx = idx.clamp(0i64, (dim_size - 1) as i64);\nlet gathered = tensor.gather(0, safe_idx);\nlet out = gathered.mask_fill(idx.lower_equal(-1i64), 0f32);","handlingStrategy":"validation","validationCode":"// validate all indices are within [0, dim_size) before the op\nlet in_range = indices\n    .clone()\n    .greater_equal_elem(0i64)\n    .bool_and(indices.clone().lower_equal_elem((dim_size - 1) as i64));\nassert!(in_range.all().into_scalar());","typeGuard":"fn indices_in_range(idx: &Tensor<B, D, Int>, dim_size: usize) -> bool {\n    let min_ok = idx.clone().greater_equal_elem(0i64).all().into_scalar();\n    let max_ok = idx.clone().lower_equal_elem((dim_size - 1) as i64).all().into_scalar();\n    min_ok && max_ok\n}","tryCatchPattern":null,"preventionTips":["Clamp indices to [0, dim_size) and mask invalid entries afterwards","Never use negative sentinels directly as indices","Re-derive indices whenever tensor shapes change"],"tags":["panic","out-of-bounds","indexing","rust"],"backgroundTag":"index-out-of-bounds","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"}