File size: 27,206 Bytes
707fcea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
"""
Complete pipeline for Best-of-N weighted selection on MATH-500.

This single script runs all steps:
1. Filter MATH-500 to 20 level 1-3 problems
2. Generate greedy (N=1) solutions and compute baseline accuracy
3. Sample N=16 solutions per problem with temperature sampling
4. Score all solutions with Skywork PRM (last-step prediction)
5. Compute weighted Best-of-N accuracy
6. Create dataset and push to HuggingFace Hub
7. Generate analysis plots and push them too

Reference papers:
- DeepMind (2408.03314): Scaling LLM Test-Time Compute, Section 5.1 + Appendix E
- Math-Shepherd (2312.08935): Process Reward Models, Section 3.4

Co-authored with Claude (Anthropic) as part of the HuggingFace internship exercise.
I can explain all code logic in detail.
"""

import json
import os
import random
import subprocess
import sys
import torch
import numpy as np
from collections import defaultdict
from typing import Optional

from datasets import Dataset, load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM


# ═══════════════════════════════════════════════════════════════════════════════
# Configuration
# ═══════════════════════════════════════════════════════════════════════════════
N_PROBLEMS = 20              # Number of problems to evaluate
N_SAMPLES = 16               # Number of solutions per problem for Best-of-N
TEMPERATURE = 0.7            # Sampling temperature for diverse solutions
MAX_NEW_TOKENS = 2048        # Max generation length
SEED = 42                    # Random seed for reproducibility
LLM_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
PRM_MODEL_ID = "Skywork/Skywork-o1-Open-PRM-Qwen-2.5-1.5B"
DATASET_ID = "cmpatino/math500-bon-weighted-results"

OUTPUT_DIR = "/tmp/exercise_outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

# System prompt: encourages chain-of-thought reasoning and \boxed{} format
SYSTEM_PROMPT = (
    "You are a helpful math assistant. Solve the problem step by step, "
    "showing your reasoning clearly. Put your final answer inside "
    "\\boxed{answer} at the end of your solution."
)


# ═══════════════════════════════════════════════════════════════════════════════
# Helper functions
# ═══════════════════════════════════════════════════════════════════════════════

def extract_boxed_solution(text: str) -> Optional[str]:
    """
    Extract content of the last \\boxed{} in text.
    Uses bracket-balanced parsing for nested braces.
    Source: https://gist.github.com/lewtun/9c2ce1937b741404090a3dc4c7c022b3
    """
    try:
        start_index = text.rindex("\\boxed{")
        content_start = start_index + 7
        bracket_count = 1
        current_pos = content_start
        while bracket_count > 0 and current_pos < len(text):
            if text[current_pos] == "{":
                bracket_count += 1
            elif text[current_pos] == "}":
                bracket_count -= 1
            current_pos += 1
        if bracket_count == 0:
            return text[content_start : current_pos - 1].strip()
        return None
    except (ValueError, Exception):
        return None


def weighted_best_of_n(extracted_answers, prm_scores):
    """
    Weighted Best-of-N selection (DeepMind 2408.03314, Eq. from Section 5.1):
    â = argmax_a  Σᵢ 𝟙(aᵢ = a) · score(sᵢ)

    Groups solutions by final answer, sums their PRM scores,
    and selects the answer group with the highest total.
    """
    answer_scores = defaultdict(float)
    for answer, score in zip(extracted_answers, prm_scores):
        if answer is None:
            continue  # Skip unparseable solutions
        answer_scores[answer] += score
    if not answer_scores:
        return None, {}
    best_answer = max(answer_scores, key=answer_scores.get)
    return best_answer, dict(answer_scores)


def standard_best_of_n(extracted_answers, prm_scores):
    """Standard Best-of-N: pick the single solution with highest PRM score."""
    best_idx, best_score = None, -1
    for i, (answer, score) in enumerate(zip(extracted_answers, prm_scores)):
        if answer is not None and score > best_score:
            best_score = score
            best_idx = i
    return extracted_answers[best_idx] if best_idx is not None else None


