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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! PyTorch's `DataLoader` model — dataset, sampler, collator, loader — as a typed Rust library.
//!
//! A [`DataLoader`] owns a [`Dataset`], a [`Sampler`], a [`Transform`] and a [`Collator`]: the
//! dataset only fetches items, the transform (none by default) turns each one into what the
//! collator batches. 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
//! `item_concurrency` 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: a source that fetches items |
//! | [`transform`] | [`Transform`]: per-item CPU work the loader applies, closures included |
//! | [`sampler`] | index order: sequential, random, distributed, batching |
//! | [`collator`] | batch assembly: `Vec`, PyTorch-like `DefaultCollator`, pinned tensors |
//! | `image` | image transforms: decode, color, crop, pad, resize, ... (feature `image`) |
//! | [`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`;
//! `async` adds `AsyncDataset` and `DataLoader::builder_async` (items fetched by futures on
//! tokio), with `FileDataset`; `http` adds `HttpDataset` and `object-store` adds
//! `ObjectStoreDataset` (in `dataset`); `image` adds the `image` module (pure-Rust decoders and
//! image transforms); `python` builds the PyO3 module behind the `dataloader-rs` Python package
//! and enables the four.
//!
//! 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 AsyncDataset;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;