-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.py
More file actions
336 lines (281 loc) · 11.6 KB
/
Copy pathutils.py
File metadata and controls
336 lines (281 loc) · 11.6 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import gzip
import pickle
from tqdm import tqdm
from typing import Union
import errno
import torch
import sys
import logging
import json
from pathlib import Path
import torch.distributed as dist
import csv
import os
ROOT = "data/repobench"
def load_data(task:str, language:str, settings: Union[str, list]):
"""
Load data from the specified task and language.
Args:
task: the task to load
language: the language to load
settings: the settings to load
Returns:
data: the loaded data
"""
if task.lower() == 'r':
task = 'retrieval'
elif task.lower() == 'c':
task = 'completion'
elif task.lower() == 'p':
task = 'pipeline'
if language.lower() == 'py':
language = 'python'
if isinstance(settings, str):
settings = [settings]
for i, setting in enumerate(settings):
if setting.lower() == 'xf-f':
settings[i] = 'cross_file_first'
elif setting.lower() == 'xf-r':
settings[i] = 'cross_file_random'
elif setting.lower() == 'if':
settings[i] = 'in_file'
# some assertions
assert task.lower() in ['r', 'c', 'p', 'retrieval', 'completion', 'pipeline'], \
"task must be one of R, C, or P"
assert language.lower() in ['python', 'java', 'py'], \
"language must be one of python, java"
if task == "retrieval":
assert all([setting.lower() in ['cross_file_first', 'cross_file_random'] for setting in settings]), \
"For RepoBench-R, settings must be one of xf-f or xf-r"
else:
assert all([setting.lower() in ['cross_file_first', 'cross_file_random', 'in_file'] for setting in settings]), \
"Settings must be one of xf-f, xf-r, or if"
# load data
data = {}
# We split retrieval data into shards due to the github file size limit
if task == "retrieval":
for setting in tqdm(settings, desc=f"Loading data"):
# we only further split the cross_file_first setting for java
if setting == "cross_file_first" and language == "java":
dic = {
"train": {},
"test": {}
}
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_train_easy_1.gz", 'rb') as f:
train_easy_1 = pickle.load(f)
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_train_easy_2.gz", 'rb') as f:
train_easy_2 = pickle.load(f)
dic['train']['easy'] = train_easy_1['easy'] + train_easy_2['easy']
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_train_hard_1.gz", 'rb') as f:
train_hard_1 = pickle.load(f)
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_train_hard_2.gz", 'rb') as f:
train_hard_2 = pickle.load(f)
dic['train']['hard'] = train_hard_1['hard'] + train_hard_2['hard']
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_test.gz", 'rb') as f:
test = pickle.load(f)
dic['test'] = test['test']
data[setting] = dic
else:
dic = {
"train": {},
"test": {}
}
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_train_easy.gz", 'rb') as f:
train_easy = pickle.load(f)
dic['train']['easy'] = train_easy['easy']
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_train_hard.gz", 'rb') as f:
train_hard = pickle.load(f)
dic['train']['hard'] = train_hard['hard']
with gzip.open(f"{ROOT}/{task}/{language}/{setting}_test.gz", 'rb') as f:
test = pickle.load(f)
dic['test'] = test['test']
data[setting] = dic
else:
for setting in tqdm(settings, desc=f"Loading data"):
with gzip.open(f"{ROOT}/{task}/{language}/{setting}.gz", 'rb') as f:
data[setting] = pickle.load(f)
if len(settings) == 1:
return data[settings[0]]
else:
return [data[setting] for setting in settings]
logger = logging.getLogger(__name__)
def init_logger(is_main=True, is_distributed=False, filename=None):
if is_distributed:
torch.distributed.barrier()
handlers = [logging.StreamHandler(sys.stdout)]
if filename is not None:
handlers.append(logging.FileHandler(filename=filename))
logging.basicConfig(
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO if is_main else logging.WARN,
format="[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s",
handlers=handlers,
)
logging.getLogger('transformers.tokenization_utils').setLevel(logging.ERROR)
logging.getLogger('transformers.tokenization_utils_base').setLevel(logging.ERROR)
return logger
def get_checkpoint_path(opt):
checkpoint_path = Path(opt.checkpoint_dir) / opt.name
checkpoint_exists = checkpoint_path.exists()
if opt.is_distributed:
torch.distributed.barrier()
checkpoint_path.mkdir(parents=True, exist_ok=True)
return checkpoint_path, checkpoint_exists
def symlink_force(target, link_name):
try:
os.symlink(target, link_name)
except OSError as e:
if e.errno == errno.EEXIST:
os.remove(link_name)
os.symlink(target, link_name)
else:
raise e
def save(model, optimizer, scheduler, step, best_eval_metric, opt, dir_path, name):
model_to_save = model.module if hasattr(model, "module") else model
path = os.path.join(dir_path, "checkpoint")
epoch_path = os.path.join(path, name) #"step-%s" % step)
os.makedirs(epoch_path, exist_ok=True)
model_to_save.save_pretrained(epoch_path)
cp = os.path.join(path, "latest")
fp = os.path.join(epoch_path, "optimizer.pth.tar")
checkpoint = {
"step": step,
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
"opt": opt,
"best_eval_metric": best_eval_metric,
}
torch.save(checkpoint, fp)
symlink_force(epoch_path, cp)
def load(model_class, dir_path, opt, reset_params=False):
epoch_path = os.path.realpath(dir_path)
optimizer_path = os.path.join(epoch_path, "optimizer.pth.tar")
logger.info("Loading %s" % epoch_path)
model = model_class.from_pretrained(epoch_path)
model = model.to(opt.device)
logger.info("loading checkpoint %s" %optimizer_path)
checkpoint = torch.load(optimizer_path, map_location=opt.device)
opt_checkpoint = checkpoint["opt"]
step = checkpoint["step"]
if "best_eval_metric" in checkpoint:
best_eval_metric = checkpoint["best_eval_metric"]
else:
best_eval_metric = checkpoint["best_dev_em"]
if not reset_params:
optimizer, scheduler = set_optim(opt_checkpoint, model)
scheduler.load_state_dict(checkpoint["scheduler"])
optimizer.load_state_dict(checkpoint["optimizer"])
else:
optimizer, scheduler = set_optim(opt, model)
return model, optimizer, scheduler, opt_checkpoint, step, best_eval_metric
class WarmupLinearScheduler(torch.optim.lr_scheduler.LambdaLR):
def __init__(self, optimizer, warmup_steps, scheduler_steps, min_ratio, fixed_lr, last_epoch=-1):
self.warmup_steps = warmup_steps
self.scheduler_steps = scheduler_steps
self.min_ratio = min_ratio
self.fixed_lr = fixed_lr
super(WarmupLinearScheduler, self).__init__(
optimizer, self.lr_lambda, last_epoch=last_epoch
)
def lr_lambda(self, step):
if step < self.warmup_steps:
return (1 - self.min_ratio)*step/float(max(1, self.warmup_steps)) + self.min_ratio
if self.fixed_lr:
return 1.0
return max(0.0,
1.0 + (self.min_ratio - 1) * (step - self.warmup_steps)/float(max(1.0, self.scheduler_steps - self.warmup_steps)),
)
class FixedScheduler(torch.optim.lr_scheduler.LambdaLR):
def __init__(self, optimizer, last_epoch=-1):
super(FixedScheduler, self).__init__(optimizer, self.lr_lambda, last_epoch=last_epoch)
def lr_lambda(self, step):
return 1.0
def set_dropout(model, dropout_rate):
for mod in model.modules():
if isinstance(mod, torch.nn.Dropout):
mod.p = dropout_rate
def set_optim(opt, model):
if opt.optim == 'adam':
optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr)
elif opt.optim == 'adamw':
optimizer = torch.optim.AdamW(model.parameters(), lr=opt.lr, weight_decay=opt.weight_decay)
if opt.scheduler == 'fixed':
scheduler = FixedScheduler(optimizer)
elif opt.scheduler == 'linear':
if opt.scheduler_steps is None:
scheduler_steps = opt.total_steps
else:
scheduler_steps = opt.scheduler_steps
scheduler = WarmupLinearScheduler(optimizer, warmup_steps=opt.warmup_steps, scheduler_steps=scheduler_steps, min_ratio=0., fixed_lr=opt.fixed_lr)
return optimizer, scheduler
def average_main(x, opt):
if not opt.is_distributed:
return x
if opt.world_size > 1:
dist.reduce(x, 0, op=dist.ReduceOp.SUM)
if opt.is_main:
x = x / opt.world_size
return x
def sum_main(x, opt):
if not opt.is_distributed:
return x
if opt.world_size > 1:
dist.reduce(x, 0, op=dist.ReduceOp.SUM)
return x
def weighted_average(x, count, opt):
if not opt.is_distributed:
return x, count
t_loss = torch.tensor([x * count], device=opt.device)
t_total = torch.tensor([count], device=opt.device)
t_loss = sum_main(t_loss, opt)
t_total = sum_main(t_total, opt)
return (t_loss / t_total).item(), t_total.item()
def write_output(glob_path, output_path):
files = list(glob_path.glob('*.txt'))
files.sort()
with open(output_path, 'w') as outfile:
for path in files:
with open(path, 'r') as f:
lines = f.readlines()
for line in lines:
outfile.write(line)
path.unlink()
glob_path.rmdir()
def save_distributed_dataset(data, opt):
dir_path = Path(opt.checkpoint_dir) / opt.name
write_path = dir_path / 'tmp_dir'
write_path.mkdir(exist_ok=True)
tmp_path = write_path / f'{opt.global_rank}.json'
with open(tmp_path, 'w') as fw:
json.dump(data, fw)
if opt.is_distributed:
torch.distributed.barrier()
if opt.is_main:
final_path = dir_path / 'dataset_wscores.json'
logger.info(f'Writing dataset with scores at {final_path}')
glob_path = write_path / '*'
results_path = write_path.glob('*.json')
alldata = []
for path in results_path:
with open(path, 'r') as f:
data = json.load(f)
alldata.extend(data)
path.unlink()
with open(final_path, 'w') as fout:
json.dump(alldata, fout, indent=4)
write_path.rmdir()
def load_passages(path):
if not os.path.exists(path):
logger.info(f'{path} does not exist')
return
logger.info(f'Loading passages from: {path}')
passages = []
with open(path) as fin:
reader = csv.reader(fin, delimiter='\t')
for k, row in enumerate(reader):
if not row[0] == 'id':
try:
passages.append((row[0], row[1], row[2]))
except:
logger.warning(f'The following input line has not been correctly loaded: {row}')
return passages