tracel-ai/burn · error

read_indices: u64 index {v} out of isize range

Error message

read_indices: u64 index {v} out of isize range

What it means

Same guard as the I64 case but for U64 index tensors: read_indices converts each u64 to isize via isize::try_from and panics if the value exceeds isize::MAX. u64 values above isize::MAX can never be valid element indices, so the panic prevents silent wraparound to a negative/wrong index.

Source

Thrown at crates/burn-flex/src/ops/gather_scatter.rs:101

            const { assert!(size_of::<i32>() == size_of::<isize>()) };
            let data = tensor.storage::<i32>();
            Cow::Borrowed(bytemuck::cast_slice(data))
        }
        DType::I16 => Cow::Owned(
            tensor
                .storage::<i16>()
                .iter()
                .map(|&v| v as isize)
                .collect(),
        ),
        DType::I8 => Cow::Owned(tensor.storage::<i8>().iter().map(|&v| v as isize).collect()),
        DType::U64 => Cow::Owned(
            tensor
                .storage::<u64>()
                .iter()
                .map(|&v| {
                    isize::try_from(v).unwrap_or_else(|_| {
                        panic!("read_indices: u64 index {v} out of isize range")
                    })
                })
                .collect(),
        ),
        #[cfg(target_pointer_width = "64")]
        DType::U32 => Cow::Owned(
            tensor
                .storage::<u32>()
                .iter()
                .map(|&v| v as isize)
                .collect(),
        ),
        #[cfg(target_pointer_width = "32")]
        DType::U32 => Cow::Owned(
            tensor
                .storage::<u32>()
                .iter()
                .map(|&v| {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Validate or clamp the u64 index tensor to [0, dim_size) before the op
  2. Prefer u32/u16/u8 index dtypes on 32-bit targets, where the range naturally fits isize
  3. Audit where the indices are produced; huge u64 values almost always indicate upstream data corruption
  4. Run on a 64-bit target if indices genuinely need the full range

Example fix

// before: raw u64 indices straight from a decode step
let idx: Tensor<..., u64> = decoder.decode();
let out = tensor.gather(0, idx);
// after: clamp into valid range
let idx = decoder.decode().clamp(0u64, (dim_size - 1) as u64);
let out = tensor.gather(0, idx);
Defensive patterns

Strategy: validation

Validate before calling

// before calling gather/scatter with a u64 indices tensor
let max_idx = indices.max_val::<u64>();
assert!(max_idx <= isize::MAX as u64);
assert!(max_idx < dim_size as u64);

Type guard

fn fits_isize_u64(v: u64) -> bool { v <= isize::MAX as u64 }

Prevention

When it happens

Trigger: gather/scatter_update/select/select_update/scatter_nd/gather_nd called with a U64 indices tensor containing a value > isize::MAX (only feasible where isize is 32-bit, or with corrupted/garbage u64 data).

Common situations: Corrupted buffers, uninitialized u64 memory interpreted as indices, indices cast from floats with huge values, or 32-bit targets (wasm32) where u64 values above 2^31-1 cannot fit.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/c0dafe431b18bab5. Report an issue: GitHub.