-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
401 lines (313 loc) · 13.4 KB
/
Copy pathutils.py
File metadata and controls
401 lines (313 loc) · 13.4 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import sys
import torch
import gc
from deepspeed.accelerator import get_accelerator
from tqdm import tqdm
import torch.distributed as dist
from torch import nn, Tensor
import torch.nn.functional as F
from transformers import PreTrainedTokenizer
from transformers import PreTrainedModel, StoppingCriteria
def set_pad_token(tokenizer: PreTrainedTokenizer, model: PreTrainedModel, pad_token='<|pad|>'):
if tokenizer.pad_token is None:
tokenizer.add_special_tokens(dict(pad_token=pad_token))
current_size = model.get_input_embeddings().num_embeddings
if len(tokenizer) > current_size:
model.resize_token_embeddings(len(tokenizer))
num_new_tokens = len(tokenizer) - current_size
input_embeddings = model.get_input_embeddings().weight.data
output_embeddings = model.get_output_embeddings().weight.data
input_embeddings_avg = input_embeddings[:-num_new_tokens].mean(dim=0, keepdim=True)
output_embeddings_avg = output_embeddings[:-num_new_tokens].mean(dim=0, keepdim=True)
input_embeddings[-num_new_tokens:] = input_embeddings_avg
output_embeddings[-num_new_tokens:] = output_embeddings_avg
class StdOutFilter(object):
def __init__(self, stream):
self.stream = stream
self.triggered = False
self.filters = ['Invalidate trace cache', 'Positional args are being deprecated', 'Could not estimate the number', 'pytorch allocator cache flushes']
def __getattr__(self, attr_name):
return getattr(self.stream, attr_name)
def write(self, data):
flag = False
for fil in self.filters:
if fil in data:
flag = True
break
if data == '\n' and self.triggered:
self.triggered = False
else:
if not flag:
self.stream.write(data)
self.stream.flush()
else:
# caught bad pattern
self.triggered = True
def flush(self):
self.stream.flush()
def enable_logging_filter():
sys.stdout = StdOutFilter(sys.stdout)
sys.stderr = StdOutFilter(sys.stderr)
class MemCache:
def __init__(self, rank, load, worldsize):
self.cache = {}
self.max_reserved = 0
self.max_allocate = 0
self.rank = rank
self.freq = 0
load = load.lower()
if '6b' in load:
self.freq = 1
elif '2.7b' in load:
self.freq = 2
elif '1.3b' in load:
self.freq = 4
elif 'large' in load:
self.freq = 4
if worldsize < 4:
self.freq = 1
self.call_cnt = 0
@staticmethod
def torch_empty_cache():
get_accelerator().empty_cache()
@staticmethod
def byte2mb(bt):
return round(bt / (1024 ** 2), 3)
def mclean(self):
self.call_cnt += 1
if self.call_cnt >= self.freq:
self.call_cnt = 0
r0 = torch.cuda.memory_reserved(self.rank)
a0 = torch.cuda.memory_allocated(self.rank)
f0 = r0 - a0
for key in list(self.cache.keys()):
del self.cache[key]
gc.collect()
get_accelerator().empty_cache()
r1 = torch.cuda.memory_reserved(self.rank)
a1 = torch.cuda.memory_allocated(self.rank)
f1 = r1 - a1
# print('Mem Free')
# print(f'Reserved \t {MemCache.byte2mb(r1 - r0)}MB')
# print(f'Allocated \t {MemCache.byte2mb(a1 - a0)}MB')
# print(f'Free \t {MemCache.byte2mb(f1 - f0)}MB')
def __setitem__(self, key, value):
self.cache[key] = value
self.max_reserved = max(self.max_reserved, torch.cuda.memory_reserved(self.rank))
self.max_allocate = max(self.max_allocate, torch.cuda.memory_allocated(self.rank))
def __getitem__(self, item):
return self.cache[item]
def __delitem__(self, *keys):
r0 = torch.cuda.memory_reserved(self.rank)
a0 = torch.cuda.memory_allocated(self.rank)
f0 = r0 - a0
for key in keys:
del self.cache[key]
r1 = torch.cuda.memory_reserved(self.rank)
a1 = torch.cuda.memory_allocated(self.rank)
f1 = r1 - a1
# print('Cuda Free')
# print(f'Reserved \t {MemCache.byte2mb(r1 - r0)}MB')
# print(f'Allocated \t {MemCache.byte2mb(a1 - a0)}MB')
# print(f'Free \t {MemCache.byte2mb(f1 - f0)}MB')
def show_cuda_info(self):
t = torch.cuda.get_device_properties(self.rank).total_memory
r = torch.cuda.memory_reserved(self.rank)
a = torch.cuda.memory_allocated(self.rank)
f = r - a
print('Cuda Info')
print(f'Total \t{MemCache.byte2mb(t)} MB')
print(f'Reserved \t{MemCache.byte2mb(r)} [{MemCache.byte2mb(self.max_reserved)}] MB')
print(f'Allocated \t{MemCache.byte2mb(a)} [{MemCache.byte2mb(self.max_allocate)}] MB')
print(f'Free \t{MemCache.byte2mb(f)} MB')
class CosineSimilarityDistance(nn.Module):
__constants__ = ['dim', 'eps']
dim: int
eps: float
def __init__(self, dim: int = 1, eps: float = 1e-8) -> None:
super(CosineSimilarityDistance, self).__init__()
self.dim = dim
self.eps = eps
def forward(self, x1: Tensor, x2: Tensor) -> Tensor:
return -F.cosine_similarity(x1, x2, self.dim, self.eps)
def prepare_model(load, checkpoint=None, fp16=False, random_init=False):
from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM, AutoConfig
if isinstance(checkpoint, str):
if checkpoint.lower() == 'none':
checkpoint = None
if random_init:
assert checkpoint is None
import torch
load = load.lower()
if 'xxl' in load or fp16:
kwargs = {'torch_dtype': torch.float16}
else:
kwargs = {}
seq2seq = ['flan', 't5']
decoder = ['gpt', 'opt']
for m in seq2seq:
if m in load:
if random_init:
return AutoModelForSeq2SeqLM.from_config(AutoConfig.from_pretrained(load))
else:
return AutoModelForSeq2SeqLM.from_pretrained(checkpoint if checkpoint else load, **kwargs)
for m in decoder:
if m in load:
if random_init:
return AutoModelForCausalLM.from_config(AutoConfig.from_pretrained(load))
else:
return AutoModelForCausalLM.from_pretrained(checkpoint if checkpoint else load, **kwargs)
raise NotImplementedError()
def get_generation_config(load: str, tokenizer, is_decode_eos_token: bool, max_new_tokens=None, dataset=None):
from transformers import GenerationConfig
from tools import tools_is_decoder_only, tools_get_logger
if max_new_tokens is None:
assert isinstance(dataset, str)
max_new_tokens = {
'gsm8k': 256,
'svamp': 192,
'multiarith': 192,
'cqa': 96,
'sqa': 128,
}[dataset]
if tools_is_decoder_only(load):
if is_decode_eos_token:
gen_config = GenerationConfig(
do_sample=False,
max_new_tokens=max_new_tokens,
num_beams=1,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.encode('\n\n').append(tokenizer.eos_token_id),
begin_suppress_tokens=tokenizer.all_special_ids,
)
else:
gen_config = GenerationConfig(
do_sample=False,
max_new_tokens=max_new_tokens,
num_beams=1,
pad_token_id=tokenizer.pad_token_id,
# eos_token_id=tokenizer.encode('\n\n')
)
# if tokenizer.pad_token is None:
# tokenizer.pad_token = tokenizer.eos_token
else:
try:
gen_config = GenerationConfig.from_pretrained(
load,
do_sample=False,
max_new_tokens=max_new_tokens,
num_beams=1,
# top_p=0.9,
# repetition_penalty=0.6,
# no_repeat_ngram_size=3,
)
except Exception as e:
tools_get_logger('tools').error(f"the generation config not exists thus reset, error msg:\n{e}")
gen_config = GenerationConfig(
do_sample=False,
max_new_tokens=max_new_tokens,
num_beams=1,
)
return gen_config
def seed_everything(seed: int):
import random, os
import numpy as np
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
class StoppingCriteriaEos(StoppingCriteria):
def __init__(self, eos_token_id, line_break_id, double_line_break_id):
super().__init__()
self.eos_token_id = eos_token_id
self.line_break_id = line_break_id
self.double_line_break_id = double_line_break_id
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs):
should_stop = True
gen_len = len(scores)
for inputs in input_ids.flip(dims=[1]):
flag = False
for i in range(gen_len):
if inputs[i] == self.eos_token_id: flag = True
elif inputs[i] == self.double_line_break_id: flag = True
elif i + 1 < gen_len and inputs[i] == self.line_break_id and inputs[i+1] == self.line_break_id: flag = True
if not flag:
should_stop = False
break
return should_stop
@torch.no_grad()
def infer_reasoning_dataset(task, test_loader, rank: int, local_rank: int, world_size: int, desc: str, model, gen_config, is_decoder_only: bool, infer_tokenizer, logger, group_gloo, verbose=False) -> dict:
"""
:return: only world_rank == 0 will return a dict
others will return None
"""
from tools import cleanup, score
correct = torch.tensor(0.0, device=rank)
total = torch.tensor(0.0, device=rank)
all_generated = []
get_accelerator().empty_cache()
stopping_criteria = [
StoppingCriteriaEos(eos_token_id=infer_tokenizer.eos_token_id, line_break_id=infer_tokenizer.encode('\n')[0], double_line_break_id=infer_tokenizer.encode('\n\n')[0])
]
# stopping_criteria = []
with tqdm(total=len(test_loader), desc=desc, disable=local_rank > 0) as pbar:
for batch in test_loader:
inputs = batch['input']
bsz = len(batch['answer'])
total += bsz
# direct generate the cot and answer, the Flan T5 default method
outs = model.generate(**inputs.to(model.device), return_dict_in_generate=True, output_scores=True,generation_config=gen_config, stopping_criteria=stopping_criteria)['sequences']
if is_decoder_only:
outs = outs[:, inputs['input_ids'].shape[1]:]
decode_outs = infer_tokenizer.batch_decode(outs, skip_special_tokens=True, clean_up_tokenization_spaces=True)
raw_outs = infer_tokenizer.batch_decode(outs, skip_special_tokens=False, clean_up_tokenization_spaces=False)
pred = []
outs = [None for _ in decode_outs]
for i, o in enumerate(decode_outs):
outs[i], p = cleanup(o, task)
pred.append(p)
if local_rank <= 0 and (pbar.n < 1 or (verbose and pbar.n % 4 == 0)):
logger.info(f"{'-'*160}\n"
f"-----------------input text--------------\n\n{batch['input_text'][0]}\n\n"
f"-----------------raw decode---------------\n\n{[raw_outs[0]]}\n\n"
f"-----------------clean special decode---------------\n\n{decode_outs[0]}\n\n"
f"-----------------cleaned generated---------------\n\n{outs[0]}\n\n"
f"-----------------prediction--------------\n\n{pred[0]}\n\n"
f"-----------------final answer------------\n\n{batch['final_ans'][0]}\n"
f"{'-'*160}\n")
correct_num, flags = score(pred, batch['final_ans'])
correct += correct_num
for i in range(len(flags)):
all_generated.append(
{
'question': batch['question'][i],
'answer': batch['answer'][i],
'final_ans': batch['final_ans'][i],
'generated': outs[i],
'pred': pred[i],
'correct': flags[i],
}
)
get_accelerator().empty_cache()
pbar.set_postfix_str(f"Acc {correct.item() / total.item() * 100:.4f}, {correct.item()} / {total.item()}")
pbar.update(1)
# save results
dist.monitored_barrier(group_gloo)
gathers = [None for _ in range(world_size)] if rank <= 0 else None
dist.gather_object(all_generated, gathers, group=group_gloo)
if rank <= 0:
res = []
unique = set()
temp_correct = 0
for dist_gather in gathers:
for item in dist_gather:
if item['question'] not in unique:
unique.add(item['question'])
res.append(item)
if item['correct']: temp_correct += 1
return {'generated': res, 'acc': temp_correct / len(res) * 100, 'correct': temp_correct, 'total': len(res)}
else:
return {}