-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_train.py
202 lines (172 loc) · 7.34 KB
/
main_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
191
192
193
194
195
196
197
198
199
200
201
202
import os
import numpy as np
import random
import torch
import transformers
import logging
import pipeline
from functools import partial
from train import TrainingConfig, BasicTrainer
from arguments import get_args
from datetime import datetime
from evaluation import ddp_evaluate
from pipeline import initialize_distributed
from init_task import build_task
from ofa.modeling_ofa import OFAModel
from architecture_configs import architecture_configs_dict
from ofa.configuration_ofa import OFAConfig
def args_check(args, logger):
if os.path.exists(args.output_dir) and os.listdir(args.output_dir):
logger.info("Output directory () already exists and is not empty.")
if args.gradient_accumulation_steps < 1:
raise ValueError(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.format(args.gradient_accumulation_steps))
if not args.do_train and not args.do_predict:
raise ValueError(
"At least one of `do_train` or `do_predict` must be True.")
if args.local_rank == -1:
device = torch.device("cuda" if torch.cuda.is_available()
and not args.no_cuda else "cpu")
n_gpu = torch.cuda.device_count() if not args.no_cuda else 0
else:
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda", args.local_rank)
n_gpu = 1
args.n_gpu = n_gpu
args.device = device
return device, n_gpu
def main():
import PIL
print("PIL version", PIL.__version__)
print("CUDA device name", torch.cuda.get_device_name(0))
# ---------- SETTINGS ----------------
torch.backends.cudnn.enabled = True
args = get_args()
cur_time = datetime.strftime(datetime.now(), '%m%d_%H%M')
if args.postfix is not None:
args.output_dir = os.path.join(args.output_dir, args.postfix)
folder_name = args.task + "/" + cur_time
args.output_dir = os.path.join(args.output_dir, folder_name)
os.makedirs(args.output_dir, exist_ok=True)
log_output_dir = args.output_dir
os.makedirs(log_output_dir, exist_ok=True)
if args.rank == 0:
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
handlers=[
logging.StreamHandler(),
logging.FileHandler(
os.path.join(log_output_dir, f'log_{args.rank}.txt'))
],
level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN)
logger = logging.getLogger("Main")
print(logger)
else:
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
handlers=[logging.StreamHandler()],
level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN)
logger = logging.getLogger("Main")
print(logger)
logger.info("---------- SETTINGS ----------------")
initialize_distributed(args)
logger.warning(
f'args.world_size = {args.world_size}, args.rank ={args.rank}, args.local_rank = {args.local_rank}'
)
device, n_gpu = args_check(args, logger)
if args.local_rank in [-1, 0]:
os.makedirs(args.output_dir, exist_ok=True)
logger.info(f"Args : {args.__dict__}")
logger.info("---------- RANDOM SEEDs ----------------")
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
build_task(args)
# ---------- DATA LOADER ----------------
logger.info("---------- DATA LOADER ----------------")
train_loader, val_loader = pipeline.get_data_loader(args, logger)
logger.info(f'train loader: {train_loader}, length: {len(train_loader)}')
if len(args.train_dataset) > 1:
args.num_train_steps = sum([len(
train_loader[i]) for i in range(len(args.train_dataset))]) // args.gradient_accumulation_steps * args.num_epochs
else:
args.num_train_steps = len(
train_loader) // args.gradient_accumulation_steps * args.num_epochs
# ---------- MODELs ----------------
logger.info("----------- MODELS ------------")
if args.init_method == 'load_pretrain':
model_path = args.load
logger.info(f"Load model from {model_path}.")
if args.generator_version == 'fairseq':
model = OFAModel.from_pretrained(model_path, use_cache=True)
else:
model = OFAModel.from_pretrained(model_path, use_cache=False)
elif args.init_method == 'random':
configs = architecture_configs_dict[args.student_model_config]
config = configs['config']
if args.generator_version == 'fairseq':
model_config = OFAConfig(**config, use_cache=True)
else:
model_config = OFAConfig(**config, use_cache=False)
model = OFAModel(model_config)
else:
raise NotImplementedError(f"Illegal init_method {args.init_method}")
logger.info(f'Config of model: {model.config.to_dict()}')
logger.info(' Number of model parameters on rank {}: {}'.format(
torch.distributed.get_rank(),
sum([p.nelement() for p in model.parameters()])))
model.to(device)
adaptor = pipeline.get_adaptors(args)
# ---------- TRAIN ----------------
if args.do_train:
logger.info("---------- TRAIN ----------------")
logger.info(model.parameters())
optimizer = transformers.AdamW(
model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay,
correct_bias=False)
scheduler_class, scheduler_args = pipeline.get_schedule(args)
if args.ckpt_frequency > -1:
train_config = TrainingConfig(
gradient_accumulation_steps=args.gradient_accumulation_steps,
ckpt_frequency=args.ckpt_frequency,
log_dir=args.output_dir,
output_dir=args.output_dir,
device=args.device,
fp16=args.fp16,
local_rank=args.local_rank)
else:
train_config = TrainingConfig(
gradient_accumulation_steps=args.gradient_accumulation_steps,
ckpt_epoch_frequency=args.ckpt_epoch_frequency,
log_dir=args.output_dir,
output_dir=args.output_dir,
device=args.device,
fp16=args.fp16,
local_rank=args.local_rank)
logger.info(f"Train_config:\n {train_config}")
trainer = BasicTrainer(train_config, model, adaptor)
# ---------- EVAL ----------------
args.evaluate_idx = 0
callback_func = partial(ddp_evaluate,
eval_dataloader=val_loader,
args=args,
logger=logger)
def batch_postprocessor(batch):
return batch
with trainer:
trainer.train(optimizer,
scheduler_class=scheduler_class,
scheduler_args=scheduler_args,
max_grad_norm=args.clip_grad,
dataloader=train_loader,
num_epochs=args.num_epochs,
callback=callback_func,
batch_postprocessor=batch_postprocessor)
if __name__ == '__main__':
main()