def majority_vote(extracted_answers):
    """Pure majority vote: pick the most frequent answer."""
    counts = defaultdict(int)
    for answer in extracted_answers:
        if answer is not None:
            counts[answer] += 1
    return max(counts, key=counts.get) if counts else None


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 1: Filter MATH-500 to level 1-3 problems
# ═══════════════════════════════════════════════════════════════════════════════
print("=" * 70)
print("STEP 1: Loading and filtering MATH-500 dataset")
print("=" * 70)

dataset = load_dataset("HuggingFaceH4/MATH-500", split="test")
print(f"Total problems: {len(dataset)}")

# Filter to levels 1-3 — these are easier problems that a 1.5B model
# can reasonably attempt. Levels 4-5 are too hard for small models.
filtered = dataset.filter(lambda x: x["level"] in [1, 2, 3])
print(f"Level 1-3 problems: {len(filtered)}")

# Fixed random sample for reproducibility
random.seed(SEED)
indices = random.sample(range(len(filtered)), k=N_PROBLEMS)
problems = filtered.select(indices)

problems_data = []
for i, p in enumerate(problems):
    problems_data.append({
        "idx": i,
        "problem": p["problem"],
        "solution": p["solution"],
        "answer": p["answer"],
        "subject": p["subject"],
        "level": p["level"],
        "unique_id": p["unique_id"],
    })
    print(f"  [{i+1:2d}] L{p['level']} {p['subject']:25s} {p['unique_id']}")

# Save for reference
with open(os.path.join(OUTPUT_DIR, "filtered_problems.json"), "w") as f:
    json.dump(problems_data, f, indent=2)


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 2: Generate greedy (N=1) solutions
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("STEP 2: Generating greedy solutions (N=1)")
print("=" * 70)

tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
    LLM_MODEL_ID, torch_dtype=torch.bfloat16, device_map="auto"
)


def generate_batch(problems_data, model, tokenizer, n, do_sample, temperature=None):
    """Generate n solutions per problem. Returns list of solution lists."""
    all_solutions = []
    for i, p in enumerate(problems_data):
        messages = [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": p["problem"]},
        ]
        prompt = tokenizer.apply_chat_template(
            messages, tokenize=False, add_generation_prompt=True
        )
        inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

        solutions = []
        for j in range(n):
            gen_kwargs = {"max_new_tokens": MAX_NEW_TOKENS, "do_sample": do_sample}
            if do_sample and temperature:
                gen_kwargs["temperature"] = temperature
                gen_kwargs["top_p"] = 0.95
            with torch.no_grad():
                output = model.generate(**inputs, **gen_kwargs)
            generated = output[0][inputs["input_ids"].shape[1]:]
            solutions.append(tokenizer.decode(generated, skip_special_tokens=True))

        all_solutions.append(solutions)
        ans = extract_boxed_solution(solutions[0]) if n == 1 else "..."
        tag = "greedy" if n == 1 else f"N={n}"
        print(f"  [{i+1:2d}/{len(problems_data)}] {tag} | {p['unique_id']} | answer={ans}")

    return all_solutions


# Greedy decoding (N=1, deterministic)
greedy_solutions = generate_batch(problems_data, model, tokenizer, n=1, do_sample=False)

# Evaluate greedy accuracy
greedy_correct = 0
for p, sols in zip(problems_data, greedy_solutions):
    extracted = extract_boxed_solution(sols[0])
    p["greedy_solution"] = sols[0]
    p["greedy_extracted_answer"] = extracted
    p["greedy_correct"] = (extracted is not None) and (extracted == p["answer"])
    if p["greedy_correct"]:
        greedy_correct += 1
    status = "✓" if p["greedy_correct"] else "✗"
    print(f"  {status} Expected: {p['answer']:20s} | Got: {str(extracted):20s} | {p['unique_id']}")

greedy_acc = greedy_correct / len(problems_data)
print(f"\n>>> Greedy accuracy: {greedy_correct}/{len(problems_data)} = {greedy_acc:.0%}")


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 3: Sample N=16 solutions per problem
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print(f"STEP 3: Sampling N={N_SAMPLES} solutions per problem (T={TEMPERATURE})")
print("=" * 70)

