-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add on_main_process decorators #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0de453
add some useful decorators
ZhiyuanChen 09dbdc4
make on_(local_)main_process member of Accelerator
ZhiyuanChen 1872522
update examples
ZhiyuanChen af539e7
add on_process and on_local_process
ZhiyuanChen 6675543
fixes wrong name for `on_local_process`
ZhiyuanChen 0642a72
Update src/accelerate/accelerator.py
ZhiyuanChen ef711c4
Update src/accelerate/accelerator.py
ZhiyuanChen 4711b43
Update src/accelerate/accelerator.py
ZhiyuanChen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |||||
| import sys | ||||||
| import warnings | ||||||
| from contextlib import contextmanager | ||||||
| from functools import wraps | ||||||
| from typing import List, Optional, Union | ||||||
|
|
||||||
| import torch | ||||||
|
|
@@ -356,23 +357,59 @@ def mixed_precision(self): | |||||
| mixed_precision = self.state.mixed_precision | ||||||
| return mixed_precision | ||||||
|
|
||||||
| @contextmanager | ||||||
| def local_main_process_first(self): | ||||||
| def on_main_process(func): | ||||||
| """ | ||||||
| Run func on main process only | ||||||
| """ | ||||||
| Lets the local main process go inside a with block. | ||||||
|
|
||||||
| The other processes will enter the with block after the main process exits. | ||||||
| @wraps(func) | ||||||
| def wrapper(self, *args, **kwargs): | ||||||
| if self.is_main_process or not self.use_distributed: | ||||||
| return func(self, *args, **kwargs) | ||||||
|
|
||||||
| return wrapper | ||||||
|
|
||||||
| def on_local_main_process(func): | ||||||
| """ | ||||||
| Run func on local main process only | ||||||
|
ZhiyuanChen marked this conversation as resolved.
Outdated
|
||||||
| """ | ||||||
| yield from self._goes_first(self.is_local_main_process) | ||||||
|
|
||||||
| @contextmanager | ||||||
| def main_process_first(self): | ||||||
| @wraps(func) | ||||||
| def wrapper(self, *args, **kwargs): | ||||||
| if self.is_local_main_process or not self.use_distributed: | ||||||
| return func(self, *args, **kwargs) | ||||||
|
|
||||||
| return wrapper | ||||||
|
|
||||||
| def on_process(process_idx): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe we could group this one and the text in one decorator since it's one that takes arguments? |
||||||
| """ | ||||||
| Run func on certain process only | ||||||
|
ZhiyuanChen marked this conversation as resolved.
Outdated
|
||||||
| """ | ||||||
| Lets the main process go first inside a with block. | ||||||
|
|
||||||
| The other processes will enter the with block after the main process exits. | ||||||
| def decorator(func): | ||||||
| @wraps(func) | ||||||
| def wrapper(self, *args, **kwargs): | ||||||
| if self.process_idx == process_idx or not self.use_distributed: | ||||||
| return func(self, *args, **kwargs) | ||||||
|
|
||||||
| return wrapper | ||||||
|
|
||||||
| return decorator | ||||||
|
|
||||||
| def on_local_process(local_process_idx): | ||||||
| """ | ||||||
| yield from self._goes_first(self.is_main_process) | ||||||
| Run func on certain local process only | ||||||
| """ | ||||||
|
|
||||||
| def decorator(func): | ||||||
| @wraps(func) | ||||||
| def wrapper(self, *args, **kwargs): | ||||||
| if self.local_process_idx == local_process_idx or not self.use_distributed: | ||||||
| return func(self, *args, **kwargs) | ||||||
|
|
||||||
| return wrapper | ||||||
|
|
||||||
| return decorator | ||||||
|
|
||||||
| def _goes_first(self, is_main): | ||||||
| if not is_main: | ||||||
|
|
@@ -383,6 +420,24 @@ def _goes_first(self, is_main): | |||||
| if is_main: | ||||||
| self.wait_for_everyone() | ||||||
|
|
||||||
| @contextmanager | ||||||
| def main_process_first(self): | ||||||
| """ | ||||||
| Lets the main process go first inside a with block. | ||||||
|
|
||||||
| The other processes will enter the with block after the main process exits. | ||||||
| """ | ||||||
| yield from self._goes_first(self.is_main_process) | ||||||
|
|
||||||
| @contextmanager | ||||||
| def local_main_process_first(self): | ||||||
| """ | ||||||
| Lets the local main process go inside a with block. | ||||||
|
|
||||||
| The other processes will enter the with block after the main process exits. | ||||||
| """ | ||||||
| yield from self._goes_first(self.is_local_main_process) | ||||||
|
|
||||||
| @contextmanager | ||||||
| def no_sync(self, model): | ||||||
| """ | ||||||
|
|
@@ -991,6 +1046,7 @@ def init_trackers(self, project_name: str, config: Optional[dict] = None, init_k | |||||
| for tracker in self.trackers: | ||||||
| tracker.store_init_configuration(config) | ||||||
|
|
||||||
| @on_main_process | ||||||
| def log(self, values: dict, step: Optional[int] = None, log_kwargs: Optional[dict] = {}): | ||||||
| """ | ||||||
| Logs `values` to all stored trackers in `self.trackers`. | ||||||
|
|
@@ -1007,17 +1063,16 @@ def log(self, values: dict, step: Optional[int] = None, log_kwargs: Optional[dic | |||||
| {"wandb": {"tags": ["tag_a", "tag_b"]}} | ||||||
| ``` | ||||||
| """ | ||||||
| if self.is_main_process: | ||||||
| for tracker in self.trackers: | ||||||
| tracker.log(values, step=step, **log_kwargs.get(tracker.name, {})) | ||||||
| for tracker in self.trackers: | ||||||
| tracker.log(values, step=step, **log_kwargs.get(tracker.name, {})) | ||||||
|
|
||||||
| @on_main_process | ||||||
| def end_training(self): | ||||||
| """ | ||||||
| Runs any special end training behaviors, such as stopping trackers | ||||||
| """ | ||||||
| if self.is_main_process: | ||||||
| for tracker in self.trackers: | ||||||
| tracker.finish() | ||||||
| for tracker in self.trackers: | ||||||
| tracker.finish() | ||||||
|
|
||||||
| def save(self, obj, f): | ||||||
| """ | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.