-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_res_pruning.py
More file actions
453 lines (390 loc) · 15.3 KB
/
Copy patheval_res_pruning.py
File metadata and controls
453 lines (390 loc) · 15.3 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
import os
import glob
import json
import re
import argparse
import torch
import numpy as np
def compute_cora_nc_accuracy(res_path, sample=-1):
data = torch.load("dataset/cora/processed_data.pt", weights_only=False)
labels = data.label_texts
short_labels = [l.split('_')[0] for l in labels]
ys = data.y.numpy().tolist()
all_sample = 0
correct = 0
with open(res_path, "r") as f:
for line in f:
all_sample += 1
res = json.loads(line)
ans = res["text"]
y = ys[res["question_id"]]
short_label = short_labels[y]
if (
short_label.strip().lower() in ans.strip().lower()
and sum(l.strip().lower() in ans.strip().lower() for l in short_labels) == 1
):
correct += 1
if sample > 0 and all_sample >= sample:
break
acc = correct / all_sample if all_sample > 0 else 0.0
return acc, all_sample, correct
def compute_pubmed_nc_accuracy(res_path, sample=-1):
data = torch.load("dataset/pubmed/processed_data.pt", weights_only=False)
labels = data.label_texts
short_labels = [l[18:] for l in labels]
ys = data.y.numpy().tolist()
all_sample = 0
correct = 0
with open(res_path, "r") as f:
for line in f:
all_sample += 1
res = json.loads(line)
ans = res["text"]
y = ys[res["question_id"]]
short_label = short_labels[y]
label = labels[y]
if ans.lower().strip() == label.lower().strip():
correct += 1
elif (
short_label.lower().strip() in ans.lower().strip()
and sum(la.lower().strip() in ans.lower().strip() for la in short_labels) == 1
):
correct += 1
if sample > 0 and all_sample >= sample:
break
acc = correct / all_sample if all_sample > 0 else 0.0
return acc, all_sample, correct
def compute_arxiv_nc_accuracy(res_path, sample=-1):
data = torch.load("dataset/arxiv/processed_data.pt", weights_only=False)
labels = data.label_texts
short_labels = [l[0:5] for l in labels]
ys = data.y.numpy().tolist()
all_sample = 0
correct = 0
with open(res_path, "r") as f:
for line in f:
all_sample += 1
res = json.loads(line)
ans = res["text"]
y = ys[res["question_id"]]
short_label = short_labels[y]
label = labels[y]
if label.lower().strip() == ans.lower().strip():
correct += 1
elif (
short_label.lower() in ans.lower()
and sum(la.lower() in ans.lower() for la in short_labels) == 1
):
correct += 1
if sample > 0 and all_sample >= sample:
break
acc = correct / all_sample if all_sample > 0 else 0.0
return acc, all_sample, correct
def compute_lp_accuracy(res_path, sample=-1):
"""LP yes/no accuracy. Mirrors eval_res.eval_lp: counts a sample as correct
when the answer says "yes" and gt contains "yes", or the answer doesn't say
"yes" and gt contains "no". Works for any dataset (gt comes from the JSONL,
not from data.y), so no dataset-specific helper is needed."""
all_sample = 0
correct = 0
with open(res_path, "r") as f:
for line in f:
all_sample += 1
res = json.loads(line)
ans = res["text"].strip().lower()
label = res["gt"].strip().lower()
if ("yes" in ans and "yes" in label) or ("yes" not in ans and "no" in label):
correct += 1
if sample > 0 and all_sample >= sample:
break
acc = correct / all_sample if all_sample > 0 else 0.0
return acc, all_sample, correct
def compute_nc_accuracy(res_path, dataset, sample=-1):
if dataset == "cora":
return compute_cora_nc_accuracy(res_path, sample=sample)
if dataset == "pubmed":
return compute_pubmed_nc_accuracy(res_path, sample=sample)
if dataset == "arxiv":
return compute_arxiv_nc_accuracy(res_path, sample=sample)
raise ValueError(f"Unsupported dataset for nc pruning evaluation: {dataset}")
def compute_accuracy(res_path, dataset, task, sample=-1):
"""Dispatch to NC or LP scoring based on task."""
if task == "nc":
return compute_nc_accuracy(res_path, dataset=dataset, sample=sample)
if task == "lp":
return compute_lp_accuracy(res_path, sample=sample)
raise ValueError(f"Unsupported task for pruning evaluation: {task}")
def get_pruning_dir(result_dir, dataset, task="nc"):
"""LP scripts write to pruning_lp_{dataset}/, NC scripts to pruning_{dataset}/."""
sub = f"pruning_{dataset}" if task == "nc" else f"pruning_{task}_{dataset}"
base = os.path.basename(os.path.normpath(result_dir))
if base == sub:
return result_dir
return os.path.join(result_dir, sub)
def extract_nonsink_metadata(path, dataset, task="nc", template="ND"):
name = os.path.basename(path)
match = re.match(
rf"^{re.escape(dataset)}_{re.escape(task)}_{re.escape(template)}.*_prune_nonsinktoken_(\d+)_run(\d+)\.jsonl$",
name,
)
if match is None:
return None
return {
"file": path,
"filename": name,
"num_pruned": int(match.group(1)),
"run_idx": int(match.group(2)),
}
def find_nonsink_result_files(result_dir, dataset, num_pruned=None, task="nc", template="ND"):
pruning_dir = get_pruning_dir(result_dir, dataset, task=task)
pattern = os.path.join(pruning_dir, f"{dataset}_{task}_{template}*_prune_nonsinktoken_*_run*.jsonl")
matched = []
for path in glob.glob(pattern):
meta = extract_nonsink_metadata(path, dataset, task=task, template=template)
if meta is None:
continue
if num_pruned is not None and meta["num_pruned"] != num_pruned:
continue
matched.append((path, meta))
matched.sort(key=lambda item: item[1]["run_idx"])
return matched
def extract_sink_metadata(path, dataset, task="nc", template="ND"):
name = os.path.basename(path)
match = re.match(
rf"^{re.escape(dataset)}_{re.escape(task)}_{re.escape(template)}.*_prune_sinktoken_(top2|all)_run(\d+)\.jsonl$",
name,
)
if match is None:
return None
return {
"file": path,
"filename": name,
"pruning_mode": match.group(1),
"run_idx": int(match.group(2)),
}
def find_sink_result_files(result_dir, dataset, pruning_mode=None, task="nc", template="ND"):
pruning_dir = get_pruning_dir(result_dir, dataset, task=task)
pattern = os.path.join(pruning_dir, f"{dataset}_{task}_{template}*_prune_sinktoken_*_run*.jsonl")
matched = []
for path in glob.glob(pattern):
meta = extract_sink_metadata(path, dataset, task=task, template=template)
if meta is None:
continue
if pruning_mode is not None and meta["pruning_mode"] != pruning_mode:
continue
matched.append((path, meta))
matched.sort(key=lambda item: item[1]["run_idx"])
return matched
def eval_sink_pruning(result_dir, dataset, pruning_mode="all", sample=-1, verbose=True,
task="nc", template="ND"):
matched = find_sink_result_files(
result_dir=result_dir,
dataset=dataset,
pruning_mode=pruning_mode,
task=task,
template=template,
)
if len(matched) == 0:
print(f"No sink pruning files found for {dataset} (pruning_mode={pruning_mode}).")
return {
"dataset": dataset,
"pruning_mode": pruning_mode,
"num_files": 0,
"num_skipped": 0,
"mean_acc": None,
"std_acc": None,
"results": [],
}
results = []
accs = []
skipped = []
for res_path, meta in matched:
try:
acc, all_sample, correct = compute_accuracy(res_path, dataset=dataset, task=task, sample=sample)
except Exception as e:
skipped.append((meta["filename"], str(e)))
if verbose:
print(f"[{dataset}] skipping {meta['filename']} | error={e}")
continue
meta["acc"] = acc
meta["all_sample"] = all_sample
meta["correct"] = correct
results.append(meta)
accs.append(acc)
if verbose:
print(
f"[{dataset}] {meta['filename']} | "
f"pruning_mode={meta['pruning_mode']} | "
f"run_idx={meta['run_idx']} | "
f"acc={acc:.4f}"
)
if len(results) == 0:
print(f"All sink pruning files failed for {dataset} (pruning_mode={pruning_mode}).")
return {
"dataset": dataset,
"pruning_mode": pruning_mode,
"num_files": 0,
"num_skipped": len(skipped),
"mean_acc": None,
"std_acc": None,
"results": [],
}
mean_acc = float(np.mean(accs))
std_acc = float(np.std(accs, ddof=0))
print(f"\nSink pruning summary for {dataset}")
print(f"Number of valid files: {len(results)}")
print(f"Number of skipped files: {len(skipped)}")
print(f"Pruning mode: {pruning_mode}")
print(f"Mean accuracy: {mean_acc:.4f}")
print(f"Std accuracy: {std_acc:.4f}")
return {
"dataset": dataset,
"pruning_mode": pruning_mode,
"num_files": len(results),
"num_skipped": len(skipped),
"mean_acc": mean_acc,
"std_acc": std_acc,
"results": results,
}
def eval_nonsink_pruning(result_dir, dataset, num_pruned=1, sample=-1, verbose=True,
task="nc", template="ND"):
matched = find_nonsink_result_files(
result_dir=result_dir,
dataset=dataset,
num_pruned=num_pruned,
task=task,
template=template,
)
if len(matched) == 0:
print(f"No nonsink pruning files found for {dataset} (num_pruned={num_pruned}).")
return {
"dataset": dataset,
"num_pruned": num_pruned,
"num_files": 0,
"num_skipped": 0,
"mean_acc": None,
"std_acc": None,
"results": [],
}
results = []
accs = []
skipped = []
for res_path, meta in matched:
try:
acc, all_sample, correct = compute_accuracy(res_path, dataset=dataset, task=task, sample=sample)
except Exception as e:
skipped.append((meta["filename"], str(e)))
if verbose:
print(f"[{dataset}] skipping {meta['filename']} | error={e}")
continue
meta["acc"] = acc
meta["all_sample"] = all_sample
meta["correct"] = correct
results.append(meta)
accs.append(acc)
if verbose:
print(
f"[{dataset}] {meta['filename']} | "
f"num_pruned={meta['num_pruned']} | "
f"run_idx={meta['run_idx']} | "
f"acc={acc:.4f}"
)
if len(results) == 0:
print(f"All nonsink pruning files failed for {dataset} (num_pruned={num_pruned}).")
return {
"dataset": dataset,
"num_pruned": num_pruned,
"num_files": 0,
"num_skipped": len(skipped),
"mean_acc": None,
"std_acc": None,
"results": [],
}
mean_acc = float(np.mean(accs))
std_acc = float(np.std(accs, ddof=0))
print(f"\nNonsink pruning summary for {dataset}")
print(f"Number of valid files: {len(results)}")
print(f"Number of skipped files: {len(skipped)}")
print(f"Num pruned: {num_pruned}")
print(f"Mean accuracy: {mean_acc:.4f}")
print(f"Std accuracy: {std_acc:.4f}")
return {
"dataset": dataset,
"num_pruned": num_pruned,
"num_files": len(results),
"num_skipped": len(skipped),
"mean_acc": mean_acc,
"std_acc": std_acc,
"results": results,
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--result_dir", type=str, default="results_phc3mn")
parser.add_argument("--dataset", type=str, default="all",
choices=["cora", "pubmed", "arxiv", "all"])
parser.add_argument("--sample", type=int, default=-1)
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--target", type=str, default="nonsink", choices=["nonsink", "sink"],
help="nonsink: aggregate _prune_nonsinktoken_* files. "
"sink: aggregate _prune_sinktoken_* files.")
parser.add_argument("--num_pruned", type=int, default=1,
help="Filter for nonsink target.")
parser.add_argument("--pruning_mode", type=str, default="all", choices=["top2", "all"],
help="Filter for sink target.")
parser.add_argument("--task", type=str, default="nc", choices=["nc", "lp"],
help="nc: scores against data.label_texts (per-dataset helpers). "
"lp: scores yes/no against the gt field in each prediction record. "
"Also selects the result subdir (pruning_{dataset} vs pruning_lp_{dataset}) "
"and the {dataset}_{task}_{template}* filename pattern.")
parser.add_argument("--template", type=str, default="ND",
help="LLaGA encoding template, used in the result filename pattern "
"{dataset}_{task}_{template}*_prune_*. Default ND matches the NC scripts.")
args = parser.parse_args()
verbose = not args.quiet
datasets = ["cora", "pubmed", "arxiv"] if args.dataset == "all" else [args.dataset]
print(f"Scanning result directory: {args.result_dir}")
print(f"Task: {args.task} | Template: {args.template}")
print(f"Target: {args.target}")
if args.target == "nonsink":
print(f"Num pruned: {args.num_pruned}")
else:
print(f"Pruning mode: {args.pruning_mode}")
print(f"Datasets: {', ' .join(datasets)}\n")
all_stats = []
for i, dataset in enumerate(datasets):
if args.target == "nonsink":
stats = eval_nonsink_pruning(
result_dir=args.result_dir,
dataset=dataset,
num_pruned=args.num_pruned,
sample=args.sample,
verbose=verbose,
task=args.task,
template=args.template,
)
else:
stats = eval_sink_pruning(
result_dir=args.result_dir,
dataset=dataset,
pruning_mode=args.pruning_mode,
sample=args.sample,
verbose=verbose,
task=args.task,
template=args.template,
)
all_stats.append(stats)
if i != len(datasets) - 1:
print("\n" + "=" * 80 + "\n")
print("\n" + "=" * 80)
print("Final summary")
print("=" * 80)
for stats in all_stats:
if stats["num_files"] > 0:
print(
f"{stats['dataset']}: mean acc {stats['mean_acc']:.4f}, "
f"std {stats['std_acc']:.4f}, "
f"files {stats['num_files']}, "
f"skipped {stats['num_skipped']}"
)
else:
print(f"{stats['dataset']}: no valid files found")