Accelerate documentation

ステートフルクラス

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ステートフルクラス

以下は、すべてのインスタンスが同じ状態を共有し、その状態が最初のインスタンス化時に初期化されるという意味でのシングルトンクラスの変形例です。

これらのクラスは不変であり、特定の構成や状態に関する情報を保存します。

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 = 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 = Nonelocal_process_index: int | None = 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 = 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 = Noneprocess_index: int | None = 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.Tensorapply_padding: bool = False )

パラメータ

  • inputs (list、tuple、torch.Tensor、list/tuple/torch.Tensor の dict、または datasets.Dataset) — プロセス間で分割する入力。
  • apply_padding (bool、optional、デフォルトはFalse) — 入力の最後の要素を繰り返すことでパディングを適用し、すべてのプロセスが同じ数の要素を持つようにするかどうか。出力に対してgather()などの操作を実行する場合や、プロセス数より少ない入力を渡す場合に有用です。この場合、後でパディングされた要素を削除することを忘れないでください。

inputをself.num_processesに素早く分割し、その後、そのプロセスで使用できます。異なるプロンプトなど、分散推論を行う場合に便利です。

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

< >

( )

現在のプロセスの実行を停止し、他のすべてのプロセスがその地点に到達するまで待機します(スクリプトが1つのプロセスのみで実行されている場合は、何もしません)。モデルを保存する前に実行するのに便利です。

例:

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

クラス accelerate.state.AcceleratorState

< >

( mixed_precision: str | None = Nonecpu: bool = Falsedynamo_plugin = Nonedeepspeed_plugin = Nonefsdp_plugin = Nonetorch_tp_plugin = Nonemegatron_lm_plugin = Noneparallelism_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_launched が True かつ group が None の場合、何も行われません。

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 = None )

name を指定して DeepSpeedPlugin を有効にし、他のすべてのプラグインを無効にします。

split_between_processes

< >

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

パラメータ

  • inputs (list、tuple、torch.Tensor、またはlist/tuple/torch.Tensorのdict) — プロセス間で分割する入力です。
  • apply_padding (bool、optional、デフォルトはFalse) — 入力の最後の要素を繰り返すことでパディングを適用し、すべてのプロセスが同じ数の要素を持つようにするかどうか。出力に対してgather()などの操作を実行する場合や、プロセス数より少ない入力を渡す場合に有用です。この場合、後でパディングされた要素を削除することを忘れないでください。

inputをself.num_processesに素早く分割し、その後、そのプロセスで使用できます。異なるプロンプトなど、分散推論を行う場合に便利です。

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

クラス accelerate.state.GradientState

< >

( gradient_accumulation_plugin: GradientAccumulationPlugin | None = None )

勾配蓄積に関する勾配同期の情報を保持するシングルトンクラス

利用可能な属性:

  • end_of_dataloader (bool) — 現在のdataloaderの末尾に到達したかどうか
  • remainder (int) — dataloaderのパディングによって追加された追加サンプルの数
  • sync_gradients (bool) — 勾配をすべてのデバイス間で同期するかどうか
  • active_dataloader (Optional[DataLoader]) — 現在反復処理されているdataloader
  • dataloader_references (List[Optional[DataLoader]]) — 反復処理されているdataloaderへの参照のリスト
  • num_steps (int) — 累積するステップ数
  • adjust_scheduler (bool) — グラデーションの蓄積を考慮してスケジューラーを調整するかどうか
  • sync_with_dataloader (bool) — dataloaderのイテレーションの終了時に勾配を同期し、総ステップ数をリセットするかどうか
  • is_xla_gradients_synced (bool) — XLA の勾配が同期済みかどうかを示します。初期値は false です。オプティマイザーのステップ実行前に勾配がリダクションされると、このフラグは true に設定されます。その後、各ステップのたびにフラグは false にリセットされます。FSDP は常に勾配を同期するため、is_xla_gradients_synced は常に true になります。
Update on GitHub