Commit
eb8e45b
1 Parent(s): 96f572b

update evals

Browse files
Files changed (1) hide show
  1. src/leaderboard/read_evals.py +55 -101
src/leaderboard/read_evals.py CHANGED
@@ -1,10 +1,8 @@
1
  import glob
2
  import json
3
- import math
4
  import os
5
  from dataclasses import dataclass
6
 
7
- import dateutil
8
  import numpy as np
9
 
10
  from src.display.formatting import make_clickable_model
@@ -16,37 +14,36 @@ from src.submission.check_validity import is_model_on_hub
16
  class EvalResult:
17
  """Represents one full evaluation. Built from a combination of the result and request file for a given run.
18
  """
19
- eval_name: str # org_model_precision (uid)
20
- full_model: str # org/model (path on hub)
21
- org: str
22
  model: str
23
- revision: str # commit hash, "" if main
24
  results: dict
 
25
  precision: Precision = Precision.Unknown
26
- model_type: ModelType = ModelType.Unknown # Pretrained, fine tuned, ...
27
- weight_type: WeightType = WeightType.Original # Original or Adapter
28
- architecture: str = "Unknown"
29
  license: str = "?"
30
  likes: int = 0
31
  num_params: int = 0
32
- date: str = "" # submission date of request file
33
  still_on_hub: bool = False
34
 
35
  @classmethod
36
- def init_from_json_file(self, json_filepath):
37
  """Inits the result from the specific model result file"""
38
  with open(json_filepath) as fp:
39
  data = json.load(fp)
40
 
41
- config = data.get("config")
42
 
43
  # Precision
44
- precision = Precision.from_str(config.get("model_dtype"))
45
 
46
  # Get model and org
47
- org_and_model = config.get("model_name", config.get("model_args", None))
48
- org_and_model = org_and_model.split("/", 1)
49
-
50
  if len(org_and_model) == 1:
51
  org = None
52
  model = org_and_model[0]
@@ -57,59 +54,50 @@ class EvalResult:
57
  result_key = f"{org}_{model}_{precision.value.name}"
58
  full_model = "/".join(org_and_model)
59
 
60
- still_on_hub, _, model_config = is_model_on_hub(
61
- full_model, config.get("model_sha", "main"), trust_remote_code=True, test_tokenizer=False
62
- )
63
- architecture = "?"
64
- if model_config is not None:
65
- architectures = getattr(model_config, "architectures", None)
66
- if architectures:
67
- architecture = ";".join(architectures)
68
-
69
- # Extract results available in this file (some results are split in several files)
70
- results = {}
71
- for task in Tasks:
72
- task = task.value
73
-
74
- # We average all scores of a given metric (not all metrics are present in all files)
75
- accs = np.array([v.get(task.metric, None) for k, v in data["results"].items() if task.benchmark == k])
76
- if accs.size == 0 or any([acc is None for acc in accs]):
77
- continue
78
-
79
- mean_acc = np.mean(accs) * 100.0
80
- results[task.benchmark] = mean_acc
81
 
82
- return self(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  eval_name=result_key,
84
  full_model=full_model,
85
  org=org,
86
  model=model,
87
- results=results,
88
- precision=precision,
89
- revision= config.get("model_sha", ""),
 
90
  still_on_hub=still_on_hub,
91
- architecture=architecture
 
 
 
 
 
 
92
  )
93
 
94
- def update_with_request_file(self, requests_path):
95
- """Finds the relevant request file for the current model and updates info with it"""
96
- request_file = get_request_file_for_model(requests_path, self.full_model, self.precision.value.name)
97
-
98
- try:
99
- with open(request_file, "r") as f:
100
- request = json.load(f)
101
- self.model_type = ModelType.from_str(request.get("model_type", ""))
102
- self.weight_type = WeightType[request.get("weight_type", "Original")]
103
- self.license = request.get("license", "?")
104
- self.likes = request.get("likes", 0)
105
- self.num_params = request.get("params", 0)
106
- self.date = request.get("submitted_time", "")
107
- except Exception:
108
- print(f"Could not find request file for {self.org}/{self.model} with precision {self.precision.value.name}")
109
-
110
  def to_dict(self):
111
  """Converts the Eval Result to a dict compatible with our dataframe display"""
