tracel-ai/burn · error

Index out of bounds for SelectionDataset: {i} >= {}

Error message

Index out of bounds for SelectionDataset: {i} >= {}

What it means

`SelectionDataset::get_many` maps each requested position through `self.indices.get(i)`; any position missing from the indices vector triggers a panic with the position and indices length. Same family as `get` (error 97) but for batched retrieval, so one bad index in a batch aborts the whole call.

Source

Thrown at crates/burn-dataset/src/transform/selection.rs:244

    D: Dataset<I>,
    I: Clone + Send + Sync,
{
    fn get(&self, index: usize) -> Result<I, DatasetError> {
        let Some(&index) = self.indices.get(index) else {
            panic!(
                "Index out of bounds for SelectionDataset: {index} >= {}",
                self.indices.len()
            );
        };
        self.wrapped.get(index)
    }

    fn get_many(&self, indexes: Vec<usize>) -> Result<Vec<I>, DatasetError> {
        let translated: Vec<usize> = indexes
            .into_iter()
            .map(|i| {
                self.indices.get(i).copied().unwrap_or_else(|| {
                    panic!(
                        "Index out of bounds for SelectionDataset: {i} >= {}",
                        self.indices.len()
                    )
                })
            })
            .collect();
        self.wrapped.get_many(translated)
    }

    fn len(&self) -> usize {
        self.indices.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::FakeDataset;

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Clamp/filter the requested positions: `indexes.into_iter().filter(|i| *i < selection.len()).collect()` before calling.
  2. Compute batch boundaries from `selection.len()` and truncate the final batch (`min(batch_size, len - start)`).
  3. Ensure the sampler/batcher was built with the selection dataset (its len), not the wrapped one.
  4. Alternatively catch mis sized batches upstream by iterating via `Dataset::batcher`/iter helpers.

Example fix

// before
let items = selection.get_many((0..batch_size).map(|i| start + i).collect()).unwrap(); // may exceed len

// after
let end = (start + batch_size).min(selection.len());
let items = selection.get_many((start..end).collect())?;
Defensive patterns

Strategy: validation

Validate before calling

let positions: Vec<usize> = requested.into_iter().filter(|&i| i < selection.len()).collect();
let items = selection.get_many(positions)?;

Prevention

When it happens

Trigger: Calling `selection.get_many(vec![...])` where any element `>= selection.len()`; batching with a fixed batch size over `total` computed from the wrapped dataset; a sampler producing positions from the wrong population size.

Common situations: DataLoader with batch_size that doesn't divide selection length and code appending full-size batches; shuffled index lists sampled from the wrong range; mixing `get_many` positions between two different selections.

Related errors


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