tracel-ai/burn · error
Index out of bounds for ComposedDataset: {index} >= {}
Error message
Index out of bounds for ComposedDataset: {index} >= {} What it means
`ComposedDataset::get` walks its child datasets, subtracting accumulated lengths, and panics if the requested `index` exceeds the total length of all composed datasets. Like the other burn datasets, out-of-range access is a panic rather than an `Err`.
Source
Thrown at crates/burn-dataset/src/transform/composed.rs:24
pub struct ComposedDataset<D> {
datasets: Vec<D>,
}
impl<D, I, E> Dataset<I, E> for ComposedDataset<D>
where
D: Dataset<I, E>,
I: Clone,
E: Error + Send + Sync + 'static,
{
fn get(&self, index: usize) -> Result<I, E> {
let mut current_index = 0;
for dataset in self.datasets.iter() {
if index < dataset.len() + current_index {
return dataset.get(index - current_index);
}
current_index += dataset.len();
}
panic!(
"Index out of bounds for ComposedDataset: {index} >= {}",
self.len()
);
}
fn len(&self) -> usize {
let mut total = 0;
for dataset in self.datasets.iter() {
total += dataset.len();
}
total
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::FakeDataset;
View on GitHub (pinned to d16f7ba2ed)
Solutions
- Guard with `if index < composed.len()` or iterate `0..composed.len()`.
- Recompute lengths from the composition itself rather than caching/splitting sizes externally.
- Verify each child dataset is non-empty and loaded correctly (log child `len()`s).
- Use `DataSplit`/index-based selection APIs that compute valid ranges across composed datasets.
Example fix
// before
let total = train.len(); // wrong: composed also includes val
let item = composed.get(i).unwrap();
// after
let total = composed.len();
if i < total {
let item = composed.get(i)?;
} Defensive patterns
Strategy: validation
Validate before calling
if index >= composed.len() {
return Err(DatasetError::InvalidArgument(format!("index {index} out of bounds for ComposedDataset (len={})", composed.len())));
} Prevention
- Take total length from `composed.len()`, never from cached or per-child sizes
- Check child datasets are non-empty after construction (log each len)
- Use half-open ranges `0..len` for iteration
- Prefer iterators over manual index math when composing datasets
When it happens
Trigger: Calling `composed.get(index)` with `index >= composed.len()`; composing N datasets but iterating with bounds from one of them or from stale metadata; empty composition (`ComposedDataset::new([])`) accessed at any index.
Common situations: Splits composed of train/val datasets where a downstream consumer assumes a single dataset length; batch loops computing bounds from a partial list of datasets; a child dataset became empty (bad file) so the total shrank.
Related errors
- Index out of bounds for InMemDataset: {index} >= {}
- Index out of bounds for wrapped dataset size: {idx} >= {size
- 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/1cbdab5f56d2a6bd.
Report an issue: GitHub.