|
| 1 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +import torch |
| 5 | +import torch.distributed as dist |
| 6 | +from torch import nn, optim |
| 7 | + |
| 8 | +from torchft.ddp import DistributedDataParallel |
| 9 | +from torchft.manager import Manager |
| 10 | +from torchft.optim import OptimizerWrapper |
| 11 | +from torchft.process_group import ProcessGroupGloo |
| 12 | +from torchft.torchft import Lighthouse |
| 13 | + |
| 14 | + |
| 15 | +class MyModel(nn.Module): |
| 16 | + def __init__(self): |
| 17 | + super().__init__() |
| 18 | + self.model = nn.Sequential( |
| 19 | + nn.Linear(3, 4), |
| 20 | + nn.Sigmoid(), |
| 21 | + ) |
| 22 | + |
| 23 | + def forward(self, x): |
| 24 | + return self.model(x) |
| 25 | + |
| 26 | + |
| 27 | +def train_loop(replica_id: int, lighthouse_address: str) -> None: |
| 28 | + store = dist.TCPStore( |
| 29 | + host_name="localhost", |
| 30 | + port=0, |
| 31 | + is_master=True, |
| 32 | + wait_for_workers=False, |
| 33 | + ) |
| 34 | + |
| 35 | + def load_state_dict(state_dict): |
| 36 | + m.load_state_dict(state_dict["model"]) |
| 37 | + optimizer.load_state_dict(state_dict["optim"]) |
| 38 | + |
| 39 | + def state_dict(): |
| 40 | + return { |
| 41 | + "model": m.state_dict(), |
| 42 | + "optim": optimizer.state_dict(), |
| 43 | + } |
| 44 | + |
| 45 | + pg = ProcessGroupGloo() |
| 46 | + manager = Manager( |
| 47 | + pg=pg, |
| 48 | + min_replica_size=2, |
| 49 | + load_state_dict=load_state_dict, |
| 50 | + state_dict=state_dict, |
| 51 | + replica_id=str(replica_id), |
| 52 | + store_addr="localhost", |
| 53 | + store_port=store.port, |
| 54 | + rank=0, |
| 55 | + world_size=1, |
| 56 | + lighthouse_addr=lighthouse_address, |
| 57 | + port=19530 + replica_id, |
| 58 | + ) |
| 59 | + m = DistributedDataParallel(manager, MyModel()) |
| 60 | + optimizer = OptimizerWrapper(manager, optim.Adam(m.parameters())) |
| 61 | + criterion = nn.CrossEntropyLoss() |
| 62 | + |
| 63 | + while True: |
| 64 | + inputs = torch.rand(2, 3) |
| 65 | + labels = torch.randint(4, (2,)) |
| 66 | + |
| 67 | + optimizer.zero_grad() |
| 68 | + out = m(inputs) |
| 69 | + loss = criterion(out, labels) |
| 70 | + |
| 71 | + loss.backward() |
| 72 | + optimizer.step() |
| 73 | + |
| 74 | + # TODO: assert weights are equal across replicas |
| 75 | + |
| 76 | + if manager.current_step() >= 5: |
| 77 | + break |
| 78 | + |
| 79 | + manager.shutdown() |
| 80 | + |
| 81 | + |
| 82 | +class ManagerIntegTest(TestCase): |
| 83 | + def test_ddp(self): |
| 84 | + lighthouse = Lighthouse( |
| 85 | + bind="[::]:0", |
| 86 | + min_replicas=2, |
| 87 | + ) |
| 88 | + num_replicas = 2 |
| 89 | + futures = [] |
| 90 | + |
| 91 | + with ThreadPoolExecutor(max_workers=num_replicas) as executor: |
| 92 | + for replica_id in range(num_replicas): |
| 93 | + futures.append( |
| 94 | + executor.submit(train_loop, replica_id, lighthouse.address()) |
| 95 | + ) |
| 96 | + |
| 97 | + for fut in as_completed(futures): |
| 98 | + fut.result() |
| 99 | + |
| 100 | + lighthouse.shutdown() |
0 commit comments