-
Notifications
You must be signed in to change notification settings - Fork 175
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
15 changed files
with
548 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
|
||
def get_distributed_module(module, backend = None, **kwargs): | ||
from .ddp import DDPModule | ||
from .fsdp import FSDPModule | ||
|
||
if backend == 'fsdp': | ||
return FSDPModule(module, **kwargs) | ||
|
||
return DDPModule(module, **kwargs) | ||
|
||
|
||
|
||
def prepare(module, backend = None, **kwargs): | ||
from ..module import ModuleMixin | ||
|
||
if backend == 'fsdp': | ||
from .fsdp import FSDP | ||
module = FSDP(module, **kwargs) | ||
|
||
from .ddp import DDP | ||
module = DDP(module, **kwargs) | ||
|
||
return ModuleMixin.mixin(module) |
Empty file.
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,77 @@ | ||
from dataclasses import dataclass | ||
from .strategy import Strategy, DDPStrategy, FSDPStrategy | ||
|
||
|
||
@dataclass | ||
class AcceleratorState: | ||
rank: int = -1 | ||
size: int = 0 | ||
strategy: Strategy = None | ||
|
||
@property | ||
def initialized(self): | ||
import torch | ||
|
||
return torch.distributed.is_initialized() | ||
|
||
|
||
class Accelerator: | ||
def __init__(self, rank = None, size = None, strategy = "ddp"): | ||
self.state = AcceleratorState( | ||
rank = rank, | ||
size = size, | ||
strategy = strategy, | ||
) | ||
|
||
|
||
@property | ||
def rank(self): | ||
return self.state.rank | ||
|
||
@property | ||
def size(self): | ||
return self.state.size | ||
|
||
@property | ||
def initialized(self): | ||
return self.state.initialized | ||
|
||
@property | ||
def strategy(self): | ||
return self.state.strategy | ||
|
||
def setup(self): | ||
import torch | ||
|
||
if not self.initialized: | ||
# choose a rpc type | ||
rpc = 'nccl' if torch.distributed.is_nccl_available() else 'gloo' | ||
|
||
torch.distributed.init_process_group( | ||
rpc, | ||
rank = self.rank, | ||
world_size = self.size, | ||
) | ||
|
||
|
||
def prepare(self, module, loader, optimizer): | ||
self.setup() | ||
|
||
module = self.prepare_module(module) | ||
|
||
return module, loader, optimizer | ||
|
||
|
||
def prepare_model(self, module): | ||
from ...module import ModuleMixin | ||
|
||
if isinstance(self.strategy, FSDPStrategy): | ||
from ..fsdp import FSDP | ||
module = FSDP(module, **kwargs) | ||
|
||
if isinstance(self.strategy, DDPStrategy): | ||
from ..ddp import DDP | ||
module = DDP(module, **kwargs) | ||
|
||
return ModuleMixin.mixin(module) | ||
|
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,17 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Strategy: | ||
method: str = None | ||
|
||
|
||
@dataclass | ||
class DDPStrategy(Strategy): | ||
method: str = "ddp" | ||
|
||
|
||
@dataclass | ||
class FSDPStrategy(DDPStrategy): | ||
method: str = "fsdp" | ||
policy: str = None |
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,8 @@ | ||
|
||
|
||
class Cluster: | ||
def __init__(self): | ||
pass | ||
|
||
def spawn(self, func): | ||
pass |
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,4 @@ | ||
|
||
|
||
class TorchCluster: | ||
pass |
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,17 @@ | ||
from torch.nn.parallel import DistributedDataParallel as DDP | ||
|
||
|
||
class DDPModule(DDP): | ||
"""distributed module class | ||
""" | ||
def fit(self, *args, **kwargs): | ||
return self.module.fit(*args, **kwargs) | ||
|
||
def save(self, *args, **kwargs): | ||
return self.module.save(*args, **kwargs) | ||
|
||
def load(self, *args, **kwargs): | ||
return self.module.load(*args, **kwargs) | ||
|
||
def log(self, *args, **kwargs): | ||
return self.module.log(*args, **kwargs) |
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,27 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class DistributedState: | ||
size: int = 0 | ||
backend: str = 'ddp' | ||
|
||
|
||
class Distributor: | ||
def __init__(self, size, backend = 'ddp', cluster = 'mp'): | ||
self.state = DistributedState( | ||
size = size, | ||
backend = backend, | ||
) | ||
|
||
|
||
def init(self): | ||
pass | ||
|
||
def prepare(self, module, loader, optimizer, scheduler): | ||
pass | ||
|
||
|
||
def spawn(self, func, *args): | ||
|
||
pass |
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
Oops, something went wrong.