File size: 11,088 Bytes
aa37927
c842ee2
 
 
 
 
 
 
aa37927
22c3520
4b7beb0
c842ee2
 
 
96f572b
 
 
 
 
 
 
c842ee2
 
 
aa37927
4159f5f
aa37927
 
 
 
 
 
 
c842ee2
 
 
cafafe0
486bbdb
c842ee2
 
 
aa37927
 
 
c842ee2
 
 
4b7beb0
 
 
 
 
 
 
 
 
 
96f572b
c842ee2
 
 
 
 
4b7beb0
 
 
 
 
 
 
c842ee2
 
 
4b7beb0
 
c842ee2
 
 
 
4b7beb0
 
c842ee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b7beb0
c842ee2
 
 
 
 
 
 
 
96f572b
 
560f753
83fc769
c842ee2
96f572b
c842ee2
96f572b
 
 
c842ee2
 
 
96f572b
 
 
 
c842ee2
 
 
 
 
 
 
 
96f572b
 
91e6eee
 
96f572b
c842ee2
 
 
 
4159f5f
7cbbcca
96f572b
c842ee2
 
 
 
4159f5f
96f572b
c842ee2
 
 
 
96f572b
 
 
 
 
 
 
 
c842ee2
 
 
96f572b
 
c842ee2
 
 
 
4b7beb0
96f572b
 
 
7cbbcca
 
c842ee2
 
4159f5f
 
 
c842ee2
 
 
 
4159f5f
 
 
 
 
 
 
 
c842ee2
 
 
96f572b
 
4b7beb0
96f572b
c842ee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f572b
4b7beb0
96f572b
7cbbcca
c842ee2
 
 
4b7beb0
 
96f572b
4b7beb0
 
 
96f572b
c842ee2
 
 
4b7beb0
96f572b
c842ee2
 
 
 
 
 
 
96f572b
 
4b7beb0
 
 
c842ee2
 
 
7cbbcca
91e6eee
aa37927
 
 
 
 
 
402ebfa
aa37927
 
 
 
 
 
c842ee2
 
 
aa37927
 
 
 
 
 
c842ee2
 
 
aa37927
 
 
c842ee2
 
 
aa37927
 
 
c842ee2
 
 
aa37927
 
 
 
c842ee2
 
 
 
aa37927
 
 
 
 
c842ee2
 
 
aa37927
 
 
 
 
c842ee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f572b
c842ee2
96f572b
 
 
 
aa37927
c842ee2
 
7cbbcca
96f572b
 
 
4159f5f
402ebfa
 
 
96f572b
402ebfa
 
c842ee2
 
 
 
96f572b
 
4b7beb0
96f572b
 
 
c842ee2
 
 
96f572b
4b7beb0
 
 
aa37927
c842ee2
 
 
96f572b
 
 
402ebfa
96f572b
c842ee2
 
 
96f572b
aa37927
96f572b
 
 
aa37927
96f572b
aa37927
 
c842ee2
96f572b
aa37927
22c3520
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
import os


# Set environment variable for better memory management
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:32"


import json
from datetime import datetime, timezone
import random




import torch
import pandas as pd
import numpy as np
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM
from langchain.prompts import PromptTemplate




from src.display.formatting import styled_error, styled_message, styled_warning
from src.envs import API, EVAL_REQUESTS_PATH, TOKEN, QUEUE_REPO, EVAL_RESULTS_PATH, RESULTS_REPO
from src.submission.check_validity import (
    already_submitted_models,
    check_model_card,
    get_model_size,
    is_model_on_hub,
)




import spaces




REQUESTED_MODELS = None
USERS_TO_SUBMISSION_DATES = None




# List of subjects to exclude from evaluation
excluded_subjects = [
    "human_sexuality",
    "professional_psychology",
    "moral_disputes",
    "public_relations",
    "jurisprudence",
    "human_aging",
    "world_religions"
]




