-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaired_eval.py
More file actions
288 lines (260 loc) · 8.76 KB
/
paired_eval.py
File metadata and controls
288 lines (260 loc) · 8.76 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
"""
Paired PPL/NLL comparison for full attention, learned search, and Quest.
Runs all methods on the exact same eval batches and bootstraps per-batch loss
deltas. This removes most dataset-slice variance from learned-vs-Quest claims.
"""
from __future__ import annotations
import argparse
import json
import math
import os
import random
import sys
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from data import build_eval_data, model_attention_mask # noqa: E402
from eval import compute_nll # noqa: E402
from inference import ( # noqa: E402
install_ann_attention,
install_quest_attention,
uninstall_ann_attention,
)
from k_sweep import config_from_checkpoint # noqa: E402
from model import SearchProjectionModule # noqa: E402
def _mean(xs: list[float]) -> float:
return sum(xs) / max(1, len(xs))
def _bootstrap_ci(
xs: list[float],
n_boot: int = 10000,
seed: int = 0,
alpha: float = 0.05,
) -> dict:
if not xs:
return {"mean": float("nan"), "lo": float("nan"), "hi": float("nan")}
rng = random.Random(seed)
n = len(xs)
means = []
for _ in range(n_boot):
means.append(sum(xs[rng.randrange(n)] for _ in range(n)) / n)
means.sort()
lo = means[int((alpha / 2) * n_boot)]
hi = means[min(n_boot - 1, int((1 - alpha / 2) * n_boot))]
return {"mean": _mean(xs), "lo": lo, "hi": hi}
def _method_nlls(
base_model,
cached_batches,
method: str,
layers: list[int],
K: int,
search=None,
use_faiss: bool = False,
page_size: int = 16,
cfg=None,
) -> list[float]:
wrappers = []
if method == "learned":
wrappers = install_ann_attention(
base_model,
search,
layers,
K_retrieve=K,
use_faiss=use_faiss,
use_hnsw=use_faiss,
hnsw_M=getattr(cfg, "faiss_hnsw_M", 32),
hnsw_ef_construction=getattr(cfg, "faiss_hnsw_ef_construction", 40),
hnsw_ef_search=getattr(cfg, "faiss_hnsw_ef_search", 64),
)
elif method == "quest":
wrappers = install_quest_attention(
base_model, layers, K_retrieve=K, page_size=page_size
)
elif method != "full":
raise ValueError(f"unknown method: {method}")
try:
return [
compute_nll(base_model, ids, model_m, pos_ids, target_mask=token_m)
for ids, token_m, model_m, pos_ids in cached_batches
]
finally:
if wrappers:
uninstall_ann_attention(wrappers)
def paired_eval(
ckpt_path: str,
K: int = 128,
num_batches: int = 32,
skip_batches: int = 0,
page_size: int = 16,
use_faiss: bool = False,
n_boot: int = 10000,
seed: int = 0,
):
ckpt = torch.load(ckpt_path, map_location="cpu", weights_only=False)
cfg = config_from_checkpoint(ckpt)
print(f"Loading base model {cfg.base_model_name} ...")
tokenizer = AutoTokenizer.from_pretrained(cfg.base_model_name)
base_model = AutoModelForCausalLM.from_pretrained(
cfg.base_model_name,
dtype=torch.bfloat16,
device_map="auto",
attn_implementation="sdpa",
)
base_model.eval()
for p in base_model.parameters():
p.requires_grad = False
layers = [
i for i in cfg.full_attention_layer_indices
if i not in cfg.reserved_full_attention_indices
]
search = SearchProjectionModule(
d_model=base_model.config.hidden_size,
d_search=cfg.d_search,
layer_indices=layers,
use_mlp=cfg.use_mlp_proj,
).to(base_model.device).to(torch.bfloat16)
search.load_state_dict(ckpt["search_module"])
search.eval()
eval_data_all = list(
build_eval_data(tokenizer, cfg, num_batches=num_batches + skip_batches)
)
eval_data = eval_data_all[skip_batches:]
if len(eval_data) < num_batches:
print(
f"[warn] requested {num_batches} batches after skip={skip_batches}, "
f"got {len(eval_data)}"
)
print("Caching eval batches...")
cached = []
for batch in eval_data:
input_ids = batch["input_ids"].to(base_model.device)
token_mask = batch.get("attention_mask")
if token_mask is not None:
token_mask = token_mask.to(base_model.device)
segment_ids = batch.get("segment_ids")
if segment_ids is not None:
segment_ids = segment_ids.to(base_model.device)
position_ids = batch.get("position_ids")
if position_ids is not None:
position_ids = position_ids.to(base_model.device)
model_mask = model_attention_mask(
token_mask,
segment_ids,
block_causal_mask=getattr(cfg, "block_causal_mask", False),
dtype=base_model.dtype,
)
cached.append((input_ids, token_mask, model_mask, position_ids))
print("Running full attention...")
full = _method_nlls(base_model, cached, "full", layers, K)
print("Running learned search...")
learned = _method_nlls(
base_model,
cached,
"learned",
layers,
K,
search=search,
use_faiss=use_faiss,
cfg=cfg,
)
print("Running Quest-style page baseline...")
quest = _method_nlls(
base_model,
cached,
"quest",
layers,
K,
page_size=page_size,
)
learned_delta = [a - b for a, b in zip(learned, full)]
quest_delta = [a - b for a, b in zip(quest, full)]
diff = [a - b for a, b in zip(learned_delta, quest_delta)]
result = {
"ckpt": ckpt_path,
"step": ckpt.get("step"),
"K": K,
"page_size": page_size,
"num_batches": len(cached),
"skip_batches": skip_batches,
"use_faiss": use_faiss,
"nll": {
"full_mean": _mean(full),
"learned_mean": _mean(learned),
"quest_mean": _mean(quest),
},
"ppl": {
"full": math.exp(_mean(full)),
"learned": math.exp(_mean(learned)),
"quest": math.exp(_mean(quest)),
},
"relative_ppl_gap": {
"learned_vs_full": math.exp(_mean(learned) - _mean(full)) - 1.0,
"quest_vs_full": math.exp(_mean(quest) - _mean(full)) - 1.0,
"learned_vs_quest": math.exp(_mean(diff)) - 1.0,
},
"paired_nll_delta": {
"learned_minus_full": _bootstrap_ci(learned_delta, n_boot, seed),
"quest_minus_full": _bootstrap_ci(quest_delta, n_boot, seed + 1),
"learned_minus_quest": _bootstrap_ci(diff, n_boot, seed + 2),
},
"per_batch": [
{
"batch": i,
"full_nll": f,
"learned_nll": l,
"quest_nll": q,
"learned_minus_full": l - f,
"quest_minus_full": q - f,
"learned_minus_quest": l - q,
}
for i, (f, l, q) in enumerate(zip(full, learned, quest))
],
}
suffix = "faiss" if use_faiss else "exact"
skip_tag = f"_skip{skip_batches}" if skip_batches else ""
out_path = (
os.path.splitext(ckpt_path)[0]
+ f".paired_K{K}_{suffix}_quest_page{page_size}{skip_tag}.json"
)
with open(out_path, "w") as f:
json.dump(result, f, indent=2)
ci = result["paired_nll_delta"]["learned_minus_quest"]
print(
f"full ppl={result['ppl']['full']:.4f} "
f"learned ppl={result['ppl']['learned']:.4f} "
f"quest ppl={result['ppl']['quest']:.4f}"
)
print(
"learned-quest NLL delta: "
f"{ci['mean']:+.6f} [{ci['lo']:+.6f}, {ci['hi']:+.6f}]"
)
print(
"relative PPL gaps: "
f"learned/full={result['relative_ppl_gap']['learned_vs_full']:+.3%} "
f"quest/full={result['relative_ppl_gap']['quest_vs_full']:+.3%} "
f"learned/quest={result['relative_ppl_gap']['learned_vs_quest']:+.3%}"
)
print(f"Wrote {out_path}")
return result
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--ckpt", required=True)
parser.add_argument("--K", type=int, default=128)
parser.add_argument("--num-batches", type=int, default=32)
parser.add_argument("--skip-batches", type=int, default=0)
parser.add_argument("--page-size", type=int, default=16)
parser.add_argument("--use-faiss", action="store_true")
parser.add_argument("--n-boot", type=int, default=10000)
parser.add_argument("--seed", type=int, default=0)
args = parser.parse_args()
paired_eval(
args.ckpt,
K=args.K,
num_batches=args.num_batches,
skip_batches=args.skip_batches,
page_size=args.page_size,
use_faiss=args.use_faiss,
n_boot=args.n_boot,
seed=args.seed,
)
if __name__ == "__main__":
main()