sampled_solutions = generate_batch(
    problems_data, model, tokenizer,
    n=N_SAMPLES, do_sample=True, temperature=TEMPERATURE
)

# Save solutions and free LLM memory
for p, sols in zip(problems_data, sampled_solutions):
    p["sampled_solutions"] = sols

with open(os.path.join(OUTPUT_DIR, "sampled_solutions.json"), "w") as f:
    json.dump(problems_data, f, indent=2)

del model
torch.cuda.empty_cache()
print("Freed LLM memory for PRM loading.")


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 4: Score with Skywork PRM (last-step prediction)
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("STEP 4: Scoring solutions with Skywork PRM")
print("=" * 70)

# Clone the Skywork PRM inference repo for the custom PRM_MODEL class
PRM_REPO_PATH = "/tmp/skywork-o1-prm-inference"
if not os.path.exists(PRM_REPO_PATH):
    print("Cloning Skywork PRM inference repo...")
    subprocess.run(
        ["git", "clone", "https://github.com/SkyworkAI/skywork-o1-prm-inference.git", PRM_REPO_PATH],
        check=True,
    )
sys.path.insert(0, PRM_REPO_PATH)

from model_utils.prm_model import PRM_MODEL
from model_utils.io_utils import prepare_input, prepare_batch_input_for_model, derive_step_rewards

prm_tokenizer = AutoTokenizer.from_pretrained(PRM_MODEL_ID, trust_remote_code=True)
prm_model = PRM_MODEL.from_pretrained(PRM_MODEL_ID, device_map="auto").eval()
prm_device = next(prm_model.pretrained_model.parameters()).device
print(f"PRM loaded on {prm_device}")


def score_solution(problem: str, solution: str) -> float:
    """
    Score a single solution using the PRM's last-step prediction.

    Per DeepMind (2408.03314, Appendix E): "We use the PRM's prediction at the
    last step as the full-answer score" — this outperforms min/product aggregation
    when the PRM is trained with soft MC-return labels.

    Returns: float in [0, 1] — the sigmoid-normalized score at the last step.
    """
    input_ids, steps, reward_flags = prepare_input(
        problem, solution, prm_tokenizer, step_token="\n"
    )
    input_ids_t, attn_mask_t, flags_t = prepare_batch_input_for_model(
        [input_ids], [reward_flags], prm_tokenizer.pad_token_id
    )
    input_ids_t = input_ids_t.to(prm_device)
    attn_mask_t = attn_mask_t.to(prm_device)
    flags_t = flags_t.to(prm_device)

    with torch.no_grad():
        _, _, rewards = prm_model(
            input_ids=input_ids_t, attention_mask=attn_mask_t, return_probs=True
        )
    step_rewards = derive_step_rewards(rewards, flags_t)
    # Return last step score (or 0.0 if no steps found)
    return step_rewards[0][-1] if step_rewards[0] else 0.0


# Score all sampled solutions
for i, p in enumerate(problems_data):
    print(f"\n  Scoring problem {i+1}/{len(problems_data)}: {p['unique_id']}")
    scores = []
    extracted_answers = []
    for j, sol in enumerate(p["sampled_solutions"]):
        score = score_solution(p["problem"], sol)
        scores.append(score)
        extracted_answers.append(extract_boxed_solution(sol))
        if (j + 1) % 8 == 0:
            print(f"    Scored {j+1}/{N_SAMPLES} (last: {score:.4f})")
    p["prm_scores"] = scores
    p["extracted_answers"] = extracted_answers

# Save scored results
with open(os.path.join(OUTPUT_DIR, "scored_results.json"), "w") as f:
    json.dump(problems_data, f, indent=2)

del prm_model
torch.cuda.empty_cache()


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 5: Compute Best-of-N with weighted selection
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("STEP 5: Computing Best-of-N accuracy")
print("=" * 70)

weighted_correct = 0
standard_correct = 0
majority_correct_count = 0

