Accelerate 文档
Fully Sharded Data Parallel 实用工具
并获得增强的文档体验
开始使用
Fully Sharded Data Parallel 实用工具
enable_fsdp_ram_efficient_loading
在环境中为 FSDP 启用 Hugging Face 模型的 RAM 高效加载。
disable_fsdp_ram_efficient_loading
在环境中为 FSDP 禁用 Hugging Face 模型的 RAM 高效加载。
merge_fsdp_weights
accelerate.utils.merge_fsdp_weights
< source >( checkpoint_dir: str output_path: str safe_serialization: bool = True remove_checkpoint_dir: bool = False )
将分片 FSDP 模型检查点中的权重合并为单个组合检查点。如果模型使用了 SHARDED_STATE_DICT
,则应使用此方法。如果 safe_serialization
为 True,则权重将保存到 {output_path}/model.safetensors
,否则保存到 pytorch_model.bin
。
注意:这是一个受 CPU 限制的过程。
FullyShardedDataParallelPlugin
class accelerate.FullyShardedDataParallelPlugin
< source >( fsdp_version: int = None sharding_strategy: typing.Union[str, ForwardRef('torch.distributed.fsdp.ShardingStrategy')] = None reshard_after_forward: typing.Union[str, ForwardRef('torch.distributed.fsdp.ShardingStrategy'), bool] = None backward_prefetch: typing.Union[str, ForwardRef('torch.distributed.fsdp.BackwardPrefetch'), NoneType] = None mixed_precision_policy: typing.Union[dict, ForwardRef('torch.distributed.fsdp.MixedPrecision'), ForwardRef('torch.distributed.fsdp.MixedPrecisionPolicy'), NoneType] = None auto_wrap_policy: typing.Union[typing.Callable, typing.Literal['transformer_based_wrap', 'size_based_wrap', 'no_wrap'], NoneType] = None cpu_offload: typing.Union[bool, ForwardRef('torch.distributed.fsdp.CPUOffload'), ForwardRef('torch.distributed.fsdp.CPUOffloadPolicy')] = None ignored_modules: typing.Optional[collections.abc.Iterable[torch.nn.modules.module.Module]] = None state_dict_type: typing.Union[str, ForwardRef('torch.distributed.fsdp.StateDictType')] = None state_dict_config: typing.Union[ForwardRef('torch.distributed.fsdp.FullStateDictConfig'), ForwardRef('torch.distributed.fsdp.ShardedStateDictConfig'), NoneType] = None optim_state_dict_config: typing.Union[ForwardRef('torch.distributed.fsdp.FullOptimStateDictConfig'), ForwardRef('torch.distributed.fsdp.ShardedOptimStateDictConfig'), NoneType] = None limit_all_gathers: bool = True use_orig_params: typing.Optional[bool] = None param_init_fn: typing.Optional[typing.Callable[[torch.nn.modules.module.Module], NoneType]] = None sync_module_states: typing.Optional[bool] = None forward_prefetch: bool = None activation_checkpointing: bool = None cpu_ram_efficient_loading: bool = None transformer_cls_names_to_wrap: typing.Optional[list[str]] = None min_num_params: typing.Optional[int] = None )
参数
- fsdp_version (
int
, 默认为1
) — 要使用的 FSDP 版本。默认为 1。如果设置为 2,则启动器希望配置转换为 FSDP2 格式。 - sharding_strategy (
Union[str, torch.distributed.fsdp.ShardingStrategy]
, 默认为'FULL_SHARD'
) — 要使用的分片策略。应为str
或torch.distributed.fsdp.fully_sharded_data_parallel.ShardingStrategy
的实例。已被弃用,推荐使用reshard_after_forward
。 - reshard_after_forward (
Union[str, torch.distributed.fsdp.ShardingStrategy, bool]
, 默认为fsdp_version=1
时为'FULL_SHARD'
,fsdp_version=2
时为True
) — 要使用的分片策略。如果fsdp_version
设置为 2,则应为 bool 值,否则应为str
或torch.distributed.fsdp.fully_sharded_data_parallel.ShardingStrategy
的实例。 - backward_prefetch (
Union[str, torch.distributed.fsdp.BackwardPrefetch]
, 默认为'NO_PREFETCH'
) — 要使用的反向预取策略。应为str
或torch.distributed.fsdp.fully_sharded_data_parallel.BackwardPrefetch
的实例。 - mixed_precision_policy (
Optional[Union[dict, torch.distributed.fsdp.MixedPrecision, torch.distributed.fsdp.MixedPrecisionPolicy]]
, defaults toNone
) — 用于启用 FullyShardedDataParallel 混合精度训练的配置。如果传入dict
,则应具有以下键:param_dtype
、reduce_dtype
和buffer_dtype
,如果fsdp_version
设置为 2,则可以是torch.distributed.fsdp.MixedPrecisionPolicy
的实例。 - auto_wrap_policy (
Optional(Union[Callable, Literal["transformer_based_wrap", "size_based_wrap", "no_wrap"]]), defaults to
NO_WRAP) -- 用于指定递归地使用 FSDP 包装层的策略的可调用对象或字符串。如果为字符串,则必须是
transformer_based_wrap、
size_based_wrap或
no_wrap之一。有关其外观的指导,请参阅
torch.distributed.fsdp.wrap.size_based_wrap_policy。 - cpu_offload (
Union[bool, torch.distributed.fsdp.CPUOffload, torch.distributed.fsdp.CPUOffloadPolicy]
, defaults toFalse
) — 是否将参数卸载到 CPU。应为bool
或torch.distributed.fsdp.fully_sharded_data_parallel.CPUOffload
或torch.distributed.fsdp.fully_sharded_data_parallel.CPUOffloadPolicy
的实例,如果fsdp_version
设置为 2。 - ignored_modules (
Optional[Iterable[torch.nn.Module]]
, defaults toNone
) — 使用 FSDP 包装时要忽略的模块列表。 - state_dict_type (
Union[str, torch.distributed.fsdp.StateDictType]
, defaults to'FULL_STATE_DICT'
) — 要使用的状态字典类型。如果为字符串,则必须是full_state_dict
、local_state_dict
或sharded_state_dict
之一。 - state_dict_config (
Optional[Union[torch.distributed.fsdp.FullStateDictConfig, torch.distributed.fsdp.ShardedStateDictConfig]
, defaults toNone
) — 要使用的状态字典配置。如果未传入,则根据state_dict_type
确定。 - optim_state_dict_config (
Optional[Union[torch.distributed.fsdp.FullOptimStateDictConfig, torch.distributed.fsdp.ShardedOptimStateDictConfig]
, defaults toNone
) — 要使用的优化器状态字典配置。如果未传入,则根据state_dict_type
确定。 - limit_all_gathers (
bool
, defaults toTrue
) — 是否让 FSDP 显式同步 CPU 线程,以防止过多的正在进行中的 all-gather 操作。此布尔值仅影响调度 all-gather 操作的分片策略。启用此功能可以帮助减少 CUDA malloc 重试的次数。 - use_orig_params (
bool
, defaults toFalse
) — 是否对优化器使用原始参数。 - param_init_fn (
Optional[Callable[[torch.nn.Module], None]
, defaults toNone
) — 一个Callable[torch.nn.Module] -> None
,用于指定当前在 meta 设备上的模块应如何在实际设备上初始化。仅当sync_module_states
为True
时适用。默认情况下是一个lambda
,它在模块上调用to_empty
。 - sync_module_states (
bool
, defaults toFalse
) — 每个单独包装的 FSDP 单元是否应从 rank 0 广播模块参数,以确保初始化后它们在所有 rank 中都相同。默认为False
,除非cpu_ram_efficient_loading
为True
,届时将强制启用。 - forward_prefetch (
bool
, defaults toFalse
) — 是否让 FSDP 在前向传递中显式预取下一个即将到来的 all-gather 操作。仅用于静态图。 - activation_checkpointing (
bool
, defaults toFalse
) — 一种通过清除某些层的激活并在反向传递期间重新计算它们来减少内存使用量的技术。实际上,这用额外的计算时间换取了减少的内存使用量。 - cpu_ram_efficient_loading (
bool
, defaults toNone
) — 如果为 True,则只有第一个进程加载预训练模型检查点,而所有其他进程都具有空权重。仅适用于 Transformers。使用此选项时,sync_module_states
需要为True
。 - transformer_cls_names_to_wrap (
Optional[List[str]]
, defaults toNone
) — 要包装的 transformer 层类名称列表。仅当auto_wrap_policy
为transformer_based_wrap
时适用。 - min_num_params (
Optional[int]
, defaults toNone
) — 模块必须具有的要包装的最小参数数量。仅当auto_wrap_policy
为size_based_wrap
时适用。
此插件用于启用完全分片数据并行。
给定 model
,基于传入的策略以及我们是否可以使用 transformer_cls_to_wrap
创建 auto_wrap_policy
。
设置 FSDP 的混合精度策略
根据 StateDictType
设置状态字典配置。
验证混合精度策略,已抽象化,以避免在不需要时引入导入。
fsdp2_load_full_state_dict
accelerate.utils.fsdp2_load_full_state_dict
< source >( accelerator model: Module full_sd: dict )
将完整状态字典(可能仅在 rank 0 上)加载到分片模型中。这是通过从 rank 0 向所有其他 rank 广播参数来完成的。此函数会修改模型本身。
fsdp2_switch_optimizer_parameters
accelerate.utils.fsdp2_switch_optimizer_parameters
< source >( optimizer: Optimizer mapping: dict )
将优化器的参数切换到新参数(通常情况下是分片参数)。此函数会修改优化器本身。
fsdp2_prepare_model
accelerate.utils.fsdp2_prepare_model
< 源代码 >( accelerator model: Module ) → torch.nn.Module
为 FSDP2 就地准备模型。同时返回该模型以避免误用原始模型。