---
title: FSDPStrategy
description: Strategy for Fully Sharded Data Parallel provided by torch.distributed.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.strategies.FSDPStrategy
---
# FSDPStrategy {#fsdpstrategy}

### `class lightning.pytorch.strategies.FSDPStrategy(accelerator=None, parallel_devices=None, cluster_environment=None, checkpoint_io=None, precision_plugin=None, process_group_backend=None, timeout=datetime.timedelta(seconds=1800), cpu_offload=None, mixed_precision=None, auto_wrap_policy=None, activation_checkpointing=None, activation_checkpointing_policy=None, sharding_strategy='FULL_SHARD', state_dict_type='full', device_mesh=None, **kwargs)` {#lightning.pytorch.strategies.FSDPStrategy}

Bases: [`ParallelStrategy`](/docs/pytorch/2.5.5/optional-api/api/lightning.pytorch.strategies.ParallelStrategy.md#lightning.pytorch.strategies.ParallelStrategy "lightning.pytorch.strategies.parallel.ParallelStrategy")

Strategy for Fully Sharded Data Parallel provided by torch.distributed.

Fully Sharded Training shards the entire model across all available GPUs, allowing you to scale model size, whilst using efficient communication to reduce overhead. In practice, this means we can remain at parity with PyTorch DDP, whilst scaling our model sizes dramatically. The technique is similar to ZeRO-Stage 3.

For more information check out [this blogpost](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api).

Defaults have been set and options have been exposed, but may require configuration based on your level of memory/speed efficiency. We suggest having a look at [this tutorial](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html) for more information.

Parameters:

*   **cpu\_offload** (`Union`\[[`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)"), [`CPUOffload`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.CPUOffload "(in PyTorch v2.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – See `cpu_offload` parameter in [`torch.distributed.fsdp.FullyShardedDataParallel`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel "(in PyTorch v2.15)").
    
*   **mixed\_precision** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`MixedPrecision`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.MixedPrecision "(in PyTorch v2.15)")\]) – See `mixed_precision` parameter in [`torch.distributed.fsdp.FullyShardedDataParallel`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel "(in PyTorch v2.15)").
    
*   **auto\_wrap\_policy** (`Union`\[[`set`](https://docs.python.org/3/builtins/stdtypes.html#set "(in Python v3.15)")\[[`type`](https://docs.python.org/3/builtins/functions.html#type "(in Python v3.15)")\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)")\]\], [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")\[\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)"), [`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)"), [`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\], [`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")\], `ModuleWrapPolicy`, [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Same as `auto_wrap_policy` parameter in [`torch.distributed.fsdp.FullyShardedDataParallel`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel "(in PyTorch v2.15)"). For convenience, this also accepts a set of the layer classes to wrap.
    
*   **activation\_checkpointing** (`Union`\[[`type`](https://docs.python.org/3/builtins/functions.html#type "(in Python v3.15)")\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)")\], [`list`](https://docs.python.org/3/builtins/stdtypes.html#list "(in Python v3.15)")\[[`type`](https://docs.python.org/3/builtins/functions.html#type "(in Python v3.15)")\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)")\]\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Deprecated. Use `activation_checkpointing_policy`.
    
*   **activation\_checkpointing\_policy** (`Union`\[[`set`](https://docs.python.org/3/builtins/stdtypes.html#set "(in Python v3.15)")\[[`type`](https://docs.python.org/3/builtins/functions.html#type "(in Python v3.15)")\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)")\]\], [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")\[\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)"), [`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)"), [`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\], [`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")\], `ModuleWrapPolicy`, [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Same as `auto_wrap_policy` parameter in [`torch.distributed.fsdp.FullyShardedDataParallel`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel "(in PyTorch v2.15)") but used when selecting the modules for which you want to enable activation checkpointing. Enabling this can free up a significant amount of memory at the cost of speed since activations in these layers need to be recomputed during backpropagation. For convenience, this also accepts a set of the layer classes to wrap.
    
*   **sharding\_strategy** (`Union`\[[`ShardingStrategy`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.ShardingStrategy "(in PyTorch v2.15)"), [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'FULL_SHARD'`, `'SHARD_GRAD_OP'`, `'NO_SHARD'`, `'HYBRID_SHARD'`\]\]) –
    
    Select whether to shard model parameters, gradients, optimizer states, or a combination of them. Available values are:
    
    *   `"FULL_SHARD"`: Shards model parameters, gradients, and optimizer states (default).
        
    *   `"SHARD_GRAD_OP"`: Shards gradients and optimizer states only. Model parameters get replicated.
        
    *   `"NO_SHARD"`: No sharding (identical to regular DDP).
        
    *   `"HYBRID_SHARD"`: Shards model parameters, gradients, and optimizer states within a single machine, but replicates across machines. See also the device\_mesh parameter below.
        
    
    Also accepts a [`torch.distributed.fsdp.ShardingStrategy`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.ShardingStrategy "(in PyTorch v2.15)") enum value.
    
*   **device\_mesh** (`Union`\[[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\], [`DeviceMesh`](https://docs.pytorch.org/docs/stable/distributed.html#torch.distributed.device_mesh.DeviceMesh "(in PyTorch v2.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – A tuple (replication size, sharding size) that defines over how many devices to shard and replicate the model. The product of the two numbers must equal the world size. Only valid in combination with the HYBRID\_SHARD sharding strategy.
    
*   **state\_dict\_type** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'full'`, `'sharded'`\]) –
    
    The format in which the state of the model and optimizers gets saved into the checkpoint.
    
    *   `"full"`: The full weights and optimizer states get assembled on rank 0 and saved to a single file.
        
    *   `"sharded"`: Each rank saves its shard of weights and optimizer states to a file. The checkpoint is a folder with as many files as the world size.
        
    
*   **\*\*kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – See available parameters in [`torch.distributed.fsdp.FullyShardedDataParallel`](https://docs.pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel "(in PyTorch v2.15)").
    

#### `barrier(name=None)` {#lightning.pytorch.strategies.FSDPStrategy.barrier}

Synchronizes all processes which blocks processes until the whole group enters this function.

Parameters:

**name** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – an optional name to pass into barrier.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `broadcast(obj, src=0)` {#lightning.pytorch.strategies.FSDPStrategy.broadcast}

Broadcasts an object to all processes.

Parameters:

*   **obj** ([`TypeVar`](https://docs.python.org/3/library/typing.html#typing.TypeVar "(in Python v3.15)")(`TBroadcast`)) – the object to broadcast
    
*   **src** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – source rank
    

Return type:

[`TypeVar`](https://docs.python.org/3/library/typing.html#typing.TypeVar "(in Python v3.15)")(`TBroadcast`)

#### `lightning_module_state_dict()` {#lightning.pytorch.strategies.FSDPStrategy.lightning_module_state_dict}

Returns model state.

Return type:

[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]

#### `model_sharded_context()` {#lightning.pytorch.strategies.FSDPStrategy.model_sharded_context}

Provide hook to create modules in a distributed aware context. This is useful for when we’d like to shard the model instantly, which is useful for extremely large models which can save memory and initialization time.

Returns: Model parallel context.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

#### `model_to_device()` {#lightning.pytorch.strategies.FSDPStrategy.model_to_device}

Moves the model to the correct device.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `optimizer_state(optimizer)` {#lightning.pytorch.strategies.FSDPStrategy.optimizer_state}

Returns state of an optimizer.

Allows for syncing/collating optimizer state from processes in custom strategies.

Return type:

[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]

#### `reduce(tensor, group=None, reduce_op='mean')` {#lightning.pytorch.strategies.FSDPStrategy.reduce}

Reduces a tensor from several distributed processes to one aggregated tensor.

Parameters:

*   **tensor** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]) – the tensor to sync and reduce
    
*   **group** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]) – the process group to gather results from. Defaults to all processes (world)
    
*   **reduce\_op** (`Union`\[[`ReduceOp`](https://docs.pytorch.org/docs/stable/distributed.html#torch.distributed.ReduceOp "(in PyTorch v2.15)"), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – the reduction operation. Defaults to ‘mean’/’avg’. Can also be a string ‘sum’ to calculate the sum during reduction.
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

reduced value, except when the input was not a tensor the output remains is unchanged

#### `save_checkpoint(checkpoint, filepath, storage_options=None)` {#lightning.pytorch.strategies.FSDPStrategy.save_checkpoint}

Save model/training states as a checkpoint file through state-dump and file-write.

Parameters:

*   **checkpoint** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]) – dict containing model and trainer state
    
*   **filepath** (`Union`\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path "(in Python v3.15)")\]) – write-target file’s path
    
*   **storage\_options** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]) – parameter for how to save to storage, passed to `CheckpointIO` plugin
    

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `setup(trainer)` {#lightning.pytorch.strategies.FSDPStrategy.setup}

Sets up the accelerator, plugins and initializes the optimizers (if needed).

Parameters:

**trainer** ([`Trainer`](/docs/pytorch/2.5.5/optional-api/api/lightning.pytorch.trainer.trainer.Trainer.md#lightning.pytorch.trainer.trainer.Trainer "lightning.pytorch.trainer.trainer.Trainer")) – the trainer instance

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `setup_environment()` {#lightning.pytorch.strategies.FSDPStrategy.setup_environment}

Setup any processes or distributed connections.

This is called before the LightningModule/DataModule setup hook which allows the user to access the accelerator environment before setup is complete.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `setup_optimizers(trainer)` {#lightning.pytorch.strategies.FSDPStrategy.setup_optimizers}

Creates optimizers and schedulers.

Parameters:

**trainer** ([`Trainer`](/docs/pytorch/2.5.5/optional-api/api/lightning.pytorch.trainer.trainer.Trainer.md#lightning.pytorch.trainer.trainer.Trainer "lightning.pytorch.trainer.trainer.Trainer")) – the Trainer, these optimizers should be connected to

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `teardown()` {#lightning.pytorch.strategies.FSDPStrategy.teardown}

This method is called to teardown the training process.

It is the right place to release memory and free other resources.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `tensor_init_context(empty_init=None)` {#lightning.pytorch.strategies.FSDPStrategy.tensor_init_context}

Controls how tensors get created (device, dtype).

Parameters:

**empty\_init** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")\]) – Whether to initialize the model with empty weights (uninitialized memory). If `None`, the strategy will decide. Some strategies may not support all options.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

#### `property lightning_restore_optimizer: bool` {#lightning.pytorch.strategies.FSDPStrategy.lightning_restore_optimizer}

Override to disable Lightning restoring optimizers/schedulers.

This is useful for strategies which manage restoring optimizers/schedulers.

#### `property restore_checkpoint_after_setup: bool` {#lightning.pytorch.strategies.FSDPStrategy.restore_checkpoint_after_setup}

Override to delay restoring from checkpoint till after the setup phase has completed. This is useful when the strategy requires all the setup hooks to run before loading checkpoint.

Returns:

If `True`, restore checkpoint after strategy setup.

#### `property root_device: device` {#lightning.pytorch.strategies.FSDPStrategy.root_device}

Return the root device.

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.5.5/optional-api/api.md)
