Accelerate 文档
有状态类
并获得增强的文档体验
开始使用
有状态类
以下是单例类的变体,因为所有实例共享相同的状态,该状态在首次实例化时初始化。
这些类是不可变的,并存储有关某些配置或状态的信息。
PartialState
class accelerate.PartialState
< 来源 >( cpu: bool = False **kwargs )
参数
- cpu (
bool
, 可选) — 是否强制脚本在 CPU 上执行。如果设置为True
,将忽略任何可用的加速器并强制在 CPU 上执行。 - kwargs (附加关键字参数, 可选) — 传递给相关
init_process_group
函数的附加关键字参数。有效的kwargs
可以在 utils.InitProcessGroupKwargs 中找到。详细用法请参见示例部分。
单例类,包含有关当前训练环境的信息以及帮助进行进程控制的函数。设计用于仅需要进程控制和设备执行状态时。不 需要从 Accelerator
初始化。
可用属性
- device (
torch.device
) — 要使用的设备。 - distributed_type (DistributedType) — 当前使用的分布式环境类型。
- local_process_index (
int
) — 当前进程在当前服务器上的索引。 - mixed_precision (
str
) — 当前脚本是否使用混合精度,如果使用,则指定执行的混合精度类型。(从 ‘no’、‘fp16’、‘bf16’ 或 ‘fp8’ 中选择)。 - num_processes (
int
) — 当前并行启动的进程数。 - process_index (
int
) — 当前进程的索引。 - is_last_process (
bool
) — 当前进程是否为最后一个进程。 - is_main_process (
bool
) — 当前进程是否为主进程。 - is_local_main_process (
bool
) — 当前进程是否为本地节点上的主进程。 - debug (
bool
) — 当前脚本是否在调试模式下运行。
示例
from accelerate.utils import InitProcessGroupKwargs
# To include `InitProcessGroupKwargs`, init then call `.to_kwargs()`
kwargs = InitProcessGroupKwargs(...).to_kwargs()
state = PartialState(**kwargs)
销毁进程组。如果未指定进程组,则销毁默认进程组。
让本地主进程先进入一个 with 块。
其他进程将在主进程退出后进入 with 块。
示例
>>> from accelerate.state import PartialState
>>> state = PartialState()
>>> with state.local_main_process_first():
... # This will be printed first by local process 0 then in a seemingly
... # random order by the other processes.
... print(f"This will be printed by process {state.local_process_index}")
让主进程先进入一个 with 块。
其他进程将在主进程退出后进入 with 块。
仅在最后一个进程上运行被装饰函数的装饰器。
on_local_main_process
< 来源 >( function: Callable[..., Any] = None )
仅在本地主进程上运行被装饰函数的装饰器。
示例
# Assume we have 2 servers with 4 processes each.
from accelerate.state import PartialState
state = PartialState()
@state.on_local_main_process
def print_something():
print("This will be printed by process 0 only on each server.")
print_something()
# On server 1:
"This will be printed by process 0 only"
# On server 2:
"This will be printed by process 0 only"
on_local_process
< 来源 >( function: Callable[..., Any] = None local_process_index: int = None )
仅在当前节点上具有给定索引的进程上运行被装饰函数的装饰器。
示例
# Assume we have 2 servers with 4 processes each.
from accelerate import Accelerator
accelerator = Accelerator()
@accelerator.on_local_process(local_process_index=2)
def print_something():
print(f"Printed on process {accelerator.local_process_index}")
print_something()
# On server 1:
"Printed on process 2"
# On server 2:
"Printed on process 2"
仅在主进程上运行被装饰函数的装饰器。
on_process
< 来源 >( function: Callable[..., Any] = None process_index: int = None )
仅在具有给定索引的进程上运行被装饰函数的装饰器。
将 self.device
中的设备设置为当前分布式环境。
split_between_processes
< 来源 >( inputs: list | tuple | dict | torch.Tensor apply_padding: bool = False )
在 self.num_processes
之间快速分割 input
,然后可以在该进程上使用。在进行分布式推理时(例如使用不同的提示)非常有用。
请注意,使用 dict
时,所有键都需要具有相同数量的元素。
示例
# Assume there are two processes
from accelerate import PartialState
state = PartialState()
with state.split_between_processes(["A", "B", "C"]) as inputs:
print(inputs)
# Process 0
["A", "B"]
# Process 1
["C"]
with state.split_between_processes(["A", "B", "C"], apply_padding=True) as inputs:
print(inputs)
# Process 0
["A", "B"]
# Process 1
["C", "C"]
将停止当前进程的执行,直到所有其他进程都达到该点(因此,当脚本仅在一个进程中运行时,此操作无效)。在保存模型之前执行此操作非常有用。
示例
>>> # Assuming two GPU processes
>>> import time
>>> from accelerate.state import PartialState
>>> state = PartialState()
>>> if state.is_main_process:
... time.sleep(2)
>>> else:
... print("I'm waiting for the main process to finish its sleep...")
>>> state.wait_for_everyone()
>>> # Should print on every process at the same time
>>> print("Everyone is here")
AcceleratorState
class accelerate.state.AcceleratorState
< 来源 >( mixed_precision: str = None cpu: bool = False dynamo_plugin = None deepspeed_plugin = None fsdp_plugin = None torch_tp_plugin = None megatron_lm_plugin = None parallelism_config = None _from_accelerator: bool = False **kwargs )
单例类,包含有关当前训练环境的信息。
可用属性
- device (
torch.device
) — 要使用的设备。 - distributed_type (DistributedType) — 当前使用的分布式环境类型。
- parallelism_config (
ParallelismConfig
) — 当前训练环境的并行配置。这用于配置分布式训练环境。 - initialized (
bool
) — `AcceleratorState` 是否已从 `Accelerator` 初始化。 - local_process_index (
int
) — 当前进程在当前服务器上的索引。 - mixed_precision (
str
) — 当前脚本是否使用混合精度,如果使用,则指定执行的混合精度类型。(从 ‘no’、‘fp16’、‘bf16’ 或 ‘fp8’ 中选择)。 - num_processes (
int
) — 当前并行启动的进程数。 - process_index (
int
) — 当前进程的索引。 - is_last_process (
bool
) — 当前进程是否为最后一个进程。 - is_main_process (
bool
) — 当前进程是否为主进程。 - is_local_main_process (
bool
) — 当前进程是否为本地节点上的主进程。 - debug (
bool
) — 当前脚本是否在调试模式下运行。
销毁进程组。如果未指定进程组,则销毁默认进程组。
如果 self.fork_lauched
是 True
且 group
是 None
,则不执行任何操作。
返回具有给定 plugin_key 的 DeepSpeedPlugin。
激活具有给定 name
的 DeepSpeedPlugin,并将禁用所有其他插件。
split_between_processes
< 来源 >( inputs: list | tuple | dict | torch.Tensor apply_padding: bool = False )
在 self.num_processes
之间快速分割 input
,然后可以在该进程上使用。在进行分布式推理时(例如使用不同的提示)非常有用。
请注意,使用 dict
时,所有键都需要具有相同数量的元素。
示例
# Assume there are two processes
from accelerate.state import AcceleratorState
state = AcceleratorState()
with state.split_between_processes(["A", "B", "C"]) as inputs:
print(inputs)
# Process 0
["A", "B"]
# Process 1
["C"]
with state.split_between_processes(["A", "B", "C"], apply_padding=True) as inputs:
print(inputs)
# Process 0
["A", "B"]
# Process 1
["C", "C"]
GradientState
class accelerate.state.GradientState
< 源代码 >( gradient_accumulation_plugin: GradientAccumulationPlugin | None = None )
包含与梯度累积的梯度同步相关信息的单例类
可用属性
- end_of_dataloader (
bool
) — 是否已到达当前数据加载器的末尾 - remainder (
int
) — 因填充数据加载器而添加的额外样本数 - sync_gradients (
bool
) — 是否应在所有设备间同步梯度 - active_dataloader (
Optional[DataLoader]
) — 当前正在迭代的数据加载器 - dataloader_references (
List[Optional[DataLoader]]
) — 指向正在迭代的数据加载器的引用列表 - num_steps (
int
) — 进行累积的步数 - adjust_scheduler (
bool
) — 是否应调整调度器以适应梯度累积 - sync_with_dataloader (
bool
) — 是否应在数据加载器迭代结束时同步梯度,并重置总步数 - is_xla_gradients_synced (
bool
) — XLA 梯度是否已同步。它被初始化为 false。一旦在优化器步骤之前对梯度进行了归约,该标志就设置为 true。随后,在每个步骤之后,该标志被重置为 false。FSDP 将始终同步梯度,因此 is_xla_gradients_synced 始终为 true。