112
- average = sum([v for v in self.results.values() if v is not None]) / len(Tasks)
113
  data_dict = {
114
  "eval_name": self.eval_name, # not a column, just a save name,
115
  AutoEvalColumn.precision.name: self.precision.value.name,
@@ -119,7 +107,7 @@ class EvalResult:
119
  AutoEvalColumn.architecture.name: self.architecture,
120
  AutoEvalColumn.model.name: make_clickable_model(self.full_model),
121
  AutoEvalColumn.revision.name: self.revision,
122
- AutoEvalColumn.average.name: average,
123
  AutoEvalColumn.license.name: self.license,
124
  AutoEvalColumn.likes.name: self.likes,
125
  AutoEvalColumn.params.name: self.num_params,
@@ -127,68 +115,34 @@ class EvalResult:
127
  }
128
 
129
  for task in Tasks:
130
- data_dict[task.value.col_name] = self.results[task.value.benchmark]
 
131
 
132
  return data_dict
133
 
134
 
135
- def get_request_file_for_model(requests_path, model_name, precision):
136
- """Selects the correct request file for a given model. Only keeps runs tagged as FINISHED"""
137
- request_files = os.path.join(
138
- requests_path,
139
- f"{model_name}_eval_request_*.json",
140
- )
141
- request_files = glob.glob(request_files)
142
-
143
- # Select correct request file (precision)
144
- request_file = ""
145
- request_files = sorted(request_files, reverse=True)
146
- for tmp_request_file in request_files:
147
- with open(tmp_request_file, "r") as f:
148
- req_content = json.load(f)
149
- if (
150
- req_content["status"] in ["FINISHED"]
151
- and req_content["precision"] == precision.split(".")[-1]
152
- ):
153
- request_file = tmp_request_file
154
- return request_file
155
-
156
-
157
  def get_raw_eval_results(results_path: str, requests_path: str) -> list[EvalResult]:
158
  """From the path of the results folder root, extract all needed info for results"""
159
  model_result_filepaths = []
160
 
161
  for root, _, files in os.walk(results_path):
162
  # We should only have json files in model results
163
- if len(files) == 0 or any([not f.endswith(".json") for f in files]):
164
- continue
165
-
166
- # Sort the files by date
167
- try:
168
- files.sort(key=lambda x: x.removesuffix(".json").removeprefix("results_")[:-7])
169
- except dateutil.parser._parser.ParserError:
170
- files = [files[-1]]
171
-
172
  for file in files:
173
- model_result_filepaths.append(os.path.join(root, file))
 
174
 
175
  eval_results = {}
176
  for model_result_filepath in model_result_filepaths:
177
  # Creation of result
178
  eval_result = EvalResult.init_from_json_file(model_result_filepath)
179
- eval_result.update_with_request_file(requests_path)
180
-
181
- # Store results of same eval together
182
  eval_name = eval_result.eval_name
183
- if eval_name in eval_results.keys():
184
- eval_results[eval_name].results.update({k: v for k, v in eval_result.results.items() if v is not None})
185
- else:
186
- eval_results[eval_name] = eval_result
187
 
188
  results = []
189
  for v in eval_results.values():
190
  try:
191
- v.to_dict() # we test if the dict version is complete
192
  results.append(v)
193
  except KeyError: # not all eval values present
194
  continue
 
1
  import glob
2
  import json
 
3
  import os
4
  from dataclasses import dataclass
5
 
 
6
  import numpy as np
7
 
8
  from src.display.formatting import make_clickable_model
 
14
  class EvalResult:
15
  """Represents one full evaluation. Built from a combination of the result and request file for a given run.
16
  """
17
+ eval_name: str # org_model_precision (uid)
18
+ full_model: str # org/model (path on hub)
19
+ org: str
20
  model: str
21
+ revision: str # commit hash, "" if main
22
  results: dict
23
+ average_accuracy: float
24
  precision: Precision = Precision.Unknown
25
+ model_type: ModelType = ModelType.Unknown # Pretrained, fine tuned, ...
26
+ weight_type: WeightType = WeightType.Original # Original or Adapter
27
+ architecture: str = "Unknown"
28
  license: str = "?"
29
  likes: int = 0
30
  num_params: int = 0
