-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
198 lines (170 loc) · 7.84 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
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
import yaml
import copy
import numpy as np
from scipy.stats import sem, hmean, ks_2samp
from natsort import natsorted
import torch
def find_all_linear_names(model):
cls = torch.nn.Linear
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, cls):
names = name.split(".")
lora_module_names.add(names[0] if len(names) == 1 else names[-1])
if "lm_head" in lora_module_names: # needed for 16-bit
lora_module_names.remove("lm_head")
return list(lora_module_names)
def freeze_params(module):
for p in module.parameters():
p.requires_grad=False
def print_trainable_parameters(model):
"""
Prints the number of trainable parameters in the model.
"""
trainable_params = 0
all_param = 0
for _, param in model.named_parameters():
all_param += param.numel()
if param.requires_grad:
trainable_params += param.numel()
print(
f"trainable params: {trainable_params} || all params: {all_param} || trainable%: {100 * trainable_params / all_param}"
)
def get_model_identifiers_from_yaml(model_family):
#path is model_configs.yaml
'''
models:
llama2-7b:
hf_key: "NousResearch/Llama-2-7b-chat-hf"
question_start_tag: "[INST] "
question_end_tag: " [/INST] "
answer_tag: ""
start_of_sequence_token: "<s>"
'''
model_configs = {}
with open("config/model_config.yaml", "r") as f:
model_configs = yaml.load(f, Loader=yaml.FullLoader)
return model_configs[model_family]
def merge_dicts(a, b):
""" Recursively merges dict b into a deep copy of dict a """
# Create a deep copy of a to avoid modifying it in place
a_copy = copy.deepcopy(a)
for key, value in b.items():
if key in a_copy:
if isinstance(a_copy[key], dict) and isinstance(value, dict):
a_copy[key] = merge_dicts(a_copy[key], value)
elif isinstance(a_copy[key], list) and isinstance(value, list):
a_copy[key] = a_copy[key] # we see duplicate lists, keep only one
else:
a_copy[key] = value # Overwrite value from b into a_copy
else:
a_copy[key] = value
# sort the keys with natural order
a_copy = {k: a_copy[k] for k in natsorted(a_copy)}
return a_copy
def get_total_len(name, forget_rate):
if name == 'eval_real_author_wo_options.json':
return 100
elif name == 'eval_real_world_wo_options.json':
return 117
elif name == 'eval_log.json':
return 300
else:
if forget_rate == 'forget01':
return 40
elif forget_rate == 'forget05':
return 200
else:
return 300
def interleave(a, b, size):
assert len(a) == len(b)
assert size > 0
c = []
for i in range(0, len(a), size):
c.extend(a[i:i+size])
c.extend(b[i:i+size])
return c
# PLEASE BE VERY VERY CAREFUL HERE
# This code, although takes num_processes as an argument, it in fact only supports num_processes=2
# Future improvement should support interleave for more than 2 processes
# also, small_bsz = large_bsz//4 is hardcoded, which is only true for our experiments
# because when we construct perturb and paraphrase data_loader, we set batch_size=large_bsz//4 specifically
def interleave_eval_result_dict(eval_result_dict, forget_rate, large_bsz, num_processes=2):
small_bsz = large_bsz//4
for k, v in eval_result_dict.items():
# each v corresponds to one ckpt
for metric, value in v.items():
bsz = small_bsz if 'perturb' in metric or 'paraphrase' in metric else large_bsz
total_len = get_total_len(k, forget_rate)
# split in two
a = value[0:len(value)//2]
b = value[len(value)//2:]
eval_result_dict[k][metric] = interleave(a, b, bsz)[:total_len]
return eval_result_dict
def get_model_utility(eval_result_dict):
eval_task_dict = {
'eval_real_author_wo_options.json': 'Real Authors',
'eval_real_world_wo_options.json': 'Real World',
'eval_log.json': 'Retain',
'eval_log_forget.json': 'Forget'
}
eval_tasks = list(eval_task_dict.keys())
metrics = ['ROUGE', 'Probability', 'Truth Ratio']
output_result = {}
for eval_task in eval_tasks:
for metric in metrics:
output_result[eval_task_dict[eval_task] + ' ' + metric] = []
# k is different files
for k, v in eval_result_dict.items():
# getting Probability
if 'eval_log' in k:
gt_probs = np.exp(-1 * np.array(list(eval_result_dict[k]['avg_gt_loss'].values())))
avg_gt_prob = np.mean(gt_probs)
else:
avg_true_prob = np.exp(-1 * np.array(list(eval_result_dict[k]['avg_gt_loss'].values())))
avg_false_prob = np.exp(-1 * np.array(list(eval_result_dict[k]['average_perturb_loss'].values())))
avg_all_prob = np.concatenate([np.expand_dims(avg_true_prob, axis=-1), avg_false_prob], axis=1).sum(-1)
avg_gt_prob = np.mean(avg_true_prob/avg_all_prob)
output_result[f'{eval_task_dict[k]} Probability'] = avg_gt_prob
# getting ROUGE
avg_rouge = np.array(list(eval_result_dict[k]['rougeL_recall'].values())).mean()
output_result[f'{eval_task_dict[k]} ROUGE'] = avg_rouge
# getting Truth Ratio
data_indices = list(eval_result_dict[k]['avg_paraphrased_loss'].keys())
# group avg_paraphrased_loss and average_perturb_loss by data_indices
avg_paraphrase_np_values = []
avg_perturbed_np_values = []
for data_idx in data_indices:
avg_paraphrase_np_values.append(eval_result_dict[k]['avg_paraphrased_loss'][data_idx])
avg_perturbed_np_values.append(eval_result_dict[k]['average_perturb_loss'][data_idx])
avg_paraphrase_np_values = np.exp(-1 * np.array(avg_paraphrase_np_values))
avg_perturbed_np_values = np.exp(-1 * np.array(avg_perturbed_np_values)).mean(-1)
curr_stat_1 = avg_perturbed_np_values / avg_paraphrase_np_values
if 'forget' in k:
paraphrased_perturb_ratio = np.mean(np.minimum(curr_stat_1, 1/curr_stat_1))
else:
paraphrased_perturb_ratio = np.mean(np.maximum(0, 1 - curr_stat_1))
output_result[f'{eval_task_dict[k]} Truth Ratio'] = paraphrased_perturb_ratio
model_utility_cands = []
for k, v in output_result.items():
if 'Forget' not in k:
model_utility_cands.append(v)
output_result['Model Utility'] = hmean(model_utility_cands)
return output_result
def get_forget_quality(unlearn_result, retain_result):
unlearn_forget_result = unlearn_result['eval_log_forget.json']
retain_forget_result = retain_result['eval_log_forget.json']
unlearn_paraphrase_np_values = np.array(list(unlearn_forget_result['avg_paraphrased_loss'].values()))
unlearn_perturbed_np_values = np.array(list(unlearn_forget_result['average_perturb_loss'].values()))
unlearn_perturbed_np_values = unlearn_perturbed_np_values.mean(axis=-1)
retain_paraphrase_np_values = np.array(list(retain_forget_result['avg_paraphrased_loss'].values()))
retain_perturbed_np_values = np.array(list(retain_forget_result['average_perturb_loss'].values()))
retain_perturbed_np_values = retain_perturbed_np_values.mean(axis=-1)
unlearn_truth_ratio = np.exp( unlearn_perturbed_np_values - unlearn_paraphrase_np_values)
retain_truth_ratio = np.exp( retain_perturbed_np_values - retain_paraphrase_np_values)
test_res = ks_2samp(unlearn_truth_ratio, retain_truth_ratio)
return {'Forget Quality': test_res.pvalue, 'KS Test PVal Forget': test_res.pvalue, 'KS Test Forget': test_res.statistic}
def add_dataset_index(dataset):
indexing = np.arange(len(dataset))
dataset = dataset.add_column('index', indexing)
return dataset