bon_summary = []
for p in problems_data:
    gt = p["answer"]

    # Weighted BoN
    w_ans, w_scores = weighted_best_of_n(p["extracted_answers"], p["prm_scores"])
    w_ok = (w_ans is not None) and (w_ans == gt)
    if w_ok: weighted_correct += 1

    # Standard BoN
    s_ans = standard_best_of_n(p["extracted_answers"], p["prm_scores"])
    s_ok = (s_ans is not None) and (s_ans == gt)
    if s_ok: standard_correct += 1

    # Majority vote
    m_ans = majority_vote(p["extracted_answers"])
    m_ok = (m_ans is not None) and (m_ans == gt)
    if m_ok: majority_correct_count += 1

    n_correct = sum(1 for a in p["extracted_answers"] if a == gt)

    bon_summary.append({
        "unique_id": p["unique_id"],
        "level": p["level"],
        "subject": p["subject"],
        "ground_truth": gt,
        "greedy_answer": p["greedy_extracted_answer"],
        "greedy_correct": p["greedy_correct"],
        "weighted_bon_answer": w_ans,
        "weighted_bon_correct": w_ok,
        "standard_bon_answer": s_ans,
        "standard_bon_correct": s_ok,
        "majority_vote_answer": m_ans,
        "majority_vote_correct": m_ok,
        "n_correct_in_16": n_correct,
        "answer_score_breakdown": w_scores,
        "prm_scores": p["prm_scores"],
    })

    sg = "✓" if p["greedy_correct"] else "✗"
    sw = "✓" if w_ok else "✗"
    print(f"  {sg}{sw} | {p['unique_id']:40s} | GT={gt:15s} | Greedy={str(p['greedy_extracted_answer']):15s} | WBoN={str(w_ans):15s} | {n_correct}/16 correct")

n = len(problems_data)
greedy_total = sum(1 for p in problems_data if p["greedy_correct"])
print(f"\n{'='*70}")
print(f"RESULTS SUMMARY")
print(f"{'='*70}")
print(f"  Greedy (N=1):              {greedy_total}/{n} = {greedy_total/n:.0%}")
print(f"  Majority Vote (N=16):      {majority_correct_count}/{n} = {majority_correct_count/n:.0%}")
print(f"  Standard Best-of-N (N=16): {standard_correct}/{n} = {standard_correct/n:.0%}")
print(f"  Weighted Best-of-N (N=16): {weighted_correct}/{n} = {weighted_correct/n:.0%}")

with open(os.path.join(OUTPUT_DIR, "bon_results.json"), "w") as f:
    json.dump(bon_summary, f, indent=2)


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 5b: Accuracy vs N analysis
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("ANALYSIS: Accuracy vs N")
print("=" * 70)

random.seed(SEED)
n_values = [1, 2, 4, 8, 16]
n_trials = 50

accuracy_by_n = {}
for n_val in n_values:
    if n_val == 16:
        correct = sum(1 for p in problems_data
                      for _ in [weighted_best_of_n(p["extracted_answers"], p["prm_scores"])]
                      if _[0] == p["answer"])
        acc = correct / len(problems_data)
    else:
        trial_accs = []
        for _ in range(n_trials):
            correct = 0
            for p in problems_data:
                idx = random.sample(range(16), n_val)
                sub_a = [p["extracted_answers"][j] for j in idx]
                sub_s = [p["prm_scores"][j] for j in idx]
                ans, _ = weighted_best_of_n(sub_a, sub_s)
                if ans == p["answer"]:
                    correct += 1
            trial_accs.append(correct / len(problems_data))
        acc = sum(trial_accs) / len(trial_accs)
    accuracy_by_n[n_val] = acc
    print(f"  N={n_val:2d}: {acc:.1%}")

with open(os.path.join(OUTPUT_DIR, "accuracy_by_n.json"), "w") as f:
    json.dump(accuracy_by_n, f, indent=2)


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 6: Generate plots
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("STEP 6: Generating analysis plots")
print("=" * 70)

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Patch

plt.rcParams.update({"font.size": 11, "figure.dpi": 150})

