rodrigomasini commited on
Commit
f1e655e
1 Parent(s): 38a6e1d

Delete backend-cli.py

Browse files
Files changed (1) hide show
  1. backend-cli.py +0 -288
backend-cli.py DELETED
@@ -1,288 +0,0 @@
1
- #!/usr/bin/env python
2
-
3
- import os
4
- import json
5
-
6
- import socket
7
- import random
8
- from datetime import datetime
9
-
10
- from src.backend.run_eval_suite import run_evaluation
11
- from src.backend.manage_requests import check_completed_evals, get_eval_requests, set_eval_request
12
- from src.backend.sort_queue import sort_models_by_priority
13
- from src.backend.envs import Tasks, EVAL_REQUESTS_PATH_BACKEND, EVAL_RESULTS_PATH_BACKEND, DEVICE, LIMIT, Task
14
-
15
- from src.backend.manage_requests import EvalRequest
16
- from src.leaderboard.read_evals import EvalResult
17
-
18
- from src.envs import QUEUE_REPO, RESULTS_REPO, API
19
- from src.utils import my_snapshot_download
20
-
21
- from src.leaderboard.read_evals import get_raw_eval_results
22
-
23
- from typing import Optional
24
-
25
- import time
26
-
27
- import logging
28
- import pprint
29
-
30
-
31
- def my_set_eval_request(api, eval_request, set_to_status, hf_repo, local_dir):
32
- for i in range(10):
33
- try:
34
- set_eval_request(api=api, eval_request=eval_request, set_to_status=set_to_status, hf_repo=hf_repo, local_dir=local_dir)
35
- return
36
- except Exception:
37
- time.sleep(60)
38
- return
39
-
40
-
41
- logging.getLogger("openai").setLevel(logging.WARNING)
42
-
43
- logging.basicConfig(level=logging.ERROR)
44
- pp = pprint.PrettyPrinter(width=80)
45
-
46
- PENDING_STATUS = "PENDING"
47
- RUNNING_STATUS = "RUNNING"
48
- FINISHED_STATUS = "FINISHED"
49
- FAILED_STATUS = "FAILED"
50
-
51
- TASKS_HARNESS = [task.value for task in Tasks]
52
-
53
-
54
- my_snapshot_download(repo_id=RESULTS_REPO, revision="main", local_dir=EVAL_RESULTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
55
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
56
-
57
-
58
- def sanity_checks():
59
- print(f'Device: {DEVICE}')
60
-
61
- # pull the eval dataset from the hub and parse any eval requests
62
- # check completed evals and set them to finished
63
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
64
- check_completed_evals(api=API, checked_status=RUNNING_STATUS, completed_status=FINISHED_STATUS,
65
- failed_status=FAILED_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND,
66
- hf_repo_results=RESULTS_REPO, local_dir_results=EVAL_RESULTS_PATH_BACKEND)
67
- return
68
-
69
-
70
- def request_to_result_name(request: EvalRequest) -> str:
71
- # Request: EvalRequest(model='meta-llama/Llama-2-13b-hf', private=False, status='FINISHED',
72
- # json_filepath='./eval-queue-bk/meta-llama/Llama-2-13b-hf_eval_request_False_False_False.json',
73
- # weight_type='Original', model_type='pretrained', precision='float32', base_model='', revision='main',
74
- # submitted_time='2023-09-09T10:52:17Z', likes=389, params=13.016, license='?')
75
- #
76
- # EvalResult(eval_name='meta-llama_Llama-2-13b-hf_float32', full_model='meta-llama/Llama-2-13b-hf',
77
- # org='meta-llama', model='Llama-2-13b-hf', revision='main',
78
- # results={'nq_open': 33.739612188365655, 'triviaqa': 74.12505572893447},
79
- # precision=<Precision.float32: ModelDetails(name='float32', symbol='')>,
80
- # model_type=<ModelType.PT: ModelDetails(name='pretrained', symbol='🟢')>,
81
- # weight_type=<WeightType.Original: ModelDetails(name='Original', symbol='')>,
82
- # architecture='LlamaForCausalLM', license='?', likes=389, num_params=13.016, date='2023-09-09T10:52:17Z', still_on_hub=True)
83
- #
84
- org_and_model = request.model.split("/", 1)
85
- if len(org_and_model) == 1:
86
- model = org_and_model[0]
87
- res = f"{model}_{request.precision}"
88
- else:
89
- org = org_and_model[0]
90
- model = org_and_model[1]
91
- res = f"{org}_{model}_{request.precision}"
92
- return res
93
-
94
-
95
- def process_evaluation(task: Task, eval_request: EvalRequest) -> dict:
96
- batch_size = "auto"
97
-
98
- try:
99
- results = run_evaluation(eval_request=eval_request, task_names=[task.benchmark], num_fewshot=task.num_fewshot,
100
- batch_size=batch_size, device=DEVICE, use_cache=None, limit=LIMIT)
101
- except RuntimeError as e:
102
- if "No executable batch size found" in str(e):
103
- batch_size = 1
104
- results = run_evaluation(eval_request=eval_request, task_names=[task.benchmark], num_fewshot=task.num_fewshot,
105
- batch_size=batch_size, device=DEVICE, use_cache=None, limit=LIMIT)
106
- else:
107
- raise
108
-
109
- print('RESULTS', results)
110
-
111
- dumped = json.dumps(results, indent=2, default=lambda o: '<not serializable>')
112
- print(dumped)
113
-
114
- output_path = os.path.join(EVAL_RESULTS_PATH_BACKEND, *eval_request.model.split("/"), f"results_{datetime.now()}.json")
115
- os.makedirs(os.path.dirname(output_path), exist_ok=True)
116
- with open(output_path, "w") as f:
117
- f.write(dumped)
118
-
119
- my_snapshot_download(repo_id=RESULTS_REPO, revision="main", local_dir=EVAL_RESULTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
120
- API.upload_file(path_or_fileobj=output_path, path_in_repo=f"{eval_request.model}/results_{datetime.now()}.json",
121
- repo_id=RESULTS_REPO, repo_type="dataset")
122
- return results
123
-
124
-
125
- def process_finished_requests(thr: int) -> bool:
126
- sanity_checks()
127
-
128
- current_finished_status = [FINISHED_STATUS, FAILED_STATUS]
129
-
130
- # Get all eval request that are FINISHED, if you want to run other evals, change this parameter
131
- eval_requests: list[EvalRequest] = get_eval_requests(job_status=current_finished_status, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
132
- # Sort the evals by priority (first submitted, first run)
133
- eval_requests: list[EvalRequest] = sort_models_by_priority(api=API, models=eval_requests)
134
-
135
- random.shuffle(eval_requests)
136
-
137
- eval_results: list[EvalResult] = get_raw_eval_results(EVAL_RESULTS_PATH_BACKEND, EVAL_REQUESTS_PATH_BACKEND, True)
138
-
139
- result_name_to_request = {request_to_result_name(r): r for r in eval_requests}
140
- result_name_to_result = {r.eval_name: r for r in eval_results}
141
-
142
- for eval_request in eval_requests:
143
- if eval_request.likes >= thr:
144
- result_name: str = request_to_result_name(eval_request)
145
-
146
- # Check the corresponding result
147
- eval_result: Optional[EvalResult] = result_name_to_result[result_name] if result_name in result_name_to_result else None
148
-
149
- breakpoint()
150
-
151
- task_lst = TASKS_HARNESS.copy()
152
- random.shuffle(task_lst)
153
-
154
- # Iterate over tasks and, if we do not have results for a task, run the relevant evaluations
155
- for task in task_lst:
156
- task_name = task.benchmark
157
-
158
- if eval_result is None or task_name not in eval_result.results:
159
- eval_request: EvalRequest = result_name_to_request[result_name]
160
-
161
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
162
- my_set_eval_request(api=API, eval_request=eval_request, set_to_status=RUNNING_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
163
-
164
- results = process_evaluation(task, eval_request)
165
-
166
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
167
- my_set_eval_request(api=API, eval_request=eval_request, set_to_status=FINISHED_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
168
-
169
- return True
170
-
171
- return False
172
-
173
-
174
- def maybe_refresh_results(thr: int) -> bool:
175
- sanity_checks()
176
-
177
- current_finished_status = [PENDING_STATUS, FINISHED_STATUS, FAILED_STATUS]
178
-
179
- # Get all eval request that are FINISHED, if you want to run other evals, change this parameter
180
- eval_requests: list[EvalRequest] = get_eval_requests(job_status=current_finished_status, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
181
- # Sort the evals by priority (first submitted, first run)
182
- eval_requests: list[EvalRequest] = sort_models_by_priority(api=API, models=eval_requests)
183
-
184
- random.shuffle(eval_requests)
185
-
186
- eval_results: list[EvalResult] = get_raw_eval_results(EVAL_RESULTS_PATH_BACKEND, EVAL_REQUESTS_PATH_BACKEND, True)
187
-
188
- result_name_to_request = {request_to_result_name(r): r for r in eval_requests}
189
- result_name_to_result = {r.eval_name: r for r in eval_results}
190
-
191
- for eval_request in eval_requests:
192
- if eval_request.likes >= thr:
193
- result_name: str = request_to_result_name(eval_request)
194
-
195
- # Check the corresponding result
196
- eval_result: Optional[EvalResult] = result_name_to_result[result_name] if result_name in result_name_to_result else None
197
-
198
- breakpoint()
199
-
200
- task_lst = TASKS_HARNESS.copy()
201
- random.shuffle(task_lst)
202
-
203
- # Iterate over tasks and, if we do not have results for a task, run the relevant evaluations
204
- for task in task_lst:
205
- task_name = task.benchmark
206
-
207
- if (eval_result is None or
208
- task_name not in eval_result.results or
209
- 'nq' in task_name or 'trivia' in task_name or 'tqa' in task_name or 'self' in task_name):
210
- eval_request: EvalRequest = result_name_to_request[result_name]
211
-
212
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
213
- my_set_eval_request(api=API, eval_request=eval_request, set_to_status=RUNNING_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
214
-
215
- results = process_evaluation(task, eval_request)
216
-
217
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
218
- my_set_eval_request(api=API, eval_request=eval_request, set_to_status=FINISHED_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
219
-
220
- return True
221
-
222
-
223
- return False
224
-
225
-
226
- def process_pending_requests() -> bool:
227
- sanity_checks()
228
-
229
- current_pending_status = [PENDING_STATUS]
230
-
231
- # Get all eval request that are PENDING, if you want to run other evals, change this parameter
232
- eval_requests = get_eval_requests(job_status=current_pending_status, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
233
- # Sort the evals by priority (first submitted, first run)
234
- eval_requests = sort_models_by_priority(api=API, models=eval_requests)
235
-
236
- random.shuffle(eval_requests)
237
-
238
- print(f"Found {len(eval_requests)} {','.join(current_pending_status)} eval requests")
239
-
240
- if len(eval_requests) == 0:
241
- return False
242
-
243
- eval_request = eval_requests[0]
244
- pp.pprint(eval_request)
245
-
246
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
247
- my_set_eval_request(api=API, eval_request=eval_request, set_to_status=RUNNING_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
248
-
249
- task_lst = TASKS_HARNESS.copy()
250
- random.shuffle(task_lst)
251
-
252
- for task in task_lst:
253
- results = process_evaluation(task, eval_request)
254
-
255
- my_snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
256
- my_set_eval_request(api=API, eval_request=eval_request, set_to_status=FINISHED_STATUS, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)
257
-
258
- return True
259
-
260
-
261
- if __name__ == "__main__":
262
- wait = True
263
-
264
- if socket.gethostname() in {'hamburg', 'neuromancer'} or os.path.isdir("/home/pminervi"):
265
- wait = False
266
-
267
- if wait:
268
- time.sleep(60 * random.randint(5, 10))
269
-
270
- res = False
271
-
272
- if random.randint(0, 1) == 0:
273
- res = process_pending_requests()
274
- time.sleep(60)
275
-
276
- if res is False:
277
- if random.randint(0, 1) == 0:
278
- res = maybe_refresh_results(100)
279
- else:
280
- res = process_finished_requests(100)
281
-
282
- time.sleep(60)
283
-
284
- if res is False:
285
- if random.randint(0, 1) == 0:
286
- res = maybe_refresh_results(0)
287
- else:
288
- res = process_finished_requests(0)