alfraser commited on
Commit
e912278
·
1 Parent(s): 7b8cf3a

Added comments throughout

Browse files
Files changed (1) hide show
  1. src/testing.py +67 -1
src/testing.py CHANGED
@@ -35,6 +35,9 @@ class TestGenerator:
35
 
36
  @classmethod
37
  def question_count(cls) -> int:
 
 
 
38
  cls.load_questions()
39
  return len(cls.questions)
40
 
@@ -109,6 +112,11 @@ class ArchitectureRequestRecord:
109
 
110
 
111
  class TestGroup:
 
 
 
 
 
112
  all: Dict[str, TestGroup] = None
113
 
114
  def __init__(self, test_group:str):
@@ -122,18 +130,36 @@ class TestGroup:
122
 
123
  @property
124
  def num_archs(self) -> int:
 
 
 
125
  return len(self.architectures)
126
 
127
  @property
128
  def num_tests(self) -> int:
 
 
 
129
  return len(self.arch_request_records)
130
 
131
  @property
132
  def num_tests_per_arch(self) -> int:
 
 
 
 
133
  # Should always be an even number but cast to int just in case
134
  return int(self.num_tests / self.num_archs)
135
 
136
  def arch_request_records_by_arch(self) -> Dict[List[ArchitectureRequestRecord]]:
 
 
 
 
 
 
 
 
137
  grouped = {}
138
  for arr in self.arch_request_records:
139
  if arr.arch not in grouped:
@@ -142,6 +168,13 @@ class TestGroup:
142
  return grouped
143
 
144
  def summary_stats_by_arch(self) -> List[Dict]:
 
 
 
 
 
 
 
145
  arch_records = self.arch_request_records_by_arch()
146
  arch_names = list(arch_records.keys())
147
  arch_names.sort()
@@ -160,6 +193,10 @@ class TestGroup:
160
  return stats
161
 
162
  def add_record(self, arr: ArchitectureRequestRecord) -> None:
 
 
 
 
163
  if arr.test_group != self.test_group:
164
  raise ValueError("Attempted to group a test record into the wrong group")
165
  self.arch_request_records.append(arr)
@@ -174,6 +211,9 @@ class TestGroup:
174
 
175
  @classmethod
176
  def load_db_records(cls) -> List[TestGroup]:
 
 
 
177
  db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
178
  con = sqlite3.connect(db_file)
179
  cur = con.cursor()
@@ -199,6 +239,10 @@ class TestGroup:
199
 
200
  @classmethod
201
  def load_all(cls, reload=False):
 
 
 
 
202
  if cls.all is None or reload:
203
  ArchitectureRequestRecord.load_all(reload=reload)
204
  records = {}
@@ -214,6 +258,10 @@ class TestGroup:
214
 
215
  @classmethod
216
  def for_test_group_tag(cls, test_group_tag: str) -> TestGroup:
 
 
 
 
217
  cls.load_all()
218
  return cls.all[test_group_tag]
219
 
@@ -228,11 +276,17 @@ def move_test_records_to_db(hf_hub_token: str) -> None:
228
  them across.
