-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_estimate_eval_gradients_instruction.py
350 lines (314 loc) · 17 KB
/
fast_estimate_eval_gradients_instruction.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
344
345
346
347
348
349
350
import argparse
import logging
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import pytorch_lightning as pl
import torch
from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from src.custom.alpaca_model import AlpacaModel
from src.custom.alpaca_data_module import AlpacaDataModule
from src.custom.instruction_data_module import InstructionDataModule
from src.custom.truthfulqa_data_module import TruthfulQADataModule
from src.custom.toxigen_data_module import ToxiGenDataModule
from peft import get_peft_model, LoraConfig
from torch.utils.data import Subset
import numpy as np
from sklearn.linear_model import LogisticRegression
import time
logging.basicConfig(level=logging.INFO)
torch.set_float32_matmul_precision("high")
def main(args):
print("arguments".upper().center(80, "-"))
print(args)
print("-" * 80)
model_key = args.model_key.replace("/", "-")
if "gpt" in args.model_key or "Llama" in model_key \
or "bloomz" in model_key or "gemma" in model_key or "Mistral" in model_key:
hf_key = args.model_key.replace("_", "-")
tokenizer = AutoTokenizer.from_pretrained(hf_key)
if args.use_qlora:
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type='nf4'
)
model = AutoModelForCausalLM.from_pretrained(hf_key, quantization_config=quantization_config, torch_dtype=torch.bfloat16, device_map={"": args.devices[0]}) #
else:
model = AutoModelForCausalLM.from_pretrained(hf_key)
model_type = "decoder"
append_eos = True
elif "flan" in model_key:
hf_key = "google/{}".format(model_key.replace("_", "-"))
model = AutoModelForSeq2SeqLM.from_pretrained(hf_key)
tokenizer = AutoTokenizer.from_pretrained(hf_key, model_max_length=512)
model_type = "encoder_decoder"
append_eos = False # t5 tokenizers already append eos
else:
raise NotImplementedError(args.model_key)
if args.train_lora:
if args.model_key == "gpt2": # for gpt2, we generally use full model
config = LoraConfig(
r=args.lora_rank,
lora_alpha=args.lora_alpha,
target_modules=["c_attn", "c_proj", "c_fc"],
lora_dropout=0.1,
bias="lora_only",
modules_to_save=[],
)
elif args.model_key == "EleutherAI/gpt-neox-20b":
config = LoraConfig(
r=args.lora_rank,
lora_alpha=args.lora_alpha,
target_modules=["query_key_value"],
lora_dropout=0.1,
bias="lora_only",
modules_to_save=[],
)
elif "flan" in args.model_key:
config = LoraConfig(
r=args.lora_rank,
lora_alpha=args.lora_alpha,
target_modules=["q", "k", "v"],
lora_dropout=0.1,
bias="lora_only",
modules_to_save=[],
)
else:
config = LoraConfig(
r=args.lora_rank,
lora_alpha=args.lora_alpha,
target_modules=["q_proj", "k_proj", "v_proj"],
lora_dropout=0.1,
bias="lora_only",
modules_to_save=[],
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
if args.train_instruction:
task_idxes = list(range(1729))
data_module = InstructionDataModule(tokenizer=tokenizer,
task_idxes=task_idxes,
batch_size = args.batch_size,
inference_batch_size = args.batch_size,
context_length=args.max_length)
elif args.load_toxigen:
data_module = ToxiGenDataModule(tokenizer=tokenizer,
data_path="./data/eval/toxigen",
batch_size=args.batch_size,
inference_batch_size=args.batch_size,
context_length=args.max_length,
dev_split_ratio=0.1,
load_full_as_train=True)
elif args.load_truthfulqa:
data_module = TruthfulQADataModule(tokenizer=tokenizer,
data_path="./data/eval/truthfulqa",
batch_size=args.batch_size,
inference_batch_size=args.batch_size,
context_length=args.max_length,
dev_split_ratio=0.1,
load_full_as_train=True,
use_preset=True)
else:
# Only load alpaca dataset
task_idxes = list(range(38))
data_module = AlpacaDataModule(tokenizer=tokenizer,
data_path="./data/alpaca_data/alpaca_final.pkl",
dev_split_path="./data/alpaca_data/alpaca_dev_split_map.pkl",
task_idxes=task_idxes,
batch_size = args.batch_size,
inference_batch_size = args.batch_size,
context_length=args.max_length,
downsample=args.downsample,
model_type=model_type)
data_module.setup(stage="fit")
save_name = ("Instruction_{}".format(model_key) if (args.train_instruction or args.load_truthfulqa or args.load_toxigen) else "Alpaca_{}".format(model_key)) + \
(f"_lora_r_{args.lora_rank}" if args.train_lora else "") + \
("_{}".format(args.save_name) if args.save_name != "none" else "")
gradient_dir = save_name + f"_dim_{args.project_dimension}_run_{args.run}" + ("_pretrained" if args.load_model_dir is None else "")
print("Gradient directory", gradient_dir)
lm = AlpacaModel(model=model, tokenizer=tokenizer, model_type=model_type,
lr=args.lr, weight_decay=args.weight_decay, max_length=args.max_length, use_wandb=args.use_wandb,
intialize_project_matrix=args.project_gradients, run_seed=args.run,
project_dim=args.project_dimension, gradient_dir=gradient_dir, use_sgd=True,
predict_steps=args.num_batches_gradients)
if args.load_model_dir is not None:
load_model_dir = f"./exported_model/{args.load_model_dir}.pt"
if os.path.exists(load_model_dir):
state_dict = torch.load(load_model_dir, map_location=lm.model.device)
model.load_state_dict(state_dict, strict=False)
print("Loaded model from checkpoint from ", load_model_dir)
args.accumulate = 1; args.epochs = 0; args.enable_checkpointing = True
default_root_dir = "external_lightning_logs/" + save_name + "/eval_output_approx/" # This is for creating a new directory
if args.use_qlora:
from lightning.pytorch.plugins import BitsandbytesPrecision
# this will pick out the compute dtype automatically, by default `bfloat16`
quant_precision = BitsandbytesPrecision(mode="nf4-dq")
trainer = pl.Trainer(accelerator="gpu", devices=args.devices, strategy=args.strategy,
default_root_dir=default_root_dir, min_epochs=args.epochs, max_epochs=args.epochs,
accumulate_grad_batches=args.accumulate, # precision=args.precision,
enable_checkpointing=args.enable_checkpointing, inference_mode=False, plugins=quant_precision
)
else:
trainer = pl.Trainer(accelerator="gpu", devices=args.devices, strategy=args.strategy,
default_root_dir=default_root_dir, min_epochs=args.epochs, max_epochs=args.epochs,
accumulate_grad_batches=args.accumulate, precision=args.precision,
enable_checkpointing=args.enable_checkpointing, inference_mode=False
)
def generate_state_dict(model, state_dict, coef, device="cpu", removing_keys = ["shared", "lm_head", "wte", "wpe", "ln", "embed_tokens", "norm", "word_embeddings" ]):
# reshape coef
new_state_dict = {}; cur_len = 0
for key, param in model.named_parameters():
if not param.requires_grad: continue
param_len = param.numel()
if any([rkey in key for rkey in removing_keys]):
continue
else:
new_state_dict[key] = state_dict[key].clone() + \
torch.FloatTensor(coef[cur_len:cur_len+param_len].reshape(param.shape)).to(device)
cur_len += param_len
return new_state_dict
def compute_norm(state_dict):
norm = 0
for key, val in state_dict.items():
if "lora" in key:
norm += val.clone().square().sum().item()
return np.math.sqrt(norm)
state_dict = {key: val.clone() for key, val in lm.model.state_dict().items()}
pretrain_norm = compute_norm(state_dict)
print("Norm of the original model", pretrain_norm)
'''First compute pretrain outputs'''
if args.compute_pretrained_outputs:
start_time = time.time()
if args.use_test:
pretrain_outputs = trainer.predict(lm, dataloaders=data_module.test_dataloader())
else:
pretrain_outputs = trainer.predict(lm, dataloaders=data_module.train_dataloader())
end_time = time.time()
print("Time for computing gradients & outputs", end_time - start_time)
pretrain_outputs = np.concatenate(pretrain_outputs, axis=0)
print("Pretrained outputs shape", pretrain_outputs.shape)
np.save(f"./gradients/{gradient_dir}/pretrain_outputs.npy", pretrain_outputs)
else:
project_matrix = lm.project_matrix
inv_project_matrix = np.linalg.pinv(project_matrix)
''' Collect graddients from a randome subset of tasks '''
train_dataset = data_module.train_dataset
skills = [train_dataset.dataset.data[i]['skill'] for i in train_dataset.indices] \
if type(train_dataset) == Subset else [tmp_data['skill'] for tmp_data in train_dataset.data]
skill_list = data_module.skills
task_num = len(skill_list)
np.random.seed(args.seed)
gradients = []
while len(gradients) == 0:
subset_idxes = np.random.choice(task_num, int(0.75*task_num), replace=False)
subset_idxes.sort()
tmp_skill_list = [skill_list[i] for i in subset_idxes]
data_idxes = [i for i in range(len(skills)) if skills[i] in tmp_skill_list]
for idx in data_idxes:
gradient_file_idx = idx // args.batch_size
gradient_file = f"./gradients/{gradient_dir}/train_batch_{gradient_file_idx}_gradients.npy"
if not os.path.exists(gradient_file): continue
tmp_gradients = np.load(gradient_file)
if tmp_gradients.shape[0] < args.batch_size: continue
gradients.append(tmp_gradients[idx % args.batch_size])
if len(gradients) == 0:
print("No gradients found")
gradients = np.array(gradients)
# randomly assign labels as 0 or 1
labels = np.random.binomial(n=1, p=0.7, size=gradients.shape[0])
# reverse the gradients for the 0 labels
mask = np.copy(labels)
mask[labels == 0] = -1
mask = mask.reshape(-1, 1)
gradients = gradients*mask
train_num = int(len(gradients)*0.8)
train_gradients, train_labels = gradients[:train_num], labels[:train_num]
test_gradients, test_labels = gradients[train_num:], labels[train_num:]
clf = LogisticRegression(random_state=0, penalty='l2', C=1e-4, solver='liblinear')
clf.fit(train_gradients, train_labels)
print(clf.score(test_gradients, test_labels))
proj_coef = clf.coef_.copy().flatten().reshape(-1, 1)
coef = project_matrix @ proj_coef.flatten()
print("L2 norm", np.linalg.norm(coef))
if args.abs_scale > 0:
cur_coef = (args.scale * args.abs_scale) * coef / np.linalg.norm(coef)
else:
cur_coef = (args.scale * pretrain_norm) * coef / np.linalg.norm(coef)
print("Current norm of the coef", np.linalg.norm(cur_coef))
new_state_dict = generate_state_dict(lm.model, state_dict, cur_coef, device=model.device)
pretrain_state_dict = state_dict
finetuned_state_dict = new_state_dict
''' Load the pretrained outputs '''
pretrain_outputs = np.load(f"./gradients/{gradient_dir}/pretrain_outputs.npy")
data_gradients = []
for gradient_idx, file in enumerate(os.listdir(f"./gradients/{gradient_dir}")):
if "outputs" in file: continue
data_gradients.append(np.load(os.path.join(f"./gradients/{gradient_dir}", file)))
if gradient_idx >= args.num_batches_gradients: break
data_gradients = np.concatenate(data_gradients, axis=0)
data_gradients = data_gradients @ inv_project_matrix
finetuned_vector = [finetuned_state_dict[key]-pretrain_state_dict[key] for key in finetuned_state_dict.keys()]
finetuned_vector = np.concatenate([vec.flatten().cpu().numpy() for vec in finetuned_vector]).reshape(1,-1)
print("Pretrained outputs:", pretrain_outputs[:4])
dot_product = (data_gradients * finetuned_vector).sum(axis=1)
print("First-order term", dot_product)
model.load_state_dict(pretrain_state_dict)
model.load_state_dict(finetuned_state_dict, strict=False)
finetuned_outputs = trainer.predict(lm, dataloaders=data_module.train_dataloader())
finetuned_outputs = np.concatenate(finetuned_outputs, axis=0)
print("Fine-tuned outputs:", finetuned_outputs[:4])
pretrain_outputs = pretrain_outputs[:dot_product.shape[0]]
finetuned_outputs = finetuned_outputs[:dot_product.shape[0]]
mask = np.logical_and(pretrain_outputs != 0, finetuned_outputs != 0)
mask = np.logical_and(mask, ~np.isnan(pretrain_outputs))
mask = np.logical_and(mask, ~np.isnan(finetuned_outputs))
pretrain_outputs[~mask] = 0
finetuned_outputs[~mask] = 0
pretrain_outputs = pretrain_outputs.sum(axis=1)/mask.sum(axis=1)
finetuned_outputs = finetuned_outputs.sum(axis=1)/mask.sum(axis=1)
print("pretrain_outputs.shape",pretrain_outputs.shape)
print("dot_product.shape",dot_product.shape)
print("finetuned_outputs.shape",finetuned_outputs.shape)
diff = np.abs(pretrain_outputs + dot_product - finetuned_outputs) / np.maximum(np.abs(finetuned_outputs), np.abs(pretrain_outputs))
diff = diff[~np.isnan(diff)]
diffs = np.square(diff).mean()
print("Mean Difference:", diffs)
diff = np.abs(pretrain_outputs - finetuned_outputs) / np.maximum(np.abs(finetuned_outputs), np.abs(pretrain_outputs))
diff = diff[~np.isnan(diff)]
diffs = np.square(diff).mean()
print("Mean Difference without gradient term:", diffs)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_key", type=str, default="EleutherAI/gpt-neo-1.3B")
parser.add_argument("--train_lora", action="store_true")
parser.add_argument("--lora_rank", type=int, default=4)
parser.add_argument("--lora_alpha", type=int, default=32)
parser.add_argument("--precision", type=str, default="32")
parser.add_argument("--strategy", type=str, default="auto")
parser.add_argument("--devices", type=int, nargs="+", default=[0, 1])
parser.add_argument("--use_qlora", action="store_true")
parser.add_argument("--lr", type=float, default=5e-5)
parser.add_argument("--weight_decay", type=float, default=1e-4)
parser.add_argument("--max_length", type=int, default=256)
parser.add_argument("--use_wandb", action="store_true")
parser.add_argument("--batch_size", type=int, default=4)
parser.add_argument("--load_model_dir", type=str, default=None)
parser.add_argument("--train_instruction", action="store_true")
parser.add_argument("--load_truthfulqa", action="store_true")
parser.add_argument("--load_toxigen", action="store_true")
parser.add_argument("--compute_pretrained_outputs", action="store_true")
parser.add_argument("--downsample", type=int, default=None)
parser.add_argument("--num_batches_gradients", type=int, default=100)
parser.add_argument("--run", type=int, default=0)
parser.add_argument("--project_gradients", action="store_true")
parser.add_argument("--project_dimension", type=int, default=200)
parser.add_argument("--abs_scale", type=float, default=-1.0)
parser.add_argument("--scale", type=float, default=0.1)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--save_name", type=str, default="none")
parser.add_argument("--use_test", action="store_true")
args = parser.parse_args()
main(args)