Accelerate documentation
ステートフルクラス
ステートフルクラス
以下は、すべてのインスタンスが同じ状態を共有し、その状態が最初のインスタンス化時に初期化されるという意味でのシングルトンクラスの変形例です。
これらのクラスは不変であり、特定の構成や状態に関する情報を保存します。
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 = 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 = Nonelocal_process_index: int | None = 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_main_process
< source >( function: Callable[..., Any] | None = None )
メインプロセスでのみ装飾された関数を実行するデコレーター。
on_process
< source >( function: Callable[..., Any] | None = Noneprocess_index: int | None = None )
指定されたインデックスのプロセスでのみ装飾された関数を実行するデコレーターです。
self.device のデバイスを、現在の分散環境に設定します。
split_between_processes
< source >( inputs: list | tuple | dict | torch.Tensorapply_padding: bool = False )
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"]現在のプロセスの実行を停止し、他のすべてのプロセスがその地点に到達するまで待機します(スクリプトが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
< source >( 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) — 現在のスクリプトがデバッグモードで実行されているかどうか。
プロセスグループを破棄します。指定されていない場合、デフォルトのプロセスグループが破棄されます。
self.fork_launched が True かつ group が None の場合、何も行われません。
指定されたplugin_keyを持つDeepSpeedPluginを返します。
ローカルメインプロセスがwithブロック内に入ることを許可します。
他のプロセスは、メインプロセスが終了した後にwithブロックに入ります。
name を指定して DeepSpeedPlugin を有効にし、他のすべてのプラグインを無効にします。
split_between_processes
< source >( inputs: list | tuple | dict | torch.Tensorapply_padding: bool = False )
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
< source >( 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 になります。