-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval.py
184 lines (159 loc) · 6.79 KB
/
eval.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
"""Evaluation harness to test editor models."""
import os
from collections import defaultdict
from contextlib import nullcontext
import numpy as np
import torch
from torch.nn.utils.rnn import pad_sequence
from tqdm import tqdm
from transformers import AutoTokenizer
from helpers import slice_and_move_batch_for_device, visualize_interventions
from logger import get_logger
from models.base import BaseEditor
from models.utils import add_fwd_hooks
logger = get_logger(__name__)
@torch.no_grad()
def evaluate(config, model: BaseEditor, dataloader):
model = model.to(torch.cuda.current_device())
summary_metrics = defaultdict(list)
tokenizer = AutoTokenizer.from_pretrained(config.model.name_or_path)
tokenizer.pad_token_id = tokenizer.eos_token_id
model.target_model.generation_config.pad_token_id = tokenizer.pad_token_id
for eval_step, batch in enumerate(tqdm(dataloader)):
batch = slice_and_move_batch_for_device(batch, 0, 1)
if config.eval.enable_editor:
editor_output = model(
editor_input_ids=batch["editor_input_ids"],
editor_attention_mask=batch["editor_attention_mask"],
target_input_ids=batch["target_input_ids"],
target_attention_mask=batch["target_attention_mask"],
stop_editing_idx=config.train.stop_editing_idx,
output_hooks=True,
output_target_hidden_states=True,
output_edit_vectors=True,
output_editor_attention=True,
)
else:
editor_output = None
if editor_output and eval_step % config.eval.visualize_interval == 0:
target_out = model.target_model(
input_ids=batch["target_input_ids"],
attention_mask=batch["target_attention_mask"],
)
visualize_interventions(
result=editor_output,
orig_logits=target_out.logits,
batch=batch,
save_path=os.path.join(
config.eval_dir, config.exp_name, "step-{}".format(eval_step)
),
tokenizer=tokenizer,
stopping_index=config.train.stop_editing_idx,
metadata=config,
)
with add_fwd_hooks(editor_output.hooks) if editor_output else nullcontext():
if not config.eval.enable_editor:
# concatenate editor input and target input, left pad at the end
target_input_ids = concat_and_left_pad(
batch["editor_input_ids"],
batch["editor_attention_mask"],
batch["target_input_ids"],
batch["target_attention_mask"],
pad_value=tokenizer.pad_token_id,
)
target_attention_mask = concat_and_left_pad(
batch["editor_attention_mask"],
batch["editor_attention_mask"],
batch["target_attention_mask"],
batch["target_attention_mask"],
pad_value=0,
)
else:
target_input_ids = batch["target_input_ids"]
target_attention_mask = batch["target_attention_mask"]
generation_results = model.target_model.generate(
input_ids=target_input_ids,
attention_mask=target_attention_mask,
max_new_tokens=config.eval.max_new_tokens,
do_sample=True,
top_p=config.eval.top_p,
top_k=config.eval.top_k,
temperature=config.eval.temperature,
)
input_shape = batch["target_input_ids"].shape[1]
generation_results = generation_results[:, input_shape:]
# decode
decoded_generation_results = tokenizer.batch_decode(
generation_results, skip_special_tokens=True
)
decoded_targets = tokenizer.batch_decode(
batch["target_input_ids"], skip_special_tokens=True
)
batch_metrics = np.array(
[
[
compute_exact_match(gen, tgt),
compute_recall(gen, tgt),
compute_f1(gen, tgt),
]
for gen, tgt in zip(decoded_generation_results, decoded_targets)
]
)
summary_metrics["exact_match"].extend(batch_metrics[:, 0].flatten().tolist())
summary_metrics["recall"].extend(batch_metrics[:, 1].flatten().tolist())
summary_metrics["f1"].extend(batch_metrics[:, 2].flatten().tolist())
for k, v in summary_metrics.items():
summary_metrics[k] = np.mean(v)
logger.info(
f"Exact match: {summary_metrics['exact_match']:.4f}, "
f"Recall: {summary_metrics['recall']:.4f}, "
f"F1: {summary_metrics['f1']:.4f}"
)
def concat_and_left_pad(tensor1, mask1, tensor2, mask2, pad_value=0):
concatenated = [
torch.cat([t1[m1 > 0], t2[m2 > 0]]).flip(-1)
for t1, m1, t2, m2 in zip(tensor1, mask1, tensor2, mask2)
]
padded = pad_sequence(concatenated, batch_first=True, padding_value=pad_value)
result = padded.flip(1)
return result
def compute_f1(prediction_tokens, ground_truth_tokens):
"""
Compute F1 score between prediction and ground truth tokens.
"""
prediction_tokens = prediction_tokens.strip()
ground_truth_tokens = ground_truth_tokens.strip()
prediction_set = set(prediction_tokens)
ground_truth_set = set(ground_truth_tokens)
if not ground_truth_set or not prediction_set:
return 0.0
intersection = prediction_set.intersection(ground_truth_set)
precision_score = len(intersection) / len(prediction_set)
recall_score = len(intersection) / len(ground_truth_set)
if precision_score + recall_score == 0:
return 0.0
f1_score = 2 * (precision_score * recall_score) / (precision_score + recall_score)
return f1_score
def compute_recall(prediction_tokens, ground_truth_tokens):
"""
Compute recall score between prediction and ground truth tokens.
"""
prediction_tokens = prediction_tokens.strip()
ground_truth_tokens = ground_truth_tokens.strip()
prediction_set = set(prediction_tokens)
ground_truth_set = set(ground_truth_tokens)
if not ground_truth_set:
return 0.0
intersection = prediction_set.intersection(ground_truth_set)
recall_score = len(intersection) / len(ground_truth_set)
return recall_score
def compute_exact_match(prediction_tokens, ground_truth_tokens):
"""
Compute exact match score between prediction and ground truth tokens.
"""
prediction_tokens = prediction_tokens.strip()
ground_truth_tokens = ground_truth_tokens.strip()
if prediction_tokens == ground_truth_tokens:
return 1.0
else:
return 0.0