-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import contextlib | ||
import logging | ||
|
||
import torch.distributed | ||
|
||
logger = logging.getLogger("truefoundry-finetune") | ||
|
||
|
||
class DistributedState: | ||
def __init__(self, world_size: int, local_rank: int): | ||
self.world_size = world_size | ||
self.local_rank = local_rank | ||
|
||
@property | ||
def is_distributed(self): | ||
return self.world_size > 1 | ||
|
||
@property | ||
def is_main_process(self): | ||
return self.local_rank <= 0 | ||
|
||
@contextlib.contextmanager | ||
def main_process_first(self): | ||
if self.is_distributed: | ||
if not self.is_main_process: | ||
torch.distributed.barrier() | ||
yield | ||
if self.is_main_process: | ||
logger.info("Getting other ranks in sync with main process") | ||
torch.distributed.barrier() | ||
else: | ||
yield | ||
|
||
def wait_for_everyone(self): | ||
if self.is_distributed: | ||
torch.distributed.barrier() |
Oops, something went wrong.