tracel-ai/burn · error

read_indices: unsupported index dtype {:?}

Error message

read_indices: unsupported index dtype {:?}

What it means

read_indices supports only I64, I32, U64, U32, U16, and U8 index tensors. Any other dtype (floats, bool) passed as the indices argument makes it panic with 'unsupported index dtype'. The library requires index tensors to be of integer dtype because they address elements.

Source

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

            tensor
                .storage::<u32>()
                .iter()
                .map(|&v| {
                    isize::try_from(v).unwrap_or_else(|_| {
                        panic!("read_indices: u32 index {v} out of isize range")
                    })
                })
                .collect(),
        ),
        DType::U16 => Cow::Owned(
            tensor
                .storage::<u16>()
                .iter()
                .map(|&v| v as isize)
                .collect(),
        ),
        DType::U8 => Cow::Owned(tensor.storage::<u8>().iter().map(|&v| v as isize).collect()),
        other => panic!("read_indices: unsupported index dtype {:?}", other),
    }
}

#[cold]
#[inline(never)]
fn index_oob(raw: isize, dim_size: usize) -> ! {
    panic!("index {raw} out of bounds for dimension of size {dim_size}");
}

/// Validate an index is non-negative and within bounds, panicking with a clear message otherwise.
#[inline(always)]
fn checked_index(raw: isize, dim_size: usize) -> usize {
    if raw < 0 || raw as usize >= dim_size {
        index_oob(raw, dim_size);
    }
    raw as usize
}

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast the indices tensor to an integer dtype: indices.cast::<i64>() (or i32/u32)
  2. If indices come from float math, use floor/round then cast to an int dtype
  3. Double-check argument order — the second tensor to gather/scatter must be the indices
  4. Ensure argmax/top-k outputs (already int dtype) are not cast to floats in between

Example fix

// before
let idx = positions.cast::<f32>();
let out = tensor.gather(0, idx); // panic: unsupported index dtype F32
// after
let out = tensor.gather(0, positions.cast::<i64>());
Defensive patterns

Strategy: type-guard

Validate before calling

// before passing indices to gather/scatter
match indices.dtype() {
    DType::I64 | DType::I32 | DType::U64 | DType::U32 | DType::U16 | DType::U8 => {},
    other => panic!("indices must be an integer dtype, got {:?}", other),
}

Type guard

fn is_valid_index_dtype(d: DType) -> bool {
    matches!(d, DType::I64 | DType::I32 | DType::U64 | DType::U32 | DType::U16 | DType::U8)
}

Prevention

When it happens

Trigger: Passing a float (F32/F64/F16/BF16) or bool tensor as the indices argument to gather, scatter_update, select, select_update, scatter_nd, or gather_nd.

Common situations: Porting NumPy/PyTorch code where indices are floats (e.g. argmax results cast implicitly, or `indices.round()` returning a float tensor), forgetting to cast after arithmetic, or API confusion between a values tensor and the indices tensor argument order.

Related errors


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