Skip to main content

DataLoaderBuilder

Struct DataLoaderBuilder 

Source
pub struct DataLoaderBuilder<Src, S, C, T = Identity> { /* private fields */ }
Expand description

Builder for DataLoader.

Implementations§

Source§

impl<Src, S, C, T> DataLoaderBuilder<Src, S, C, T>

Source

pub fn batch_size(self, n: usize) -> Self

Number of items per batch. Default: 1.

Source

pub fn prefetch_depth(self, n: usize) -> Self

Number of batches workers may run ahead of the consumer. At most num_workers + prefetch_depth batches are in flight or buffered. Default: 2.

Source

pub fn drop_last(self, v: bool) -> Self

Whether to drop a non-full final batch. Default: false.

Source

pub fn num_workers(self, n: usize) -> Self

Number of batches processed in parallel — PyTorch’s num_workers.

Batches run on the loader’s thread pool, built once and reused by every epoch. 0 (default) processes batches directly in Iterator::next.

Source

pub fn item_concurrency(self, n: usize) -> Self

Number of items of one batch fetched in parallel. Default: 1.

With 1, a batch is fetched with one Dataset::get_batch call. With n > 1, each batch is fetched by n concurrent lanes, each calling Dataset::get for the batch’s next item, so at most n items of a batch are in flight. Useful for I/O-bound datasets (network, object storage) and for few, large CPU-bound batches.

The loader’s pool has max(num_workers, 1) × item_concurrency threads; sizing it for the machine is the caller’s choice.

Source

pub fn items_in_parallel(self, n: usize) -> Self

👎Deprecated since 0.3.0:

renamed to item_concurrency

Former name of item_concurrency.

Source

pub fn sampler<S2: Sampler>( self, sampler: S2, ) -> DataLoaderBuilder<Src, S2, C, T>

Replace the sampler and preserve all other settings.

Source

pub fn transform<T2>(self, transform: T2) -> DataLoaderBuilder<Src, S, C, T2>
where Src: LoaderSource, T2: Transform<Src::Item>,

Apply transform to every item, with its index, before collation; it replaces any previous one (chain them with Transform::then). Default: Identity, at no cost.

It runs where the item is loaded: right after get / get_batch on the worker for a Dataset (in each item lane when item_concurrency is above 1, so items are transformed in parallel), or on the loader’s pool once the item is fetched for an AsyncDataset, so the async runtime never does CPU work. The collator receives the transformed items. An error fails the item’s batch; a panic is re-raised by the iterator.

use dataloader_rs::{DataLoader, Dataset, error::Result};

struct Numbers;

impl Dataset for Numbers {
    type Item = u64;
    fn get(&self, index: usize) -> Result<u64> { Ok(index as u64) }
    fn len(&self) -> usize { 4 }
}

let mut loader = DataLoader::builder(Numbers)
    .batch_size(2)
    .transform(|index: usize, x: u64| -> Result<String> { Ok(format!("{index}:{}", x * 10)) })
    .build();
let batches: Vec<Vec<String>> = loader.iter().map(|b| b.unwrap()).collect();
assert_eq!(batches, [["0:0", "1:10"], ["2:20", "3:30"]]);
Source

pub fn collator<C2>(self, collator: C2) -> DataLoaderBuilder<Src, S, C2, T>

Replace the collator and preserve all other settings.

Source

pub fn build(self) -> DataLoader<Src, S, C, T>
where Src: LoaderSource, S: Sampler, T: Transform<Src::Item>, C: Collator<T::Output>,

Finalize configuration and construct a DataLoader.

Auto Trait Implementations§

§

impl<Src, S, C, T> Forget for DataLoaderBuilder<Src, S, C, T>

§

impl<Src, S, C, T> Freeze for DataLoaderBuilder<Src, S, C, T>
where Src: Freeze, S: Freeze, T: Freeze, C: Freeze,

§

impl<Src, S, C, T> RefUnwindSafe for DataLoaderBuilder<Src, S, C, T>

§

impl<Src, S, C, T> Send for DataLoaderBuilder<Src, S, C, T>
where Src: Send, S: Send, T: Send, C: Send,

§

impl<Src, S, C, T> Sync for DataLoaderBuilder<Src, S, C, T>
where Src: Sync, S: Sync, T: Sync, C: Sync,

§

impl<Src, S, C, T> Unpin for DataLoaderBuilder<Src, S, C, T>
where Src: Unpin, S: Unpin, T: Unpin, C: Unpin,

§

impl<Src, S, C, T> UnsafeUnpin for DataLoaderBuilder<Src, S, C, T>

§

impl<Src, S, C, T> UnwindSafe for DataLoaderBuilder<Src, S, C, T>
where Src: UnwindSafe, S: UnwindSafe, T: UnwindSafe, C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.