-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
176 lines (146 loc) · 7.59 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
import torch
from torch.utils.data import DataLoader
import numpy as np
import pandas as pd
from tqdm import tqdm
from peft import PeftModel
from collections import defaultdict
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
def get_preprocessed_dataset(tokenizer, dataset, chat_template, max_length):
def apply_prompt_template(sample):
return {
'text': chat_template.format(prompt=sample['prompts'], response=sample['response'])
}
dataset = dataset.map(apply_prompt_template, remove_columns=list(dataset.features))
def tokenized_dataset(text):
input_text = text['text']
tokenized_output = tokenizer(input_text, truncation=True, padding='max_length', max_length=max_length)
tokenized_output['labels'] = tokenized_output['input_ids'].copy()
return tokenized_output
return dataset.map(tokenized_dataset, batched=True, remove_columns=['text'])
def collect_gradient(model_name, lora_adapter_path, tokenizer, tokenized_tr, tokenized_val):
# quantization_config = BitsAndBytesConfig(load_in_8bit=True, load_in_4bit=False)
quantization_config = None
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config, device_map='auto')
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.padding_side = 'left'
tokenizer.pad_token = tokenizer.eos_token
model = PeftModel.from_pretrained(model, lora_adapter_path, is_trainable=True)
collate_fn = lambda x: tokenizer.pad(x, padding="longest", return_tensors="pt")
train_dataloader_stochastic = DataLoader(tokenized_tr,
shuffle=False,
collate_fn=collate_fn,
batch_size=1)
val_dataloader_stochastic = DataLoader(tokenized_val,
shuffle=False,
collate_fn=collate_fn,
batch_size=1)
model.eval()
tr_grad_dict = {}
for step, batch in enumerate(tqdm(train_dataloader_stochastic)):
model.zero_grad()
batch['labels'] = batch['input_ids']
batch.to('cuda')
outputs = model(**batch)
loss = outputs.loss
loss.backward()
grad_dict = {}
for k, v in model.named_parameters():
if 'lora_A' in k:
grad_dict[k] = v.grad.cpu()
elif 'lora_B' in k:
grad_dict[k] = v.grad.cpu().T
else: pass
tr_grad_dict[step] = grad_dict
del grad_dict
val_grad_dict = {}
for step, batch in enumerate(tqdm(val_dataloader_stochastic)):
model.zero_grad()
batch['labels'] = batch['input_ids']
batch.to('cuda')
outputs = model(**batch)
loss = outputs.loss
loss.backward()
grad_dict = {}
for k, v in model.named_parameters():
if 'lora_A' in k:
grad_dict[k] = v.grad.cpu()
elif 'lora_B' in k:
grad_dict[k] = v.grad.cpu().T
else: pass
val_grad_dict[step] = grad_dict
del grad_dict
return tr_grad_dict, val_grad_dict
def influence_function(tr_grad_dict, val_grad_dict, hvp_cal='gradient_match', lambda_const_param=10, n_iteration=10, alpha_const=1.):
hvp_dict = defaultdict(dict)
IF_dict = defaultdict(dict)
n_train = len(tr_grad_dict.keys())
def calculate_lambda_const(tr_grad_dict, weight_name):
S = torch.zeros(len(tr_grad_dict.keys()))
for tr_id in tr_grad_dict:
tmp_grad = tr_grad_dict[tr_id][weight_name]
S[tr_id] = torch.mean(tmp_grad**2)
return torch.mean(S) / lambda_const_param
if hvp_cal == 'Original':
for val_id in tqdm(val_grad_dict.keys()):
for weight_name in val_grad_dict[val_id]:
lambda_const = calculate_lambda_const(tr_grad_dict, weight_name)
AAt_matrix = torch.zeros(torch.outer(tr_grad_dict[0][weight_name].reshape(-1),
tr_grad_dict[0][weight_name].reshape(-1)).shape)
for tr_id in tr_grad_dict:
tmp_mat = torch.outer(tr_grad_dict[tr_id][weight_name].reshape(-1),
tr_grad_dict[tr_id][weight_name].reshape(-1))
AAt_matrix += tmp_mat
L, V = torch.linalg.eig(AAt_matrix)
L, V = L.float(), V.float()
hvp = val_grad_dict[val_id][weight_name].reshape(-1) @ V
hvp = (hvp / (lambda_const + L / n_train)) @ V.T
hvp_dict[val_id][weight_name] = hvp.reshape(len(tr_grad_dict[0][weight_name]), -1)
del tmp_mat, AAt_matrix, V
elif hvp_cal == 'DataInf':
for val_id in tqdm(val_grad_dict.keys()):
for weight_name in val_grad_dict[val_id]:
lambda_const = calculate_lambda_const(tr_grad_dict, weight_name)
hvp = torch.zeros(val_grad_dict[val_id][weight_name].shape)
for tr_id in tr_grad_dict:
tmp_grad = tr_grad_dict[tr_id][weight_name]
C_tmp = torch.sum(val_grad_dict[val_id][weight_name] * tmp_grad) / (lambda_const + torch.sum(tmp_grad**2))
hvp += (val_grad_dict[val_id][weight_name] - C_tmp * tmp_grad) / (n_train * lambda_const)
hvp_dict[val_id][weight_name] = hvp
elif hvp_cal == 'LiSSA':
for val_id in tqdm(val_grad_dict.keys()):
for weight_name in val_grad_dict[val_id]:
lambda_const = calculate_lambda_const(tr_grad_dict, weight_name)
running_hvp = val_grad_dict[val_id][weight_name]
for _ in range(n_iteration):
hvp_tmp = torch.zeros(val_grad_dict[val_id][weight_name].shape)
for tr_id in tr_grad_dict:
tmp_grad = tr_grad_dict[tr_id][weight_name]
hvp_tmp += (torch.sum(tmp_grad * running_hvp) * tmp_grad - lambda_const * running_hvp) / n_train / 1e3
running_hvp = val_grad_dict[val_id][weight_name] + running_hvp - alpha_const * hvp_tmp
hvp_dict[val_id][weight_name] = running_hvp
elif hvp_cal == 'gradient_match':
hvp_dict = val_grad_dict.copy()
else:
raise Exception("hvp calculation options: [Original, DataInf, LiSSA, gradient_match]")
for tr_id in tr_grad_dict:
for val_id in val_grad_dict:
if_tmp_value = 0
for weight_name in val_grad_dict[0]:
if_tmp_value += torch.sum(hvp_dict[val_id][weight_name] * tr_grad_dict[tr_id][weight_name])
IF_dict[tr_id][val_id] = -if_tmp_value
return pd.DataFrame(IF_dict, dtype=float)
def check_acc_cov(influence, train_dataset, validation_dataset):
acc = 0
cov = 0
cov_cnt = int(len(train_dataset) / len(set(train_dataset['variation'])))
for i in range(len(influence)):
array = -(influence.loc[i].to_numpy())
indices = np.argpartition(array, -cov_cnt)[-cov_cnt:]
topk_indices = indices[np.argsort(array[indices])[::-1]]
if train_dataset['variation'][int(topk_indices[0])] == validation_dataset['variation'][i]:
acc += 1
for ele in topk_indices:
if train_dataset['variation'][int(ele)] == validation_dataset['variation'][i]:
cov += 1
print("Acc:", acc / len(influence), '\nCover:', cov / (len(influence) * cov_cnt))