tracel-ai/burn · error
Index out of bounds for wrapped dataset size: {idx} >= {size
Error message
Index out of bounds for wrapped dataset size: {idx} >= {size} What it means
`SelectionDataset::from_indices_checked` validates that every index used to select items from the wrapped dataset is within bounds (`idx < dataset.len()`) and panics otherwise, before building the selection dataset via `from_indices_unchecked`. It exists so invalid index lists fail immediately at construction rather than later at `get`.
Source
Thrown at crates/burn-dataset/src/transform/selection.rs:83
///
/// # Arguments
///
/// * `dataset` - The original dataset to select from.
/// * `indices` - A slice of indices to select from the dataset.
/// These indices must be within the bounds of the dataset.
///
/// # Panics
///
/// Panics if any index is out of bounds for the dataset.
pub fn from_indices_checked<S>(dataset: S, indices: Vec<usize>) -> Self
where
S: Into<Arc<D>>,
{
let dataset = dataset.into();
let size = dataset.len();
if let Some(idx) = indices.iter().find(|&i| *i >= size) {
panic!("Index out of bounds for wrapped dataset size: {idx} >= {size}");
}
Self::from_indices_unchecked(dataset, indices)
}
/// Creates a new selection dataset with the given dataset and indices without checking bounds.
///
/// # Arguments
///
/// * `dataset` - The original dataset to select from.
/// * `indices` - A vector of indices to select from the dataset.
///
/// # Safety
///
/// This function does not check if the indices are within the bounds of the dataset.
pub fn from_indices_unchecked<S>(dataset: S, indices: Vec<usize>) -> Self
where
S: Into<Arc<D>>,View on GitHub (pinned to d16f7ba2ed)
Solutions
- Generate indices with `0..dataset.len()` (e.g. `(0..len).choose(&mut rng)` or `rand::seq::index::sample`).
- Filter the index list before constructing: `indices.into_iter().filter(|i| *i < len).collect()`.
- If indices are intentionally untrusted/out-of-range, use `from_indices_unchecked` only if you also handle skipping at get-time — otherwise fix the source of the indices.
- Assert the wrapped dataset is the same one the indices were derived from (same len).
Example fix
// before let indices: Vec<usize> = (0..1000).map(|_| rng.random_range(0..=len)).collect(); // can equal len -> panic let selection = SelectionDataset::from_indices_checked(dataset, indices); // after let indices: Vec<usize> = rand::seq::index::sample(&mut rng, len, 1000).into_iter().collect(); let selection = SelectionDataset::from_indices_checked(dataset, indices);
Defensive patterns
Strategy: validation
Validate before calling
let size = dataset.len();
assert!(indices.iter().all(|&i| i < size), "all selection indices must be < {size}");
let selection = SelectionDataset::from_indices_checked(dataset, indices); Prevention
- Sample indices with rand::seq (sample/choose) over 0..len instead of ad-hoc random_range with inclusive bounds
- Regenerate index lists whenever the wrapped dataset changes size
- Filter indices (`retain(|i| *i < len)`) when constructing selections from untrusted input
- Keep index lists paired with the dataset they were derived from (store len alongside)
When it happens
Trigger: Calling `SelectionDataset::from_indices_checked(dataset, indices)` where any element of `indices >= dataset.len()`; e.g. building a random selection with an RNG seeded over the wrong range, or selecting from a shrunken/replaced wrapped dataset using old index lists.
Common situations: Generating indices with `rand::random::<usize>()` unbounded; sampling k indices from `0..=len` (inclusive off-by-one); reusing indices computed for the full dataset against a filtered/shorter version; cross-split index reuse.
Related errors
- Index out of bounds for InMemDataset: {index} >= {}
- Index out of bounds for ComposedDataset: {index} >= {}
- Index out of bounds for SelectionDataset: {index} >= {}
- Index out of bounds for SelectionDataset: {i} >= {}
- Index out of bounds for WindowsDataset: {index} >= {}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/a5641383f0413fd6.
Report an issue: GitHub.