1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! PyTorch's `DataLoader` model — dataset, sampler, collator, loader — as a typed Rust library.
//!
//! A [`DataLoader`] owns a [`Dataset`], a [`Sampler`] and a [`Collator`]; each
//! [`iter`](DataLoader::iter) runs one epoch and yields `Result<Batch>` in epoch order, loading
//! `num_workers` batches in parallel on the loader's thread pool, with `items_in_parallel` items of
//! each batch fetched in parallel.
//!
//! ```rust
//! use dataloader_rs::{DataLoader, Dataset, RandomSampler, error::Result};
//!
//! struct Squares(usize);
//!
//! impl Dataset for Squares {
//! type Item = u64;
//! fn get(&self, index: usize) -> Result<u64> {
//! Ok((index as u64).pow(2))
//! }
//! fn len(&self) -> usize {
//! self.0
//! }
//! }
//!
//! let mut loader = DataLoader::builder(Squares(1000))
//! .batch_size(32)
//! .sampler(RandomSampler::new(42))
//! .num_workers(4)
//! .prefetch_depth(8)
//! .drop_last(true)
//! .build();
//!
//! for batch in &mut loader {
//! let batch: Vec<u64> = batch.unwrap();
//! assert_eq!(batch.len(), 32);
//! }
//! ```
//!
//! | Module | Content |
//! |---|---|
//! | [`dataset`] | the [`Dataset`] contract |
//! | [`sampler`] | index order: sequential, random, distributed, batching |
//! | [`collator`] | batch assembly: `Vec`, PyTorch-like `DefaultCollator`, pinned tensors |
//! | [`loader`] | [`DataLoader`], its builder and iterator, the worker threads |
//! | [`error`] | [`Error`] and [`Result`] |
//!
//! Features: `ndarray` and `torch-rs` add collation of `ndarray::Array` and `tch::Tensor`;
//! `python` builds the PyO3 module behind the `dataloader-rs` Python package.
//!
//! Design, threads and failure handling:
//! [ARCHITECTURE.md](https://github.com/AzHicham/dataloader-rs/blob/dev/ARCHITECTURE.md).
//! Benchmarks against PyTorch:
//! [BENCH.md](https://github.com/AzHicham/dataloader-rs/blob/dev/BENCH.md).
// The dataset and collator are shared with worker threads through `Arc`, so
// the crate needs no unsafe code at all.
// ── Top-level re-exports ──────────────────────────────────────────────────────
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;