-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_experiments.py
More file actions
1507 lines (1254 loc) · 65 KB
/
Copy pathrun_experiments.py
File metadata and controls
1507 lines (1254 loc) · 65 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
import os
import argparse
import json
import logging
import random
import glob
from datetime import datetime
from .optimization import Architect, Optimizer
from .data_loader import get_loader #
from .evaluation import get_evaluator, BaseEvaluator #
from .llm_apis import get_llm #
from .config import DATASET_CONFIG, OPTIMIZATION_PARAMS, RESULTS_DIR, DATA_PATHS, DATA_SPLIT_CONFIG #
from .baselines import (
run_apsf_nostructure,
run_apsf_nofactor,
run_apsf_nodap,
run_apsf_randselect,
run_apsf_feedback,
run_apsf_smallarchitect,
run_apsf_thompson,
run_apsf_roundrobin,
run_apsf_greedy,
run_apsf_prompt_transfer,
run_apsf_worker_llm_comparison,
run_apsf_vs_manual_fewshot,
run_apsf_stability_test
)
from typing import List, Dict, Any, Optional
from .checkpoint_manager import CheckpointManager, BBHAllCheckpointManager
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('experiment_logs.log'),
logging.StreamHandler()
]
)
def set_random_seed(seed=None):
"""Set random seed for reproducibility"""
if seed is None:
seed = DATA_SPLIT_CONFIG['random_seed']
random.seed(seed)
import numpy as np
np.random.seed(seed)
# Set torch seed if available
try:
import torch
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
except ImportError:
pass
logging.info(f"Random seed set to: {seed}")
def run_apsf_pipeline(
task_desc: str,
eval_data: List[Dict[str, Any]],
test_data: List[Dict[str, Any]],
evaluator: BaseEvaluator,
dataset_config: Dict[str, Any],
enable_feedback: bool = False,
step: Optional[int] = None,
initial_prompt: Optional[str] = None
) -> Dict[str, Any]:
"""Run the complete aPSF pipeline
Args:
task_desc: Task description
eval_data: Validation data
test_data: Test data
evaluator: Evaluator instance
dataset_config: Dataset configuration
enable_feedback: Whether to enable feedback mechanism
step: Number of optimization steps
initial_prompt: Initial prompt (optional, e.g., "Let's think step by step")
If provided, optimization starts from this prompt instead of scratch
Returns:
Dictionary containing optimization results
"""
# Ensure random seed consistency for reproducible data splits
set_random_seed()
logging.info(" Starting aPSF pipeline...")
# Log initial prompt if provided
if initial_prompt:
logging.info(f" Using initial prompt: '{initial_prompt}'")
logging.info(" aPSF will perform factor discovery and optimization on this basis")
# Merge optimization params into dataset config
merged_config = dataset_config.copy()
merged_config.update(OPTIMIZATION_PARAMS)
# Override config if step parameter is specified
if step is not None:
merged_config['total_optimization_steps'] = step
logging.info(f"Optimization steps set to: {step}")
# Use Architect to discover prompt structure
architect = Architect()
# Construct example data for structure discovery - using dataset-specific format (with solution steps)
dataset_name = dataset_config.get('dataset', '')
example_data = _construct_universal_discovery_examples(dataset_name, eval_data)
# Pass initial_prompt parameter
prompt_struct = architect.discover_structure(task_desc, example_data, initial_prompt=initial_prompt)
# Use standard optimizer, pass feedback parameter
optimizer = Optimizer(
prompt_struct, eval_data, evaluator, merged_config,
enable_feedback=enable_feedback
)
logging.info(" Using Standard aPSF Optimizer")
# Run optimization steps iteratively
total_steps = merged_config.get("total_optimization_steps", 10)
logging.info(f" Starting optimization for {total_steps} steps...")
for step_num in range(total_steps):
logging.info(f" Optimization Step {step_num + 1}/{total_steps}")
optimizer.step()
# Check for early stopping condition
if optimizer.current_optimization_step >= total_steps:
break
# Reflection optimization phase
enable_reflection = merged_config.get("enable_reflection", True) # Reflection enabled by default
if enable_reflection:
logging.info(" Starting reflection optimization phase...")
reflection_improved = optimizer.reflection_optimization(eval_data)
if reflection_improved:
logging.info(" Reflection optimization succeeded, prompt updated")
else:
logging.info(" Reflection optimization did not improve performance, keeping original prompt")
else:
logging.info(" Skipping reflection optimization phase")
# Evaluate on test set
test_score = optimizer.evaluate_on_test_set(test_data)
# Print optimization summary statistics (including regression rate)
optimizer.print_optimization_summary()
# Also call print_final_summary if available
if hasattr(optimizer, 'print_final_summary'):
optimizer.print_final_summary(test_score)
# Save factor analysis report
dataset_name = dataset_config.get('dataset', 'unknown')
factor_analysis_path = optimizer.save_factor_analysis(dataset_name)
logging.info(f" Factor analysis report saved to: {factor_analysis_path}")
# Serialize PromptStructure object to dict
best_structure = optimizer.get_best_structure()
best_structure_dict = None
if best_structure:
try:
best_structure_dict = best_structure.to_dict()
except Exception as e:
logging.warning(f" Cannot serialize PromptStructure: {e}")
best_structure_dict = {
"task_description": getattr(best_structure, 'task_description', ''),
"fusion_prompt": getattr(best_structure, 'fusion_prompt', ''),
"factors": getattr(best_structure, 'factors', {}),
"serialization_error": str(e)
}
# Get regression rate statistics
regression_rate = optimizer.get_regression_rate()
regression_stats_summary = {
'regression_rate': regression_rate,
'total_accepted_updates': optimizer.regression_stats['total_accepted_updates'],
'total_regressions': optimizer.regression_stats['total_regressions'],
'per_factor_regressions': optimizer.regression_stats['per_factor_regressions']
}
# Final output (ensure printed at the end)
print(f"\n{'='*80}", flush=True)
print(f" aPSF complete, test set final score: {test_score:.4f}", flush=True)
print(f" Best prompt obtained at optimization step {optimizer.global_best_step}", flush=True)
# Token statistics
worker_stats = optimizer.worker_llm.get_token_stats()
architect_stats = optimizer.architect_llm.get_token_stats()
total_tokens = worker_stats['total_tokens'] + architect_stats['total_tokens']
print(f" TOKENS AT BEST STEP: {optimizer.global_best_tokens:,} tokens")
print(f" TOTAL TOKENS: {total_tokens:,} tokens")
print(f"{'='*80}\n", flush=True)
return {
"final_score": test_score,
"optimized_prompt": optimizer.get_optimized_prompt(),
"best_structure": best_structure_dict, # Returns serializable dict
"factor_analysis_path": factor_analysis_path, # Factor analysis file path
"regression_stats": regression_stats_summary # Regression rate statistics
}
def save_results(method_name: str, dataset_name: str, results_data: dict):
"""Unified function for saving experiment results to file."""
if not os.path.exists(RESULTS_DIR):
os.makedirs(RESULTS_DIR)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{method_name}_{dataset_name}_{timestamp}.json"
filepath = os.path.join(RESULTS_DIR, filename)
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(results_data, f, indent=2, ensure_ascii=False)
logging.info(f" Results saved to: {filepath}")
def _construct_universal_discovery_examples(dataset_name: str, eval_data: List[Dict[str, Any]]) -> str:
"""Construct structure discovery examples - dataset-agnostic version"""
if not eval_data:
return "No examples available for structure discovery."
# Randomly select 3-5 samples for structure discovery
num_samples = min(5, len(eval_data))
selected_samples = random.sample(eval_data, num_samples)
# Dataset-agnostic mode: use generic format only, let algorithm discover patterns
if not dataset_name: # Empty string indicates dataset-agnostic mode
return _construct_generic_examples(selected_samples)
# Compatibility: keep original logic for non-blind scenarios
if "gsm" in dataset_name.lower():
return _construct_gsm_examples(selected_samples)
elif "bbh" in dataset_name.lower() or "colored_objects" in dataset_name.lower():
return _construct_bbh_examples(selected_samples, dataset_name)
elif "aqua" in dataset_name.lower():
return _construct_aqua_examples(selected_samples)
elif "multiarith" in dataset_name.lower():
return _construct_multiarith_examples(selected_samples)
elif "humaneval" in dataset_name.lower():
return _construct_humaneval_examples(selected_samples)
elif "mmlu" in dataset_name.lower():
return _construct_mmlu_examples(selected_samples)
elif "squad" in dataset_name.lower():
return _construct_squad_examples(selected_samples)
else:
# Generic format
return _construct_generic_examples(selected_samples)
def _construct_generic_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct generic format examples - includes input, reasoning process, output"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
# Try different key names
input_text = sample.get('input', sample.get('prompt', sample.get('question', '')))
output_text = sample.get('target', sample.get('answer', sample.get('output', '')))
reasoning = sample.get('solution', sample.get('rationale', sample.get('explanation', '')))
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {input_text}")
if reasoning:
example_parts.append(f"Reasoning: {reasoning}")
example_parts.append(f"Expected Output: {output_text}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_gsm_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct GSM8K-specific example format - includes question, solution steps, answer"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
question = sample.get('question', sample.get('input', ''))
answer = sample.get('answer', sample.get('target', ''))
solution = sample.get('solution', '') # Get solution steps
# Build example: question + solution process + answer
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {question}")
if solution:
# Clean solution (remove possible b' ' wrapper)
if isinstance(solution, str) and solution.startswith("b'"):
solution = solution[2:-1].replace('\\n', '\n')
example_parts.append(f"Solution Steps: {solution}")
example_parts.append(f"Expected Output: {answer}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_bbh_examples(selected_samples: List[Dict[str, Any]], dataset_name: str) -> str:
"""Construct BBH-specific example format - includes question, reasoning process, answer"""
# Extract task type
task_type = "BBH Reasoning"
if "colored_objects" in dataset_name:
task_type = "Colored Objects Reasoning"
elif "web_of_lies" in dataset_name:
task_type = "Web of Lies"
elif "movie_recommendation" in dataset_name:
task_type = "Movie Recommendation"
example_strings = []
for i, sample in enumerate(selected_samples, 1):
input_text = sample.get('input', '')
output_text = sample.get('target', '')
reasoning = sample.get('rationale', sample.get('explanation', ''))
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {input_text}")
if reasoning:
example_parts.append(f"Reasoning: {reasoning}")
example_parts.append(f"Expected Output: {output_text}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_aqua_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct AQuA-specific example format - includes question, solution process, answer"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
question = sample.get('input', sample.get('question', ''))
answer = sample.get('correct', sample.get('target', ''))
rationale = sample.get('rationale', sample.get('solution', ''))
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {question}")
if rationale:
example_parts.append(f"Solution Steps: {rationale}")
example_parts.append(f"Expected Output: {answer}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_multiarith_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct MultiArith-specific example format - includes question, solution steps, answer"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
question = sample.get('question', sample.get('input', ''))
answer = sample.get('answer', sample.get('target', ''))
solution = sample.get('solution', sample.get('rationale', ''))
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {question}")
if solution:
example_parts.append(f"Solution Steps: {solution}")
example_parts.append(f"Expected Output: {answer}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_humaneval_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct HumanEval-specific example format - includes problem, solution"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
prompt = sample.get('prompt', '')
solution = sample.get('canonical_solution', sample.get('target', ''))
docstring = sample.get('docstring', '')
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {prompt}")
if docstring:
example_parts.append(f"Description: {docstring}")
example_parts.append(f"Expected Output: {solution}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_mmlu_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct MMLU-specific example format - includes question, reasoning, answer"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
question = sample.get('input', sample.get('question', ''))
answer = sample.get('target', sample.get('answer', ''))
explanation = sample.get('explanation', sample.get('rationale', ''))
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Input: {question}")
if explanation:
example_parts.append(f"Reasoning: {explanation}")
example_parts.append(f"Expected Output: {answer}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def _construct_squad_examples(selected_samples: List[Dict[str, Any]]) -> str:
"""Construct SQuAD 2.0-specific example format - includes passage context, question, answer"""
example_strings = []
for i, sample in enumerate(selected_samples, 1):
context = sample.get('context', '')
question = sample.get('question', sample.get('input', ''))
answer = sample.get('answer', sample.get('target', ''))
is_impossible = sample.get('is_impossible', False)
# Build example
example_parts = [f"--- Example {i} ---"]
example_parts.append(f"Passage: {context[:300]}...") # Show first 300 chars of context
example_parts.append(f"Question: {question}")
if is_impossible:
example_parts.append(f"Expected Output: The answer cannot be found in the passage.")
else:
example_parts.append(f"Expected Output: {answer}")
example_strings.append("\n".join(example_parts))
return "\n\n".join(example_strings)
def run_baseline_method(method_name: str, task_desc: str, val_data: List[Dict],
test_data: List[Dict], evaluator, config: Dict, step: Optional[int] = None,
resume: bool = False) -> Dict[str, Any]:
"""Run baseline method - display full process for all samples"""
from .evaluation.unified_scoring import evaluate_with_unified_scoring, UnifiedScorer
# Ensure random seed consistency for reproducible data splits
set_random_seed()
worker_llm = get_llm("worker")
architect_llm = get_llm("architect")
print(f" Running baseline method: {method_name}")
print(f" Data size: validation {len(val_data)}, test {len(test_data)}")
# Unified display format for validation and test evaluation
def detailed_evaluation_with_display(data, data_type, prompt_template, method_display_name, do_scoring=False):
"""Generate responses, optionally with scoring"""
predictions = []
correct_count = 0
print(f"\n Starting {method_display_name} {data_type} detailed evaluation", flush=True)
print(f" Sample count: {len(data)}", flush=True)
for i, item in enumerate(data):
input_key = 'prompt' if 'prompt' in item else ('input' if 'input' in item else 'question')
question = item.get(input_key, '')
# Format prompt
if '{input}' in prompt_template:
formatted_prompt = prompt_template.format(input=question)
else:
# Combine instruction with question directly for pure instructions
formatted_prompt = f"{prompt_template}\n\n{question}"
# Display question and current template
print(f"\n{'='*80}", flush=True)
print(f" {method_display_name} {data_type} sample {i+1}/{len(data)}", flush=True)
print(f"{'='*80}", flush=True)
print(f" Question:", flush=True)
print(f" {question}", flush=True)
print(f"\n Current {method_display_name} template:", flush=True)
print(f" {prompt_template}", flush=True)
print(f"\n {method_display_name} response:", flush=True)
# Generate response
prediction = worker_llm.generate(formatted_prompt)
predictions.append(prediction)
print(f" {prediction}", flush=True)
# Score if needed (only for test set)
if do_scoring:
from .evaluation.unified_scoring import UnifiedScorer
scorer = UnifiedScorer(worker_llm, f"bbh_{data_type}")
extracted_answer, target_answer, is_correct = scorer.extract_and_score(
prediction, item, evaluator
)
print(f"\n Extracted answer: '{extracted_answer}'", flush=True)
print(f" Target answer: '{target_answer}'", flush=True)
verdict = " Correct" if is_correct else "Incorrect"
print(f"Answer match: {verdict}", flush=True)
if is_correct:
correct_count += 1
# Return accuracy if scoring was performed
if do_scoring:
accuracy = correct_count / len(data) if len(data) > 0 else 0.0
# Check for MMLU task and output category results
if hasattr(evaluator, 'subject_mapping'):
print("\n" + "="*80, flush=True)
print(" Using MMLU evaluator to generate subject category results", flush=True)
print("="*80, flush=True)
mmlu_results = evaluator.evaluate(predictions, data)
accuracy = mmlu_results.get("Average", accuracy)
print(f"\n MMLU average accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)", flush=True)
else:
print(f"\n {data_type} final results:", flush=True)
print(f" Correct: {correct_count}/{len(data)}", flush=True)
print(f" Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)", flush=True)
return accuracy, predictions
else:
return predictions
# def detailed_evaluation_with_display(data, data_type, prompt_template, method_display_name):
# """Detailed evaluation with full sample display"""
# predictions = []
# correct_count = 0
#
# print(f"\n Starting {method_display_name} {data_type} detailed evaluation")
# print(f" Sample count: {len(data)}")
#
# for i, item in enumerate(data):
# input_key = 'prompt' if 'prompt' in item else ('input' if 'input' in item else 'question')
# question = item.get(input_key, '')
#
# if '{input}' in prompt_template:
# formatted_prompt = prompt_template.format(input=question)
# else:
# formatted_prompt = f"{prompt_template}\n\n{question}"
#
# print(f"\n{'='*80}")
# print(f"{method_display_name} {data_type} sample {i+1}/{len(data)}")
# print(f"{'='*80}")
# print(f" Question:")
# print(f" {question}")
# print(f"\n Current {method_display_name} template:")
# print(f" {prompt_template}")
# print(f"\n {method_display_name} response:")
#
# prediction = worker_llm.generate(formatted_prompt)
# predictions.append(prediction)
#
# print(f" {prediction}")
#
# scorer = UnifiedScorer(worker_llm, f"bbh_{task_desc.split()[3] if 'task' in task_desc else 'unknown'}")
# extracted_answer, target_answer, is_correct = scorer.extract_and_score(prediction, item, evaluator)
#
# print(f"\n Extracted answer: '{extracted_answer}'")
# print(f" Target answer: '{target_answer}'")
# verdict = "Correct" if is_correct else "Wrong"
# print(f" Answer match: {verdict}")
#
# if is_correct:
# correct_count += 1
#
# print(f"{'='*80}")
#
# eval_results = evaluate_with_unified_scoring(
# predictions=predictions,
# eval_data=data,
# evaluator=evaluator,
# llm=worker_llm,
# verbose=False,
# dataset_name=f"bbh_{data_type}_{task_desc.split()[3] if 'task' in task_desc else 'unknown'}"
# )
# accuracy = correct_count / len(data) if len(data) > 0 else 0.0
# return accuracy, predictions
# Process based on method
dataset_name = config.get('dataset', 'unknown')
if dataset_name == 'unknown':
logging.warning(
"Baseline config missing 'dataset'; OPRO checkpoints may collide and prompt optimization may appear stuck. "
"Pass a config dict with config['dataset']=<dataset_id>."
)
if method_name == 'opro':
print(" Starting OPRO optimization...")
# Run OPRO first to get optimized prompt (without showing detailed process)
print(" OPRO internal optimization in progress...")
opro_results = run_opro(
task_desc=task_desc,
eval_data=val_data,
test_data=None, # Don't pass test_data yet to avoid duplicate evaluation
evaluator=evaluator,
worker_llm=worker_llm,
architect_llm=architect_llm,
use_llm_extraction=True,
step=step, # Pass step parameter
dataset=dataset_name, # Pass dataset name
resume=resume
)
best_prompt = opro_results["prompt"]
best_step = opro_results.get("best_step", "unknown")
print(f" OPRO optimized best prompt: {best_prompt}")
print(f" Best prompt obtained at step {best_step}", flush=True)
# Print OPRO regression rate statistics
if 'regression_stats' in opro_results:
regression_stats = opro_results['regression_stats']
print(f"\n{'='*80}", flush=True)
print(f" OPRO Regression Rate Statistics", flush=True)
print(f"{'='*80}", flush=True)
print(f" Total accepted updates: {regression_stats['total_accepted_updates']}", flush=True)
print(f" Total regressions: {regression_stats['total_regressions']}", flush=True)
print(f" Regression rate: {regression_stats['regression_rate']:.2%} "
f"({regression_stats['total_regressions']}/{regression_stats['total_accepted_updates']})", flush=True)
print(f"{'='*80}\n", flush=True)
# Validation set: generate responses and show answer matching
val_accuracy, val_predictions = detailed_evaluation_with_display(val_data, "validation", best_prompt, "OPRO", do_scoring=True)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", best_prompt, "OPRO", do_scoring=True)
print(f"\n{'='*80}", flush=True)
print(f" OPRO complete, test set final score: {test_score:.4f}", flush=True)
print(f" Best prompt obtained at optimization step {best_step}", flush=True)
# Token statistics
worker_stats = worker_llm.get_token_stats()
architect_stats = architect_llm.get_token_stats()
total_tokens = worker_stats['total_tokens'] + architect_stats['total_tokens']
best_step_tokens = opro_results.get("best_step_tokens", 0)
print(f" TOKENS AT BEST STEP: {best_step_tokens:,} tokens")
print(f" TOTAL TOKENS: {total_tokens:,} tokens")
print(f"{'='*80}\n", flush=True)
return {
"final_score": test_score,
"optimized_prompt": best_prompt,
"test_predictions": test_predictions,
"regression_stats": opro_results.get('regression_stats', {}), # Add regression stats
"status": "success"
}
elif method_name == 'protegi':
print(" Starting ProTeGi optimization...")
# Run ProTeGi first to get optimized prompt
print(" ProTeGi internal optimization in progress...")
protegi_results = run_protegi(task_desc, val_data, None, evaluator, worker_llm, architect_llm, step=step)
best_prompt = protegi_results["prompt"]
best_step = protegi_results.get("best_achieved_at_step", "unknown")
print(f" ProTeGi optimized best prompt: {best_prompt}")
print(f" Best prompt obtained at step {best_step}", flush=True)
# Validation set: generate responses and show answer matching
val_accuracy, val_predictions = detailed_evaluation_with_display(val_data, "validation", best_prompt, "ProTeGi", do_scoring=True)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", best_prompt, "ProTeGi", do_scoring=True)
print(f"\n{'='*80}", flush=True)
print(f" ProTeGi complete, test set final score: {test_score:.4f}", flush=True)
print(f" Best prompt obtained at optimization step {best_step}", flush=True)
# Token statistics
worker_stats = worker_llm.get_token_stats()
architect_stats = architect_llm.get_token_stats()
total_tokens = worker_stats['total_tokens'] + architect_stats['total_tokens']
best_step_tokens = protegi_results.get("best_step_tokens", 0)
print(f" TOKENS AT BEST STEP: {best_step_tokens:,} tokens")
print(f" TOTAL TOKENS: {total_tokens:,} tokens")
print(f"{'='*80}\n", flush=True)
return {
"final_score": test_score,
"optimized_prompt": best_prompt,
"test_predictions": test_predictions,
"status": "success"
}
elif method_name == 'ape':
print(" Starting APE optimization...")
# Run APE first to get optimized prompt
print(" APE internal optimization in progress...")
ape_results = run_ape(task_desc, val_data, evaluator, worker_llm, architect_llm, step=step)
best_prompt = ape_results["prompt"]
best_step = ape_results.get("best_achieved_at_iteration", "unknown")
print(f" APE optimized best prompt: {best_prompt}")
print(f" Best prompt obtained at iteration {best_step}", flush=True)
# Validation set: generate responses and show answer matching
val_accuracy, val_predictions = detailed_evaluation_with_display(val_data, "validation", best_prompt, "APE", do_scoring=True)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", best_prompt, "APE", do_scoring=True)
print(f"\n{'='*80}", flush=True)
print(f" APE complete, test set final score: {test_score:.4f}", flush=True)
print(f" Best prompt obtained at optimization iteration {best_step}", flush=True)
# Token statistics
worker_stats = worker_llm.get_token_stats()
architect_stats = architect_llm.get_token_stats()
total_tokens = worker_stats['total_tokens'] + architect_stats['total_tokens']
best_step_tokens = ape_results.get("best_step_tokens", 0)
print(f" TOKENS AT BEST STEP: {best_step_tokens:,} tokens")
print(f" TOTAL TOKENS: {total_tokens:,} tokens")
print(f"{'='*80}\n", flush=True)
return {
"final_score": test_score,
"optimized_prompt": best_prompt,
"test_predictions": test_predictions,
"status": "success"
}
elif method_name == 'grips':
print(" Starting GRIPS optimization...")
print(" GRIPS internal optimization in progress...")
grips_results = run_grips(task_desc, val_data, evaluator, worker_llm, architect_llm, step=step)
best_prompt = grips_results["prompt"]
best_step = grips_results.get("best_achieved_at_iteration", "unknown")
print(f" GRIPS optimized best prompt: {best_prompt}")
print(f" Best prompt obtained at step {best_step}", flush=True)
# Validation set: generate responses and show answer matching
val_accuracy, val_predictions = detailed_evaluation_with_display(val_data, "validation", best_prompt, "GRIPS", do_scoring=True)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", best_prompt, "GRIPS", do_scoring=True)
print(f"\n{'='*80}", flush=True)
print(f" GRIPS complete, test set final score: {test_score:.4f}", flush=True)
print(f" Best prompt obtained at optimization step {best_step}", flush=True)
# Token statistics
worker_stats = worker_llm.get_token_stats()
architect_stats = architect_llm.get_token_stats()
total_tokens = worker_stats['total_tokens'] + architect_stats['total_tokens']
best_step_tokens = grips_results.get("best_step_tokens", 0)
print(f" TOKENS AT BEST STEP: {best_step_tokens:,} tokens")
print(f" TOTAL TOKENS: {total_tokens:,} tokens")
print(f"{'='*80}\n", flush=True)
return {
"final_score": test_score,
"optimized_prompt": best_prompt,
"test_predictions": test_predictions,
"status": "success"
}
elif method_name == 'empty_cot':
print(" Starting Empty CoT evaluation...")
print(" Empty CoT internal optimization in progress...")
empty_cot_results = run_empty_cot(task_desc, val_data, evaluator, worker_llm, architect_llm)
best_prompt = empty_cot_results["prompt"]
print(f" Empty CoT best prompt: {best_prompt}")
# Validation set: generate responses only, no scoring
#val_predictions = detailed_evaluation_with_display(val_data, "validation", best_prompt, "Empty CoT", do_scoring=False)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", best_prompt, "Empty CoT", do_scoring=True)
print(f" Empty CoT complete, test set final score: {test_score:.4f}")
return {
"final_score": test_score,
"optimized_prompt": best_prompt,
"test_predictions": test_predictions,
"status": "success"
}
elif method_name == 'dspy':
print(" Starting DSPy optimization...")
print(" DSPy internal optimization in progress...")
optimization_steps = step if step is not None else OPTIMIZATION_PARAMS.get("total_optimization_steps", 10)
dspy_results = run_dspy(task_desc, val_data, evaluator, worker_llm, architect_llm=architect_llm, optimization_steps=optimization_steps)
best_prompt = dspy_results.get("prompt", "Let's think step by step.")
best_step = dspy_results.get("best_achieved_at_step", "unknown")
print(f" DSPy optimized best prompt: {best_prompt}")
print(f" Best prompt obtained at step {best_step}", flush=True)
# Validation set: generate responses and show answer matching
val_accuracy, val_predictions = detailed_evaluation_with_display(val_data, "validation", best_prompt, "DSPy", do_scoring=True)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", best_prompt, "DSPy", do_scoring=True)
print(f"\n{'='*80}", flush=True)
print(f" DSPy complete, test set final score: {test_score:.4f}", flush=True)
print(f" Best prompt obtained at optimization step {best_step}", flush=True)
print(f"{'='*80}\n", flush=True)
return {
"final_score": test_score,
"optimized_prompt": best_prompt,
"test_predictions": test_predictions,
"status": "success"
}
elif method_name == 'qwen3_direct':
print(" Starting Qwen3 direct inference...")
# Qwen3 direct inference uses fixed prompt
direct_prompt = "Please analyze the question and provide your answer."
print(f" Qwen3 direct inference prompt: {direct_prompt}")
# Validation set: generate responses and show answer matching
val_accuracy, val_predictions = detailed_evaluation_with_display(val_data, "validation", direct_prompt, "Qwen3Direct", do_scoring=True)
# Test set: generate responses with scoring, output final score when done
test_score, test_predictions = detailed_evaluation_with_display(test_data, "test", direct_prompt, "Qwen3Direct", do_scoring=True)
print(f" Qwen3 direct inference complete, test set final score: {test_score:.4f}")
return {
"final_score": test_score,
"optimized_prompt": direct_prompt,
"test_predictions": test_predictions,
"status": "success"
}
# ============ aPSF Ablation Experiments ============
elif method_name == 'apsf_nostructure':
from .baselines.apsf_ablation import run_apsf_nostructure
print(" Starting aPSF-NoStructure ablation experiment...")
return run_apsf_nostructure(task_desc, val_data, test_data, evaluator, config, step)
elif method_name == 'apsf_notone':
from .baselines.apsf_ablation import run_apsf_nofactor
print(" Starting aPSF-NoTone ablation experiment...")
return run_apsf_nofactor(task_desc, val_data, test_data, evaluator, config, "tone", step)
elif method_name == 'apsf_noformat':
from .baselines.apsf_ablation import run_apsf_nofactor
print(" Starting aPSF-NoFormat ablation experiment...")
return run_apsf_nofactor(task_desc, val_data, test_data, evaluator, config, "format", step)
elif method_name == 'apsf_noperspective':
from .baselines.apsf_ablation import run_apsf_nofactor
print(" Starting aPSF-NoPerspective ablation experiment...")
return run_apsf_nofactor(task_desc, val_data, test_data, evaluator, config, "perspective", step)
elif method_name == 'apsf_nodap':
from .baselines.apsf_ablation import run_apsf_nodap
print(" Starting aPSF-NoDAP ablation experiment...")
return run_apsf_nodap(task_desc, val_data, test_data, evaluator, config, step)
elif method_name == 'apsf_randselect':
from .baselines.apsf_ablation import run_apsf_randselect
print(" Starting aPSF-RandSelect ablation experiment...")
return run_apsf_randselect(task_desc, val_data, test_data, evaluator, config, step)
elif method_name == 'apsf_feedback':
from .baselines.apsf_ablation import run_apsf_feedback
print(" Starting aPSF-Feedback ablation experiment...")
return run_apsf_feedback(task_desc, val_data, test_data, evaluator, config, step)
elif method_name == 'apsf_smallarchitect':
from .baselines.apsf_ablation import run_apsf_smallarchitect
print(" Starting aPSF-SmallArchitect ablation experiment...")
return run_apsf_smallarchitect(task_desc, val_data, test_data, evaluator, config, step)
# ============ Multi-Armed Bandit Algorithm Comparison ============
elif method_name == 'apsf_thompson':
from .baselines.apsf_ablation import run_apsf_thompson
print(" Starting aPSF-Thompson Sampling comparison...")
return run_apsf_thompson(task_desc, val_data, test_data, evaluator, config, step)
elif method_name == 'apsf_roundrobin':
from .baselines.apsf_ablation import run_apsf_roundrobin
print(" Starting aPSF-Round-robin comparison...")
return run_apsf_roundrobin(task_desc, val_data, test_data, evaluator, config, step)
elif method_name == 'apsf_greedy':
from .baselines.apsf_ablation import run_apsf_greedy
print(" Starting aPSF-Greedy-best comparison...")
return run_apsf_greedy(task_desc, val_data, test_data, evaluator, config, step)
# ============ aPSF Comparative Experiments ============
elif method_name == 'apsf_transfer':
from .baselines.apsf_comparative import run_apsf_prompt_transfer
print(" Starting aPSF-Transfer comparison experiment...")
# Simplified handling for source and target tasks
source_task = "mathematical_reasoning"
target_task = task_desc
return run_apsf_prompt_transfer(
source_task, target_task, val_data[:5], val_data[5:], test_data, evaluator, config
)
elif method_name == 'apsf_worker_llm':
from .baselines.apsf_comparative import run_apsf_worker_llm_comparison
print(" Starting aPSF-WorkerLLM comparison experiment...")
return run_apsf_worker_llm_comparison(task_desc, val_data, test_data, evaluator, config)
elif method_name == 'apsf_vs_manual':
from .baselines.apsf_comparative import run_apsf_vs_manual_fewshot
print(" Starting aPSF vs Manual Few-shot comparison experiment...")
return run_apsf_vs_manual_fewshot(task_desc, val_data, test_data, evaluator, config)
elif method_name == 'apsf_stability':
from .baselines.apsf_comparative import run_apsf_stability_test
print(" Starting aPSF stability test...")
return run_apsf_stability_test(task_desc, val_data, test_data, evaluator, config, num_runs=3)
else:
raise ValueError(f"Unsupported method: {method_name}")
def create_experiment_id(method: str, dataset: str, args) -> str:
"""Create unique experiment ID"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
feedback_suffix = "_feedback" if getattr(args, 'feedback', False) else ""
return f"{method}_{dataset}{feedback_suffix}_{timestamp}"
def should_resume_experiment(args) -> bool:
"""Check whether to resume experiment"""
return getattr(args, 'resume', False)
def find_resumable_checkpoint(method: str, dataset: str) -> Optional[str]:
"""Find resumable checkpoint"""
checkpoint_manager = CheckpointManager()
checkpoints = checkpoint_manager.list_checkpoints()
# Find matching checkpoints
matching_checkpoints = [
cp for cp in checkpoints
if cp['method'] == method and cp['dataset'] == dataset and cp['status'] != 'completed'
]
if not matching_checkpoints:
return None
# Return most recent checkpoint
latest_checkpoint = max(matching_checkpoints, key=lambda x: x['timestamp'])
return latest_checkpoint['filename'].replace('_checkpoint.json', '')
def run_bbh_all_tasks_evaluation(method_name: str, config: Dict[str, Any], args, step: Optional[int] = None) -> Dict[str, Any]:
"""
Run evaluation on all BBH tasks - supports resume
"""
print(f"\n Starting BBH full task evaluation - Method: {method_name.upper()}")
# Initialize checkpoint manager
bbh_checkpoint_manager = BBHAllCheckpointManager()
# Check if resume needed
if should_resume_experiment(args):
print(" Checking for resumable checkpoint...")
experiment_id = find_resumable_checkpoint(method_name, "bbh_all")
if experiment_id:
print(f" Found resumable experiment: {experiment_id}")
checkpoint = bbh_checkpoint_manager.load_checkpoint(method_name, "bbh_all", experiment_id)
if checkpoint and checkpoint.get("status") == "completed":
print(f" Experiment already completed, returning saved results")
return checkpoint.get("final_results", {})
else:
print(" No resumable checkpoint found, starting new experiment")
print("=" * 80)
# Load data
loader = get_loader("bbh_all")
evaluator = get_evaluator("bbh_all")
# Get all task names
all_task_names = loader.get_all_task_names()
if not all_task_names:
logging.error("Failed to load any BBH tasks")
return {}
# Get remaining incomplete tasks (supports resume)
if should_resume_experiment(args):
remaining_tasks = bbh_checkpoint_manager.get_remaining_tasks(method_name, all_task_names)
if not remaining_tasks:
print(" All tasks completed!")
# Load and return final results
checkpoint = bbh_checkpoint_manager.load_checkpoint(method_name, "bbh_all")
if checkpoint:
return checkpoint.get("final_results", {})
else:
remaining_tasks = all_task_names
print(f" Will evaluate {len(remaining_tasks)} BBH tasks:")
for i, task_name in enumerate(remaining_tasks, 1):
print(f" {i:2d}. {task_name}")
print("-" * 80)
# Load existing results (if resuming)
task_results = {}
successful_tasks = 0
total_score = 0.0
if should_resume_experiment(args):
checkpoint = bbh_checkpoint_manager.load_checkpoint(method_name, "bbh_all")
if checkpoint:
task_results = checkpoint.get("completed_tasks", {})
successful_tasks = len(task_results)