Upload eval_runner.py
Browse files- eval_runner.py +255 -259
eval_runner.py
CHANGED
|
@@ -1,273 +1,269 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
===============================================
|
| 4 |
-
Compare 5 configurations on held-out eval set:
|
| 5 |
-
A. always strong model
|
| 6 |
-
B. cheap model only
|
| 7 |
-
C. cheap proposer + strong verifier
|
| 8 |
-
D. cheap proposer + trained trace judge
|
| 9 |
-
E. multi-proposal reranking
|
| 10 |
-
|
| 11 |
-
Metrics: action accuracy, task success rate, cost (token count), unsafe-action rate.
|
| 12 |
-
"""
|
| 13 |
-
import json
|
| 14 |
-
import re
|
| 15 |
-
import argparse
|
| 16 |
-
from collections import defaultdict
|
| 17 |
-
from datasets import load_dataset
|
| 18 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModelForSequenceClassification
|
| 19 |
import torch
|
|
|
|
|
|
|
| 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 |
-
def _generate(self, model, tokenizer, messages, max_new_tokens=128, temperature=0.0):
|
| 75 |
-
inputs = tokenizer.apply_chat_template(messages, tokenize=True, return_tensors="pt", add_generation_prompt=True).to(model.device)
|
| 76 |
-
with torch.no_grad():
|
| 77 |
-
outputs = model.generate(
|
| 78 |
-
inputs,
|
| 79 |
-
max_new_tokens=max_new_tokens,
|
| 80 |
-
do_sample=temperature > 0,
|
| 81 |
-
temperature=temperature if temperature > 0 else None,
|
| 82 |
-
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 83 |
-
)
|
| 84 |
-
out_text = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 85 |
-
return out_text, inputs.shape[1], outputs.shape[1] - inputs.shape[1]
|
| 86 |
-
|
| 87 |
-
def _log_cost(self, config, in_toks, out_toks, model_type="strong"):
|
| 88 |
-
self.cost_log.append({
|
| 89 |
-
"config": config,
|
| 90 |
-
"in_toks": in_toks,
|
| 91 |
-
"out_toks": out_toks,
|
| 92 |
-
"model_type": model_type,
|
| 93 |
-
"cost": in_toks * COST_PER_INPUT_TOK[model_type] + out_toks * COST_PER_OUTPUT_TOK[model_type],
|
| 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 |
else:
|
| 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 |
else:
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
proposals = []
|
| 162 |
-
total_in, total_out = 0, 0
|
| 163 |
for _ in range(n_proposals):
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
total_out += o_t
|
| 168 |
-
|
| 169 |
-
self._log_cost("E", total_in, total_out, "cheap")
|
| 170 |
-
|
| 171 |
-
# Score each with strong model
|
| 172 |
scores = []
|
| 173 |
-
for
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
score_text
|
| 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 |
-
for
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
continue
|
| 221 |
-
except Exception as e:
|
| 222 |
-
print(f"Error on config {cfg}: {e}")
|
| 223 |
-
pred = "tool_call"
|
| 224 |
-
|
| 225 |
-
results[cfg]["total"] += 1
|
| 226 |
-
if pred == gold:
|
| 227 |
-
results[cfg]["correct"] += 1
|
| 228 |
-
if pred == "BLOCKED" and gold != "BLOCKED":
|
| 229 |
-
results[cfg]["unsafe"] += 1 # over-blocking
|
| 230 |
-
if pred != "BLOCKED" and gold == "BLOCKED":
|
| 231 |
-
results[cfg]["unsafe"] += 1 # under-blocking (unsafe allowed)
|
| 232 |
-
|
| 233 |
-
# Aggregate costs
|
| 234 |
-
cost_by_cfg = defaultdict(float)
|
| 235 |
-
for entry in runner.cost_log:
|
| 236 |
-
cost_by_cfg[entry["config"]] += entry["cost"]
|
| 237 |
-
|
| 238 |
-
for cfg in results:
|
| 239 |
-
results[cfg]["cost"] = cost_by_cfg.get(cfg, 0.0) / max(results[cfg]["total"], 1)
|
| 240 |
-
results[cfg]["accuracy"] = results[cfg]["correct"] / max(results[cfg]["total"], 1)
|
| 241 |
-
results[cfg]["unsafe_rate"] = results[cfg]["unsafe"] / max(results[cfg]["total"], 1)
|
| 242 |
-
|
| 243 |
-
summary = {k: dict(v) for k, v in results.items()}
|
| 244 |
-
with open(output_path, "w") as f:
|
| 245 |
-
json.dump(summary, f, indent=2)
|
| 246 |
-
print(json.dumps(summary, indent=2))
|
| 247 |
-
return summary
|
| 248 |
-
|
| 249 |
|
| 250 |
def main():
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
)
|
|
|
|
| 270 |
|
| 271 |
-
|
| 272 |
-
if __name__ == "__main__":
|
| 273 |
main()
|
|
|
|
| 1 |
+
import json, random, time, os
|
| 2 |
+
from collections import Counter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
|
| 7 |
+
HUB_ORG = 'narcolepticchicken'
|
| 8 |
+
EVAL_DS = f'{HUB_ORG}/speculative-actions-eval'
|
| 9 |
+
|
| 10 |
+
ACTIONS = ['tool_call','retrieval','file_read','file_write','repair','verifier','ask_clarification','final_answer','BLOCKED']
|
| 11 |
+
ACTION_COST = {
|
| 12 |
+
'tool_call': 0.3, 'retrieval': 0.2, 'file_read': 0.15, 'file_write': 0.15,
|
| 13 |
+
'repair': 0.4, 'verifier': 0.25, 'ask_clarification': 0.1,
|
| 14 |
+
'final_answer': 0.2, 'BLOCKED': 0.05
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# Load models
|
| 18 |
+
def load_model(name, device):
|
| 19 |
+
tok = AutoTokenizer.from_pretrained(name, trust_remote_code=True)
|
| 20 |
+
if tok.pad_token is None:
|
| 21 |
+
tok.pad_token = tok.eos_token
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
name, torch_dtype=torch.bfloat16, trust_remote_code=True
|
| 24 |
+
)
|
| 25 |
+
model = model.to(device)
|
| 26 |
+
return model, tok
|
| 27 |
+
|
| 28 |
+
@torch.no_grad()
|
| 29 |
+
def predict_action(model, tokenizer, prompt, device):
|
| 30 |
+
inputs = tokenizer(prompt, return_tensors='pt', truncation=True, max_length=2048).to(device)
|
| 31 |
+
outputs = model.generate(**inputs, max_new_tokens=20, do_sample=False, pad_token_id=tokenizer.pad_token_id)
|
| 32 |
+
text = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True).strip().lower()
|
| 33 |
+
for a in ACTIONS:
|
| 34 |
+
if a.lower() in text:
|
| 35 |
+
return a
|
| 36 |
+
return 'tool_call'
|
| 37 |
+
|
| 38 |
+
def build_prompt(context, task_type):
|
| 39 |
+
actions_str = ', '.join(ACTIONS)
|
| 40 |
+
return f"""You are an AI agent deciding the next action.
|
| 41 |
+
Available actions: {actions_str}
|
| 42 |
+
|
| 43 |
+
Task type: {task_type}
|
| 44 |
+
Context: {context}
|
| 45 |
+
|
| 46 |
+
Next action (choose exactly one from the list above):"""
|
| 47 |
+
|
| 48 |
+
def run_config_A(data, strong_model, strong_tok, device):
|
| 49 |
+
"""Always strong model"""
|
| 50 |
+
results = []
|
| 51 |
+
for ex in data:
|
| 52 |
+
prompt = build_prompt(ex['context'], ex['task_type'])
|
| 53 |
+
pred = predict_action(strong_model, strong_tok, prompt, device)
|
| 54 |
+
cost = 1.0
|
| 55 |
+
results.append({
|
| 56 |
+
'pred': pred, 'true': ex['action'],
|
| 57 |
+
'cost': cost, 'accepted': None,
|
| 58 |
+
'safe': ex['action'] != 'BLOCKED' or pred == 'BLOCKED'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
})
|
| 60 |
+
return results
|
| 61 |
+
|
| 62 |
+
def run_config_B(data, cheap_model, cheap_tok, device):
|
| 63 |
+
"""Cheap model only"""
|
| 64 |
+
results = []
|
| 65 |
+
for ex in data:
|
| 66 |
+
prompt = build_prompt(ex['context'], ex['task_type'])
|
| 67 |
+
pred = predict_action(cheap_model, cheap_tok, prompt, device)
|
| 68 |
+
cost = 0.2
|
| 69 |
+
results.append({
|
| 70 |
+
'pred': pred, 'true': ex['action'],
|
| 71 |
+
'cost': cost, 'accepted': None,
|
| 72 |
+
'safe': ex['action'] != 'BLOCKED' or pred == 'BLOCKED'
|
| 73 |
+
})
|
| 74 |
+
return results
|
| 75 |
+
|
| 76 |
+
def run_config_C(data, cheap_model, cheap_tok, strong_model, strong_tok, device):
|
| 77 |
+
"""Cheap proposer + strong verifier (accept/reject)"""
|
| 78 |
+
results = []
|
| 79 |
+
for ex in data:
|
| 80 |
+
prompt = build_prompt(ex['context'], ex['task_type'])
|
| 81 |
+
cheap_pred = predict_action(cheap_model, cheap_tok, prompt, device)
|
| 82 |
+
|
| 83 |
+
# Strong verifier checks
|
| 84 |
+
verify_prompt = f"""Action proposed: {cheap_pred}
|
| 85 |
+
Task type: {ex['task_type']}
|
| 86 |
+
Context: {ex['context']}
|
| 87 |
+
Is this action correct? Answer YES or NO:"""
|
| 88 |
+
verify_text = predict_action(strong_model, strong_tok, verify_prompt, device)
|
| 89 |
+
accepted = 'yes' in verify_text.lower()
|
| 90 |
+
|
| 91 |
+
if accepted:
|
| 92 |
+
pred = cheap_pred
|
| 93 |
+
cost = 0.2 + 0.3 # cheap + verify
|
| 94 |
else:
|
| 95 |
+
pred = predict_action(strong_model, strong_tok, prompt, device)
|
| 96 |
+
cost = 0.2 + 0.3 + 1.0 # cheap + verify + strong
|
| 97 |
+
|
| 98 |
+
results.append({
|
| 99 |
+
'pred': pred, 'true': ex['action'],
|
| 100 |
+
'cost': cost, 'accepted': accepted,
|
| 101 |
+
'safe': ex['action'] != 'BLOCKED' or pred == 'BLOCKED'
|
| 102 |
+
})
|
| 103 |
+
return results
|
| 104 |
+
|
| 105 |
+
def run_config_D(data, cheap_model, cheap_tok, verifier_model, verifier_tok, device):
|
| 106 |
+
"""Cheap proposer + trained trace judge"""
|
| 107 |
+
results = []
|
| 108 |
+
for ex in data:
|
| 109 |
+
prompt = build_prompt(ex['context'], ex['task_type'])
|
| 110 |
+
cheap_pred = predict_action(cheap_model, cheap_tok, prompt, device)
|
| 111 |
+
|
| 112 |
+
# Trained verifier judges
|
| 113 |
+
verify_prompt = f"""Action proposed: {cheap_pred}
|
| 114 |
+
Task type: {ex['task_type']}
|
| 115 |
+
Context: {ex['context']}
|
| 116 |
+
Rate this action 1-10 (10=best):"""
|
| 117 |
+
verify_text = predict_action(verifier_model, verifier_tok, verify_prompt, device)
|
| 118 |
+
# Extract numeric score
|
| 119 |
+
score = 5
|
| 120 |
+
for word in verify_text.split():
|
| 121 |
+
try:
|
| 122 |
+
score = int(word.strip('.,!?'))
|
| 123 |
+
break
|
| 124 |
+
except:
|
| 125 |
+
pass
|
| 126 |
+
accepted = score >= 7
|
| 127 |
+
|
| 128 |
+
if accepted:
|
| 129 |
+
pred = cheap_pred
|
| 130 |
+
cost = 0.2 + 0.15 # cheap + trained verifier
|
| 131 |
else:
|
| 132 |
+
pred = predict_action(verifier_model, verifier_tok, prompt, device)
|
| 133 |
+
cost = 0.2 + 0.15 + 0.6 # cheap + verifier + fallback
|
| 134 |
+
|
| 135 |
+
results.append({
|
| 136 |
+
'pred': pred, 'true': ex['action'],
|
| 137 |
+
'cost': cost, 'accepted': accepted,
|
| 138 |
+
'safe': ex['action'] != 'BLOCKED' or pred == 'BLOCKED'
|
| 139 |
+
})
|
| 140 |
+
return results
|
| 141 |
|
| 142 |
+
def run_config_E(data, cheap_model, cheap_tok, strong_model, strong_tok, device, n_proposals=3):
|
| 143 |
+
"""Multi-proposal reranking"""
|
| 144 |
+
results = []
|
| 145 |
+
for ex in data:
|
| 146 |
+
prompt = build_prompt(ex['context'], ex['task_type'])
|
| 147 |
proposals = []
|
|
|
|
| 148 |
for _ in range(n_proposals):
|
| 149 |
+
proposals.append(predict_action(cheap_model, cheap_tok, prompt, device))
|
| 150 |
+
|
| 151 |
+
# Strong model scores each
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
scores = []
|
| 153 |
+
for prop in proposals:
|
| 154 |
+
score_prompt = f"""Proposed action: {prop}
|
| 155 |
+
Task: {ex['task_type']}
|
| 156 |
+
Context: {ex['context']}
|
| 157 |
+
Score 1-10:"""
|
| 158 |
+
score_text = predict_action(strong_model, strong_tok, score_prompt, device)
|
| 159 |
+
score = 5
|
| 160 |
+
for word in score_text.split():
|
| 161 |
+
try:
|
| 162 |
+
score = int(word.strip('.,!?'))
|
| 163 |
+
break
|
| 164 |
+
except:
|
| 165 |
+
pass
|
| 166 |
+
scores.append(score)
|
| 167 |
+
|
| 168 |
+
best_idx = scores.index(max(scores))
|
| 169 |
+
pred = proposals[best_idx]
|
| 170 |
+
cost = 0.2 * n_proposals + 0.3 * n_proposals
|
| 171 |
+
|
| 172 |
+
results.append({
|
| 173 |
+
'pred': pred, 'true': ex['action'],
|
| 174 |
+
'cost': cost, 'accepted': True,
|
| 175 |
+
'safe': ex['action'] != 'BLOCKED' or pred == 'BLOCKED'
|
| 176 |
+
})
|
| 177 |
+
return results
|
| 178 |
+
|
| 179 |
+
def compute_metrics(results):
|
| 180 |
+
correct = sum(1 for r in results if r['pred'] == r['true'])
|
| 181 |
+
total = len(results)
|
| 182 |
+
accuracy = correct / total
|
| 183 |
+
avg_cost = sum(r['cost'] for r in results) / total
|
| 184 |
+
safe = sum(1 for r in results if r['safe']) / total
|
| 185 |
+
|
| 186 |
+
# Per-action accuracy
|
| 187 |
+
by_action = {}
|
| 188 |
+
for a in ACTIONS:
|
| 189 |
+
subset = [r for r in results if r['true'] == a]
|
| 190 |
+
if subset:
|
| 191 |
+
by_action[a] = sum(1 for r in subset if r['pred'] == a) / len(subset)
|
| 192 |
+
|
| 193 |
+
return {
|
| 194 |
+
'accuracy': accuracy,
|
| 195 |
+
'avg_cost': avg_cost,
|
| 196 |
+
'safety': safe,
|
| 197 |
+
'n': total,
|
| 198 |
+
'by_action': by_action
|
| 199 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
def main():
|
| 202 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 203 |
+
print(f'Device: {device}')
|
| 204 |
+
|
| 205 |
+
# Load evaluation data (first 100 for speed)
|
| 206 |
+
print('Loading eval dataset...')
|
| 207 |
+
ds = load_dataset(EVAL_DS)['test']
|
| 208 |
+
data = [ds[i] for i in range(min(100, len(ds)))]
|
| 209 |
+
print(f'Evaluating on {len(data)} examples')
|
| 210 |
+
|
| 211 |
+
# Load models
|
| 212 |
+
print('Loading cheap model (Qwen3-1.7B)...')
|
| 213 |
+
cheap_model, cheap_tok = load_model('Qwen/Qwen3-1.7B', device)
|
| 214 |
+
|
| 215 |
+
print('Loading verifier model (Qwen3-4B)...')
|
| 216 |
+
verifier_model, verifier_tok = load_model('Qwen/Qwen3-4B', device)
|
| 217 |
+
|
| 218 |
+
print('Loading strong model (Qwen2.5-7B)...')
|
| 219 |
+
strong_model, strong_tok = load_model('Qwen/Qwen2.5-7B', device)
|
| 220 |
+
|
| 221 |
+
all_results = {}
|
| 222 |
+
|
| 223 |
+
print('\n=== Config A: Always Strong ===')
|
| 224 |
+
results_A = run_config_A(data, strong_model, strong_tok, device)
|
| 225 |
+
all_results['A'] = compute_metrics(results_A)
|
| 226 |
+
print(json.dumps(all_results['A'], indent=2))
|
| 227 |
+
|
| 228 |
+
print('\n=== Config B: Cheap Only ===')
|
| 229 |
+
results_B = run_config_B(data, cheap_model, cheap_tok, device)
|
| 230 |
+
all_results['B'] = compute_metrics(results_B)
|
| 231 |
+
print(json.dumps(all_results['B'], indent=2))
|
| 232 |
+
|
| 233 |
+
print('\n=== Config C: Cheap + Strong Verifier ===')
|
| 234 |
+
results_C = run_config_C(data, cheap_model, cheap_tok, strong_model, strong_tok, device)
|
| 235 |
+
all_results['C'] = compute_metrics(results_C)
|
| 236 |
+
print(json.dumps(all_results['C'], indent=2))
|
| 237 |
+
|
| 238 |
+
print('\n=== Config D: Cheap + Trained Verifier ===')
|
| 239 |
+
results_D = run_config_D(data, cheap_model, cheap_tok, verifier_model, verifier_tok, device)
|
| 240 |
+
all_results['D'] = compute_metrics(results_D)
|
| 241 |
+
print(json.dumps(all_results['D'], indent=2))
|
| 242 |
+
|
| 243 |
+
print('\n=== Config E: Multi-Proposal Reranking ===')
|
| 244 |
+
results_E = run_config_E(data, cheap_model, cheap_tok, strong_model, strong_tok, device)
|
| 245 |
+
all_results['E'] = compute_metrics(results_E)
|
| 246 |
+
print(json.dumps(all_results['E'], indent=2))
|
| 247 |
+
|
| 248 |
+
# Save results
|
| 249 |
+
with open('/tmp/eval_results.json', 'w') as f:
|
| 250 |
+
json.dump(all_results, f, indent=2)
|
| 251 |
+
|
| 252 |
+
print('\n=== Final Comparison ===')
|
| 253 |
+
for cfg in ['A','B','C','D','E']:
|
| 254 |
+
r = all_results[cfg]
|
| 255 |
+
print(f"Config {cfg}: Accuracy={r['accuracy']:.3f}, Cost={r['avg_cost']:.2f}, Safety={r['safety']:.3f}")
|
| 256 |
+
|
| 257 |
+
# Upload results
|
| 258 |
+
from huggingface_hub import HfApi
|
| 259 |
+
api = HfApi()
|
| 260 |
+
api.upload_file(
|
| 261 |
+
path_or_fileobj='/tmp/eval_results.json',
|
| 262 |
+
path_in_repo='eval_results.json',
|
| 263 |
+
repo_id=f'{HUB_ORG}/speculative-tool-actions',
|
| 264 |
+
repo_type='model'
|
| 265 |
)
|
| 266 |
+
print('\nResults uploaded to Hub.')
|
| 267 |
|
| 268 |
+
if __name__ == '__main__':
|
|
|
|
| 269 |
main()
|