forked from epfml/landmark-attention
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
executable file
·147 lines (118 loc) · 5.59 KB
/
main.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
# Copyright 2023 Amirkeivan Mohtashami, Martin Jaggi
#
# 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
import sys
import numpy as np
import torch
import inspect
import json
import copy
import argparse
import random
import wandb
import config
import models
from data import get_dataset, prepare_dataset
from optim.base import train_base
from optim.transformer_xl import train_xl
import distributed
def get_args():
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument('--config_format', default='base', choices=config.registered_formats())
args, rem_args = parser.parse_known_args()
return config.parse_args_with_format(format=args.config_format, base_parser=parser, args=rem_args, namespace=args)
def main(args):
torch.backends.cuda.matmul.allow_tf32 = True # allows us to make sure we're able to use tensorfloat32 during training
torch.backends.cudnn.allow_tf32 = True
distributed_backend = distributed.make_backend_from_args(args)
args = distributed_backend.get_adjusted_args_for_process(args)
args.device = torch.device(args.device)
torch.cuda.set_device(args.device)
device_type = 'cuda' if 'cuda' in str(args.device) else 'cpu'
torch.manual_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
print(f"Loading dataset '{args.dataset}'")
if distributed_backend.is_master_process():
prepare_dataset(args)
distributed_backend.sync()
data = get_dataset(args) # data is a dict: {'train': train_tokenized, 'val': eval_tokenized}
print(f"Num training tokens: {len(data['train'])}")
print(f"Num validation tokens: {len(data['val'])}")
model = models.make_model_from_args(args).to(args.device)
model = distributed_backend.transform_model(model)
group_specs = distributed_backend.get_raw_model(model).get_parameter_group_specs()
param_name_mapping = {p_name: p for p_name, p in model.named_parameters()}
optimized_params_cnt = 0
for g in group_specs:
params = []
for p_name in g["params"]:
translated_p_names = distributed_backend.translate_model_parameter_name_for_node(p_name)
params += [param_name_mapping[p_name] for p_name in translated_p_names]
g["params"] = params
optimized_params_cnt += sum([p.numel() for p in g["params"]])
print("number of optimized parameters: %.2fM" % (optimized_params_cnt/1e6,))
if args.opt == 'adamw':
use_fused = (device_type == 'cuda') and ('fused' in inspect.signature(torch.optim.AdamW).parameters)
print(f"using fused AdamW: {use_fused}")
extra_args = dict(fused=True) if use_fused else dict()
opt = torch.optim.AdamW(group_specs, lr=args.lr, betas=(args.beta1, args.beta2),
weight_decay=args.weight_decay, **extra_args)
elif args.opt == 'adafactor':
from optim.adafactor import Adafactor
opt = Adafactor(group_specs, lr=args.lr)
else:
opt = torch.optim.SGD(group_specs, lr=args.lr, momentum=0.9, weight_decay=args.weight_decay)
if args.scheduler != 'none':
if args.scheduler in ['cos', 'linear']:
scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer=opt, max_lr=args.lr, total_steps=args.iterations,
pct_start=args.warmup_percent, anneal_strategy=args.scheduler,
cycle_momentum=False, div_factor=1e2, final_div_factor=.05)
else:
raise NotImplementedError(f"Unknown scheduler type: {args.scheduler}.")
else:
scheduler = None
args.world_size = distributed_backend.get_world_size()
exp_name = args.exp_name
if distributed_backend.is_master_process() and args.wandb:
params_copy = copy.deepcopy(vars(args))
del params_copy['device']
wandb.init(project=args.wandb_project, name=exp_name, config=params_copy)
ckpt_path = f"{args.results_base_folder}/{args.dataset}/{args.model}/{exp_name}"
if not os.path.exists(ckpt_path):
if distributed_backend.is_master_process():
os.makedirs(ckpt_path)
else:
if os.path.isfile(f"{ckpt_path}/summary.json"): # the experiment was already completed
print(f"Already found experiment '{ckpt_path}'.\nSkipping.")
sys.exit(0)
if args.optimization_process == 'transformer_xl':
train = train_xl
else:
train = train_base
print(f"\nTraining model={args.model} \n{vars(args)}\n")
stats = train(model, opt, data, scheduler, args.iterations, args.acc_steps, args.batch_size, args.sequence_length,
eval_freq=args.eval_freq,
distributed_backend=distributed_backend,
ckpt_path=ckpt_path, extra_args=args)
args.device = None
args.dtype = None
stats['args'] = vars(args)
if distributed_backend.is_master_process():
with open(f"{ckpt_path}/summary.json", "w") as fs:
json.dump(stats, fs)
distributed_backend.finalize()
if __name__ == "__main__":
args = get_args()
main(args)