31
+ date: str = "" # submission date of request file
32
  still_on_hub: bool = False
33
 
34
  @classmethod
35
+ def init_from_json_file(cls, json_filepath):
36
  """Inits the result from the specific model result file"""
37
  with open(json_filepath) as fp:
38
  data = json.load(fp)
39
 
40
+ config = data.get("config", {})
41
 
42
  # Precision
43
+ precision = Precision.from_str(config.get("model_dtype", "Unknown"))
44
 
45
  # Get model and org
46
+ org_and_model = config.get("model_name", "").split("/", 1)
 
 
47
  if len(org_and_model) == 1:
48
  org = None
49
  model = org_and_model[0]
 
54
  result_key = f"{org}_{model}_{precision.value.name}"
55
  full_model = "/".join(org_and_model)
56
 
57
+ results_data = data.get("results", {})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # Extract per-subject accuracies
60
+ per_subject_results = {}
61
+ for task in Tasks:
62
+ subject = task.value.benchmark
63
+ accuracy = results_data.get(subject, None)
64
+ if accuracy is not None:
65
+ per_subject_results[subject] = accuracy
66
+
67
+ average_accuracy = results_data.get('average', None)
68
+
69
+ # Set other fields from config
70
+ model_type = ModelType.from_str(config.get("model_type", ""))
71
+ weight_type = WeightType[config.get("weight_type", "Original")]
72
+ license = config.get("license", "?")
73
+ likes = config.get("likes", 0)
74
+ num_params = config.get("params", 0)
75
+ date = config.get("submitted_time", "")
76
+ still_on_hub = config.get("still_on_hub", True)
77
+ architecture = config.get("architecture", "Unknown")
78
+
79
+ # Create EvalResult instance
80
+ return cls(
81
  eval_name=result_key,
82
  full_model=full_model,
83
  org=org,
84
  model=model,
85
+ results=per_subject_results,
86
+ average_accuracy=average_accuracy,
87
+ precision=precision,
88
+ revision=config.get("model_sha", ""),
89
  still_on_hub=still_on_hub,
90
+ architecture=architecture,
91
+ model_type=model_type,
92
+ weight_type=weight_type,
93
+ license=license,
94
+ likes=likes,
95
+ num_params=num_params,
96
+ date=date,
97
  )
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def to_dict(self):
100
  """Converts the Eval Result to a dict compatible with our dataframe display"""
 
101
  data_dict = {
102
  "eval_name": self.eval_name, # not a column, just a save name,
103
  AutoEvalColumn.precision.name: self.precision.value.name,
 
107
  AutoEvalColumn.architecture.name: self.architecture,
108
  AutoEvalColumn.model.name: make_clickable_model(self.full_model),
109
  AutoEvalColumn.revision.name: self.revision,
110
+ AutoEvalColumn.average.name: self.average_accuracy,
111
  AutoEvalColumn.license.name: self.license,
112
  AutoEvalColumn.likes.name: self.likes,
113
  AutoEvalColumn.params.name: self.num_params,
 
115
  }
116
 
117
  for task in Tasks:
118
+ subject = task.value.benchmark
119
+ data_dict[task.value.col_name] = self.results.get(subject, None)
120
 
121
  return data_dict
122
 
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  def get_raw_eval_results(results_path: str, requests_path: str) -> list[EvalResult]:
125
  """From the path of the results folder root, extract all needed info for results"""
126
  model_result_filepaths = []
127
 
128
  for root, _, files in os.walk(results_path):
129
  # We should only have json files in model results
 
 
 
 
 
 
 
 
 
130
  for file in files:
131
+ if file.endswith(".json"):
132
+ model_result_filepaths.append(os.path.join(root, file))
133
 
134
  eval_results = {}
135
  for model_result_filepath in model_result_filepaths:
136
  # Creation of result
137
  eval_result = EvalResult.init_from_json_file(model_result_filepath)
138
+ # Store results
 
 
139
  eval_name = eval_result.eval_name
140
+ eval_results[eval_name] = eval_result
 
 
 
141
 
142
  results = []
143
  for v in eval_results.values():
144
  try:
145
+ v.to_dict() # we test if the dict version is complete
146
  results.append(v)
147
  except KeyError: # not all eval values present
148
  continue