-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
test_parity.py
226 lines (186 loc) · 8.49 KB
/
test_parity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from contextlib import contextmanager
from copy import deepcopy
from functools import partial
from typing import Callable, Generator
import pytest
import torch
import torch.distributed
import torch.multiprocessing as mp
import torch.nn.functional
from lightning_utilities.core.apply_func import apply_to_collection
from tests_lite.helpers.models import RandomDataset
from tests_lite.helpers.runif import RunIf
from torch import nn
from torch.nn.parallel.distributed import DistributedDataParallel
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from lightning_lite.lite import LightningLite
from lightning_lite.plugins.environments.lightning import find_free_network_port
from lightning_lite.strategies.ddp_spawn import DDPSpawnStrategy
from lightning_lite.utilities.apply_func import move_data_to_device
from lightning_lite.utilities.cloud_io import _atomic_save
class BoringModel(nn.Module):
def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 2, bias=False)
def forward(self, x):
x = self.layer(x)
return torch.nn.functional.mse_loss(x, torch.ones_like(x))
def configure_optimizers(module: nn.Module):
return torch.optim.SGD(module.parameters(), lr=0.0001)
def main(
move_to_device: Callable,
model: nn.Module,
train_dataloader: DataLoader,
num_epochs: int = 10,
):
model = move_to_device(model)
optimizer = configure_optimizers(model)
for _ in range(num_epochs):
model.train()
for batch in train_dataloader:
batch = move_to_device(batch)
optimizer.zero_grad()
loss = model(batch)
loss.backward()
optimizer.step()
return model.state_dict()
class LiteRunner(LightningLite):
def run(self, model: nn.Module, train_dataloader: DataLoader, num_epochs: int = 10, tmpdir: str = None):
optimizer = configure_optimizers(model)
model, optimizer = self.setup(model, optimizer)
train_dataloader = self.setup_dataloaders(train_dataloader)
model.train()
for _ in range(num_epochs):
for batch in train_dataloader:
batch = self.to_device(batch)
optimizer.zero_grad()
loss = model(batch)
self.backward(loss)
optimizer.step()
if isinstance(self._strategy, DDPSpawnStrategy) and tmpdir and self.global_rank == 0:
checkpoint_path = os.path.join(tmpdir, "model.pt")
_atomic_save(model.state_dict(), checkpoint_path)
return checkpoint_path
@contextmanager
def precision_context(precision, accelerator) -> Generator[None, None, None]:
if precision == 32:
yield
return
if accelerator == "gpu":
with torch.cuda.amp.autocast():
yield
elif accelerator == "cpu":
with torch.cpu.amp.autocast():
yield
@pytest.mark.parametrize(
"precision, strategy, devices, accelerator",
[
pytest.param(32, None, 1, "cpu"),
pytest.param(32, None, 1, "gpu", marks=RunIf(min_cuda_gpus=1)),
pytest.param(16, None, 1, "gpu", marks=RunIf(min_cuda_gpus=1)),
pytest.param("bf16", None, 1, "gpu", marks=RunIf(min_cuda_gpus=1, bf16_cuda=True)),
pytest.param(32, None, 1, "mps", marks=RunIf(mps=True)),
],
)
def test_boring_lite_model_single_device(precision, strategy, devices, accelerator, tmpdir):
LightningLite.seed_everything(42)
train_dataloader = DataLoader(RandomDataset(32, 8))
model = BoringModel()
num_epochs = 1
state_dict = deepcopy(model.state_dict())
lite = LiteRunner(precision=precision, strategy=strategy, devices=devices, accelerator=accelerator)
lite.run(model, train_dataloader, num_epochs=num_epochs)
lite_state_dict = model.state_dict()
with precision_context(precision, accelerator):
model.load_state_dict(state_dict)
pure_state_dict = main(lite.to_device, model, train_dataloader, num_epochs=num_epochs)
state_dict = apply_to_collection(state_dict, torch.Tensor, lite.to_device)
for w_pure, w_lite in zip(state_dict.values(), lite_state_dict.values()):
# TODO: This should be torch.equal, but MPS does not yet support this operation (torch 1.12)
assert not torch.allclose(w_pure, w_lite)
for w_pure, w_lite in zip(pure_state_dict.values(), lite_state_dict.values()):
# TODO: This should be torch.equal, but MPS does not yet support this operation (torch 1.12)
assert torch.allclose(w_pure, w_lite)
def run(rank, model, train_dataloader, num_epochs, precision, accelerator, tmpdir):
os.environ["LOCAL_RANK"] = str(rank)
if torch.distributed.is_available() and not torch.distributed.is_initialized():
torch.distributed.init_process_group("gloo", rank=rank, world_size=2)
to_device = partial(move_data_to_device, device=torch.device("cuda", rank))
model = DistributedDataParallel(
to_device(model),
device_ids=[rank],
)
train_dataloader = DataLoader(
train_dataloader.dataset,
sampler=DistributedSampler(train_dataloader.dataset, rank=rank, num_replicas=2, seed=42, drop_last=False),
)
with precision_context(precision, accelerator):
main(to_device, model, train_dataloader, num_epochs=num_epochs)
if rank == 0:
_atomic_save(model.state_dict(), os.path.join(tmpdir, "model_spawn.pt"))
@pytest.mark.skip(reason="Skipping as it takes 80 seconds.")
@RunIf(min_cuda_gpus=2)
@pytest.mark.parametrize(
"precision, strategy, devices, accelerator",
[
(32, "ddp_spawn", 2, "gpu"),
],
)
def test_boring_lite_model_ddp_spawn(precision, strategy, devices, accelerator, tmpdir):
LightningLite.seed_everything(42)
train_dataloader = DataLoader(RandomDataset(32, 8))
model = BoringModel()
num_epochs = 1
state_dict = deepcopy(model.state_dict())
lite = LiteRunner(precision=precision, strategy=strategy, devices=devices, accelerator=accelerator)
checkpoint_path = lite.run(model, train_dataloader, num_epochs=num_epochs, tmpdir=tmpdir)
spawn_model_state_dict = torch.load(checkpoint_path)
for w_pure, w_lite in zip(state_dict.values(), spawn_model_state_dict.values()):
assert not torch.equal(w_pure.cpu(), w_lite.cpu())
model.load_state_dict(state_dict)
os.environ["MASTER_ADDR"] = "127.0.0.1"
os.environ["MASTER_PORT"] = str(find_free_network_port())
mp.spawn(run, args=(model, train_dataloader, num_epochs, precision, accelerator, tmpdir), nprocs=2)
spawn_pure_model_state_dict = torch.load(os.path.join(tmpdir, "model_spawn.pt"))
for w_pure, w_lite in zip(spawn_pure_model_state_dict.values(), spawn_model_state_dict.values()):
assert torch.equal(w_pure.cpu(), w_lite.cpu())
@RunIf(min_cuda_gpus=2, standalone=True)
@pytest.mark.parametrize(
"precision, strategy, devices, accelerator",
[
(32, "ddp", 2, "gpu"),
],
)
def test_boring_lite_model_ddp(precision, strategy, devices, accelerator, tmpdir):
LightningLite.seed_everything(42)
train_dataloader = DataLoader(RandomDataset(32, 4))
model = BoringModel()
num_epochs = 1
state_dict = deepcopy(model.state_dict())
lite = LiteRunner(precision=precision, strategy=strategy, devices=devices, accelerator=accelerator)
lite.run(model, train_dataloader, num_epochs=num_epochs, tmpdir=tmpdir)
lite_model_state_dict = model.state_dict()
for w_pure, w_lite in zip(state_dict.values(), lite_model_state_dict.values()):
assert not torch.equal(w_pure.cpu(), w_lite.cpu())
LightningLite.seed_everything(42)
train_dataloader = DataLoader(RandomDataset(32, 4))
model = BoringModel()
run(lite.global_rank, model, train_dataloader, num_epochs, precision, accelerator, tmpdir)
pure_model_state_dict = model.state_dict()
for w_pure, w_lite in zip(pure_model_state_dict.values(), lite_model_state_dict.values()):
assert torch.equal(w_pure.cpu(), w_lite.cpu())