Accelerate 文档
有状态的类
并获得增强的文档体验
开始使用
有状态的类
以下是 单例模式 的变体,因为所有实例共享相同的状态,该状态在首次实例化时初始化。
这些类是不可变的,并存储有关特定配置或状态的信息。
PartialState
class accelerate.PartialState
< source >( 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
< source >( 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
< source >( 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 _from_accelerator: bool = False **kwargs )
单例类,其中包含有关当前训练环境的信息。
可用属性
- device (
torch.device
) — 要使用的设备。 - distributed_type (DistributedType) — 当前使用的分布式环境类型。
- 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。
local_main_process_first
local_main_process_first( )
让本地主进程进入 with 代码块。
其他进程将在主进程退出后进入 with 代码块。
main_process_first
main_process_first( )
让主进程首先进入 with 代码块。
其他进程将在主进程退出后进入 with 代码块。
激活具有给定 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。