-
Notifications
You must be signed in to change notification settings - Fork 27
/
train.py
190 lines (167 loc) · 6.12 KB
/
train.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
# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# 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 torch
from dgl.dataloading import GraphDataLoader
from torch.cuda.amp import autocast, GradScaler
from torch.nn.parallel import DistributedDataParallel
import time, os
import wandb as wb
try:
import apex
except:
pass
from modulus.models.meshgraphnet import MeshGraphNet
from modulus.datapipes.gnn.vortex_shedding_dataset import VortexSheddingDataset
from modulus.distributed.manager import DistributedManager
from modulus.launch.logging import (
PythonLogger,
initialize_wandb,
RankZeroLoggingWrapper,
)
from modulus.launch.utils import load_checkpoint, save_checkpoint
from constants import Constants
# Instantiate constants
C = Constants()
class MGNTrainer:
def __init__(self, wb, dist, rank_zero_logger):
self.dist = dist
# instantiate dataset
dataset = VortexSheddingDataset(
name="vortex_shedding_train",
data_dir=C.data_dir,
split="train",
num_samples=C.num_training_samples,
num_steps=C.num_training_time_steps,
)
# instantiate dataloader
self.dataloader = GraphDataLoader(
dataset,
batch_size=C.batch_size,
shuffle=True,
drop_last=True,
pin_memory=True,
use_ddp=dist.world_size > 1,
)
# instantiate the model
self.model = MeshGraphNet(
C.num_input_features, C.num_edge_features, C.num_output_features
)
if C.jit:
self.model = torch.jit.script(self.model).to(dist.device)
else:
self.model = self.model.to(dist.device)
if C.watch_model and not C.jit and dist.rank == 0:
wb.watch(self.model)
# distributed data parallel for multi-node training
if dist.world_size > 1:
self.model = DistributedDataParallel(
self.model,
device_ids=[dist.local_rank],
output_device=dist.device,
broadcast_buffers=dist.broadcast_buffers,
find_unused_parameters=dist.find_unused_parameters,
)
# enable train mode
self.model.train()
# instantiate loss, optimizer, and scheduler
self.criterion = torch.nn.MSELoss()
try:
self.optimizer = apex.optimizers.FusedAdam(self.model.parameters(), lr=C.lr)
rank_zero_logger.info("Using FusedAdam optimizer")
except:
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=C.lr)
self.scheduler = torch.optim.lr_scheduler.LambdaLR(
self.optimizer, lr_lambda=lambda epoch: C.lr_decay_rate**epoch
)
self.scaler = GradScaler()
# load checkpoint
if dist.world_size > 1:
torch.distributed.barrier()
self.epoch_init = load_checkpoint(
os.path.join(C.ckpt_path, C.ckpt_name),
models=self.model,
optimizer=self.optimizer,
scheduler=self.scheduler,
scaler=self.scaler,
device=dist.device,
)
def train(self, graph):
graph = graph.to(self.dist.device)
self.optimizer.zero_grad()
loss = self.forward(graph)
self.backward(loss)
self.scheduler.step()
return loss
def forward(self, graph):
# forward pass
with autocast(enabled=C.amp):
pred = self.model(graph.ndata["x"], graph.edata["x"], graph)
loss = self.criterion(pred, graph.ndata["y"])
return loss
def backward(self, loss):
# backward pass
if C.amp:
self.scaler.scale(loss).backward()
self.scaler.step(self.optimizer)
self.scaler.update()
else:
loss.backward()
self.optimizer.step()
if __name__ == "__main__":
# initialize distributed manager
DistributedManager.initialize()
dist = DistributedManager()
# save constants to JSON file
if dist.rank == 0:
os.makedirs(C.ckpt_path, exist_ok=True)
with open(
os.path.join(C.ckpt_path, C.ckpt_name.replace(".pt", ".json")), "w"
) as json_file:
json_file.write(C.json(indent=4))
# initialize loggers
initialize_wandb(
project="Modulus-Launch",
entity="Modulus",
name="Vortex_Shedding-Training",
group="Vortex_Shedding-DDP-Group",
mode=C.wandb_mode,
) # Wandb logger
logger = PythonLogger("main") # General python logger
rank_zero_logger = RankZeroLoggingWrapper(logger, dist) # Rank 0 logger
logger.file_logging()
trainer = MGNTrainer(wb, dist, rank_zero_logger)
start = time.time()
rank_zero_logger.info("Training started...")
for epoch in range(trainer.epoch_init, C.epochs):
for graph in trainer.dataloader:
loss = trainer.train(graph)
rank_zero_logger.info(
f"epoch: {epoch}, loss: {loss:10.3e}, time per epoch: {(time.time()-start):10.3e}"
)
wb.log({"loss": loss.detach().cpu()})
# save checkpoint
if dist.world_size > 1:
torch.distributed.barrier()
if dist.rank == 0:
save_checkpoint(
os.path.join(C.ckpt_path, C.ckpt_name),
models=trainer.model,
optimizer=trainer.optimizer,
scheduler=trainer.scheduler,
scaler=trainer.scaler,
epoch=epoch,
)
logger.info(f"Saved model on rank {dist.rank}")
start = time.time()
rank_zero_logger.info("Training completed!")