File size: 7,479 Bytes
970eef1
2a8ebbd
970eef1
 
 
2a8ebbd
970eef1
 
2a8ebbd
 
 
 
 
 
 
d6b6619
970eef1
 
 
2a8ebbd
970eef1
 
 
 
 
 
 
 
 
 
 
 
 
2a8ebbd
 
 
 
970eef1
2a8ebbd
970eef1
2a8ebbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
970eef1
2a8ebbd
 
 
 
d6b6619
2a8ebbd
 
970eef1
2a8ebbd
970eef1
 
 
2a8ebbd
970eef1
 
f8ec36f
970eef1
 
 
 
2a8ebbd
 
970eef1
 
2a8ebbd
970eef1
 
 
 
 
f8ec36f
2a8ebbd
970eef1
 
2a8ebbd
970eef1
d6b6619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a8ebbd
 
 
 
d6b6619
 
2a8ebbd
 
 
 
 
 
 
 
 
 
 
 
 
970eef1
2a8ebbd
 
 
 
 
 
 
 
 
 
 
970eef1
2a8ebbd
 
 
 
 
 
 
 
 
d6b6619
970eef1
d6b6619
970eef1
2a8ebbd
 
970eef1
2a8ebbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6b6619
 
 
 
 
 
 
2a8ebbd
 
 
 
970eef1
2a8ebbd
 
970eef1
2a8ebbd
 
 
 
970eef1
2a8ebbd
970eef1
 
2a8ebbd
970eef1
2a8ebbd
 
 
970eef1
2a8ebbd
970eef1
 
2a8ebbd
970eef1
d6b6619
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
"""
Task to run evaluation using lighteval
"""
import os
import time
import subprocess
import tempfile
from pathlib import Path
import concurrent.futures
from dotenv import load_dotenv
from datetime import datetime
import json
from typing import List, Dict
from tasks.get_model_providers import get_model_providers
from huggingface_hub import HfApi
import asyncio

