Spaces:
Sleeping
Sleeping
File size: 14,947 Bytes
77d4add b748395 532a4a4 3404c97 b748395 77d4add 84c9e35 3404c97 b748395 77d4add 00afad7 532a4a4 00afad7 532a4a4 84c9e35 532a4a4 84c9e35 532a4a4 84c9e35 532a4a4 84c9e35 00afad7 77d4add 6cf3cc0 3404c97 714de6d 5d2ee20 3404c97 714de6d 532a4a4 3404c97 532a4a4 3404c97 532a4a4 3404c97 5d2ee20 3404c97 532a4a4 3404c97 532a4a4 3404c97 532a4a4 3404c97 532a4a4 3404c97 77d4add 3404c97 6d1be3a 3404c97 77d4add 3404c97 6d1be3a 532a4a4 714de6d 6d1be3a 532a4a4 77d4add 532a4a4 714de6d 00afad7 3404c97 532a4a4 3404c97 6d1be3a 3404c97 6d1be3a 714de6d 532a4a4 714de6d 6d1be3a 714de6d 77d4add 532a4a4 6d1be3a |
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 |
import torch
import evaluate
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM
import logging
import numpy as np
import pandas as pd
from tqdm import tqdm
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
accuracy_metric = evaluate.load("accuracy")
option_letters = ["A", "B", "C", "D"]
MAX_CONTEXT_WINDOW = 4096
def load_dataset_from_hf(verbose=False):
mmlu_dataset = load_dataset("cais/mmlu", "all")
if verbose:
for split in mmlu_dataset.keys():
dataset = mmlu_dataset[split] # Access the dataset split
# Log number of rows and columns
num_rows = len(dataset)
num_cols = len(dataset.column_names)
logger.info(f"Dataset Split: {split}")
logger.info(f"Number of Rows: {num_rows}")
logger.info(f"Number of Columns: {num_cols}")
# Log column names and their types
column_types = {col: str(dataset.features[col].dtype) for col in dataset.column_names}
logger.info(f"Column Names: {dataset.column_names}")
logger.info(f"Column Types: {column_types}")
# Log a sample of 5 rows
sample_rows = dataset.select(range(min(5, num_rows))) # Ensure we don't exceed available rows
logger.info("Sample Rows:")
for row in sample_rows:
logger.info(row)
logger.info("=" * 50) # Separator for readability
return mmlu_dataset
def format_subject(subject):
l = subject.split("_")
s = ""
for entry in l:
s += " " + entry
return s
def format_example(df, idx, include_answer=True):
"""
Format a single example for the prompt based on the actual dataset structure:
- Column 0: question text
- Column 1: subject
- Column 2: choices as a list of strings
- Column 3: answer as a numeric index (0-3)
"""
# Get the question text
prompt = df.iloc[idx, 0]
# Get the choices from the dataframe
options_list = df.iloc[idx, 2]
assert(isinstance(options_list, list))
for j, option in enumerate(options_list):
prompt += f"\n{option_letters[j]}. {option}"
prompt += "\nAnswer:"
if include_answer:
# Convert numeric answer to letter
answer_num = df.iloc[idx, 3]
answer_letter = {0: "A", 1: "B", 2: "C", 3: "D"}[answer_num]
prompt += f" {answer_letter}\n\n"
return prompt
def gen_prompt(df, subject, k=-1):
prompt = "The following are multiple choice questions (with answers) about {}.\n\n".format(
format_subject(subject)
)
if k == -1:
k = df.shape[0]
for i in range(k):
prompt += format_example(df, i, include_answer=True)
return prompt
@torch.no_grad()
def eval_batched(subject, model, tokenizer, dev_df, test_df, num_questions_per_subject=5, train_shots=5, batch_size=4):
"""
Improved eval function that uses batched processing on GPU
"""
assert all(dev_df['subject'] == subject), f"Not all items in dev_df match subject {subject}"
assert all(test_df['subject'] == subject), f"Not all items in test_df match subject {subject}"
logger.info(f"Subject: {subject}, processing with batch_size={batch_size}")
cors = []
all_probs = []
if (train_shots < 0):
train_shots = 0 # Make positive.
# Generate the few-shot examples for this subject once
train_prompt = gen_prompt(dev_df, subject, train_shots)
# Process test examples in batches
for batch_start in range(0, test_df.shape[0], batch_size):
batch_end = min(batch_start + batch_size, test_df.shape[0])
batch_size_actual = batch_end - batch_start
# Prepare batch prompts
batch_prompts = []
batch_labels = []
for i in range(batch_start, batch_end):
prompt_end = format_example(test_df, i, include_answer=False)
prompt = train_prompt + prompt_end
batch_prompts.append(prompt)
label = test_df.iloc[i, 3]
label_letter = {0: "A", 1: "B", 2: "C", 3: "D"}[label]
batch_labels.append(label_letter)
# Tokenize all prompts in batch
tokenized_inputs = tokenizer(batch_prompts, padding=True, return_tensors="pt")
input_ids = tokenized_inputs.input_ids.to(model.device)
attention_mask = tokenized_inputs.attention_mask.to(model.device)
# Check if any example exceeds context window and adjust if needed
if input_ids.shape[1] > MAX_CONTEXT_WINDOW:
logger.warning(f"Some examples exceed max context window ({input_ids.shape[1]} > {MAX_CONTEXT_WINDOW})")
logger.warning(f"Reducing train_shots from {train_shots}")
# Find the lowest train_shots that fits
while train_shots > 0:
train_shots -= 1
train_prompt = gen_prompt(dev_df, subject, train_shots)
# Recalculate prompts with fewer shots
temp_prompt = train_prompt + format_example(test_df, batch_start, include_answer=False)
temp_tokens = tokenizer(temp_prompt, return_tensors="pt").input_ids
if temp_tokens.shape[1] <= MAX_CONTEXT_WINDOW:
logger.info(f"Reduced to train_shots={train_shots}")
# Regenerate all prompts in the batch with fewer shots
batch_prompts = []
for i in range(batch_start, batch_end):
prompt_end = format_example(test_df, i, include_answer=False)
prompt = train_prompt + prompt_end
batch_prompts.append(prompt)
# Retokenize with reduced shots
tokenized_inputs = tokenizer(batch_prompts, padding=True, return_tensors="pt")
input_ids = tokenized_inputs.input_ids.to(model.device)
attention_mask = tokenized_inputs.attention_mask.to(model.device)
break
# If we still can't fit even with 0 shots, we have to skip
if input_ids.shape[1] > MAX_CONTEXT_WINDOW:
logger.error(f"Even with 0 shots, context is too long ({input_ids.shape[1]} > {MAX_CONTEXT_WINDOW})")
# Process individually as fallback
for i in range(batch_start, batch_end):
single_prompt = format_example(test_df, i, include_answer=False)
single_tokens = tokenizer(single_prompt, return_tensors="pt").input_ids.to(model.device)
if single_tokens.shape[1] <= MAX_CONTEXT_WINDOW:
single_output = model(input_ids=single_tokens)
single_logits = single_output.logits[0, -1]
single_probs = get_option_probs(tokenizer, single_logits)
pred = {0: "A", 1: "B", 2: "C", 3: "D"}[np.argmax(single_probs)]
cors.append(pred == batch_labels[i-batch_start])
all_probs.append(single_probs)
else:
logger.error(f"Example {i} is too long even by itself, skipping")
continue
# Run model on batch
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
# Extract predictions for each example in batch
for j in range(batch_size_actual):
# Get logits for the last token in each sequence
sequence_len = attention_mask[j].sum()
logits = outputs.logits[j, sequence_len-1]
# Calculate probabilities for A, B, C, D
probs = get_option_probs(tokenizer, logits)
pred = {0: "A", 1: "B", 2: "C", 3: "D"}[np.argmax(probs)]
cor = pred == batch_labels[j]
# Log first example for debugging
if batch_start == 0 and j == 0:
logger.info(f"Prompt (truncated): {batch_prompts[j][:200]}...")
logger.info(f"Label_Letter: {batch_labels[j]}")
logger.info(f"Probabilities: {probs}")
logger.info(f"Prediction: {pred}")
logger.info(f"Correct: {cor}")
cors.append(cor)
all_probs.append(probs)
acc = np.mean(cors)
cors = np.array(cors)
all_probs = np.array(all_probs)
print("Average accuracy {:.3f} - {}".format(acc, subject))
return subject, cors, acc, all_probs
def get_option_probs(tokenizer, logits):
"""Helper function to extract option probabilities from logits"""
option_probs = torch.nn.functional.softmax(
torch.tensor(
[
logits[tokenizer("A").input_ids[-1]],
logits[tokenizer("B").input_ids[-1]],
logits[tokenizer("C").input_ids[-1]],
logits[tokenizer("D").input_ids[-1]],
]
).float(),
dim=0,
).detach().cpu().numpy()
return option_probs
def get_max_batch_size(model, tokenizer, example_text, max_memory_fraction=0.8):
"""
Estimate the maximum possible batch size based on available GPU memory
Args:
model: The model to evaluate
tokenizer: The tokenizer to use
example_text: A sample text input
max_memory_fraction: Maximum fraction of GPU memory to use (0.8 = 80%)
Returns:
Estimated maximum batch size
"""
import torch
# Get total GPU memory and currently allocated memory
total_memory = torch.cuda.get_device_properties(0).total_memory
# Keep a safe buffer to avoid OOM
safe_memory = int(total_memory * max_memory_fraction)
# Tokenize example to get size
example_tokens = tokenizer(example_text, return_tensors="pt").to(model.device)
example_len = example_tokens.input_ids.shape[1]
# Run a single forward pass to measure memory usage
torch.cuda.empty_cache()
torch.cuda.reset_peak_memory_stats()
_ = model(**example_tokens)
single_forward_memory = torch.cuda.max_memory_allocated()
# Calculate memory per example and estimate max batch size
estimated_max_batch = safe_memory // single_forward_memory
# Reduce by a factor for safety (activations, gradients, etc.)
safe_batch_size = max(1, int(estimated_max_batch * 0.8))
logger.info(f"Estimated max batch size: {safe_batch_size} for sequence length {example_len}")
logger.info(f"Memory usage: {single_forward_memory / 1e9:.2f} GB per example")
logger.info(f"Total memory: {total_memory / 1e9:.2f} GB, Safe memory: {safe_memory / 1e9:.2f} GB")
return safe_batch_size
def evaluate_mmlu_batched(model, tokenizer, num_subjects=10, num_questions=10, num_shots=5, batch_size=8, auto_batch_size=False):
"""
Evaluates the model on MMLU using batched GPU processing for faster inference.
Args:
model: The model to evaluate
tokenizer: The tokenizer to use
num_subjects (int): Number of subjects to evaluate. If -1, evaluates all subjects
num_questions (int): Number of questions per subject
num_shots (int): Number of few-shot examples to use
batch_size (int): Batch size for processing multiple examples at once
auto_batch_size (bool): If True, automatically determine the optimal batch size
"""
# If auto_batch_size is enabled, estimate the optimal batch size
if auto_batch_size:
# Get a sample prompt
dataset = load_dataset_from_hf(verbose=False)
test_df = pd.DataFrame(dataset['test'])
dev_df = pd.DataFrame(dataset['dev'])
test_df = test_df.sort_values(['subject', 'question'])
dev_df = dev_df.sort_values(['subject', 'question'])
subject = test_df['subject'].iloc[0]
test_sample = test_df[test_df['subject'] == subject].head(1)
dev_sample = dev_df[dev_df['subject'] == subject].head(num_shots)
# Generate a sample prompt
train_prompt = gen_prompt(dev_sample, subject, num_shots)
sample_prompt = train_prompt + format_example(test_sample, 0, include_answer=False)
# Estimate the max batch size
batch_size = get_max_batch_size(model, tokenizer, sample_prompt)
logger.info(f"Auto-adjusted batch size: {batch_size}")
model.eval() # Ensure Dropout and BatchNorm behave appropriately for inference
dataset = load_dataset_from_hf(verbose=True)
# Convert dataset partitions to pandas DataFrames
test_df = pd.DataFrame(dataset['test'])
dev_df = pd.DataFrame(dataset['dev'])
# Sort datasets by subject and other relevant columns
test_df = test_df.sort_values(['subject', 'question'])
dev_df = dev_df.sort_values(['subject', 'question'])
# Get all unique subjects
all_subjects = sorted(test_df['subject'].unique())
# Select subjects based on num_subjects parameter
if num_subjects == -1 or num_subjects >= len(all_subjects):
subjects = all_subjects
else:
# Take the first num_subjects subjects
subjects = all_subjects[:num_subjects]
results = {}
all_cors = []
results_table = []
for subject in tqdm(subjects, desc="Processing subjects"):
test_samples = test_df[test_df['subject'] == subject].head(num_questions)
dev_samples = dev_df[dev_df['subject'] == subject].head(num_shots)
# Log subject and sample counts
logger.info(f"Subject: {subject}, Test Samples: {len(test_samples)}, Dev Samples: {len(dev_samples)}")
subject, cors, acc, probs = eval_batched(
subject,
model,
tokenizer,
dev_samples,
test_samples,
num_questions_per_subject=num_questions,
train_shots=num_shots,
batch_size=batch_size
)
results[subject] = acc
all_cors.append(cors)
results_table.append({
'Subject': subject,
'Num_samples': len(test_samples),
'Num_correct': int(np.sum(cors)),
'Accuracy': acc
})
weighted_acc = np.mean(np.concatenate(all_cors))
min_acc_subject = min(results.items(), key=lambda x: x[1])[0]
max_acc_subject = max(results.items(), key=lambda x: x[1])[0]
return {
"overall_accuracy": weighted_acc,
"min_accuracy_subject": (min_acc_subject, results[min_acc_subject]),
"max_accuracy_subject": (max_acc_subject, results[max_acc_subject]),
"full_accuracy_table": results_table,
} |