229
  """
230
  def download_latest_json_file(hf_hub_token: str) -> Repository:
 
 
 
231
  if os.path.exists(Architecture.trace_dir):
232
  shutil.rmtree(Architecture.trace_dir)
233
  return Repository(local_dir=Architecture.trace_dir, clone_from=Architecture.save_repo_url, token=hf_hub_token)
234
 
235
  def create_local_db():
 
 
 
236
  db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
237
  con = sqlite3.connect(db_file)
238
 
@@ -246,12 +300,19 @@ def move_test_records_to_db(hf_hub_token: str) -> None:
246
  con.execute(sql)
247
 
248
  def get_local_db() -> sqlite3.Connection:
 
 
 
249
  db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
250
  if not os.path.exists(db_file):
251
  create_local_db()
252
  return sqlite3.connect(db_file)
253
 
254
  def load_test_group_to_db(test_group: TestGroup, con: sqlite3.Connection) -> None:
 
 
 
 
255
  cur = con.cursor()
256
  sql = f'SELECT count(*) from test_groups where test_group ="{test_group.test_group}"'
257
  cur.execute(sql)
@@ -272,11 +333,16 @@ def move_test_records_to_db(hf_hub_token: str) -> None:
272
  print(f"Warning TestGroup {tg_not_in_db} was not added to the DB as it already existed there")
273
 
274
  def load_all_test_groups_to_db(con: sqlite3.Connection) -> None:
 
 
 
275
  TestGroup.load_all()
276
  for tg in TestGroup.all.values():
277
  load_test_group_to_db(tg, con)
278
 
279
- # Main control flow using utility local functions above for better structure
 
 
280
  download_latest_json_file(hf_hub_token)
281
  conn = get_local_db()
282
  load_all_test_groups_to_db(conn)
 
35
 
36
  @classmethod
37
  def question_count(cls) -> int:
38
+ """
39
+ The total number of questions in the question set
40
+ """
41
  cls.load_questions()
42
  return len(cls.questions)
43
 
 
112
 
113
 
114
  class TestGroup:
115
+ """
116
+ A class representing a single batch run of tests from the UI. Identified by the tag
117
+ which was assigned from the UI when the test was run, and including summary items
118
+ (start, end, elapse) for convenience
119
+ """
120
  all: Dict[str, TestGroup] = None
121
 
122
  def __init__(self, test_group:str):
 
130
 
131
  @property
132
  def num_archs(self) -> int:
133
+ """
134
+ The number of LLM Architectures which were included in this test run from the UI
135
+ """
136
  return len(self.architectures)
137
 
138
  @property
139
  def num_tests(self) -> int:
140
+ """
141
+ The total number of Architecture tests (inferences) done in this test run from the UI
142
+ """
143
  return len(self.arch_request_records)
144
 
145
  @property
146
  def num_tests_per_arch(self) -> int:
147
+ """
148
+ The calculated number of tests run through each architecture (simple divide as the UI
149
+ forces each architecture to get the same number of requests)
150
+ """
151
  # Should always be an even number but cast to int just in case
152
  return int(self.num_tests / self.num_archs)
153
 
154
  def arch_request_records_by_arch(self) -> Dict[List[ArchitectureRequestRecord]]:
155
+ """
156
+ Get all the tests ArchitectureRequestRecords grouped by the architecture.
157
+ :return: dict keyed by the architecture name containing a list of ArchitectureRequestRecords
158
+ detailing the tests run through that architecture. Note - the keys are intended to be used for
159
+ display purposes - attempting to use them to load the original architecture will be
160
+ dependent on the availability of that architecture at look up time and changes in architecture
161
+ config could cause that lookup to fail (i.e. the tested architecture is no longer configured).
162
+ """
163
  grouped = {}
164
  for arr in self.arch_request_records:
165
  if arr.arch not in grouped:
 
168
  return grouped
169
 
170
  def summary_stats_by_arch(self) -> List[Dict]:
171
+ """
172
+ Get a pack of statistics for use in the UI, detailing this TestGroup.
173
+ :return: a list, sorted by architecture name, of statistics per architecture. Each list item
174
+ is a dict of information (arch_name, elapsed[list of elapsed times in ms],
175
+ response_len[list of the lengthe of the final response in characters], steps[list of the
176
+ individual architecture steps each containing dict of name, mean_elapsed(ms)])
177
+ """
178
  arch_records = self.arch_request_records_by_arch()
179
  arch_names = list(arch_records.keys())
180
  arch_names.sort()
 
193
  return stats
194
 
195
  def add_record(self, arr: ArchitectureRequestRecord) -> None:
196
+ """
197
+ Add an ArchitectureRequestRecord into this test group. Update the
198
+ TestGroup level start, end and elapsed with the new data
199
+ """
200
  if arr.test_group != self.test_group:
201
  raise ValueError("Attempted to group a test record into the wrong group")
202
  self.arch_request_records.append(arr)
 
211
 
212
  @classmethod
213
  def load_db_records(cls) -> List[TestGroup]:
214
+ """
215
+ Load all the test groups from the DataBase
216
+ """
217
  db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
218
  con = sqlite3.connect(db_file)
219
  cur = con.cursor()
 
239
 
240
  @classmethod
241
  def load_all(cls, reload=False):
242
+ """
243
+ Load all the available TestGroups, from both the json file and the DB
244
+ into the class variable - for efficiency do not reload unless requested
245
+ """
246
  if cls.all is None or reload:
247
  ArchitectureRequestRecord.load_all(reload=reload)
248
  records = {}
 
258
 
259
  @classmethod
260
  def for_test_group_tag(cls, test_group_tag: str) -> TestGroup:
261
+ """
262
+ Get a single TestGroup based on the test_group_tag which was assigned
263
+ when the test was run
264
+ """
265
  cls.load_all()
266
  return cls.all[test_group_tag]
267
 
 
276
  them across.
277
  """
278
  def download_latest_json_file(hf_hub_token: str) -> Repository:
279
+ """
280
+ Wipe any local version of the json file and re-downlad from the HF Hub
281
+ """
282
  if os.path.exists(Architecture.trace_dir):
283
  shutil.rmtree(Architecture.trace_dir)
284
  return Repository(local_dir=Architecture.trace_dir, clone_from=Architecture.save_repo_url, token=hf_hub_token)
285
 
286
  def create_local_db():
287
+ """
288
+ Create the local database if it does not exist
289
+ """
290
  db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
291
  con = sqlite3.connect(db_file)
292
 
 
300
  con.execute(sql)
301
 
302
  def get_local_db() -> sqlite3.Connection:
303
+ """
304
+ Get a connection to the local database and create it if it is not already there
305
+ """
306
  db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
307
  if not os.path.exists(db_file):
308
  create_local_db()
309
  return sqlite3.connect(db_file)
310
 
311
  def load_test_group_to_db(test_group: TestGroup, con: sqlite3.Connection) -> None:
312
+ """
313
+ Load a single TestGroup object into the DB, decomposing to the TestGroup,
314
+ ArchitectureRequest within that and ArchitectureRequestSteps within those
315
+ """
316
  cur = con.cursor()
317
  sql = f'SELECT count(*) from test_groups where test_group ="{test_group.test_group}"'
318
  cur.execute(sql)
 
333
  print(f"Warning TestGroup {tg_not_in_db} was not added to the DB as it already existed there")
334
 
335
  def load_all_test_groups_to_db(con: sqlite3.Connection) -> None:
336
+ """
337
+ Load a list of TestGroups to the DB, one at a time
338
+ """
339
  TestGroup.load_all()
340
  for tg in TestGroup.all.values():
341
  load_test_group_to_db(tg, con)
342
 
343
+ """
344
+ Main control flow using utility nested functions above for better structure and readability
345
+ """
346
  download_latest_json_file(hf_hub_token)
347
  conn = get_local_db()
348
  load_all_test_groups_to_db(conn)