Accelerate 文件

有狀態類

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

有狀態類

以下是單例類的變體,因為所有例項共享相同的狀態,該狀態在首次例項化時初始化。

這些類是不可變的,並存儲有關某些配置或狀態的資訊。

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)

destroy_process_group

< >

( group = None )

銷燬程序組。如果未指定程序組,則銷燬預設程序組。

local_main_process_first

< >

( )

讓本地主程序先進入一個 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}")

main_process_first

< >

( )

讓主程序先進入一個 with 塊。

其他程序將在主程序退出後進入 with 塊。

示例

>>> from accelerate import Accelerator

>>> accelerator = Accelerator()
>>> with accelerator.main_process_first():
...     # This will be printed first by process 0 then in a seemingly
...     # random order by the other processes.
...     print(f"This will be printed by process {accelerator.process_index}")

on_last_process

< >

( function: Callable[..., Any] )

引數

  • function (Callable) — 要裝飾的函式。

僅在最後一個程序上執行被裝飾函式的裝飾器。

示例

# Assume we have 4 processes.
from accelerate.state import PartialState

state = PartialState()


@state.on_last_process
def print_something():
    print(f"Printed on process {state.process_index}")


print_something()
"Printed on process 3"

on_local_main_process

< >

( function: Callable[..., Any] = None )

引數

  • function (Callable) — 要裝飾的函式。

僅在本地主程序上執行被裝飾函式的裝飾器。

示例

# 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 )

引數

  • function (Callable, 可選) — 要裝飾的函式。
  • local_process_index (int, 可選) — 執行函式的本地程序索引。

僅在當前節點上具有給定索引的程序上執行被裝飾函式的裝飾器。

示例

# 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_main_process

< >

( function: Callable[..., Any] = None )

引數

  • function (Callable) — 要裝飾的函式。

僅在主程序上執行被裝飾函式的裝飾器。

示例

>>> from accelerate.state import PartialState

>>> state = PartialState()


>>> @state.on_main_process
... def print_something():
...     print("This will be printed by process 0 only.")


>>> print_something()
"This will be printed by process 0 only"

on_process

< >

( function: Callable[..., Any] = None process_index: int = None )

引數

  • function (Callable, `optional`) — 要裝飾的函式。
  • process_index (int, `optional`) — 執行函式的程序索引。

僅在具有給定索引的程序上執行被裝飾函式的裝飾器。

示例

# Assume we have 4 processes.
from accelerate.state import PartialState

state = PartialState()


@state.on_process(process_index=2)
def print_something():
    print(f"Printed on process {state.process_index}")


print_something()
"Printed on process 2"

set_device

< >

( )

self.device 中的裝置設定為當前分散式環境。

split_between_processes

< >

( inputs: list | tuple | dict | torch.Tensor apply_padding: bool = False )

引數

  • inputs (list, tuple, torch.Tensor, dict of list/tuple/torch.Tensor, 或 datasets.Dataset) — 在程序間分割的輸入。
  • apply_padding (bool, 可選, 預設為 False) — 是否透過重複輸入的最後一個元素來應用填充,以使所有程序具有相同數量的元素。這在嘗試對輸出執行 gather() 等操作或傳入的輸入少於程序數時非常有用。如果這樣做,請記得之後丟棄填充的元素。

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"]

wait_for_everyone

< >

( )

將停止當前程序的執行,直到所有其他程序都達到該點(因此,當指令碼僅在一個程序中執行時,此操作無效)。在儲存模型之前執行此操作非常有用。

示例

>>> # 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) — 當前指令碼是否在除錯模式下執行。

destroy_process_group

< >

( group = None )

銷燬程序組。如果未指定程序組,則銷燬預設程序組。

如果 self.fork_lauchedTruegroupNone,則不執行任何操作。

get_deepspeed_plugin

< >

( name: str )

返回具有給定 plugin_key 的 DeepSpeedPlugin。

local_main_process_first

< >

( )

讓本地主程序先進入一個 with 塊。

其他程序將在主程序退出後進入 with 塊。

main_process_first

< >

( )

讓主程序先進入一個 with 塊。

其他程序將在主程序退出後進入 with 塊。

select_deepspeed_plugin

< >

( name: str = None )

啟用具有給定 name 的 DeepSpeedPlugin,並將停用所有其他外掛。

split_between_processes

< >

( inputs: list | tuple | dict | torch.Tensor apply_padding: bool = False )

引數

  • inputs (list, tuple, torch.Tensor, or dict of list/tuple/torch.Tensor) — 在程序間分割的輸入。
  • apply_padding (bool, 可選, 預設為 False) — 是否透過重複輸入的最後一個元素來應用填充,以使所有程序具有相同數量的元素。這在嘗試對輸出執行 gather() 等操作或傳入的輸入少於程序數時非常有用。如果這樣做,請記得之後丟棄填充的元素。

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。
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.