-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
145 lines (118 loc) · 4.36 KB
/
run.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
import os
import copy
import torch
import pytorch_lightning as pl
from scl.config import config_dict
from scl.modules import SCLTransformer
from scl.datamodules.multitask_datamodule import MTDataModule
from pytorch_lightning.plugins.environments import ClusterEnvironment
from pytorch_lightning.plugins.training_type import DDPPlugin
import torch.distributed as dist
import argparse
class MyCluster(ClusterEnvironment):
def creates_children(self) -> bool:
# return True if the cluster is managed (you don't launch processes yourself)
return True
def master_address(self):
return os.environ['CHIEF_IP']
def master_port(self) -> int:
return int(os.environ["MASTER_PORT"])
def world_size(self):
return int(os.environ['WORLD_SIZE'])
def global_rank(self) -> int:
return int(os.environ['RANK'])
def local_rank(self) -> int:
return int(os.environ['LOCAL_RANK'])
def node_rank(self) -> int:
return int(os.environ["INDEX"])
def set_global_rank(self, rank: int) -> None:
pass
def set_world_size(self, size: int) -> None:
pass
class MyDDPPlugin(DDPPlugin):
def init_ddp_connection(self, global_rank = None, world_size = None) -> None:
master_uri = "tcp://%s:%s" % (os.environ['CHIEF_IP'], os.environ['MASTER_PORT'])
dist.init_process_group(
backend=self.torch_distributed_backend,
init_method=master_uri,
world_size=int(os.environ['WORLD_SIZE']),
rank=int(os.environ['RANK']),
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default='pretrain')
return parser.parse_args()
if __name__ == '__main__':
config = parse_args()
_config = copy.deepcopy(config_dict[config.task])
pl.seed_everything(_config["seed"])
dm = MTDataModule(_config, dist=True)
model = SCLTransformer(_config)
exp_name = f'{_config["exp_name"]}'
os.makedirs(_config["log_dir"], exist_ok=True)
# model save setting
if config.task == 'pretrain':
checkpoint_callback = pl.callbacks.ModelCheckpoint(
save_top_k=5,
verbose=True,
monitor="val/the_metric",
mode="max",
save_last=True,
every_n_train_steps=5000, # to save checkpoints each 5k steps according to val metrics
)
# checkpoint_callback = pl.callbacks.ModelCheckpoint(
# every_n_train_steps=500,
# )
else:
checkpoint_callback = pl.callbacks.ModelCheckpoint(
save_top_k=1,
verbose=True,
monitor="val/the_metric",
mode="max",
save_last=True,
)
logger = pl.loggers.TensorBoardLogger(
_config["log_dir"],
name=f'{exp_name}_seed{_config["seed"]}_from_{_config["load_path"].split("/")[-1][:-5]}',
)
lr_callback = pl.callbacks.LearningRateMonitor(logging_interval="step")
callbacks = [checkpoint_callback, lr_callback]
num_gpus = (
_config["num_gpus"]
if isinstance(_config["num_gpus"], int)
else len(_config["num_gpus"])
)
grad_steps = _config["batch_size"] // (
_config["per_gpu_batchsize"] * num_gpus * _config["num_nodes"]
)
max_steps = _config["max_steps"] if _config["max_steps"] is not None else None
trainer = pl.Trainer(
# plugins=[MyCluster(), MyDDPPlugin()], # for multi-machine ddp
gpus=_config["num_gpus"],
num_nodes=_config["num_nodes"],
precision=_config["precision"],
accelerator="ddp",
benchmark=True,
deterministic=True,
max_epochs=_config["max_epoch"] if max_steps is None else 1000,
max_steps=max_steps,
callbacks=callbacks,
logger=logger,
prepare_data_per_node=False,
replace_sampler_ddp=False,
accumulate_grad_batches=grad_steps,
log_every_n_steps=10,
flush_logs_every_n_steps=10,
resume_from_checkpoint=_config["resume_from"],
weights_summary="top",
fast_dev_run=_config["fast_dev_run"],
val_check_interval=_config["val_check_interval"],
terminate_on_nan = True,
amp_level='O1',
# limit_train_batches=5,
# limit_val_batches=1
)
if not _config["test_only"]:
trainer.fit(model, datamodule=dm)
else:
trainer.test(model, datamodule=dm)