def get_top_prediction(batch_texts, tokenizer, model):
    inputs = tokenizer(batch_texts, return_tensors='pt', padding=True, truncation=True)
    if torch.cuda.is_available():
        model = model.cuda()
        inputs = {k: v.cuda() for k, v in inputs.items()}
    else:
        model = model.cpu()
        inputs = {k: v.cpu() for k, v in inputs.items()}




    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits[:, -1, :]  # Get logits of the last token for each input in the batch




    options = [' A', ' B', ' C', ' D']
    predictions = []




    for i in range(len(batch_texts)):
        option_logits = []
        for option in options:
            option_ids = tokenizer(option).input_ids
            if option_ids and option_ids[-1] < logits.size(1):
                option_logit = logits[i, option_ids[-1]].item()
                option_logits.append((option_logit, option.strip()))
            else:
                print(f"Skipping option '{option}' due to index out of range for input {i}.")




        if not option_logits:
            predictions.append("No valid options")
        else:
            top_option = max(option_logits, key=lambda x: x[0])[1]
            predictions.append(top_option)




    return predictions




@spaces.GPU(duration=120)
def evaluate_model_accuracy_by_subject(model_name, num_questions_per_subject=100, batch_size=32):  
    try:
        # Load the model and tokenizer
        tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
        tokenizer.pad_token = tokenizer.eos_token




        model = AutoModelForCausalLM.from_pretrained(
            model_name,
            trust_remote_code=True
        )
        
        
        # Convert model to FP16 (half precision) to reduce memory usage
        model = model.half()




        if torch.cuda.is_available():
            model = model.cuda()  # Move model to GPU if available
        else:
            model = model.cpu()




        # Load your custom MMMLU dataset from HuggingFace
        dataset = load_dataset("Omartificial-Intelligence-Space/Arabic_Openai_MMMLU")
        dataset = dataset['test']




        # Filter out excluded subjects
        dataset = dataset.filter(lambda x: x['Subject'] not in excluded_subjects)




        # Define prompt template
        template = """Answer the following multiple choice question by giving the most appropriate response. Answer should be one among [A, B, C, D].
Question: {Question}
A) {A}
B) {B}
C) {C}
D) {D}
Answer:"""




        prompt_template = PromptTemplate(template=template, input_variables=['Question', 'A', 'B', 'C', 'D'])




        # Initialize results storage
        subject_results = {}
        overall_correct_predictions = 0
        overall_total_questions = 0




        subjects = dataset.unique('Subject')
        for subject in subjects:
            subject_data = dataset.filter(lambda x: x['Subject'] == subject)




            # Sample num_questions_per_subject from each subject
            if num_questions_per_subject > 0:
                if len(subject_data) < num_questions_per_subject:
                    print(f"Warning: Not enough questions for subject '{subject}'. Using all available questions.")
                    selected_indices = range(len(subject_data))
                else:
                    selected_indices = random.sample(range(len(subject_data)), num_questions_per_subject)
                subject_data = subject_data.select(selected_indices)




            correct_predictions = 0
            total_questions = 0
            results = []




            model.eval()
            # Batch processing
            for i in range(0, len(subject_data), batch_size):
                batch_data = subject_data[i:i + batch_size]
                
                # Generate batch texts
                batch_texts = [
                    prompt_template.format(
                        Question=batch_data['Question'][j],
                        A=batch_data['A'][j],
                        B=batch_data['B'][j],
                        C=batch_data['C'][j],
                        D=batch_data['D'][j]
                    ) for j in range(len(batch_data['Question']))
                ]




                # Get the top predictions for the batch
                batch_predictions = get_top_prediction(batch_texts, tokenizer, model)




                for j in range(len(batch_data['Question'])):
                    top_prediction = batch_predictions[j]
                    is_correct = (top_prediction == batch_data['Answer'][j])
                    correct_predictions += int(is_correct)
                    total_questions += 1
                    overall_correct_predictions += int(is_correct)
                    overall_total_questions += 1




                    results.append({
                        'Question': batch_data['Question'][j],
                        'Answer': batch_data['Answer'][j],
                        'Prediction': top_prediction,
                        'Correct': is_correct
                    })




            # Clear GPU memory after processing each subject
            torch.cuda.empty_cache()




            accuracy = correct_predictions / total_questions if total_questions > 0 else 0




            # Store results for this subject
            subject_results[subject] = {
                'Correct Predictions': correct_predictions,
                'Total Questions': total_questions,
                'Accuracy': accuracy * 100,
                'Results DataFrame': pd.DataFrame(results)
            }




        overall_accuracy = (overall_correct_predictions / overall_total_questions) * 100 if overall_total_questions > 0 else 0




        return overall_accuracy, subject_results




    except Exception as e:
        import traceback
        tb = traceback.format_exc()
        print(f"Error in evaluate_model_accuracy_by_subject: {e}\n{tb}")
        return f"Error: {str(e)}", {}




