-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_util.py
343 lines (287 loc) · 15.6 KB
/
evaluate_util.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
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
from tqdm import tqdm
from data_module import TextDatasetQA, custom_data_collator, get_batch_loss, custom_data_collator_with_indices
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
import os, hydra
import evaluate
import json
from pathlib import Path
from rouge_score import rouge_scorer
from utils import get_model_identifiers_from_yaml, get_model_utility, get_forget_quality
import torch.nn as nn
import csv
import numpy as np
import datasets
def eval_perturbation_ratio(eval_dataloader, perturb_dataloader, model):
eval_logs = {}
for batch, perturb_batch in tqdm(zip(eval_dataloader, perturb_dataloader)):
input_ids, labels, attention_mask, indices = batch
batch = {"input_ids": input_ids, "labels": labels, "attention_mask": attention_mask}
perturb_input_ids, perturb_labels, perturb_attention_mask, _ = perturb_batch
if len(perturb_input_ids.shape) > 2:
bsz, seq_len = perturb_input_ids.shape[0:2]
else:
bsz = perturb_input_ids.shape[0]
seq_len = 1
perturb_batch = {"input_ids": perturb_input_ids.view(bsz*seq_len, -1), "labels": perturb_labels.view(bsz*seq_len, -1), "attention_mask": perturb_attention_mask.view(bsz*seq_len, -1)}
#send to device
for k, v in batch.items():
batch[k] = v.to(model.device)
for k, v in perturb_batch.items():
perturb_batch[k] = v.to(model.device)
with torch.no_grad():
outputs = model(**batch)
perturb_outputs = model(**perturb_batch)
gt_loss = get_batch_loss(outputs.logits, batch['labels'])
perturb_loss = get_batch_loss(perturb_outputs.logits, perturb_batch['labels']).view(bsz, seq_len)
num_token_gt = (batch['labels']!=-100).sum(-1)
num_token_perturb = (perturb_batch['labels']!=-100).view(bsz, seq_len, -1).sum(-1)
perturb_loss_per_token = perturb_loss/num_token_perturb
gt_loss_per_token = gt_loss/num_token_gt
# truth_ratio = torch.exp(-1 * perturb_loss_per_token).mean(-1) / torch.exp(-1 * gt_loss_per_token)
truth_ratio = torch.exp(gt_loss_per_token - perturb_loss_per_token.mean(-1))
# zip index and each stat into a dict
perturb_loss_per_token = dict(zip(indices.cpu().numpy().tolist(), perturb_loss_per_token.cpu().numpy().tolist()))
gt_loss_per_token = dict(zip(indices.cpu().numpy().tolist(), gt_loss_per_token.cpu().numpy().tolist()))
truth_ratio = dict(zip(indices.cpu().numpy().tolist(), truth_ratio.cpu().numpy().tolist()))
gt_loss = dict(zip(indices.cpu().numpy().tolist(), gt_loss.cpu().numpy().tolist()))
perturb_loss = dict(zip(indices.cpu().numpy().tolist(), perturb_loss.cpu().numpy().tolist()))
num_token_gt = dict(zip(indices.cpu().numpy().tolist(), num_token_gt.cpu().numpy().tolist()))
num_token_perturb = dict(zip(indices.cpu().numpy().tolist(), num_token_perturb.cpu().numpy().tolist()))
# merge dicts
if 'average_perturb_loss' not in eval_logs:
eval_logs['average_perturb_loss'] = {}
if 'avg_paraphrased_loss' not in eval_logs:
eval_logs['avg_paraphrased_loss'] = {}
if 'truth_ratio' not in eval_logs:
eval_logs['truth_ratio'] = {}
if 'paraphrased_loss' not in eval_logs:
eval_logs['paraphrased_loss'] = {}
if 'perturb_loss' not in eval_logs:
eval_logs['perturb_loss'] = {}
if 'num_token_paraphrased' not in eval_logs:
eval_logs['num_token_paraphrased'] = {}
if 'num_token_perturb' not in eval_logs:
eval_logs['num_token_perturb'] = {}
eval_logs['average_perturb_loss'].update(perturb_loss_per_token)
eval_logs['avg_paraphrased_loss'].update(gt_loss_per_token)
eval_logs['truth_ratio'].update(truth_ratio)
eval_logs['paraphrased_loss'].update(gt_loss)
eval_logs['perturb_loss'].update(perturb_loss)
eval_logs['num_token_paraphrased'].update(num_token_gt)
eval_logs['num_token_perturb'].update(num_token_perturb)
return eval_logs
def get_dataloader(cfg, eval_task, tokenizer, folder, split, question_key, answer_key, base_answer_key, perturbed_answer_key, prompt_unlearn=False, unlearn_targets=[]):
torch_format_dataset = TextDatasetQA(
folder,
tokenizer=tokenizer,
model_family=cfg.model_family,
max_length=cfg.generation.max_length,
split=split,
question_key=question_key,
answer_key=answer_key,
prompt_unlearn=prompt_unlearn,
unlearn_targets=unlearn_targets
)
base_torch_format_dataset = TextDatasetQA(
folder,
tokenizer=tokenizer,
model_family=cfg.model_family,
max_length=cfg.generation.max_length,
split=split,
question_key=question_key,
answer_key=base_answer_key,
prompt_unlearn=prompt_unlearn,
unlearn_targets=unlearn_targets
)
perturb_torch_format_dataset = TextDatasetQA(
folder,
tokenizer=tokenizer,
model_family=cfg.model_family,
max_length=cfg.generation.max_length,
split=split,
question_key=question_key,
answer_key=perturbed_answer_key,
prompt_unlearn=prompt_unlearn,
unlearn_targets=unlearn_targets
)
if cfg.ds_size:
torch_format_dataset.data = torch_format_dataset.data.select(range(min(cfg.ds_size, len(torch_format_dataset.data))))
base_torch_format_dataset.data = base_torch_format_dataset.data.select(range(min(cfg.ds_size, len(base_torch_format_dataset.data))))
perturb_torch_format_dataset.data = perturb_torch_format_dataset.data.select(range(min(cfg.ds_size, len(perturb_torch_format_dataset.data))))
eval_dataloader = torch.utils.data.DataLoader(
torch_format_dataset, batch_size=cfg.batch_size, collate_fn=custom_data_collator_with_indices
)
base_eval_dataloader = torch.utils.data.DataLoader(
base_torch_format_dataset, batch_size=cfg.batch_size//4, collate_fn=custom_data_collator_with_indices
)
perturb_dataloader = torch.utils.data.DataLoader(
perturb_torch_format_dataset, batch_size=cfg.batch_size//4, collate_fn=custom_data_collator_with_indices
)
return eval_dataloader, base_eval_dataloader, perturb_dataloader
def get_all_evals(cfg, model, tokenizer, eval_task, eval_dataloader, base_eval_dataloader, perturb_dataloader, normalize_gt=False, only_perturb=False):
eval_logs = {}
eval_logs.update(eval_perturbation_ratio(base_eval_dataloader, perturb_dataloader, model))
if only_perturb:
return eval_logs
gen_outputs = []
ground_truths = []
input_strings = []
all_indices = []
for batch in tqdm(eval_dataloader):
input_ids, labels, attention_mask, indices = batch
all_indices.extend(indices.cpu().numpy().tolist())
batch = {"input_ids": input_ids, "labels": labels, "attention_mask": attention_mask}
#send to device
for k, v in batch.items():
batch[k] = v.to(model.device)
with torch.no_grad():
outputs = model(**batch)
input_string, gen_output, gt = run_generation(cfg, batch, model, tokenizer=tokenizer)
gen_outputs.extend(gen_output)
ground_truths.extend(gt)
input_strings.extend(input_string)
gt_loss = get_batch_loss(outputs.logits, batch['labels'])
num_token_gt = (batch['labels']!=-100).sum(-1)
gt_loss_per_token = gt_loss/num_token_gt
if 'avg_gt_loss' not in eval_logs:
eval_logs['avg_gt_loss'] = {}
if 'gt_loss' not in eval_logs:
eval_logs['gt_loss'] = {}
if 'num_token_gt' not in eval_logs:
eval_logs['num_token_gt'] = {}
if 'generated_text' not in eval_logs:
eval_logs['generated_text'] = {}
# print(gt_loss.shape, num_token_gt.shape)
eval_logs['avg_gt_loss'].update(dict(zip(indices.cpu().numpy().tolist(), gt_loss_per_token.cpu().numpy().tolist())))
eval_logs['gt_loss'].update(dict(zip(indices.cpu().numpy().tolist(), gt_loss.cpu().numpy().tolist())))
eval_logs['num_token_gt'].update(dict(zip(indices.cpu().numpy().tolist(), num_token_gt.cpu().numpy().tolist())))
eval_logs['generated_text'].update(dict(zip(indices.cpu().numpy().tolist(), zip(input_string, gen_output,gt))))
eval_logs.update(eval_rouge_recall(gen_outputs, ground_truths, all_indices))
if normalize_gt:
avg_gt_loss = eval_logs['avg_gt_loss']
avg_perturb_loss = eval_logs['average_perturb_loss']
data_indices = avg_gt_loss.keys()
normalized_gt_loss = {}
for idx in data_indices:
truth_prob = np.exp(-1 * avg_gt_loss[idx])
perturb_prob = np.exp(-1 * np.array(avg_perturb_loss[idx]))
all_prob = np.array([truth_prob, *perturb_prob])
normalized_gt_prob = truth_prob / all_prob.sum()
normalized_gt_loss[idx] = -1 * np.log(normalized_gt_prob)
eval_logs['normalized_gt_loss'] = normalized_gt_loss
return eval_logs
@hydra.main(version_base=None, config_path="config", config_name="eval_everything")
def main(cfg):
assert len(cfg.data_path)==len(cfg.split_list)==len(cfg.eval_task)==len(cfg.question_key)==len(cfg.answer_key)==len(cfg.base_answer_key)==len(cfg.perturbed_answer_key), "data_path, split, eval_task, question_key, and answer_key must be the same length"
Path(cfg.save_dir).mkdir(parents=True, exist_ok=True)
aggregated_eval_log_filename = os.path.join(cfg.save_dir, "eval_log_aggregated.json")
if os.path.exists(aggregated_eval_log_filename) and not cfg.overwrite:
print(f"Skipping because {aggregated_eval_log_filename} already exists")
return
os.environ["WANDB_DISABLED"] = "true"
model_cfg = get_model_identifiers_from_yaml(cfg.model_family)
model_id = model_cfg["hf_key"]
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token
model = None
config = AutoConfig.from_pretrained(model_id)
for attempt in range(3):
try:
# do thing
if cfg.use_pretrained:
print(f"Loading pretrained from {model_id}")
model = AutoModelForCausalLM.from_pretrained(model_id, config=config, use_flash_attention_2=model_cfg["flash_attention2"]=="true", torch_dtype=torch.bfloat16, trust_remote_code = True, device_map=0)
else:
print(f"Loading checkpoint from {cfg.model_path}")
model = AutoModelForCausalLM.from_pretrained(cfg.model_path, config=config, use_flash_attention_2=model_cfg["flash_attention2"]=="true", torch_dtype=torch.bfloat16, trust_remote_code = True, device_map=0)
except Exception as e:
print(e)
continue
# perhaps reconnect, etc.
else:
break
else:
print("Error: could not load model")
model = model.eval()
def reinitialize_weights(model) -> None:
for module in model.modules():
if isinstance(module, nn.Linear):
nn.init.normal_(module.weight, mean=0, std=0.02)
if module.bias is not None:
nn.init.constant_(module.bias, 0)
if cfg.reinitialize_weights:
print("Reinitializing weights")
reinitialize_weights(model)
# get unlearn targets
if cfg.prompt_unlearn:
unlearn_targets = set()
with open('data/tofu_author.txt', 'r') as f:
all_people = f.readlines()
all_people = [i.strip() for i in all_people]
data = datasets.load_dataset(cfg.data_path[-1], cfg.split_list[-1])['train']
for item in data:
target = [i for i in all_people if i in item['question'] + item['answer']]
if len(target) != 1:
target = [i for i in all_people if any([w in item['question'] + item['answer'] for w in i.split()])]
assert len(target) == 1
unlearn_targets.add(target[0])
unlearn_targets = sorted(unlearn_targets)
print(f"Unlearn targets: {unlearn_targets}")
cfg.generation.max_length += 300
else:
unlearn_targets = []
#write custom eval loop using compute_metrics
aggregated_eval_logs = {}
for i, (folder, split, question_key, answer_key, eval_task, base_answer_key, perturbed_answer_key) in enumerate(zip(cfg.data_path, cfg.split_list, cfg.question_key, cfg.answer_key, cfg.eval_task, cfg.base_answer_key, cfg.perturbed_answer_key)):
world_size = int(os.environ.get('WORLD_SIZE', '1'))
print(f'Working on eval task {eval_task} with split {split}')
save_filename = os.path.join(cfg.save_dir, f"{eval_task}.json")
save_filename = save_filename if world_size == 1 else os.path.join(cfg.save_dir, f"{eval_task}_{os.environ.get('LOCAL_RANK', '0')}.json")
eval_dataloader, base_eval_dataloader, perturb_dataloader = get_dataloader(cfg, eval_task, tokenizer, folder, split, question_key, answer_key, base_answer_key, perturbed_answer_key, prompt_unlearn=cfg.prompt_unlearn, unlearn_targets=unlearn_targets)
normalize_gt = False
if 'eval_log' not in eval_task:
normalize_gt = True
eval_logs = get_all_evals(cfg, model, tokenizer, eval_task, eval_dataloader, base_eval_dataloader, perturb_dataloader, normalize_gt=normalize_gt, only_perturb=cfg.only_perturb)
with open(save_filename, "w") as f:
# pretty write json to f
json.dump(eval_logs, f, indent=4)
aggregated_eval_logs[f'{eval_task}.json'] = eval_logs
with open(aggregated_eval_log_filename, "w") as f:
# pretty write json to f
json.dump(aggregated_eval_logs, f, indent=4)
def run_generation(cfg, batch, model, tokenizer):
input_ids = batch["input_ids"]
input_strings = tokenizer.batch_decode(input_ids, skip_special_tokens=True)
if cfg.model_family == 'llama2-7b':
split_symbol = " [/INST]"
elif cfg.model_family == 'alpaca':
split_symbol = '### Response:\n'
else:
split_symbol = 'Answer: '
ground_truth = [s.split(split_symbol)[1] for s in input_strings]
input_strings = [s.split(split_symbol)[0] for s in input_strings]
if cfg.model_family in ['llama2-7b', 'alpaca']:
input_strings = [s + split_symbol for s in input_strings]
#now tokenize the strings with left padding
left_pad_tokenizer = tokenizer
left_pad_tokenizer.padding_side = 'left'
left_pad_tokenizer.padding_size = 'longest'
left_pad_tokenizer.pad_token = left_pad_tokenizer.eos_token
left_pad_tokenizer.pad_token_id = left_pad_tokenizer.eos_token_id
inputs = left_pad_tokenizer.batch_encode_plus(input_strings, add_special_tokens=True, return_tensors='pt', padding=True).to(model.device)
#now generate
out = model.generate(inputs.input_ids, attention_mask=inputs.attention_mask, max_length=cfg.generation.max_length, max_new_tokens=cfg.generation.max_new_tokens, do_sample=False, use_cache=True, pad_token_id=left_pad_tokenizer.eos_token_id)
strs = left_pad_tokenizer.batch_decode(out[:, inputs.input_ids.shape[-1]:], skip_special_tokens=True)
return input_strings, strs, ground_truth
def eval_rouge_recall(gen_outputs, ground_truths, indices):
scorer = rouge_scorer.RougeScorer(['rouge1', 'rougeL'], use_stemmer=True)
rouge1_recall = {}
rougeL_recall = {}
for gen, gt, idx in zip(gen_outputs, ground_truths, indices):
rouge_scores = scorer.score(gt, gen)
rouge1_recall[idx] = rouge_scores['rouge1'].recall
rougeL_recall[idx] = rouge_scores['rougeL'].recall
return {'rouge1_recall': rouge1_recall, 'rougeL_recall': rougeL_recall}
if __name__ == "__main__":
main()