-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
73 lines (70 loc) · 3.04 KB
/
utils.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
def slice_list(matrix,start_indices,left):
if isinstance(matrix[0], list):
if left:
return [row[start_index-1:-1] for row, start_index in zip(matrix, start_indices)]
else:
return [row[start_index:] for row, start_index in zip(matrix, start_indices)]
else:
if left:
return matrix[start_indices[0]-1:-1]
else:
return matrix[start_indices[0]:]
def test_prediction_acc(model, tok, prompts, targets, device, locality=False, vanilla_generation=False):
if vanilla_generation:
if isinstance(prompts, str):
prompts, targets = [prompts, ], [targets, ]
results = []
for prompt, target_new in zip(prompts, targets):
target_new_tokens = tok.encode(' '+target_new, add_special_tokens=False)
prompt_tok = tok(
prompt,
return_tensors="pt",
).to(device)
gen_token = model.generate(
input_ids=prompt_tok['input_ids'],
max_new_tokens=len(target_new_tokens),
)
if locality:
results.append(gen_token.detach().cpu().numpy().tolist()[0][-len(target_new_tokens):])
else:
results.append(np.mean(np.equal(target_new_tokens, gen_token.detach().cpu().numpy().tolist()[0][-len(target_new_tokens):])))
return results
if isinstance(prompts, str):
prompts,targets = [prompts,], [targets,]
prompt_target = [prompt + ' ' + target for prompt, target in zip(prompts,targets)]
max_prompt_len = max([len(tok.encode(_)) for _ in prompt_target]) + 1
prompt_target_tok = tok(
prompt_target,
max_length=max_prompt_len,
return_tensors="pt",
).to(f"cuda:0")
prompt_tok = tok(
prompts,
max_length=max_prompt_len,
return_tensors="pt",
)
num_prompt_toks = [int((i != tok.pad_token_id).sum()) for i in prompt_tok['input_ids']]
num_pad_toks = [int((i == tok.pad_token_id).sum()) for i in prompt_target_tok['input_ids'].cpu()]
prompt_len = [x+y for x,y in zip(num_pad_toks,num_prompt_toks)]
with torch.no_grad():
outputs = model.model(**prompt_target_tok)
if type(outputs) is torch.Tensor:
logits = outputs
else:
logits = outputs.logits
answers = torch.argmax(logits, dim=-1).squeeze().detach().cpu().numpy().tolist()
labels = prompt_target_tok['input_ids'].squeeze().detach().cpu().numpy().tolist()
answers = slice_list(answers,prompt_len,left=True)
labels = slice_list(labels,prompt_len,left=False)
if locality:
return answers if type(answers[0]) is list else [answers,]
if isinstance(answers[0], list):
res = []
for ans,label in zip(answers,labels):
temp_acc = np.mean(np.equal(ans, label))
if np.isnan(temp_acc):
continue
res.append(temp_acc)
return res
else:
return [np.mean(np.equal(answers, labels))]