Skip to content

Commit 09dbdc4

Browse files
committed
make on_(local_)main_process member of Accelerator
1 parent c0de453 commit 09dbdc4

2 files changed

Lines changed: 43 additions & 45 deletions

File tree

src/accelerate/accelerator.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import sys
2020
import warnings
2121
from contextlib import contextmanager
22+
from functools import wraps
2223
from typing import List, Optional, Union
2324

2425
import torch
@@ -356,23 +357,29 @@ def mixed_precision(self):
356357
mixed_precision = self.state.mixed_precision
357358
return mixed_precision
358359

359-
@contextmanager
360-
def local_main_process_first(self):
360+
def on_main_process(func):
361361
"""
362-
Lets the local main process go inside a with block.
363-
364-
The other processes will enter the with block after the main process exits.
362+
Run func on main process only
365363
"""
366-
yield from self._goes_first(self.is_local_main_process)
367364

368-
@contextmanager
369-
def main_process_first(self):
370-
"""
371-
Lets the main process go first inside a with block.
365+
@wraps(func)
366+
def wrapper(self, *args, **kwargs):
367+
if self.is_main_process or not self.use_distributed:
368+
return func(self, *args, **kwargs)
372369

373-
The other processes will enter the with block after the main process exits.
370+
return wrapper
371+
372+
def on_local_main_process(func):
373+
"""
374+
Run func on local main process only
374375
"""
375-
yield from self._goes_first(self.is_main_process)
376+
377+
@wraps(func)
378+
def wrapper(self, *args, **kwargs):
379+
if self.is_local_main_process or not self.use_distributed:
380+
return func(self, *args, **kwargs)
381+
382+
return wrapper
376383

377384
def _goes_first(self, is_main):
378385
if not is_main:
@@ -383,6 +390,24 @@ def _goes_first(self, is_main):
383390
if is_main:
384391
self.wait_for_everyone()
385392

393+
@contextmanager
394+
def main_process_first(self):
395+
"""
396+
Lets the main process go first inside a with block.
397+
398+
The other processes will enter the with block after the main process exits.
399+
"""
400+
yield from self._goes_first(self.is_main_process)
401+
402+
@contextmanager
403+
def local_main_process_first(self):
404+
"""
405+
Lets the local main process go inside a with block.
406+
407+
The other processes will enter the with block after the main process exits.
408+
"""
409+
yield from self._goes_first(self.is_local_main_process)
410+
386411
@contextmanager
387412
def no_sync(self, model):
388413
"""
@@ -991,6 +1016,7 @@ def init_trackers(self, project_name: str, config: Optional[dict] = None, init_k
9911016
for tracker in self.trackers:
9921017
tracker.store_init_configuration(config)
9931018

1019+
@on_main_process
9941020
def log(self, values: dict, step: Optional[int] = None, log_kwargs: Optional[dict] = {}):
9951021
"""
9961022
Logs `values` to all stored trackers in `self.trackers`.
@@ -1007,17 +1033,16 @@ def log(self, values: dict, step: Optional[int] = None, log_kwargs: Optional[dic
10071033
{"wandb": {"tags": ["tag_a", "tag_b"]}}
10081034
```
10091035
"""
1010-
if self.is_main_process:
1011-
for tracker in self.trackers:
1012-
tracker.log(values, step=step, **log_kwargs.get(tracker.name, {}))
1036+
for tracker in self.trackers:
1037+
tracker.log(values, step=step, **log_kwargs.get(tracker.name, {}))
10131038

1039+
@on_main_process
10141040
def end_training(self):
10151041
"""
10161042
Runs any special end training behaviors, such as stopping trackers
10171043
"""
1018-
if self.is_main_process:
1019-
for tracker in self.trackers:
1020-
tracker.finish()
1044+
for tracker in self.trackers:
1045+
tracker.finish()
10211046

10221047
def save(self, obj, f):
10231048
"""

src/accelerate/decorator.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)