Skip to main content

FSDP

Struct FSDP 

Source
pub struct FSDP<M: Module<T>, T: Float> { /* private fields */ }
Expand description

Fully Sharded Data Parallel module wrapper.

Wraps an inner Module and shards each parameter across ranks so that each rank only stores 1 / world_size of the full parameter tensor.

§Forward pass

Before calling the inner module’s forward(), FSDP all-gathers each shard to reconstruct the full parameter tensor and installs it into the module. The full-parameter tensors are stored in [full_params] so that backward can accumulate gradients on them.

§Gradient synchronization

After backward(), call [sync_gradients] to:

  1. Read gradients from the full-parameter tensors stored during forward.
  2. Reduce-scatter the full gradients so each rank gets only its shard portion of the gradient.
  3. Set each shard parameter’s gradient from the reduce-scattered result.

§Example

let mut fsdp = FSDP::new(model, backend)?;

loop {
    let output = fsdp.forward(&input)?;
    let loss = criterion.forward(&output, &target)?;
    ferrotorch_core::backward(&loss)?;
    fsdp.sync_gradients()?;
    optimizer.step()?;
    optimizer.zero_grad()?;
}

Implementations§

Source§

impl<M: Module<T>, T: Float> FSDP<M, T>

Source

pub fn new(module: M, backend: Arc<dyn Backend>) -> FerrotorchResult<Self>

Wrap a module for fully-sharded data-parallel training.

Each parameter is split evenly across world_size ranks. This rank keeps only its shard (the rank-th chunk). The original parameter shapes are recorded for reconstruction during forward.

§Panics

Panics if any parameter’s element count is not evenly divisible by world_size.

Source

pub fn module(&self) -> &M

Immutable access to the inner module.

Source

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

Mutable access to the inner module.

Source

pub fn into_inner(self) -> M

Consume the wrapper and return the inner module.

Source

pub fn backend(&self) -> &Arc<dyn Backend>

The backend used for communication.

Source

pub fn forward(&mut self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>>

Reconstruct full parameters from shards across all ranks and run the inner module’s forward pass.

The all-gathered full-parameter tensors are stored in self.full_params so their gradients can be read after backward.

Source

pub fn sync_gradients(&mut self) -> FerrotorchResult<()>

Reduce-scatter gradients from the full-parameter tensors stored during forward, then set each shard parameter’s gradient.

Call this after backward() and before optimizer.step().

§How it works
  1. For each parameter, read the gradient from the full-param tensor that was used during forward (stored in self.full_params).
  2. Reduce-scatter the full gradient across ranks (mean reduction) so each rank gets only its shard portion.
  3. Set the shard parameter’s .grad() to the reduce-scattered result.

Using reduce-scatter (not allreduce) is correct for FSDP because each rank only needs its own shard of the gradient to update its shard of the parameter.

Source

pub fn update_shards(&mut self, flat_data: &[T]) -> FerrotorchResult<()>

Update shard parameters from a flat data slice.

This is used by optimizers that produce a flat parameter buffer. The slice must have exactly the number of elements expected for this rank’s shards.

Auto Trait Implementations§

§

impl<M, T> Freeze for FSDP<M, T>
where M: Freeze,

§

impl<M, T> !RefUnwindSafe for FSDP<M, T>

§

impl<M, T> Send for FSDP<M, T>

§

impl<M, T> Sync for FSDP<M, T>

§

impl<M, T> Unpin for FSDP<M, T>
where M: Unpin, T: Unpin,

§

impl<M, T> UnsafeUnpin for FSDP<M, T>
where M: UnsafeUnpin,

§

impl<M, T> !UnwindSafe for FSDP<M, T>

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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

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

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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

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

Source§

fn vzip(self) -> V

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,