Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Prefetching feature #17

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rand = "0.8.5"
tch = { version = "0.13.0", optional = true, features = ["download-libtorch"] }
rayon = { version = "1.7.0", optional = true }
once_cell = { version = "1.17.1", optional = true }
crossbeam-channel = "0.5.8"


[dev-dependencies]
Expand Down
9 changes: 9 additions & 0 deletions examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ fn main() {
);
}

let dataset = FaceLandmarksDataset::new(
"examples/image/dataset/face_landmarks.csv",
env::current_dir().unwrap().join("examples/image/dataset/"),
);
let loader = DataLoader::builder(dataset)
.batch_size(4)
.collate_fn(TorchCollate)
.build();

loader
.into_iter()
.enumerate()
Expand Down
15 changes: 14 additions & 1 deletion src/indexable/dataloader/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
sampler::{BatchSampler, RandomSampler, Sampler, SequentialSampler},
Dataset,
};
use std::cmp::max;

#[cfg(feature = "rayon")]
use crate::THREAD_POOL;
Expand All @@ -29,6 +30,8 @@ where
#[cfg(feature = "rayon")]
/// Number of threads to use.
num_threads: usize,
/// Prefetch buffer size.
prefetch_size: usize,
}

// FIXME: kind of strange that we require DefaultCollatte even if in the end we may won't use it
Expand Down Expand Up @@ -56,6 +59,7 @@ where
collate_fn: DefaultCollate,
#[cfg(feature = "rayon")]
num_threads,
prefetch_size: 0,
}
}
}
Expand All @@ -72,7 +76,7 @@ where
}
/// Set the number of elements in a batch.
pub fn batch_size(mut self, batch_size: usize) -> Self {
self.batch_sampler.batch_size = batch_size;
self.batch_sampler.batch_size = max(batch_size, 1);
self
}

Expand All @@ -83,6 +87,12 @@ where
self
}

/// Set the size of the prefetch buffer.
pub fn prefetch_size(mut self, prefetch_size: usize) -> Self {
self.prefetch_size = prefetch_size;
self
}

/// Drop the lasts element if they don't feat into a batch. For instance if a dataset have 13
/// samples and a `batch_size` of 5, the last 3 samples will be dropped.
pub fn drop_last(mut self) -> Self {
Expand All @@ -102,6 +112,7 @@ where
collate_fn,
#[cfg(feature = "rayon")]
num_threads: self.num_threads,
prefetch_size: self.prefetch_size,
}
}

Expand All @@ -122,6 +133,7 @@ where
collate_fn: self.collate_fn,
#[cfg(feature = "rayon")]
num_threads: self.num_threads,
prefetch_size: self.prefetch_size,
}
}
/// Create a `Dataloader` from a [`Builder`].
Expand Down Expand Up @@ -156,6 +168,7 @@ where
dataset: self.dataset,
batch_sampler: self.batch_sampler,
collate_fn: self.collate_fn,
prefetch_size: self.prefetch_size,
}
}
}
Expand Down
Loading