DataParallel

Struct DataParallel 

Source
pub struct DataParallel<M> {
    pub module: M,
    pub num_workers: usize,
}
Expand description

Wraps a Module and splits each input batch across num_workers threads.

The forward pass:

  1. Split input along dimension 0 into num_workers chunks
  2. Run each chunk through the module in parallel (rayon)
  3. Concatenate the outputs

Because all workers share the same parameters (Tensor uses Arc), the autograd graph correctly tracks all operations. After computing loss on the concatenated output and calling .backward(), the gradients are automatically accumulated across all chunks.

§Example

let model = Sequential::new(vec![...]);
let dp = DataParallel::new(model, 4);  // 4 workers
let output = dp.forward(&big_batch)?;  // splits into 4 chunks

Fields§

§module: M

The underlying module (shared across workers).

§num_workers: usize

Number of parallel workers.

Implementations§

Source§

impl<M> DataParallel<M>

Source

pub fn new(module: M, num_workers: usize) -> Self

Create a DataParallel wrapper with the given number of workers.

num_workers controls how many chunks the batch is split into. For CPU, this maps to rayon thread-pool parallelism.

Source

pub fn inner(&self) -> &M

Get a reference to the underlying module.

Source

pub fn inner_mut(&mut self) -> &mut M

Get a mutable reference to the underlying module.

Source

pub fn into_inner(self) -> M

Unwrap the DataParallel, returning the inner module.

Trait Implementations§

Source§

impl<M: Clone> Clone for DataParallel<M>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug> Debug for DataParallel<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M, B> Module<B> for DataParallel<M>
where M: Module<B> + Send + Sync, B: Backend,

Source§

fn forward(&self, x: &Tensor<B>) -> Result<Tensor<B>>

Compute the output tensor from the input tensor. This defines the layer’s computation (forward pass).
Source§

fn parameters(&self) -> Vec<Tensor<B>>

Return all trainable parameters of this module. The optimizer uses these to update weights during training.
Source§

fn named_parameters(&self) -> Vec<(String, Tensor<B>)>

Return all trainable parameters with human-readable names. Read more
Source§

fn set_training(&self, training: bool)

Set training or evaluation mode. Read more
Source§

fn is_training(&self) -> bool

Whether the module is in training mode (default: true).
Source§

fn train(&self)

Convenience: set training mode.
Source§

fn eval(&self)

Convenience: set evaluation mode.
Source§

fn num_parameters(&self) -> usize

Total number of scalar parameters in this module.
Source§

fn trainable_params_count(&self) -> usize

Number of trainable (variable) parameters.
Source§

fn frozen_parameters(&self) -> Vec<Tensor<B>>

Freeze all parameters: returns new parameter tensors with is_variable = false, preventing gradient accumulation. Read more
Source§

fn state_dict(&self) -> Vec<(String, Tensor<B>)>

Returns a state_dict-style map of parameter name → tensor. Read more

Auto Trait Implementations§

§

impl<M> Freeze for DataParallel<M>
where M: Freeze,

§

impl<M> RefUnwindSafe for DataParallel<M>
where M: RefUnwindSafe,

§

impl<M> Send for DataParallel<M>
where M: Send,

§

impl<M> Sync for DataParallel<M>
where M: Sync,

§

impl<M> Unpin for DataParallel<M>
where M: Unpin,

§

impl<M> UnwindSafe for DataParallel<M>
where M: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V