class EvaluationTask:
    """
    Task to run evaluation using lighteval
    """

    def __init__(self, session_uid: str, dataset_name: str):
        """
        Initialize the evaluation task
        
        Args:
            session_uid: Session ID for this task
            dataset_name: Name of the dataset to evaluate
        """
        self.session_uid = session_uid
        self.dataset_name = dataset_name
        self.is_completed = False
        self.results = []
        self.hf_api = HfApi()

    def _save_results_to_hub(self) -> None:
        """
        Save evaluation results to the dataset on the Hub
        """
        try:
            # Create results directory if it doesn't exist
            results_dir = Path("data/lighteval_results")
            results_dir.mkdir(parents=True, exist_ok=True)
            
            # Save results to JSON file
            results_file = results_dir / "lighteval_results.json"
            with open(results_file, "w") as f:
                json.dump(self.results, f, indent=2)
            
            # Push to Hub
            self.hf_api.upload_file(
                path_or_fileobj=str(results_file),
                path_in_repo="lighteval_results.json",
                repo_id=self.dataset_name,
                repo_type="dataset",
                commit_message="Add lighteval evaluation results"
            )
            
            print(f"[{datetime.now().strftime('%H:%M:%S')}] Results saved to Hub at {self.dataset_name}/lighteval_results.json")
        except Exception as e:
            print(f"[{datetime.now().strftime('%H:%M:%S')}] Failed to save results to Hub: {str(e)}")

    async def _run_lighteval(self, model_name: str, provider: str, dataset_name: str) -> dict:
        start_time = time.time()
        print(f"[{datetime.now().strftime('%H:%M:%S')}] Starting evaluation with {provider} provider for {model_name}")
        
        # Create temporary task file
        temp_file_path = tempfile.mktemp(suffix=".py")
        with open(temp_file_path, 'w') as temp_file:
            temp_file.write(f"""
from lighteval_task.lighteval_task import create_yourbench_task

# Create yourbench task
yourbench = create_yourbench_task("{dataset_name}", "multi_hop_questions")

# Define TASKS_TABLE needed by lighteval
TASKS_TABLE = [yourbench]
""")

        # LightEval command
        cmd_args = [
            "lighteval",
            "endpoint",
            "inference-providers",
            f"model={model_name},provider={provider}",
            "custom|yourbench|0|0",
            "--custom-tasks",
            temp_file_path,
            "--max-samples", "30",
            "--output-dir", "data/lighteval_results",
            "--no-push-to-hub"
        ]

        try:
            # Run the command with environment variables and increased timeout of 300 seconds
            process = await asyncio.create_subprocess_exec(
                *cmd_args,
                env=os.environ,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE
            )
            
            try:
                await asyncio.wait_for(process.communicate(), timeout=60)
            except asyncio.TimeoutError:
                process.kill()
                print(f"[{datetime.now().strftime('%H:%M:%S')}] Evaluation timed out for {model_name} after {time.time() - start_time:.2f}s")
                return {
                    "model": model_name,
                    "provider": provider,
                    "accuracy": 0.0,
                    "execution_time": 60.0,
                    "status": "timeout"
                }
        except Exception as e:
            print(f"[{datetime.now().strftime('%H:%M:%S')}] Error running evaluation for {model_name}: {str(e)}")
            return {
                "model": model_name,
                "provider": provider,
                "accuracy": 0.0,
                "execution_time": time.time() - start_time,
                "status": "error"
            }

        # Calculate execution time
        execution_time = time.time() - start_time
        print(f"[{datetime.now().strftime('%H:%M:%S')}] Finished evaluation for {model_name} in {execution_time:.2f}s")

        # Clean up
        os.unlink(temp_file_path)

        try:
            # Get results from the output file
            results_dir = Path("data/lighteval_results/results") / model_name.replace("/", "/")
            results_file = next(results_dir.glob("results_*.json"))
            
            with open(results_file) as f:
                results = json.load(f)
                accuracy = results["results"]["all"]["accuracy"]

            return {
                "model": model_name,
                "provider": provider,
                "accuracy": accuracy,
                "execution_time": execution_time,
                "status": "success"
            }
        except Exception as e:
            print(f"[{datetime.now().strftime('%H:%M:%S')}] Failed to parse results for {model_name} after {execution_time:.2f}s: {str(e)}")
            return {
                "model": model_name,
                "provider": provider,
                "accuracy": 0.0,
                "execution_time": execution_time,
                "status": "parse_error"
            }

    async def run(self) -> None:
        """
        Run the evaluation task asynchronously
        """
        # Start global timer
        script_start_time = time.time()
        
        # Load environment variables
        load_dotenv()

        # Models to evaluate
        models = [
            "Qwen/QwQ-32B",
            "Qwen/Qwen2.5-72B-Instruct",
            "deepseek-ai/DeepSeek-V3-0324",
            "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
        ]

        # Get providers for each model
        model_providers = get_model_providers(models)
        
        print(f"[{datetime.now().strftime('%H:%M:%S')}] Starting parallel evaluations")
        
        # Run evaluations in parallel using asyncio
        tasks = []
        for model_name, providers in model_providers:
            if providers:  # Only run if providers are available
                tasks.append(self._run_lighteval(model_name, providers[0], self.dataset_name))
        
        self.results = await asyncio.gather(*tasks)

        # Calculate total script execution time
        total_time = time.time() - script_start_time
        print(f"[{datetime.now().strftime('%H:%M:%S')}] All evaluations completed in {total_time:.2f}s")
        
        # Save results to Hub
        self._save_results_to_hub()
        
        # Mark the task as completed
        self.is_completed = True

    def get_logs(self) -> List[str]:
        """
        Get logs for this task (empty list since we don't track logs anymore)
        
        Returns:
            Empty list of logs
        """
        return []

    def is_task_completed(self) -> bool:
        """
        Check if the task is completed
        
        Returns:
            True if completed, False otherwise
        """
        return self.is_completed