Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
1156 lines (965 loc) · 39.7 KB
/
Copy pathevaluate.py
File metadata and controls
1156 lines (965 loc) · 39.7 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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# evaluate.py
import os
os.environ["UNSLOTH_COMPILE_OVERWRITE"] = "0"
from unsloth import FastLanguageModel
from src.models.model_module import (
load_model_and_tokenizer,
irl_load_model_and_tokenizer,
)
import hydra
from omegaconf import DictConfig, OmegaConf
from tqdm import tqdm
from torch.utils.data import DataLoader
from src.utils.utils import set_seed, save_results_to_jsonl
from src.data.dataset import get_dataset
from src.rewards.reward_functions import (
xmlcount_reward_func,
gsm8k_correctness_reward_func,
countdown_correctness_function,
medical_correctness_reward_func,
scienceqa_correctness_reward_func,
mmlu_correctness_reward_func,
eval_correctness_gsm8k,
eval_correctness_countdown,
eval_correctness_medical,
eval_correctness_scienceqa,
eval_correctness_mmlu,
)
import torch
import numpy as np
import pandas as pd
from src.eval.eval_module import (
compute_pass_at_k,
compute_reward_weighted_pass_at_k_from_scores,
compute_success_at_k_from_scores,
compute_oracle_at_1_from_N,
)
from src.eval.eval_mode_utils import (
MODE_AIME,
MODE_GENERATE,
MODE_PREGENERATED_POLICY_AND_REWARD,
canonical_eval_mode,
default_output_filename,
eval_mode_uses_pregenerated,
resolve_pregenerated_jsonl_path,
)
from vllm import SamplingParams
import wandb
from trl.trainer.grpo_trainer import maybe_apply_chat_template, apply_chat_template
# --- NEW IMPORTS FOR GUIDANCE ---
import copy
import re
wandb.login()
class TopKRewardLogitsProcessor:
def __init__(self, reward_model, reward_tokenizer, alpha=1.0, k=10, device="cuda"):
self.reward_model = reward_model
self.reward_tokenizer = reward_tokenizer
self.alpha = alpha
self.k = k
self.device = device
def __call__(self, prompt_tokens_ids, generated_tokens_ids, logits):
"""
Implements Algorithm 1: Reward-Augmented Decoding
1. Get Top-K tokens from Policy (logits).
2. Compute Rewards only for those K tokens.
3. Reweight and return.
"""
# 1. Identify Top-K candidates from the Policy Model
# logits shape: [vocab_size]
top_k_scores, top_k_indices = torch.topk(logits, self.k)
# 2. Prepare inputs for the Reward Model
# We need to construct K sequences: [Prompt + Generated + Candidate_i]
base_seq = prompt_tokens_ids + generated_tokens_ids
# Create a batch of K sequences
# shape: [K, seq_len + 1]
candidate_seqs = []
for token_idx in top_k_indices:
candidate_seqs.append(base_seq + [token_idx.item()])
inputs = torch.tensor(candidate_seqs, device=self.device)
# 3. Compute Rewards (Batched for Efficiency)
with torch.no_grad():
# Run the RM on the K candidates
# Assuming RM outputs [Batch, Seq_Len, Vocab] (Dense) OR [Batch] (Scalar)
output = self.reward_model(inputs)
# Logic to extract the specific scalar reward for the last token
if hasattr(output, "logits"):
rm_scores = output.logits[:, -1].mean(dim=-1) # Fallback heuristic
else:
# If Scalar RM
rm_scores = output[:, -1] if output.ndim > 1 else output
new_logits = torch.full_like(logits, float("-inf"))
guided_scores = top_k_scores + (self.alpha * rm_scores)
new_logits.scatter_(0, top_k_indices, guided_scores)
return new_logits
# ==========================================
# 2. Chunk-Level Guidance (Step Search)
# ==========================================
def generate_with_chunk_guidance(
model,
reward_model,
reward_tokenizer,
prompts_text,
sampling_params,
step_size=5,
n_candidates=4,
max_tokens=256,
):
"""
Performs generation by stepping 'step_size' tokens at a time,
generating 'n_candidates', and selecting the best one via Reward Model.
"""
# Initialize current generation with prompts
current_gens = prompts_text
# We iterate until we hit max length (simplified loop)
# Note: vLLM is most efficient with batching, this manual loop
# splits the batch logic somewhat.
for _ in range(0, max_tokens, step_size):
# 1. Generate Candidates for the next step
step_params = copy.deepcopy(sampling_params)
step_params.max_tokens = step_size
step_params.n = n_candidates
# model.fast_generate typically returns a list of RequestOutputs
outputs = model.fast_generate(
current_gens, sampling_params=step_params, use_tqdm=False
)
new_current_gens = []
# 2. Evaluate Candidates
for i, out in enumerate(outputs):
candidates_text = [o.text for o in out.outputs] # Just the NEW text
parent_text = current_gens[i]
# Construct full sequences for scoring
full_candidates = [parent_text + c for c in candidates_text]
# Tokenize for RM
inputs = reward_tokenizer(
full_candidates, return_tensors="pt", padding=True, truncation=True
).to(reward_model.device)
with torch.no_grad():
rm_out = reward_model(**inputs)
# Assuming dense rewards [B, Seq], take mean of the NEW chunk
# or just the score of the last token.
if hasattr(rm_out, "logits"):
scores = rm_out.logits.mean(dim=1).squeeze().cpu().numpy()
else:
scores = rm_out.mean(dim=1).squeeze().cpu().numpy()
# Handle single candidate edge case
if scores.ndim == 0:
scores = [scores]
# 3. Select Best Candidate
best_idx = np.argmax(scores)
best_extension = candidates_text[best_idx]
new_current_gens.append(parent_text + best_extension)
current_gens = new_current_gens
final_outputs = []
for gen in current_gens:
final_outputs.append([{"content": gen[len(p) :]} for p in prompts_text])
return current_gens
# Module-level cache
_BOUNDARY_TOKEN_DECODE_CACHE = {}
def sentence_boundary_mask(reward_tokenizer, full_batch, base_completion_mask, device):
"""
Robust step-boundary detector for process reward modelling.
Args:
full_batch: dict with key "input_ids" -> LongTensor [bs, L]
base_completion_mask: Bool/0-1 tensor [bs, L], True only on assistant completion tokens
reward_tokenizer: HuggingFace tokenizer used to decode token pieces
Returns:
boundary_mask: Bool tensor [bs, L]
"""
global _BOUNDARY_TOKEN_DECODE_CACHE
input_ids = full_batch["input_ids"]
bs, L = input_ids.shape
boundary_mask = torch.zeros((bs, L), dtype=torch.bool, device=device)
explicit_boundaries = [
"</think>",
"<|im_end|>",
"<|endoftext|>",
"<|eot_id|>",
"####",
"\r\n\r\n",
"\n\n\n",
"\n\n",
".\n",
"!\n",
"?\n",
";\n",
":\n",
"\n- ",
"\n* ",
"\n• ",
"\n1.",
"\n2.",
"\n3.",
"\n4.",
"\n5.",
"\n6.",
"\n7.",
"\n8.",
"\n9.",
"\n10.",
]
explicit_boundaries = sorted(explicit_boundaries, key=len, reverse=True)
max_explicit_len = max(len(x) for x in explicit_boundaries)
suffix_window = max(96, max_explicit_len + 48)
_abbr = {
"e.g.",
"i.e.",
"etc.",
"vs.",
"cf.",
"mr.",
"mrs.",
"ms.",
"dr.",
"prof.",
"sr.",
"jr.",
"no.",
"fig.",
"eq.",
"sec.",
"resp.",
}
_wrapper_tags = {
"<think>",
"</think>",
"<answer>",
"</answer>",
"<reasoning>",
"</reasoning>",
}
def decode_one(tok_id: int) -> str:
if tok_id not in _BOUNDARY_TOKEN_DECODE_CACHE:
_BOUNDARY_TOKEN_DECODE_CACHE[tok_id] = reward_tokenizer.decode(
[tok_id],
skip_special_tokens=False,
clean_up_tokenization_spaces=False,
)
return _BOUNDARY_TOKEN_DECODE_CACHE[tok_id]
def _last_nonspace_char(s: str):
for ch in reversed(s):
if not ch.isspace():
return ch
return None
def _strip_trailing_space(s: str) -> str:
return s.rstrip(" \t")
def _looks_like_abbreviation(s: str) -> bool:
s = _strip_trailing_space(s).lower()
m = re.search(r"([a-z]{1,10}\.)$", s)
if m and m.group(1) in _abbr:
return True
m2 = re.search(r"([a-z]\.[a-z]\.)$", s)
if m2 and m2.group(1) in _abbr:
return True
return False
def _piece_is_only_layout(piece: str) -> bool:
return piece.strip() == ""
def _normalise_visible_text(s: str) -> str:
x = s
for tag in _wrapper_tags:
x = x.replace(tag, "")
x = re.sub(r"<\|[^>]+?\|>", "", x)
x = re.sub(r"\s+", "", x)
return x
def _starts_with_digit(piece: str) -> bool:
if piece is None:
return False
m = re.match(r"^[ \t\r\n]*([0-9])", piece)
return m is not None
def _is_explicit_boundary(s: str) -> bool:
return any(s.endswith(x) for x in explicit_boundaries)
def _is_sentence_punct_boundary(
s: str, just_added_piece: str, next_piece: str
) -> bool:
if just_added_piece != "" and just_added_piece.strip(" \t") == "":
return False
s = _strip_trailing_space(s)
if not s:
return False
last = s[-1]
if last not in ".!?;:":
return False
if last == "." and _looks_like_abbreviation(s):
return False
# Avoid splitting on decimal points like 90.2
if last == "." and _starts_with_digit(next_piece):
return False
if last in "!?":
return True
if last in ";:":
return True
return True
def _is_newline_boundary(s: str, just_added_piece: str) -> bool:
if "\n" not in just_added_piece and "\r" not in just_added_piece:
return False
if not s.endswith("\n"):
return False
if s.endswith("\n\n"):
return True
prefix = s[:-1]
ch = _last_nonspace_char(prefix)
if ch is None:
return False
if ch in ".!?;:)":
return True
if prefix.endswith("</think>") or prefix.endswith("####"):
return True
return False
def _ends_reasoning_step(s: str, just_added_piece: str, next_piece: str) -> bool:
if _is_explicit_boundary(s):
return True
if _is_newline_boundary(s, just_added_piece):
return True
if _is_sentence_punct_boundary(s, just_added_piece, next_piece):
return True
return False
for b in range(bs):
completion_positions = torch.nonzero(
base_completion_mask[b].bool(), as_tuple=False
).flatten()
if completion_positions.numel() == 0:
continue
completion_positions_list = completion_positions.tolist()
decoded_pieces = [
decode_one(int(input_ids[b, pos].item()))
for pos in completion_positions_list
]
suffix = ""
seen_meaningful_content = False
prev_was_boundary = False
for i, pos in enumerate(completion_positions_list):
piece = decoded_pieces[i]
next_piece = decoded_pieces[i + 1] if i + 1 < len(decoded_pieces) else None
suffix += piece
if len(suffix) > suffix_window:
suffix = suffix[-suffix_window:]
if not seen_meaningful_content and _normalise_visible_text(suffix) != "":
seen_meaningful_content = True
is_boundary = _ends_reasoning_step(suffix, piece, next_piece)
if is_boundary and not seen_meaningful_content:
is_boundary = False
if is_boundary and prev_was_boundary and _piece_is_only_layout(piece):
is_boundary = False
if is_boundary:
boundary_mask[b, pos] = True
prev_was_boundary = True
else:
prev_was_boundary = False
# Always include the final completion token so the last segment gets a reward
boundary_mask[b, int(completion_positions[-1].item())] = True
boundary_mask &= base_completion_mask.bool()
return boundary_mask
def every_n_tokens_mask(full_batch, base_completion_mask, n: int):
"""
Returns mask [bs, L] that is True every n tokens within the completion
and always True at the final completion token.
"""
input_ids = full_batch["input_ids"] # [bs, L]
bs, L = input_ids.shape
device = input_ids.device
# Count token positions within the completion (1-based)
token_indices = base_completion_mask.long().cumsum(dim=1) # [bs, L]
# Mark every n-th token (ignore positions outside completion)
every_n_mask = (token_indices % n == 0) & base_completion_mask
# Find last completion token index per batch
last_indices = token_indices.argmax(dim=1) # [bs]
# Ensure last completion token is always included
every_n_mask[
torch.arange(bs, device=device), last_indices
] |= base_completion_mask.any(dim=1)
return every_n_mask
def backfill_rewards(rewards, mask):
B, T = rewards.shape
indices = torch.arange(T, device=rewards.device).expand(B, T)
masked_indices = torch.where(
mask.bool(), indices, torch.tensor(T, device=rewards.device)
)
next_valid_index = torch.cummin(masked_indices.flip(1), dim=1)[0].flip(1)
next_valid_index = next_valid_index.clamp(max=T - 1).long()
result = torch.gather(rewards, 1, next_valid_index)
return result
@torch.no_grad()
def score_with_policy_model(
policy_model,
policy_tokenizer,
prompts_msgs,
decoded_per_prompt,
max_length=512,
micro_batch=16,
):
FastLanguageModel.for_inference(policy_model)
device = next(policy_model.parameters()).device
# 1. Flatten prompts and completions into a single list of strings
texts = []
completion_texts = []
for p_msgs, completions in zip(prompts_msgs, decoded_per_prompt):
for c in completions:
content = c if isinstance(c, str) else c.get("content", "")
msgs = p_msgs + [{"role": "assistant", "content": content}]
texts.append(
apply_chat_template({"messages": msgs}, policy_tokenizer)["text"]
)
comp_text = content + (policy_tokenizer.eos_token or "")
completion_texts.append(comp_text)
if not texts:
return [[] for _ in prompts_msgs]
# Calculate global max seq_len strictly for the final output array shape
global_tokens = policy_tokenizer(
completion_texts,
return_attention_mask=True,
add_special_tokens=False,
padding=False,
)
seq_len = min(max(len(t) for t in global_tokens["input_ids"]), max_length)
all_log_probs = []
# 2. Dynamic batching loop
for i in range(0, len(texts), micro_batch):
batch_texts = texts[i : i + micro_batch]
batch_completion_texts = completion_texts[i : i + micro_batch]
batch_inputs = policy_tokenizer(
text=batch_texts,
return_tensors="pt",
padding=True,
add_special_tokens=False,
truncation=True,
max_length=max_length,
padding_side="right",
).to(device)
batch_completions = policy_tokenizer(
text=batch_completion_texts,
return_tensors="pt",
padding=True,
add_special_tokens=False,
truncation=True,
max_length=max_length,
).to(device)
with torch.inference_mode():
outputs = policy_model(**batch_inputs)
# Find where the completions start
completion_lens = batch_completions["attention_mask"].sum(dim=1).long()
full_lens = batch_inputs["attention_mask"].sum(dim=1).long()
start_indices = (full_lens - completion_lens).clamp(min=0)
current_micro_batch_size = batch_inputs["input_ids"].size(0)
batch_res = torch.full(
(current_micro_batch_size, seq_len), float("nan"), device=device
)
# Process log probs sequence-by-sequence to avoid large temporary tensors
loss_fct = torch.nn.CrossEntropyLoss(reduction="none")
for b in range(current_micro_batch_size):
comp_len = completion_lens[b].item()
# The logit at index i predicts the token at index i+1
start_idx = max(start_indices[b].item() - 1, 0)
# Prevent out-of-bounds if the sequence hit max_length
end_idx = min(
start_idx + comp_len, batch_inputs["input_ids"].size(1) - 1
)
actual_len = end_idx - start_idx
if actual_len > 0:
seq_logits = outputs.logits[b, start_idx:end_idx, :]
seq_labels = batch_inputs["input_ids"][
b, start_idx + 1 : end_idx + 1
]
# Cross entropy gives -log(p). Invert it to get log(p).
seq_log_probs = -loss_fct(seq_logits, seq_labels)
copy_len = min(actual_len, seq_len)
batch_res[b, :copy_len] = seq_log_probs[:copy_len]
all_log_probs.append(batch_res.cpu().numpy())
del outputs
torch.cuda.empty_cache()
B = len(prompts_msgs)
final_log_probs = np.concatenate(all_log_probs, axis=0).reshape(B, -1, seq_len)
return final_log_probs
# ==========================================
# 3. Helper for Scoring (Existing)
# ==========================================
@torch.no_grad()
def score_with_reward_model(
reward_model,
reward_tokenizer,
prompts_msgs,
decoded_per_prompt,
dense_reward=False,
max_length=512,
micro_batch=16,
clip_reward_model=False,
reward_lb=-5.0,
reward_ub=5.0,
dense_partial_fixed_n=10,
):
# --- Optimization 1: Enable Unsloth Inference Kernels ---
FastLanguageModel.for_inference(reward_model)
device = next(reward_model.parameters()).device
# 1. Flatten prompts and completions into a single list of strings
texts = []
completion_texts = []
for p_msgs, completions in zip(prompts_msgs, decoded_per_prompt):
for c in completions:
content = c if isinstance(c, str) else c.get("content", "")
# Build full text
msgs = p_msgs + [{"role": "assistant", "content": content}]
texts.append(
apply_chat_template({"messages": msgs}, reward_tokenizer)["text"]
)
# Build completion text for length calculation
comp_text = content + (reward_tokenizer.eos_token or "")
completion_texts.append(comp_text)
if not texts:
return [[] for _ in prompts_msgs]
# Calculate global max seq_len strictly for the final output array shape
# We batch this strictly for CPU side length checking
global_tokens = reward_tokenizer(
completion_texts,
return_attention_mask=True,
add_special_tokens=False,
padding=False,
)
# The max length of any completion in the dataset (clamped to max_length limit)
seq_len = min(max(len(t) for t in global_tokens["input_ids"]), max_length)
new_logits = []
# --- Optimization 2: Dynamic Batching Loop ---
for i in range(0, len(texts), micro_batch):
batch_texts = texts[i : i + micro_batch]
batch_completion_texts = completion_texts[i : i + micro_batch]
# Tokenize ONLY this batch with padding=True (Dynamic Padding)
# This makes the tensor width = length of longest sequence in THIS batch, not 512.
batch_inputs = reward_tokenizer(
text=batch_texts,
return_tensors="pt",
padding=True,
add_special_tokens=False,
truncation=True,
max_length=max_length,
padding_side="right",
).to(device)
# Tokenize completions just for length calculations
batch_completions = reward_tokenizer(
text=batch_completion_texts,
return_tensors="pt",
padding=True,
add_special_tokens=False,
truncation=True,
max_length=max_length,
).to(device)
with torch.inference_mode():
# Model Forward Pass
# logits shape: [micro_batch, dynamic_seq_len] or [micro_batch]
reward_outputs = reward_model(**batch_inputs)
reward_logits = reward_outputs.logits.squeeze(-1)
current_batch_max_len = batch_inputs["input_ids"].shape[1]
# Handle Non-Dense (Scalar) Rewards
if not dense_reward:
# --- Optimization 3: Use expand instead of repeat (Memory View) ---
reward_logits = reward_logits.unsqueeze(1).expand(
-1, current_batch_max_len
)
if clip_reward_model:
reward_logits = torch.clamp(reward_logits, min=reward_lb, max=reward_ub)
# Calculate indices
completion_lens = batch_completions["attention_mask"].sum(dim=1).long()
full_lens = batch_inputs["attention_mask"].sum(dim=1).long()
start_indices = (full_lens - completion_lens).clamp(min=0)
# Generate gather indices
# We must clamp to seq_len because the final output expects fixed width
gather_indices = (
start_indices[:, None] + torch.arange(seq_len, device=device)[None, :]
)
# Important: Clamp indices to the current batch's dynamic width to avoid out-of-bounds
gather_indices_safe = gather_indices.clamp(max=current_batch_max_len - 1)
# Gather
reward_comp = reward_logits.gather(1, gather_indices_safe)
# --- Handle Dense Partial Logic (Optional) ---
if dense_reward in ["partial", "partial_fixed"]:
# Note: These masks need to be regenerated for the dynamic batch shape
if dense_reward == "partial":
end_of_thought_mask = sentence_boundary_mask(
reward_tokenizer,
batch_inputs,
batch_inputs["attention_mask"],
device,
)
# # --- DEBUGGING VISUALIZATION START ---
# # Check only the first sample in the micro-batch
# sample_ids = batch_inputs["input_ids"][0]
# sample_tokens = reward_tokenizer.convert_ids_to_tokens(sample_ids)
# sample_mask = end_of_thought_mask[0]
# print("\n" + "="*50)
# print("DEBUG: Reward Model Token Alignment")
# print(f"BOS Token: {reward_tokenizer.bos_token} (ID: {reward_tokenizer.bos_token_id})")
# print("-" * 50)
# for idx, (token, is_boundary) in enumerate(zip(sample_tokens, sample_mask)):
# # Only print non-padding tokens for clarity
# if token == reward_tokenizer.pad_token:
# continue
# boundary_marker = " [STEP END] <---" if is_boundary else ""
# print(f"Token {idx:3}: '{token:15}' {boundary_marker}")
# print("="*50 + "\n")
# # --- DEBUGGING VISUALIZATION END ---
else:
end_of_thought_mask = every_n_tokens_mask(
batch_inputs,
batch_inputs["attention_mask"],
dense_partial_fixed_n,
)
# We need to act carefully here because gather_indices might be wider than the dynamic batch
# But since we clamped gather_indices_safe, it is valid for gathering.
end_of_thought_mask = end_of_thought_mask.gather(1, gather_indices_safe)
reward_comp = backfill_rewards(reward_comp, end_of_thought_mask)
# Apply NaN mask for padding/invalid
# import IPython; IPython.embed(); exit()
output_mask = (
torch.arange(seq_len, device=device)[None, :] < completion_lens[:, None]
)
reward_comp[~output_mask] = float("nan")
out_cpu = reward_comp.detach().float().cpu()
new_logits.append(out_cpu)
B = len(prompts_msgs)
all_scores = np.concatenate(new_logits, axis=0).reshape(B, -1, seq_len)
return all_scores
def _resolve_eval_functions(dataset_name: str):
if dataset_name in {"gsm8k", "gsm8k_kd"} or "aime" in dataset_name:
reward_fns = [
("xmlcount_reward_func", xmlcount_reward_func),
("correctness_reward_func", gsm8k_correctness_reward_func),
]
eval_correctness = eval_correctness_gsm8k
elif dataset_name in {"countdown", "countdown_kd"}:
reward_fns = [("correctness_reward_func", countdown_correctness_function)]
eval_correctness = eval_correctness_countdown
elif dataset_name in {"medreason", "medreason_kd"}:
reward_fns = [("correctness_reward_func", medical_correctness_reward_func)]
eval_correctness = eval_correctness_medical
elif dataset_name in {"science", "science_kd"}:
reward_fns = [("correctness_reward_func", scienceqa_correctness_reward_func)]
eval_correctness = eval_correctness_scienceqa
elif dataset_name in {"mmlu", "mmlu_kd"}:
reward_fns = [("correctness_reward_func", mmlu_correctness_reward_func)]
eval_correctness = eval_correctness_mmlu
elif dataset_name in {"medical", "medical_kd"}:
reward_fns = [("correctness_reward_func", medical_correctness_reward_func)]
eval_correctness = eval_correctness_medical
else:
raise ValueError(f"Dataset {dataset_name} not supported")
return reward_fns, eval_correctness
def _cfg_bool(cfg_eval: DictConfig, key: str, default: bool) -> bool:
value = getattr(cfg_eval, key, None)
return default if value is None else bool(value)
def _zero_scores_like(completions):
return [[[0]] * len(c) for c in completions]
@hydra.main(config_path="configs", config_name="config_eval", version_base="1.3")
def main(cfg: DictConfig):
print("Evaluation configuration:\n", OmegaConf.to_yaml(cfg))
os.makedirs(cfg.model.name, exist_ok=True)
config_save_path = os.path.join(cfg.model.name, "evaluation_config.yaml")
with open(config_save_path, "w") as f:
OmegaConf.save(config=cfg, f=f)
print(f"Configuration saved to: {config_save_path}")
set_seed(cfg.seed)
eval_mode = canonical_eval_mode(getattr(cfg.eval, "mode", MODE_GENERATE))
use_pregenerated = eval_mode_uses_pregenerated(eval_mode)
compute_policy_log_probs = _cfg_bool(
cfg.eval, "compute_policy_log_probs", use_pregenerated
)
default_compute_reward = cfg.airl and eval_mode in {
MODE_GENERATE,
MODE_AIME,
MODE_PREGENERATED_POLICY_AND_REWARD,
}
compute_reward_model_scores = _cfg_bool(
cfg.eval,
"compute_reward_model_scores",
default_compute_reward,
)
print(
"Evaluation mode: "
f"{eval_mode} | pregenerated={use_pregenerated} | "
f"policy_log_probs={compute_policy_log_probs} | "
f"reward_model_scores={compute_reward_model_scores}"
)
reward_fns, eval_correctness = _resolve_eval_functions(cfg.dataset.name)
if cfg.eval.report_to == "wandb":
wandb_config = OmegaConf.to_container(cfg, resolve=True)
wandb.init(
project=cfg.wandb.project,
entity=cfg.wandb.entity,
config=wandb_config,
name=f"eval_{cfg.wandb.run_name}-cp{cfg.model.checkpoint}",
)
no_system = getattr(cfg.dataset, "no_system", False)
dataset = get_dataset(
cfg.dataset.name, split=cfg.dataset.split, ratio=1, no_system=no_system
)
loader = DataLoader(
dataset,
batch_size=cfg.eval.per_device_eval_batch_size,
shuffle=False,
collate_fn=lambda examples: examples,
)
model = None
tokenizer = None
policy_tokenizer = None
reward_model = None
reward_tokenizer = None
lora_req = None
if cfg.airl:
model, reward_model, tokenizer, reward_tokenizer = irl_load_model_and_tokenizer(
cfg, pretrained=True
)
model.eval()
policy_tokenizer = tokenizer
if compute_reward_model_scores:
reward_model.eval()
else:
del reward_model
reward_model = None
torch.cuda.empty_cache()
else:
model, tokenizer = load_model_and_tokenizer(cfg)
model.eval()
policy_tokenizer = tokenizer
if compute_reward_model_scores and reward_model is None:
raise ValueError(
"Reward-model scoring requested, but no reward model is loaded. "
"Set `airl=true` or disable `eval.compute_reward_model_scores`."
)
# Generation parameters
n = cfg.sampling.n_samples
sampling_params = SamplingParams(
n=n,
seed=cfg.seed,
max_tokens=cfg.model.max_completion_length,
temperature=cfg.sampling.temperature,
top_p=cfg.sampling.top_p,
)
guidance_method = getattr(cfg, "guidance", {}).get("method", "none")
if not use_pregenerated:
if hasattr(model, "load_lora"):
lora_req = model.load_lora(cfg.model.name, load_tensors=True)
if guidance_method == "topk" and cfg.airl:
print(
f"--- ACTIVATING REWARD-AUGMENTED DECODING (Top-K={cfg.guidance.k}) ---"
)
rw_processor = TopKRewardLogitsProcessor(
reward_model=reward_model,
reward_tokenizer=reward_tokenizer,
alpha=getattr(cfg.guidance, "alpha", 1.0),
k=getattr(cfg.guidance, "k", 5),
device=next(reward_model.parameters()).device,
)
sampling_params.logits_processors = [rw_processor]
elif guidance_method == "chunk" and cfg.airl:
print("--- ACTIVATING CHUNK-LEVEL GUIDANCE ---")
else:
print("--- STANDARD GENERATION (No Active Guidance) ---")
pregenerated_df = None
if use_pregenerated:
explicit_jsonl = getattr(cfg.eval, "pregenerated_jsonl_path", None)
source_dir = getattr(cfg.eval, "pregenerated_source_dir", None)
candidates = getattr(cfg.eval, "pregenerated_candidates", None)
candidates = list(candidates) if candidates is not None else None
pregenerated_jsonl_path = resolve_pregenerated_jsonl_path(
mode=eval_mode,
model_name=cfg.model.name,
policy_name=getattr(cfg.model, "policy_name", None),
explicit_path=explicit_jsonl,
source_dir_override=source_dir,
candidate_filenames=candidates,
)
print(f"Loading pregenerated completions from: {pregenerated_jsonl_path}")
pregenerated_df = pd.read_json(pregenerated_jsonl_path, lines=True)
# Metrics storage
all_correct_flags = []
all_reward_scores = []
sums = {name: 0.0 for name, _ in reward_fns}
sum_sqs = {name: 0.0 for name, _ in reward_fns}
count = 0
all_results = []
bs = cfg.eval.per_device_eval_batch_size
batch_counter = 0
for batch in tqdm(loader):
prompts = [b["prompt"] for b in batch]
answers = [b["answer"] for b in batch]
if use_pregenerated:
start = batch_counter * bs * n
end = start + len(batch) * n
sub_df = pregenerated_df.iloc[start:end].copy().reset_index(drop=True)
expected_rows = len(batch) * n
if len(sub_df) != expected_rows:
raise ValueError(
f"Pregenerated jsonl has {len(sub_df)} rows for batch {batch_counter}, "
f"expected {expected_rows}. start={start}, end={end}."
)
if len(sub_df) > 0:
assert (
sub_df.iloc[0]["prompt"][1]["content"]
== batch[0]["prompt"][1]["content"]
), "DataLoader and JSONL are misaligned!"
assert (
sub_df.iloc[-1]["prompt"][1]["content"]
== batch[-1]["prompt"][1]["content"]
), "DataLoader and JSONL are misaligned!"
gens = sub_df["generation"].tolist()
completions = [gens[i : i + n] for i in range(0, len(gens), n)]
batch_counter += 1
else:
prompts_text = [
maybe_apply_chat_template({"prompt": p}, tokenizer)["prompt"]
for p in prompts
]
if guidance_method == "chunk" and cfg.airl:
generated_texts = generate_with_chunk_guidance(
model=model,
reward_model=reward_model,
reward_tokenizer=reward_tokenizer,
prompts_text=prompts_text,
sampling_params=sampling_params,
step_size=getattr(cfg.guidance, "step_size", 10),
n_candidates=getattr(cfg.guidance, "n_candidates", 4),
)
completions = [
[{"content": t[len(p) :]}]
for p, t in zip(prompts_text, generated_texts)
]
else:
outputs = model.fast_generate(
prompts_text,
sampling_params=sampling_params,
use_tqdm=False,