pytorch
125 строк · 4.1 Кб
1# Owner(s): ["oncall: distributed"]
2
3import torch
4import torch.nn as nn
5from torch.distributed._tensor import DTensor
6from torch.distributed.checkpoint.state_dict import get_state_dict
7from torch.distributed.device_mesh import _mesh_resources, init_device_mesh
8from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
9from torch.testing._internal.common_utils import run_tests
10from torch.testing._internal.distributed._tensor.common_dtensor import (
11DTensorTestBase,
12skip_if_lt_x_gpu,
13with_comms,
14)
15from torch.testing._internal.distributed.checkpoint_utils import with_temp_dir
16from torch.testing._internal.distributed.common_state_dict import VerifyStateDictMixin
17
18
19class Dummymodel(nn.Module):
20def __init__(self):
21super().__init__()
22
23def forward(self, x):
24raise NotImplementedError()
25
26
27class EPModel(nn.Module):
28def __init__(self, rank):
29super().__init__()
30self.net1 = nn.Sequential(nn.Linear(16, 16), nn.ReLU())
31self.net2 = nn.Sequential(nn.Linear(16, 16), nn.ReLU())
32
33def forward(self, x):
34raise NotImplementedError()
35
36
37class SecondTier(nn.Module):
38def __init__(self, rank):
39super().__init__()
40self.ep_layers = nn.ModuleList(
41[EPModel(rank) if rank % 4 == i else Dummymodel() for i in range(4)]
42)
43self.net = nn.Sequential(nn.Linear(16, 16), nn.ReLU())
44
45def forward(self, x):
46raise NotImplementedError()
47
48
49class TopModel(nn.Module):
50def __init__(self, rank):
51super().__init__()
52torch.manual_seed(0)
53
54self.second = SecondTier(rank)
55self.net = nn.Sequential(nn.Linear(16, 16), nn.ReLU())
56
57def forward(self, x):
58raise NotImplementedError()
59
60
61class TestFSDPWithEP(DTensorTestBase, VerifyStateDictMixin):
62@property
63def world_size(self) -> int:
64return min(8, torch.cuda.device_count())
65
66@with_comms
67@skip_if_lt_x_gpu(8)
68@with_temp_dir
69def test_e2e(self):
70model = TopModel(self.rank).cuda()
71
72mesh_fsdp_tp = init_device_mesh(
73self.device_type, (2, 4), mesh_dim_names=("dp", "tp")
74)
75# TODO: we are using an internal API atm. Change to a publich API once it is ready.
76mesh_fsdp_ep = _mesh_resources.create_child_mesh(mesh_fsdp_tp, 0, "dp")
77del _mesh_resources.child_to_parent_mapping[mesh_fsdp_ep]
78
79mesh_fsdp = init_device_mesh(self.device_type, (8,))
80for i, l in enumerate(model.second.ep_layers):
81model.second.ep_layers[i] = FSDP(
82l, use_orig_params=True, device_mesh=mesh_fsdp_ep
83)
84model.second = FSDP(model.second, use_orig_params=True, device_mesh=mesh_fsdp)
85model = FSDP(model, use_orig_params=True, device_mesh=mesh_fsdp)
86optim = torch.optim.Adam(model.parameters(), lr=0.1)
87msd, osd = get_state_dict(model, optim)
88
89# FSDP only params
90for key in (
91"net.0.weight",
92"net.0.bias",
93"second.net.0.weight",
94"second.net.0.bias",
95):
96msd_v = msd[key]
97osd_v = osd["state"][key]["exp_avg"]
98for v in (msd_v, osd_v):
99self.assertTrue(isinstance(v, DTensor))
100self.assertEqual(tuple(v.device_mesh.mesh), tuple(range(8)))
101
102# FSDP/EP params
103layer = self.rank % 4
104ranks = (layer, layer + 4)
105for i in range(4):
106for key in (
107f"second.ep_layers.{i}.net1.0.weight",
108f"second.ep_layers.{i}.net1.0.bias",
109f"second.ep_layers.{i}.net2.0.weight",
110f"second.ep_layers.{i}.net2.0.bias",
111):
112if layer != i:
113self.assertTrue(key not in msd)
114else:
115msd_v = msd[key]
116osd_v = osd["state"][key]["exp_avg"]
117for v in (msd_v, osd_v):
118self.assertTrue(isinstance(v, DTensor))
119self.assertEqual(tuple(v.device_mesh.mesh), ranks)
120
121self.assertEqual(set(osd["state"].keys()), set(msd.keys()))
122
123
124if __name__ == "__main__":
125run_tests()
126