-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_logits_chat.py
More file actions
472 lines (394 loc) · 18.4 KB
/
Copy pathgenerate_logits_chat.py
File metadata and controls
472 lines (394 loc) · 18.4 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import json
import os
os.environ['TRANSFORMERS_CACHE'] = os.environ['HF_HOME']
os.environ['HF_DATASETS_CACHE'] = os.environ['HF_HOME']
import random
import torch
import argparse
import pickle
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForCausalLM
from transformers import LlamaForCausalLM, GenerationConfig
from tqdm import tqdm
import prompt as pt
seed = 1
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
few_shot_exp_ids = {
"MMLU": [1, 3, 5, 7, 9],
"HellaSwag": [1, 3, 5, 7, 9],
"CosmosQA": [1, 3, 5, 7, 9],
"Halu-OpenDialKG": [5, 7, 9],
"Halu-CNN/DailyMail": [9]
}
options = ["Answer: A", "Answer: B", "Answer: C", "Answer: D", "Answer: E", "Answer: F"]
options_alt = ["\nA", "\nB", "\nC", "\nD", "\nE", "\nF"]
def load_data(data_file):
data = json.load(open(data_file, "r"))
return data
def load_model(args):
if "Yi" in args.model:
tokenizer = AutoTokenizer.from_pretrained(args.model, use_fast=False)
else:
tokenizer = AutoTokenizer.from_pretrained(args.model)
tokenizer.truncation_side = "left"
tokenizer.model_max_length = min(tokenizer.model_max_length, 2048)
if "falcon" in args.model:
model = AutoModelForCausalLM.from_pretrained(args.model, torch_dtype=torch.bfloat16, device_map="auto")
elif "deepseek" in args.model:
model = AutoModelForCausalLM.from_pretrained(args.model, torch_dtype=torch.bfloat16, device_map="auto")
model.generation_config = GenerationConfig.from_pretrained(args.model)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
elif "Llama" in args.model:
model = LlamaForCausalLM.from_pretrained(args.model, torch_dtype=torch.float16, device_map="auto")
elif "Qwen" in args.model:
model = AutoModelForCausalLM.from_pretrained(args.model, torch_dtype=torch.bfloat16, device_map="auto")
elif "Yi" in args.model:
model = AutoModelForCausalLM.from_pretrained(args.model, device_map="auto", torch_dtype="auto")
elif "gemma" in args.model.lower(): # Add this for Gemma support
model = AutoModelForCausalLM.from_pretrained(args.model, torch_dtype=torch.bfloat16, device_map="auto")
else:
raise NotImplementedError
model.eval()
return tokenizer, model
def get_fewshot_exps(data):
src = data[0]["source"]
fewshot_exps = []
for idx in few_shot_exp_ids[src]:
fewshot_exps.append(data[idx])
assert data[idx]["id"] == idx
return fewshot_exps
def format_example(example, prompt, with_answer=False, num_options=4):
# QA
if example["source"] == "MMLU":
prompt += "Question: " + example["question"] + "\nChoices:\n"
# Reading Comprehension
elif example["source"] == "CosmosQA":
prompt += "Context: " + example["context"] + "\n" + "Question: " + example["question"] + "\nChoices:\n"
# Commonsense NLI
elif example["source"] == "HellaSwag":
prompt += "Context: " + example["context"] + "\n" + "Question: " + example["question"] + "\nChoices:\n"
# Dialogue Response
elif example["source"] == "Halu-OpenDialKG":
prompt += "Dialogue: " + example["context"] + "\n" + "Question: " + example["question"] + "\nChoices:\n"
# Document Summarization
elif example["source"] == "Halu-CNN/DailyMail":
prompt += "Document: " + example["context"] + "\n" + "Question: " + example["question"] + "\nChoices:\n"
else:
raise NotImplementedError("Not supported dataset.")
# Only include first num_options choices (e.g., A, B, C, D)
valid_options = ["A", "B", "C", "D", "E", "F"][:num_options]
for k, v in example["choices"].items():
if k in valid_options:
prompt += k + ". " + str(v) + "\n"
prompt += "Answer:"
if with_answer:
prompt += " " + example["answer"] + "\n"
return prompt
def format_base_prompt(example, args, tokenizer, fewshot_exps=None):
exp = {}
exp["id"] = example["id"]
if args.few_shot == 0 and not args.cot:
prompt = ""
elif args.few_shot > 0 and not args.cot:
prompt = ""
for fs_exp in fewshot_exps:
prompt = format_example(fs_exp, prompt, with_answer=True, num_options=args.num_options)
elif args.few_shot == 0 and args.cot:
prompt = pt.base_cot_prompt
else:
raise NotImplementedError("Not supported method.")
prompt = format_example(example, prompt, num_options=args.num_options)
# We treat the prompt message by now as the user input
if "falcon" in args.model:
prompt = "User: " + prompt + "\n" + "Assistant:"
else:
message = [
{"role": "user", "content": prompt}
]
prompt = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
exp["prompt"] = prompt
return exp
def format_shared_prompt(example, args, tokenizer, fewshot_exps=None):
exp = {}
exp["id"] = example["id"]
if args.few_shot == 0 and not args.cot:
prompt = pt.shared_zero_prompt
elif args.few_shot > 0 and not args.cot:
prompt = pt.shared_few_prompt
for fs_exp in fewshot_exps:
prompt = format_example(fs_exp, prompt, with_answer=True, num_options=args.num_options)
prompt += "\nNow make your best effort and select the correct answer for the following question. You only need to output the option.\n\n"
elif args.few_shot == 0 and args.cot:
prompt = pt.shared_cot_prompt
else:
raise NotImplementedError("Not supported method.")
prompt = format_example(example, prompt, num_options=args.num_options)
# We treat the prompt message by now as the user input
if "falcon" in args.model:
prompt = "User: " + prompt + "\n" + "Assistant:"
else:
message = [
{"role": "user", "content": prompt}
]
prompt = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
exp["prompt"] = prompt
return exp
def format_task_prompt(example, args, tokenizer, fewshot_exps=None):
exp = {}
exp["id"] = example["id"]
if args.few_shot == 0 and not args.cot:
pt_dict = json.loads(pt.task_zero_prompt, strict=False)
prompt = pt_dict[example["source"]]
elif args.few_shot > 0 and not args.cot:
pt_dict = json.loads(pt.task_few_prompt, strict=False)
prompt = pt_dict[example["source"]]
for fs_exp in fewshot_exps:
prompt = format_example(fs_exp, prompt, with_answer=True, num_options=args.num_options)
prompt += "\nNow make your best effort and select the correct answer for the following question. You only need to output the option.\n\n"
elif args.few_shot == 0 and args.cot:
pt_dict = json.loads(pt.task_cot_prompt, strict=False)
prompt = pt_dict[example["source"]]
else:
raise NotImplementedError("Not supported method.")
prompt = format_example(example, prompt, num_options=args.num_options)
# We treat the prompt message by now as the user input
if "falcon" in args.model:
prompt = "User: " + prompt + "\n" + "Assistant:"
else:
message = [
{"role": "user", "content": prompt}
]
prompt = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
exp["prompt"] = prompt
return exp
def prepare_inputs(tokenizer, exp, device):
"""Prepare inputs and move to the correct device."""
inputs = tokenizer(exp["prompt"], return_tensors="pt", truncation=True)
# Move all tensors to the model's device
inputs = {k: v.to(device) for k, v in inputs.items()}
return inputs
def log_softmax(logits):
logits = logits - max(logits)
return F.log_softmax(logits, dim=0)
def extract_activations(model, inputs, verbose=True):
"""
Extract MLP activations from the final token using forward hooks.
"""
collected = []
def hook_fn(module, inp, out):
if isinstance(out, torch.Tensor) and out.ndim == 3:
# Immediately move to CPU and detach to free GPU memory
collected.append(out[:, -1, :].detach().cpu().clone())
# Handle Falcon's different architecture
model_type = type(model).__name__
if model_type == "FalconForCausalLM":
layers = model.transformer.h # Falcon uses transformer.h instead of model.layers
else:
layers = model.model.layers # Works for Llama, Qwen, DeepSeek, Gemma
handles = [
layer.mlp.register_forward_hook(hook_fn)
for layer in layers if hasattr(layer, "mlp")
]
with torch.no_grad():
outputs = model(**inputs)
for h in handles:
h.remove()
# Stack and immediately free the list
activations = torch.stack(collected) # (L, B, H)
collected.clear()
return outputs, activations
# def extract_activations(model, inputs, verbose=True):
# """
# Extract residual stream hidden states from the final token using forward hooks.
# """
# collected = []
# def hook_fn(module, inp, out):
# # Transformer blocks return (hidden_state, past_key_value, ...) tuples
# hidden = out[0] if isinstance(out, tuple) else out
# if isinstance(hidden, torch.Tensor) and hidden.ndim == 3:
# collected.append(hidden[:, -1, :].detach().cpu().clone())
# model_type = type(model).__name__
# if model_type == "FalconForCausalLM":
# layers = model.transformer.h
# else:
# layers = model.model.layers
# handles = [
# layer.register_forward_hook(hook_fn) # block, not layer.mlp
# for layer in layers
# ]
# with torch.no_grad():
# outputs = model(**inputs)
# for h in handles:
# h.remove()
# activations = torch.stack(collected).squeeze(1) # (L, H)
# collected.clear()
# return outputs, activations
def get_model_outputs(model, tokenizer, data, args):
all_outputs = []
device = next(model.parameters()).device
batch_size = args.batch_size
if "Yi" in args.model:
option_ids = [tokenizer.encode(opt)[-1] for opt in options_alt[:args.num_options]]
else:
option_ids = [tokenizer.encode(opt)[-1] for opt in options[:args.num_options]]
# Left-padding so last token is always at position -1 for all samples
original_padding_side = tokenizer.padding_side
tokenizer.padding_side = "left"
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
for batch_start in tqdm(range(0, len(data), batch_size)):
batch = data[batch_start:batch_start + batch_size]
prompts = [exp["prompt"] for exp in batch]
inputs = tokenizer(prompts, return_tensors="pt", truncation=True, padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
if args.extract_activations:
verbose = (batch_start == 0)
outputs, activations = extract_activations(model, inputs, verbose=verbose)
else:
with torch.no_grad():
outputs = model(**inputs)
activations = None
# Logits at last position (left-padded, so -1 is the real last token)
batch_logits = outputs.logits[:, -1, :].detach().cpu() # (B, vocab)
for i, exp in enumerate(batch):
logits_options = batch_logits[i][option_ids]
out = {}
out["id"] = exp["id"]
out["logits_options"] = logits_options.float().numpy()
if args.extract_activations and activations is not None:
out["activations"] = activations[:, i, :] # (L, H)
all_outputs.append(out)
del outputs, batch_logits, inputs
if activations is not None:
del activations
tokenizer.padding_side = original_padding_side
return all_outputs
def validate_batching(model, tokenizer, data, args, n=10):
"""Compare batch_size=1 vs batch_size=N on first n samples to verify correctness."""
subset = data[:min(n, len(data))]
orig_batch = args.batch_size
print(f"\n Validating batching (batch_size={orig_batch} vs 1) on {len(subset)} samples...")
args.batch_size = 1
outputs_single = get_model_outputs(model, tokenizer, subset, args)
args.batch_size = orig_batch
outputs_batched = get_model_outputs(model, tokenizer, subset, args)
max_diff = 0
for s, b in zip(outputs_single, outputs_batched):
diff = abs(s["logits_options"] - b["logits_options"]).max()
max_diff = max(max_diff, float(diff))
print(f" Max logit difference: {max_diff:.2e}")
if max_diff < 1e-3:
print(f" PASS — batching is safe\n")
else:
print(f" WARNING — differences too large, falling back to batch_size=1\n")
args.batch_size = 1
def main(args):
if args.file != "xxx.json":
all_data_files = [args.file]
else:
all_data_files = ['mmlu_10k.json', 'cosmosqa_10k.json', 'hellaswag_10k.json',
'halu_dialogue.json', 'halu_summarization.json']
print(all_data_files)
# Pre-check which files need processing before loading the model
files_to_run = []
for file in all_data_files:
save_file = args.model.split("/")[-1] + "_" + file.split(".json")[0] + "_" + args.prompt_method
save_file += "_icl" + str(args.few_shot)
if args.cot:
save_file += "_cot"
save_file = os.path.join(args.output_dir, save_file)
os.makedirs(args.output_dir, exist_ok=True)
pkl_filename = save_file + (f"_{args.pkl_suffix}.pkl" if args.pkl_suffix else ".pkl")
act_filename = save_file + f"_{args.activation_suffix}.pt"
if not args.force and os.path.exists(pkl_filename):
skip = True
try:
with open(pkl_filename, "rb") as f:
existing = pickle.load(f)
if len(existing) > 0 and len(existing[0]["logits_options"]) != args.num_options:
print(f" Re-running {file} — existing has {len(existing[0]['logits_options'])} logits, need {args.num_options}")
skip = False
except Exception:
skip = False
if skip and (not args.extract_activations or os.path.exists(act_filename)):
print(f" Skipping {file} — output already exists with {args.num_options} logits.")
continue
files_to_run.append(file)
if not files_to_run:
print("Nothing to do — all outputs exist.")
return
tokenizer, model = load_model(args)
validated = False
for file in files_to_run:
# Compute output paths
save_file = args.model.split("/")[-1] + "_" + file.split(".json")[0] + "_" + args.prompt_method
save_file += "_icl" + str(args.few_shot)
if args.cot:
save_file += "_cot"
save_file = os.path.join(args.output_dir, save_file)
pkl_filename = save_file + (f"_{args.pkl_suffix}.pkl" if args.pkl_suffix else ".pkl")
act_filename = save_file + f"_{args.activation_suffix}.pt"
data = load_data(os.path.join(args.data_path, file))
# get few-shot examples
if args.few_shot > 0:
fewshot_exps = get_fewshot_exps(data)
else:
fewshot_exps = None
prompt_data = []
for datum in data:
if args.prompt_method == "base":
prompt_data.append(format_base_prompt(datum, args, tokenizer, fewshot_exps=fewshot_exps))
elif args.prompt_method == "shared":
prompt_data.append(format_shared_prompt(datum, args, tokenizer, fewshot_exps=fewshot_exps))
elif args.prompt_method == "task":
prompt_data.append(format_task_prompt(datum, args, tokenizer, fewshot_exps=fewshot_exps))
# Validate batching correctness on first non-skipped dataset
if args.validate and args.batch_size > 1 and not validated:
validate_batching(model, tokenizer, prompt_data, args)
validated = True
model_outputs = get_model_outputs(model, tokenizer, prompt_data, args)
logits_only = [{"id": out["id"], "logits_options": out["logits_options"]}
for out in model_outputs]
with open(pkl_filename, "wb") as f:
pickle.dump(logits_only, f)
if args.extract_activations:
save_dict = {
"outputs": model_outputs,
"model_name": args.model,
"file": file,
"prompt_method": args.prompt_method,
"few_shot": args.few_shot,
"cot": args.cot
}
torch.save(save_dict, save_file + f"_{args.activation_suffix}.pt")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--data_path', type=str, default="../data")
parser.add_argument('--file', type=str, default="xxx.json", help="Specify which dataset to use")
parser.add_argument('--prompt_method', type=str, default="base", help="Select from 'base', 'shared', 'task'")
parser.add_argument('--output_dir', type=str, default='outputs')
parser.add_argument('--few_shot', type=int, default=0)
parser.add_argument('--cot', action="store_true", default=False)
parser.add_argument('--extract_activations', action="store_true", default=False,
help="Extract MLP activations from final token")
parser.add_argument('--activation_suffix', type=str, default='activations',
help="Suffix for activation file (default: 'activations' -> _activations.pt)")
parser.add_argument('--pkl_suffix', type=str, default='',
help="Suffix for pkl file (default: '' -> .pkl, 'residual' -> _residual.pkl)")
parser.add_argument('--num_options', type=int, default=4,
help="Number of answer options to use (default: 4 for A,B,C,D)")
parser.add_argument('--batch_size', type=int, default=1,
help="Batch size for inference (uses left-padding)")
parser.add_argument('--validate', action="store_true", default=False,
help="Validate batching by comparing batch=1 vs batch=N on first 10 samples")
parser.add_argument('--force', action="store_true", default=False,
help="Force re-run even if output files already exist")
args = parser.parse_args()
main(args)