from accelerate.utils import InitProcessGroupKwargs
# To include `InitProcessGroupKwargs`, init then call `.to_kwargs()`
kwargs = InitProcessGroupKwargs(...).to_kwargs()
state = PartialState(**kwargs)
>>> 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}")
>>> 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}")
# Assume we have 4 processes.from accelerate.state import PartialState
state = PartialState()
@state.on_last_processdefprint_something():
print(f"Printed on process {state.process_index}")
print_something()
"Printed on process 3"
# Assume we have 2 servers with 4 processes each.from accelerate.state import PartialState
state = PartialState()
@state.on_local_main_processdefprint_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"
# Assume we have 2 servers with 4 processes each.from accelerate import Accelerator
accelerator = Accelerator()
@accelerator.on_local_process(local_process_index=2)defprint_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"
>>> from accelerate.state import PartialState
>>> state = PartialState()
>>> @state.on_main_process
... defprint_something():
... print("This will be printed by process 0 only.")
>>> print_something()
"This will be printed by process 0 only"
# Assume we have 4 processes.from accelerate.state import PartialState
state = PartialState()
@state.on_process(process_index=2)defprint_something():
print(f"Printed on process {state.process_index}")
print_something()
"Printed on process 2"
# Assume there are two processesfrom 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")
# Assume there are two processesfrom 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"]