Repository navigation
Expand file tree
/
Copy pathextract_dove_scores_OLMoE.py
More file actions
382 lines (314 loc) Β· 13.6 KB
/
Copy pathextract_dove_scores_OLMoE.py
File metadata and controls
382 lines (314 loc) Β· 13.6 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
import json
import os
from pathlib import Path
import pandas as pd
from tqdm import tqdm
import warnings
warnings.filterwarnings("ignore")
class DoveLiteMMPipelineLocal:
"""
Complete pipeline to process local DOVE_Lite MMLU data for OLMoE-1B-7B-0924-Instruct
"""
def __init__(self, base_path="app/results_local/nlphuji_DOVE_Lite/en"):
self.base_path = Path(base_path)
self.model_name = "OLMoE-1B-7B-0924-Instruct"
self.language = "en"
self.shots = [0, 5]
# All 57 MMLU datasets (based on your directory structure)
self.mmlu_datasets = [
"mmlu.abstract_algebra",
"mmlu.anatomy",
"mmlu.astronomy",
"mmlu.business_ethics",
"mmlu.clinical_knowledge",
"mmlu.college_biology",
"mmlu.college_chemistry",
"mmlu.college_computer_science",
"mmlu.college_mathematics",
"mmlu.college_medicine",
"mmlu.college_physics",
"mmlu.computer_security",
"mmlu.conceptual_physics",
"mmlu.econometrics",
"mmlu.electrical_engineering",
"mmlu.elementary_mathematics",
"mmlu.formal_logic",
"mmlu.global_facts",
"mmlu.high_school_biology",
"mmlu.high_school_chemistry",
"mmlu.high_school_computer_science",
"mmlu.high_school_european_history",
"mmlu.high_school_geography",
"mmlu.high_school_government_and_politics",
"mmlu.high_school_macroeconomics",
"mmlu.high_school_mathematics",
"mmlu.high_school_microeconomics",
"mmlu.high_school_physics",
"mmlu.high_school_psychology",
"mmlu.high_school_statistics",
"mmlu.high_school_us_history",
"mmlu.high_school_world_history",
"mmlu.human_aging",
"mmlu.human_sexuality",
"mmlu.international_law",
"mmlu.jurisprudence",
"mmlu.logical_fallacies",
"mmlu.machine_learning",
"mmlu.management",
"mmlu.marketing",
"mmlu.medical_genetics",
"mmlu.miscellaneous",
"mmlu.moral_disputes",
"mmlu.moral_scenarios",
"mmlu.nutrition",
"mmlu.philosophy",
"mmlu.prehistory",
"mmlu.professional_accounting",
"mmlu.professional_law",
"mmlu.professional_medicine",
"mmlu.professional_psychology",
"mmlu.public_relations",
"mmlu.security_studies",
"mmlu.sociology",
"mmlu.us_foreign_policy",
"mmlu.virology",
"mmlu.world_religions",
]
print(f"Initialized pipeline for {len(self.mmlu_datasets)} MMLU datasets")
print(f"Model: {self.model_name}")
print(f"Shots: {self.shots}")
print(f"Base path: {self.base_path}")
def load_local_data(self):
"""
Load all DOVE_Lite data from local parquet files
"""
print("π Loading DOVE_Lite dataset from local files...")
all_data = []
# Check if base path exists
if not self.base_path.exists():
print(f"β Base path does not exist: {self.base_path}")
return None
# Process each shot configuration
for shot in self.shots:
shot_dir = self.base_path / f"Shots_{shot}" / self.model_name
if not shot_dir.exists():
print(f"β οΈ Shot directory does not exist: {shot_dir}")
continue
print(f"π Processing {shot}-shot data from: {shot_dir}")
# Process each MMLU dataset
for dataset in tqdm(self.mmlu_datasets, desc=f"Loading {shot}-shot data"):
dataset_dir = shot_dir / dataset
parquet_file = dataset_dir / "low_performance_questions_0.1.parquet"
if parquet_file.exists():
try:
# Load the parquet file
df = pd.read_parquet(parquet_file)
# Add metadata columns to match HF format
df["model"] = self.model_name
df["dataset"] = dataset
df["dimensions_5: shots"] = shot
df["language"] = self.language
all_data.append(df)
except Exception as e:
print(f"β Error loading {parquet_file}: {e}")
else:
print(f"β οΈ File not found: {parquet_file}")
if not all_data:
print("β No data files were loaded successfully!")
return None
# Combine all data
combined_df = pd.concat(all_data, ignore_index=True)
print(f"β
Loaded {len(combined_df)} total records from local files")
print(f"Available datasets: {combined_df['dataset'].nunique()} unique datasets")
print(
f"Shot breakdown: {combined_df['dimensions_5: shots'].value_counts().to_dict()}"
)
return combined_df
def validate_data_structure(self, df):
"""
Validate that the loaded data has the expected structure
"""
print("π Validating data structure...")
required_columns = ["sample_index"]
score_columns = ["score", "average_score"] # Check for either column name
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
print(f"β Missing required columns: {missing_columns}")
print(f"Available columns: {list(df.columns)}")
return False
# Check for score column (either 'score' or 'average_score')
score_col_found = any(col in df.columns for col in score_columns)
if not score_col_found:
print(f"β Missing score column. Expected one of: {score_columns}")
print(f"Available columns: {list(df.columns)}")
return False
# Rename 'average_score' to 'score' if needed for consistency
if "average_score" in df.columns and "score" not in df.columns:
df["score"] = df["average_score"]
print("β
Renamed 'average_score' to 'score' for consistency")
print(f"β
Data structure validated")
print(f"Columns: {list(df.columns)}")
print(f"Data types: {df.dtypes.to_dict()}")
return True
def filter_data(self, df):
"""
Filter data for our specific requirements (already filtered during loading)
"""
print("π Data already filtered during loading, validating...")
print(f"Total records: {len(df)}")
print(f"Model: {df['model'].unique()}")
print(f"Datasets: {df['dataset'].nunique()} unique datasets")
print(f"Shots: {sorted(df['dimensions_5: shots'].unique())}")
# Show breakdown by dataset
print(f"\nDataset breakdown:")
dataset_counts = df["dataset"].value_counts()
print(f"Found data for {len(dataset_counts)} MMLU datasets:")
for dataset, count in dataset_counts.head(10).items():
print(f" {dataset}: {count} records")
if len(dataset_counts) > 10:
print(f" ... and {len(dataset_counts) - 10} more datasets")
# Show breakdown by shots
print(f"\nShots breakdown:")
shots_counts = df["dimensions_5: shots"].value_counts()
for shots, count in shots_counts.items():
print(f" {shots}-shot: {count} records")
return df
def calculate_mean_scores(self, df):
"""
Calculate mean scores for each sample_index across all permutations
"""
print("π Calculating mean scores per sample_index...")
# Group by sample_index and calculate mean score
# This averages across all dimensions: shots, datasets, and any other variations
mean_scores = (
df.groupby("sample_index")["score"]
.agg(["mean", "count", "std"])
.reset_index()
)
mean_scores.columns = ["sample_index", "mean_score", "count", "std_score"]
print(f"β
Calculated mean scores for {len(mean_scores)} unique sample indices")
print(f"Score statistics:")
print(f" Mean score: {mean_scores['mean_score'].mean():.3f}")
print(
f" Score range: {mean_scores['mean_score'].min():.3f} to {mean_scores['mean_score'].max():.3f}"
)
print(f" Average records per sample: {mean_scores['count'].mean():.1f}")
print(f" Min records per sample: {mean_scores['count'].min()}")
print(f" Max records per sample: {mean_scores['count'].max()}")
return mean_scores
def create_json_output(
self, mean_scores_df, output_file="mmlu_dove_lite_scores_local.json"
):
"""
Create final JSON output with sample_index -> mean_score mapping
"""
print(f"π Creating JSON output...")
# Create dictionary mapping sample_index to mean_score
scores_dict = dict(
zip(mean_scores_df["sample_index"], mean_scores_df["mean_score"])
)
# Save as JSON
with open(output_file, "w") as f:
json.dump(scores_dict, f, indent=2)
print(f"β
JSON file saved: {output_file}")
print(f" Contains {len(scores_dict)} sample indices")
return scores_dict
def save_detailed_results(self, df, mean_scores_df):
"""
Save additional detailed files for analysis
"""
print("π Saving detailed results...")
# Save full filtered data
df.to_parquet("mmlu_dove_lite_full_data_local.parquet", index=False)
print("β
Saved full data: mmlu_dove_lite_full_data_local.parquet")
# Save mean scores with statistics
mean_scores_df.to_parquet(
"mmlu_dove_lite_mean_scores_local.parquet", index=False
)
print("β
Saved mean scores: mmlu_dove_lite_mean_scores_local.parquet")
# Create summary by dataset
dataset_summary = (
df.groupby("dataset")
.agg({"score": ["mean", "std", "count"], "sample_index": "nunique"})
.round(3)
)
dataset_summary.columns = [
"mean_score",
"std_score",
"total_records",
"unique_samples",
]
dataset_summary = dataset_summary.reset_index()
# Save dataset summary
dataset_summary.to_json(
"mmlu_dove_lite_dataset_summary_local.json", orient="records", indent=2
)
print("β
Saved dataset summary: mmlu_dove_lite_dataset_summary_local.json")
# Create summary by shots
shots_summary = (
df.groupby("dimensions_5: shots")
.agg({"score": ["mean", "std", "count"], "sample_index": "nunique"})
.round(3)
)
shots_summary.columns = [
"mean_score",
"std_score",
"total_records",
"unique_samples",
]
shots_summary = shots_summary.reset_index()
print(f"\nShots performance summary:")
for _, row in shots_summary.iterrows():
print(
f" {int(row['dimensions_5: shots'])}-shot: {row['mean_score']:.3f} Β± {row['std_score']:.3f} "
f"({row['total_records']} records, {row['unique_samples']} unique samples)"
)
def run_complete_pipeline(self):
"""
Run the complete pipeline
"""
print("π Starting DOVE_Lite MMLU Pipeline (Local Files)")
print("=" * 60)
# Step 1: Load data from local files
df = self.load_local_data()
if df is None:
return None
# Step 2: Validate data structure
if not self.validate_data_structure(df):
return None
# Step 3: Filter/validate data (already filtered during loading)
df_filtered = self.filter_data(df)
if len(df_filtered) == 0:
print("β No data found after filtering!")
return None
# Step 4: Calculate mean scores per sample_index
mean_scores_df = self.calculate_mean_scores(df_filtered)
# Step 5: Create final JSON output
scores_dict = self.create_json_output(mean_scores_df)
# Step 6: Save detailed results
self.save_detailed_results(df_filtered, mean_scores_df)
print("\nπ Pipeline completed successfully!")
print("=" * 60)
print(f"Final output: mmlu_dove_lite_scores_local.json")
print(f"Contains {len(scores_dict)} sample indices with mean scores")
# Show a few examples
print(f"\nExample entries:")
sample_items = list(scores_dict.items())[:5]
for idx, score in sample_items:
print(f" Sample {idx}: {score:.3f}")
return scores_dict
def main():
"""
Main function to run the pipeline
"""
# You can customize the base path here if needed
base_path = "app/results_local/nlphuji_DOVE_Lite/en"
# Initialize and run pipeline
pipeline = DoveLiteMMPipelineLocal(base_path=base_path)
result = pipeline.run_complete_pipeline()
if result:
print(f"\nβ
Successfully processed {len(result)} samples")
else:
print("\nβ Pipeline failed")
if __name__ == "__main__":
main()