def add_new_eval(
    model: str,
    base_model: str,
    revision: str,
    precision: str,
    weight_type: str,
    model_type: str
):
    global REQUESTED_MODELS
    global USERS_TO_SUBMISSION_DATES
    if not REQUESTED_MODELS:
        REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(EVAL_REQUESTS_PATH)




    user_name = ""
    model_path = model
    if "/" in model:
        user_name = model.split("/")[0]
        model_path = model.split("/")[1]




    precision = precision.split(" ")[0]
    current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")




    if model_type is None or model_type == "":
        return styled_error("Please select a model type.")




    # Does the model actually exist?
    if revision == "":
        revision = "main"




    # Is the model on the hub?
    if weight_type in ["Delta", "Adapter"]:
        base_model_on_hub, error, _ = is_model_on_hub(model_name=base_model, revision=revision, token=TOKEN, test_tokenizer=True)
        if not base_model_on_hub:
            return styled_error(f'Base model "{base_model}" {error}')




    if not weight_type == "Adapter":
        model_on_hub, error, _ = is_model_on_hub(model_name=model, revision=revision, token=TOKEN, test_tokenizer=True)
        if not model_on_hub:
            return styled_error(f'Model "{model}" {error}')




    # Is the model info correctly filled?
    try:
        model_info = API.model_info(repo_id=model, revision=revision)
    except Exception:
        return styled_error("Could not get your model information. Please fill it up properly.")




    model_size = get_model_size(model_info=model_info, precision=precision)




    # Were the model card and license filled?
    try:
        license = model_info.cardData["license"]
    except Exception:
        return styled_error("Please select a license for your model")




    modelcard_OK, error_msg = check_model_card(model)
    if not modelcard_OK:
        return styled_error(error_msg)




    # Check for duplicate submission
    if f"{model}_{revision}_{precision}" in REQUESTED_MODELS:
        return styled_warning("This model has been already submitted.")




    # Now, perform the evaluation
    try:
        overall_accuracy, subject_results = evaluate_model_accuracy_by_subject(model, num_questions_per_subject=100, batch_size=32)
        if isinstance(overall_accuracy, str) and overall_accuracy.startswith("Error"):
            return styled_error(overall_accuracy)
    except Exception as e:
        return styled_error(f"An error occurred during evaluation: {str(e)}")




    # Prepare results for storage
    results_dict = {
        "config": {
            "model_name": model,
            "base_model": base_model,
            "revision": revision,
            "precision": precision,
            "weight_type": weight_type,
            "model_type": model_type,
            "submitted_time": current_time,
            "license": license,
            "likes": model_info.likes,
            "params": model_size,
            "still_on_hub": True,
        },
        "results": {
            "average": overall_accuracy,
        },
    }




    # Include per-subject accuracies
    for subject, data in subject_results.items():
        accuracy = data['Accuracy']
        results_dict['results'][subject] = accuracy




    # Save results to a JSON file
    results_file_path = f"{EVAL_RESULTS_PATH}/{model.replace('/', '_')}_results.json"
    with open(results_file_path, "w") as f:
        json.dump(results_dict, f, indent=4)




    # Upload the results file
    API.upload_file(
        path_or_fileobj=results_file_path,
        path_in_repo=results_file_path.split(f"{EVAL_RESULTS_PATH}/")[1],
        repo_id=RESULTS_REPO,
        repo_type="dataset",
        commit_message=f"Add results for {model}"
    )

    # Remove the local results file
    os.remove(results_file_path)

    return styled_message("Your model has been evaluated and the results are now on the leaderboard!")