tracel-ai/burn · error
window size must be non-zero
Error message
window size must be non-zero
What it means
Dataset::windows(size) converts the requested window size into a NonZeroUsize and panics when size is 0, because zero-sized overlapping windows are mathematically undefined. The library enforces this eagerly at iterator construction rather than producing an empty iterator.
Source
Thrown at crates/burn-dataset/src/transform/window.rs:56
/// Panics if `size` is 0.
///
/// # Examples
///
/// ```
/// use crate::burn_dataset::{
/// transform::{Windows, WindowsDataset},
/// Dataset, InMemDataset,
/// };
///
/// let items = [1, 2, 3, 4].to_vec();
/// let dataset = InMemDataset::new(items.clone());
///
/// for window in dataset.windows(2) {
/// // window is a Result<Vec<I>, DatasetError>
/// }
/// ```
fn windows(&self, size: usize) -> WindowsIterator<'_, I> {
let size = NonZeroUsize::new(size).expect("window size must be non-zero");
WindowsIterator::new(self, size)
}
}
/// Overlapping windows iterator.
pub struct WindowsIterator<'a, I> {
/// The size of the windows.
pub size: NonZeroUsize,
current: usize,
len: usize,
dataset: &'a dyn Dataset<I>,
}
impl<'a, I> WindowsIterator<'a, I> {
/// Creates a new `WindowsIterator` instance. The windows overlap.
/// Is empty if the input `Dataset` is shorter than `size`.
///
/// # ParametersView on GitHub (pinned to d16f7ba2ed)
Solutions
- Pass a window size >= 1; clamp with size.max(1) if the value is dynamic.
- Validate configuration before constructing the dataset.
- Use NonZeroUsize in your own config type so 0 is unrepresentable.
Example fix
// before let windows = dataset.windows(config.window_size); // after assert!(config.window_size > 0, "window_size must be > 0"); let windows = dataset.windows(config.window_size.max(1));
Defensive patterns
Strategy: validation
Validate before calling
fn safe_windows<'a, I: Clone>(ds: &'a dyn Dataset<I>, size: usize) -> WindowsIterator<'a, I> {
assert!(size >= 1, "window size must be >= 1, got {size}");
ds.windows(size)
} Type guard
fn is_valid_window_size(size: usize) -> bool { size > 0 } Prevention
- Validate window_size at config load time
- Use NonZeroUsize in your config types
- Clamp computed sizes with .max(1)
When it happens
Trigger: Calling dataset.windows(0) on any implementor of the Dataset trait.
Common situations: Window size read from CLI/config defaulting to 0 before a value is set; computing size as len - k where k >= len; off-by-one in parameter parsing.
Related errors
- Name is not set
- capture tensor operations must run inside CaptureDevice::cap
- Index out of bounds for InMemDataset: {index} >= {}
- The database file does not exist
- Index out of bounds for ComposedDataset: {index} >= {}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/640dd5a49b70c994.
Report an issue: GitHub.