# --- Plot 1: Overall accuracy comparison ---
fig, ax = plt.subplots(figsize=(8, 5))
methods = ["Greedy\n(N=1)", "Majority Vote\n(N=16)", "Standard BoN\n(N=16)", "Weighted BoN\n(N=16)"]
accs = [
    greedy_total / n,
    majority_correct_count / n,
    standard_correct / n,
    weighted_correct / n,
]
colors = ["#4C72B0", "#55A868", "#C44E52", "#8172B2"]
bars = ax.bar(methods, accs, color=colors, edgecolor="white", linewidth=1.5)
for bar, a in zip(bars, accs):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.01,
            f"{a:.0%}", ha="center", va="bottom", fontweight="bold", fontsize=12)
ax.set_ylabel("Accuracy")
ax.set_title("Math Problem Accuracy: Greedy vs Best-of-N Methods\n(20 MATH-500 problems, Levels 1-3)")
ax.set_ylim(0, 1.15)
ax.grid(axis="y", alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(OUTPUT_DIR, "plot1_accuracy_comparison.png"))
plt.close()

# --- Plot 2: Accuracy vs N ---
fig, ax = plt.subplots(figsize=(7, 5))
ns = sorted(accuracy_by_n.keys())
acc_vals = [accuracy_by_n[nv] for nv in ns]
ax.plot(ns, acc_vals, "o-", color="#8172B2", linewidth=2, markersize=8, label="Weighted BoN")
ax.axhline(y=greedy_total/n, color="#4C72B0", linestyle="--", linewidth=1.5,
           label=f"Greedy baseline ({greedy_total/n:.0%})")
for nv, a in zip(ns, acc_vals):
    ax.annotate(f"{a:.0%}", (nv, a), textcoords="offset points", xytext=(0, 10), ha="center")
ax.set_xlabel("N (number of samples)")
ax.set_ylabel("Accuracy")
ax.set_title("Weighted Best-of-N Accuracy vs Number of Samples")
ax.set_xticks(ns)
ax.set_ylim(0, 1.1)
ax.legend()
ax.grid(alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(OUTPUT_DIR, "plot2_accuracy_vs_n.png"))
plt.close()

# --- Plot 3: Per-problem analysis ---
fig, ax = plt.subplots(figsize=(12, 5))
cat_colors = {
    "Both correct": "#55A868", "Only BoN correct": "#8172B2",
    "Only Greedy correct": "#C44E52", "Both wrong": "#CCCCCC"
}
bar_colors = []
for s in bon_summary:
    g, b = s["greedy_correct"], s["weighted_bon_correct"]
    if g and b: bar_colors.append(cat_colors["Both correct"])
    elif not g and b: bar_colors.append(cat_colors["Only BoN correct"])
    elif g and not b: bar_colors.append(cat_colors["Only Greedy correct"])
    else: bar_colors.append(cat_colors["Both wrong"])

x = range(len(bon_summary))
heights = [s["n_correct_in_16"] for s in bon_summary]
ax.bar(x, heights, color=bar_colors, edgecolor="white", linewidth=0.5)
ax.set_xticks(x)
labels = [f"L{s['level']}: {s['unique_id'].split('/')[-1].replace('.json','')[:12]}" for s in bon_summary]
ax.set_xticklabels(labels, rotation=45, ha="right", fontsize=8)
ax.set_ylabel("# Correct Solutions (out of 16)")
ax.set_title("Per-Problem: Correct Solutions in N=16 Sample")
legend_elements = [Patch(facecolor=c, label=l) for l, c in cat_colors.items()]
ax.legend(handles=legend_elements, loc="upper right", fontsize=9)
ax.grid(axis="y", alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(OUTPUT_DIR, "plot3_per_problem.png"))
plt.close()

# --- Plot 4: PRM score distribution ---
fig, ax = plt.subplots(figsize=(7, 5))
correct_scores, incorrect_scores = [], []
for p in problems_data:
    for ans, sc in zip(p["extracted_answers"], p["prm_scores"]):
        (correct_scores if ans == p["answer"] else incorrect_scores).append(sc)

