-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
193 lines (168 loc) · 7 KB
/
config.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
import os
import argparse
import json
root_path = "/mnt/inspurfs/user-fs/zhaoyu/GlyphBERT"
data_path = "/mnt/inspurfs/user-fs/zhaoyu/pretrain_data"
def get_path(p, is_data=False):
if is_data:
return os.path.join(data_path, p)
else:
return os.path.join(root_path, p)
config = {
# env config
"device": "4,5,6,7", # "4,5,6,7"
"parallel": None, # "data_parallel" "DDP" None
"local_rank": None,
# training config
"batch_per_card": 16,
"batch_expand_times": 16,
"epoch": 20,
"warm_up": 10000,
"lr": 1e-4,
"weight_decay": 0.01,
"batch": None, # batch will be calculate based on batch_per_card and batch_expand_times
"glyph_map": True, # False True
"dataloader_workers": 4,
# model config
"CNN_name": "AddBertResPos3",
"use_res2bert": True,
"cnn_and_embed_mat": True,
"add_nsp_task": True,
"state_dict": "./save/time[11-06-15-47]-step[16000].pt",
"board_path": "WikiFromEpoch4",
# path config
"bmp_path": get_path("data/bmp", is_data=True),
"vocab_path": get_path("data/vocab_bmp.txt", is_data=True),
"bert_config_path": get_path("pretrained_model/config.json"),
"pretrained_data_name": "dupe4wiki", # one_wiki DEBUG overflow dupe4wiki
}
parser = argparse.ArgumentParser()
# env config
parser.add_argument('--device', default=config.get('device'), type=str, required=False)
parser.add_argument('--parallel', default=config.get('parallel'), type=str, required=False)
# training config
parser.add_argument('--batch_per_card', default=config.get('batch_per_card'), type=int, required=False)
parser.add_argument('--batch_expand_times', default=config.get('batch_expand_times'), type=int, required=False)
parser.add_argument('--epoch', default=config.get('epoch'), type=int, required=False)
parser.add_argument('--warm_up', default=config.get('warm_up'), type=int, required=False)
parser.add_argument('--lr', default=config.get('lr'), type=float, required=False)
parser.add_argument('--glyph_map', default=config.get('glyph_map'), type=bool, required=False)
# model config
parser.add_argument('--CNN_name', default=config.get('CNN_name'), type=str, required=False)
parser.add_argument('--use_res2bert', default=config.get('use_res2bert'), type=bool, required=False)
parser.add_argument('--cnn_and_embed_mat', default=config.get('cnn_and_embed_mat'), type=bool, required=False)
parser.add_argument('--state_dict', default=config.get('state_dict'), type=str, required=False)
parser.add_argument('--add_nsp_task', default=config.get('add_nsp_task'), type=bool, required=False)
# path config
parser.add_argument('--pretrained_data_name', default=config.get('pretrained_data_name'), type=str, required=False)
# DDP
parser.add_argument('--local_rank', default=config.get('local_rank'), type=int, required=False)
args = vars(parser.parse_args())
cards_num = len(list(args['device'].split(',')))
batch_per_card = args['batch_per_card']
if args['parallel'] != "DDP":
config['batch_size'] = cards_num * batch_per_card
else:
config['batch_size'] = batch_per_card
for k in args.keys():
if k not in config.keys():
print("add new config key: {}={}".format(k, args[k]))
config[k] = args[k]
if config.get('sentence_path') is None:
vocab_size = 18612
if config['pretrained_data_name'] == "13w":
config['data_path'] = "./data/sentence_pair_with_mask_data_large.pkl"
elif config['pretrained_data_name'] == "100w":
config['data_path'] = "./data/sentence_pair_2_1.pkl"
elif config['pretrained_data_name'] == "DEBUG":
config['data_path'] = "./data/sentence_pair_with_mask_data.pkl"
elif config['pretrained_data_name'] == "all":
all_data_path_list = [
"./data/all-dupe4-2021-4-2/{}.pkl".format(i) for i in range(13)
]
all_data_path_list += [
"./data/4-22-data/sentence_pair/THUC_836070_{}.pkl".format(i) for i in range(14)
]
all_data_path_list += [
"./data/4-22-data/sentence_pair/sogou0103data406500_{}.pkl".format(i) for i in range(3)
]
all_data_path_list += [
"./data/4-22-data/sentence_pair/QAbaike409600_{}.pkl".format(i) for i in range(3)
]
config['data_path'] = all_data_path_list
elif config['pretrained_data_name'] == '300w':
config['data_path'] = ["./data/4-18-300w/{}.pkl".format(i) for i in range(3)]
elif config['pretrained_data_name'] == "dupe4wiki":
all_data_path_list = [
"./data/dupe4wiki-10-13/{}.pkl".format(i) for i in range(13)
]
config['data_path'] = all_data_path_list
elif config['pretrained_data_name'] == "dupe4wiki16":
all_data_path_list = [
"./data/for16gpus/{}.pkl".format(i) for i in range(16)
]
config['data_path'] = all_data_path_list
elif config['pretrained_data_name'] == "one_wiki":
config['data_path'] = "./data/dupe4wiki-10-13/0.pkl"
else:
if input("\n Build sentence pair, input[y]: ") != 'y':
exit(-1)
config.update({
"data_path": "./data/2021-04-18-sentence-pair-300w.pkl"
})
vocab_size = 18612
if isinstance(config['data_path'], str):
config['data_path'] = [get_path(config['data_path'], is_data=True)]
else:
for idx in range(len(config['data_path'])):
config['data_path'][idx] = get_path(config['data_path'][idx], is_data=True)
config['vocab_size'] = vocab_size
os.environ["CUDA_VISIBLE_DEVICES"] = config['device']
# recover the previous training state
if config.get('state_dict'):
import torch
state_dict = torch.load(config['state_dict'], map_location='cpu')
training_state = state_dict['training_state']
prev_config = state_dict['config']
config['lr'] = training_state['last_lr']
if type(prev_config['warm_up']) is float:
warm_up_step = training_state['total_optimize_step'] * prev_config['warm_up']
else:
warm_up_step = config['warm_up']
assert training_state['optimize_step'] >= warm_up_step
config['warm_up'] = 0
config['rest_optimize_step'] = training_state['total_optimize_step'] - training_state['optimize_step']
config['training_state'] = training_state
print("")
print("-" * 12 + "Config" + "-" * 12)
for k, v in config.items():
if isinstance(v, list):
if len(v) == 0:
print(k, v)
else:
print(k, ':')
for i in v:
print("\t{}".format(i))
else:
print("{}: {}".format(k, v))
print("-" * 30)
# import random
# import pickle
# from tqdm import tqdm
#
# all_data = []
# for i in config['data_path']:
# print("load {}".format(i))
# all_data.extend(pickle.load(open(i, 'rb')))
# random.shuffle(all_data)
# random.shuffle(all_data)
# random.shuffle(all_data)
# each_size = 1000000
# cnt = 0
# for i in tqdm(range(0, len(all_data), each_size), total=len(all_data) // each_size + 1):
# end = min(len(all_data), i + each_size)
# pickle.dump(
# all_data[i: end],
# open("/mnt/inspurfs/user-fs/zhaoyu/pretrain_data/data/dupe4wiki-10-13/{}.pkl".format(cnt), 'wb')
# )
# cnt += 1