bins = np.linspace(0, 1, 25)
ax.hist(correct_scores, bins=bins, alpha=0.7, label=f"Correct ({len(correct_scores)})", color="#55A868")
ax.hist(incorrect_scores, bins=bins, alpha=0.7, label=f"Incorrect ({len(incorrect_scores)})", color="#C44E52")
ax.set_xlabel("PRM Last-Step Score")
ax.set_ylabel("Count")
ax.set_title("PRM Score Distribution: Correct vs Incorrect Solutions")
ax.legend()
ax.grid(alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(OUTPUT_DIR, "plot4_prm_scores.png"))
plt.close()

print("All plots saved.")


# ═══════════════════════════════════════════════════════════════════════════════
# STEP 7: Push dataset to Hub
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("STEP 7: Pushing dataset to HuggingFace Hub")
print("=" * 70)

rows = []
for p, s in zip(problems_data, bon_summary):
    rows.append({
        "problem": p["problem"],
        "ground_truth_solution": p["solution"],
        "ground_truth_answer": p["answer"],
        "subject": p["subject"],
        "level": p["level"],
        "unique_id": p["unique_id"],
        "greedy_solution": p["greedy_solution"],
        "greedy_extracted_answer": p["greedy_extracted_answer"],
        "greedy_correct": p["greedy_correct"],
        "bon_weighted_answer": s["weighted_bon_answer"],
        "bon_weighted_correct": s["weighted_bon_correct"],
        "bon_standard_answer": s["standard_bon_answer"],
        "bon_standard_correct": s["standard_bon_correct"],
        "bon_majority_answer": s["majority_vote_answer"],
        "bon_majority_correct": s["majority_vote_correct"],
        "sampled_solutions": p["sampled_solutions"],
        "sampled_extracted_answers": p["extracted_answers"],
        "sampled_prm_scores": p["prm_scores"],
        "n_correct_in_16": s["n_correct_in_16"],
        "answer_score_breakdown": json.dumps(s["answer_score_breakdown"]),
    })

hf_dataset = Dataset.from_list(rows)
hf_dataset.push_to_hub(DATASET_ID, split="test")
print(f"Dataset pushed to: https://huggingface.co/datasets/{DATASET_ID}")

# Also upload the plots as artifacts
from huggingface_hub import HfApi
api = HfApi()
for plot_file in ["plot1_accuracy_comparison.png", "plot2_accuracy_vs_n.png",
                   "plot3_per_problem.png", "plot4_prm_scores.png"]:
    plot_path = os.path.join(OUTPUT_DIR, plot_file)
    if os.path.exists(plot_path):
        api.upload_file(
            path_or_fileobj=plot_path,
            path_in_repo=f"plots/{plot_file}",
            repo_id=DATASET_ID,
            repo_type="dataset",
        )
        print(f"  Uploaded {plot_file}")

# Upload the results JSON files too
for json_file in ["filtered_problems.json", "bon_results.json", "accuracy_by_n.json"]:
    json_path = os.path.join(OUTPUT_DIR, json_file)
    if os.path.exists(json_path):
        api.upload_file(
            path_or_fileobj=json_path,
            path_in_repo=f"results/{json_file}",
            repo_id=DATASET_ID,
            repo_type="dataset",
        )
        print(f"  Uploaded {json_file}")


# ═══════════════════════════════════════════════════════════════════════════════
# Final summary
# ═══════════════════════════════════════════════════════════════════════════════
print("\n" + "=" * 70)
print("FINAL RESULTS")
print("=" * 70)
print(f"  Greedy (N=1):              {greedy_total}/{len(problems_data)} = {greedy_total/len(problems_data):.0%}")
print(f"  Majority Vote (N=16):      {majority_correct_count}/{len(problems_data)} = {majority_correct_count/len(problems_data):.0%}")
print(f"  Standard Best-of-N (N=16): {standard_correct}/{len(problems_data)} = {standard_correct/len(problems_data):.0%}")
print(f"  Weighted Best-of-N (N=16): {weighted_correct}/{len(problems_data)} = {weighted_correct/len(problems_data):.0%}")
print(f"\n  Dataset: https://huggingface.co/datasets/{DATASET_ID}")
print("=" * 70)
print("DONE!")