File size: 18,979 Bytes
82130cb
 
a732fe2
 
843d9d3
 
 
a732fe2
843d9d3
bb7db2c
b897a48
e35ef72
bb7db2c
a732fe2
e35ef72
 
a732fe2
 
bb7db2c
c0a1e47
 
 
 
 
bb7db2c
 
 
 
 
 
 
 
 
 
 
 
c0a1e47
bb7db2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a732fe2
 
82130cb
a732fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e912278
 
 
a732fe2
 
 
 
 
 
 
 
82130cb
b897a48
82130cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34061f5
82130cb
 
 
34061f5
 
82130cb
 
 
 
 
 
 
 
 
 
 
 
34061f5
 
4332953
 
 
 
34061f5
82130cb
 
 
 
 
 
 
 
 
 
 
 
34061f5
82130cb
 
 
 
 
 
 
 
 
 
 
1fb12dc
82130cb
 
 
 
e912278
 
 
 
 
843d9d3
82130cb
 
 
 
 
 
 
 
 
 
 
 
e912278
 
 
82130cb
 
 
 
e912278
 
 
82130cb
 
 
 
e912278
 
 
 
82130cb
 
 
 
e912278
 
 
 
 
 
 
 
82130cb
 
 
 
 
 
 
 
e912278
 
 
 
 
 
 
82130cb
 
 
 
 
 
9cec719
 
 
 
82130cb
 
 
 
 
 
 
 
 
 
 
e912278
 
 
 
82130cb
 
 
 
 
 
 
 
 
 
 
 
1fb12dc
c76e6f5
bcc302b
 
 
c76e6f5
 
 
 
 
 
 
 
 
 
 
e912278
 
 
1fb12dc
 
 
 
 
 
 
 
 
 
 
34061f5
1fb12dc
34061f5
 
1fb12dc
 
 
34061f5
1fb12dc
 
 
 
e35ef72
 
 
 
 
 
 
 
82130cb
c76e6f5
e912278
 
 
 
e35ef72
 
 
 
82130cb
c76e6f5
 
 
 
 
 
 
 
 
 
 
82130cb
 
 
e912278
 
 
 
82130cb
 
843d9d3
 
 
 
 
 
 
 
 
 
 
 
e912278
 
 
843d9d3
 
 
 
 
e912278
 
 
843d9d3
 
 
 
 
 
34061f5
843d9d3
 
 
 
 
 
e912278
 
 
843d9d3
 
 
 
 
 
e912278
 
 
 
843d9d3
 
 
 
 
 
 
 
 
34061f5
 
 
843d9d3
 
 
 
 
 
3a9dec1
843d9d3
 
e912278
 
 
843d9d3
 
 
 
e912278
 
 
843d9d3
 
 
 
 
 
 
 
 
 
 
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
456
from __future__ import annotations  # For self-referencing annotations

import json
import os
import shutil
import sqlite3
import sys

from huggingface_hub import Repository
from queue import Queue
from random import sample
from threading import Thread
from typing import Dict, List, Optional, Tuple

from src.architectures import Architecture, ArchitectureRequest, LogWorker
from src.common import data_dir


class ArchitectureTestWorker(Thread):
    """
    This class is worker which takes a test request off the queue and passes
    it to an architecture for execution.  Used to multi-thread the testing process
    for speed as there is a tonne of i/o blocking waiting for the LLM
    """
    def __init__(self, work_queue: Queue, worker_name: str, trace_tags: List[str], trace_comment: str):
        Thread.__init__(self)
        self.work_queue = work_queue
        self.worker_name = worker_name
        self.trace_tags = trace_tags
        self.trace_comment = trace_comment

    def run(self):
        running: bool = True
        while running:
            arch, request = self.work_queue.get()
            try:
                if arch is None:  # None passed to signal end of test requests
                    running = False
                else:
                    print(f'{self.worker_name} running "{request.request}" through {arch}')
                    architecture = Architecture.get_architecture(arch)
                    architecture(request, trace_tags=self.trace_tags, trace_comment=self.trace_comment)
            finally:
                self.work_queue.task_done()


def batch_test(questions: List[str], architectures: List[str], trace_comment: str = "",
               trace_tags: List[str] = [], num_workers: int = 16) -> List[Tuple[str, str, str]]:
    """
    Creates a worked pool and dispatches the questions, returnin the answers per architecture, question
    :param questions: A list of the questions
    :param architectures: A list of the names of the architectures
    :param num_workers: The number of works to run
    :return: A list of Tuples of (arch_name, question, answer)
    """
    queue = Queue()

    question_record: Dict[Tuple[str, str], ArchitectureRequest] = {}
    for q in questions:
        for a in architectures:
            request = ArchitectureRequest(q)
            question_record[(a, q)] = request
            queue.put((a, request))

    for i in range(num_workers):
        worker = ArchitectureTestWorker(work_queue=queue, worker_name=f'Worker {i+1}',
                                        trace_tags=trace_tags, trace_comment=trace_comment)
        worker.daemon = True
        worker.start()
        queue.put((None, None))  # Flag to finish

    queue.join()

    # Repackage and return just the list of (arch_name, question, answer)
    return [(k[0], k[1], v.response) for k, v in question_record.items()]


class TestGenerator:
    """
    Wrapper class to hold testing questions and serve up examples
    """
    questions: List[str] = None

    @classmethod
    def load_questions(cls, reload=False) -> None:
        """
        Load the available questions from the json file.
        Default to not re-loading if already done, but allow for the option to do so
        """
        if cls.questions is not None and not reload:
            return
        question_file = os.path.join(data_dir, 'json', 'test_questions.json')
        with open(question_file, 'r') as f:
            question_json = json.load(f)
        cls.questions = question_json['questions']

    @classmethod
    def question_count(cls) -> int:
        """
        The total number of questions in the question set
        """
        cls.load_questions()
        return len(cls.questions)

    @classmethod
    def get_random_questions(cls, n: int):
        """
        Return n random questions
        """
        cls.load_questions()
        return sample(cls.questions, k=n)


class ArchitectureRequestRecord:
    """
    Representation of the test data associated with each invocation of an architecture
    """
    all: List[ArchitectureRequestRecord] = None

    class ArchStep:
        """
        Inner class to just hold this data
        """
        def __init__(self, name: str, start: int, end: int):
            self.name = name
            self.start = start
            self.end = end
            self.elapsed = end - start

    def __init__(self, arch: str, request: str, response: str, response_len: int, start: int, end: int,
                 elapsed: int, tags: List[str], test_group: Optional[str],
                 comment: str, steps: List[ArchitectureRequestRecord.ArchStep]):
        self.arch = arch
        self.request = request
        self.response = response
        self.response_len = response_len
        self.start = start
        self.end = end
        self.elapsed = elapsed
        self.tags = tags
        self.test_group = test_group
        self.comment = comment
        self.steps = steps

    @classmethod
    def from_dict(cls, test: Dict) -> ArchitectureRequestRecord:
        arch = test['architecture']
        request = test['request']['request_evolution'][0]
        response = ""
        if len(test['request']['response_evolution']) == 0:
            response_len = 0
        else:
            response_len = len(test['request']['response_evolution'][-1])
            response = test['request']['response_evolution'][-1]
        start = test['trace']['steps'][0]['start_ms']
        end = test['trace']['steps'][-1]['end_ms']
        elapsed = end - start
        tags = test['test_tags']
        test_group = None
        for tag in tags:
            if tag.startswith("TestGroup"):
                test_group = tag
        comment = test['test_comment']
        steps = []
        for s in test['trace']['steps']:
            steps.append(ArchitectureRequestRecord.ArchStep(s['name'], s['start_ms'], s['end_ms']))
        return ArchitectureRequestRecord(arch, request, response, response_len, start, end, elapsed, tags, test_group, comment, steps)

    @classmethod
    def load_all(cls, reload=False) -> None:
        """
        Load all the traces from json trace log
        """
        if cls.all is None or reload:
            records = []
            test_traces = Architecture.get_trace_records()
            for trace in test_traces:
                records.append(ArchitectureRequestRecord.from_dict(trace))

            cls.all = records


class TestGroup:
    """
    A class representing a single batch run of tests from the UI. Identified by the tag
    which was assigned from the UI when the test was run, and including summary items
    (start, end, elapse) for convenience
    """
    all: Dict[str, TestGroup] = None

    def __init__(self, test_group:str):
        self.arch_request_records: List[ArchitectureRequestRecord] = []
        self.test_group = test_group
        self.comment = None
        self.start = None
        self.end = None
        self.elapsed = None
        self.architectures = set()

    @property
    def num_archs(self) -> int:
        """
        The number of LLM Architectures which were included in this test run from the UI
        """
        return len(self.architectures)

    @property
    def num_tests(self) -> int:
        """
        The total number of Architecture tests (inferences) done in this test run from the UI
        """
        return len(self.arch_request_records)

    @property
    def num_tests_per_arch(self) -> int:
        """
        The calculated number of tests run through each architecture (simple divide as the UI
        forces each architecture to get the same number of requests)
        """
        # Should always be an even number but cast to int just in case
        return int(self.num_tests / self.num_archs)

    def arch_request_records_by_arch(self) -> Dict[List[ArchitectureRequestRecord]]:
        """
        Get all the tests ArchitectureRequestRecords grouped by the architecture.
        :return: dict keyed by the architecture name containing a list of ArchitectureRequestRecords
        detailing the tests run through that architecture.  Note - the keys are intended to be used for
        display purposes - attempting to use them to load the original architecture will be
        dependent on the availability of that architecture at look up time and changes in architecture
        config could cause that lookup to fail (i.e. the tested architecture is no longer configured).
        """
        grouped = {}
        for arr in self.arch_request_records:
            if arr.arch not in grouped:
                grouped[arr.arch] = []
            grouped[arr.arch].append(arr)
        return grouped

    def summary_stats_by_arch(self) -> List[Dict]:
        """
        Get a pack of statistics for use in the UI, detailing this TestGroup.
        :return: a list, sorted by architecture name, of statistics per architecture. Each list item
        is a dict of information (arch_name, elapsed[list of elapsed times in ms],
        response_len[list of the lengthe of the final response in characters], steps[list of the
        individual architecture steps each containing dict of name, mean_elapsed(ms)])
        """
        arch_records = self.arch_request_records_by_arch()
        arch_names = list(arch_records.keys())
        arch_names.sort()
        stats = []
        for a in arch_names:
            stat_pack = {'arch_name': a, 'elapsed': [rec.elapsed for rec in arch_records[a]],
                         'response_len': [rec.response_len for rec in arch_records[a]], 'steps': [],
                         'q_and_a': {}}
            for rec in arch_records[a]:
                stat_pack['q_and_a'][rec.request] = rec.response
            for i in range(len(arch_records[a][0].steps)):
                stat_pack['steps'].append({'step_name': arch_records[a][0].steps[i].name})
                num_recs = len(arch_records[a])
                total_elapsed = 0
                for j in range(num_recs):
                    total_elapsed += arch_records[a][j].steps[i].elapsed
                stat_pack['steps'][-1]['mean_elapsed'] = total_elapsed / num_recs
            stats.append(stat_pack)
        return stats

    def add_record(self, arr: ArchitectureRequestRecord) -> None:
        """
        Add an ArchitectureRequestRecord into this test group. Update the
        TestGroup level start, end and elapsed with the new data
        """
        if arr.test_group != self.test_group:
            raise ValueError("Attempted to group a test record into the wrong group")
        self.arch_request_records.append(arr)
        self.architectures.add(arr.arch)
        if self.comment is None:
            self.comment = arr.comment
        if self.start is None or self.start > arr.start:
            self.start = arr.start
        if self.end is None or self.end < arr.end:
            self.end = arr.end
        self.elapsed = self.end - self.start

    @classmethod
    def load_json_test_groups(cls, reload: bool = False) -> List[TestGroup]:
        """
        Load all the test groups from the local json file, reloading from the HF Hub if requested
        """
        ArchitectureRequestRecord.load_all(reload=reload)
        test_groups: Dict[str, TestGroup] = {}
        for arr in ArchitectureRequestRecord.all:
            if arr.test_group is not None:
                if arr.test_group not in test_groups:
                    test_groups[arr.test_group] = TestGroup(arr.test_group)
                test_groups[arr.test_group].add_record(arr)
        return list(test_groups.values())

    @classmethod
    def load_db_test_groups(cls) -> List[TestGroup]:
        """
        Load all the test groups from the DataBase
        """
        db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
        con = sqlite3.connect(db_file)
        cur = con.cursor()

        sql = "SELECT id, test_group from test_groups"
        cur.execute(sql)
        tg_id_names =  [(r[0], r[1]) for r in cur.fetchall()]
        test_groups: List[TestGroup] = []

        for tg_id, tg_name in tg_id_names:
            tg = TestGroup(tg_name)
            sql = f"SELECT id, arch_name, request, response, response_len, start, end, comment FROM arch_requests WHERE test_group_id={tg_id}"
            cur.execute(sql)
            arch_requests = [(r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]) for r in cur.fetchall()]
            for ar_id, ar_arch_name, ar_req, ar_resp, ar_resp_len, ar_start, ar_end, ar_comment in arch_requests:
                sql = f"SELECT name, start, end FROM arch_req_steps WHERE arch_req_id={ar_id}"
                cur.execute(sql)
                steps = [ArchitectureRequestRecord.ArchStep(r[0], r[1], r[2]) for r in cur.fetchall()]
                arch_req_record = ArchitectureRequestRecord(ar_arch_name, ar_req, ar_resp, ar_resp_len, ar_start, ar_end, (ar_end - ar_start), [tg_name], tg_name, f"(DB persisted) {ar_comment}", steps)
                tg.add_record(arch_req_record)
            test_groups.append(tg)
        return test_groups

    @classmethod
    def force_load_all(cls):
        """
        Convenience wrapper to allow a no parameter call to force the reload of the
        TestGroups without any parameters, for the LogWorker callback
        """
        cls.load_all(True)

    @classmethod
    def load_all(cls, reload: bool = False):
        """
        Load all the available TestGroups, from both the json file and the DB
        into the class variable - for efficiency do not reload unless requested
        """
        if cls.force_load_all not in LogWorker.timeout_functions:
            print("TestGroup adding forced refresh to LogWorker timeout")
            LogWorker.timeout_functions.append(TestGroup.force_load_all)

        if cls.all is None or reload:
            working_test_groups = {}

            json_tgs = cls.load_json_test_groups(reload=reload)
            for test_group in json_tgs:
                working_test_groups[test_group.test_group] = test_group

            db_tgs = cls.load_db_test_groups()
            for test_group in db_tgs:
                working_test_groups[test_group.test_group] = test_group

            cls.all = working_test_groups

    @classmethod
    def for_test_group_tag(cls, test_group_tag: str) -> TestGroup:
        """
        Get a single TestGroup based on the test_group_tag which was assigned
        when the test was run
        """
        cls.load_all()
        return cls.all[test_group_tag]


def move_test_records_to_db(hf_hub_token: str) -> None:
    """
    This is an offline utility to move the transaction logs from
    a flat file to a database. To keep things simpler transaction logs
    are initially stored into a json file as a Hugging Face dataset, but
    this can get cumbersome, so this utility will move the records
    into an sqlite database.  It can be run periodically just to move
    them across.
    """
    def download_latest_json_file(hf_hub_token: str) -> Repository:
        """
        Wipe any local version of the json file and re-downlad from the HF Hub
        """
        if os.path.exists(Architecture.trace_dir):
            shutil.rmtree(Architecture.trace_dir)
        return Repository(local_dir=Architecture.trace_dir, clone_from=Architecture.save_repo_url, token=hf_hub_token)

    def create_local_db():
        """
        Create the local database if it does not exist
        """
        db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
        con = sqlite3.connect(db_file)

        sql = "CREATE TABLE test_groups (id INTEGER PRIMARY KEY AUTOINCREMENT, test_group TEXT NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL);"
        con.execute(sql)

        sql = "CREATE TABLE arch_requests (id INTEGER PRIMARY KEY AUTOINCREMENT, arch_name TEXT NOT NULL, request TEXT NOT NULL, response TEXT NOT NULL, response_len INTEGER NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL, comment TEXT NOT NULL, test_group_id INTEGER NOT NULL, FOREIGN KEY (test_group_id) REFERENCES test_groups (id))"
        con.execute(sql)

        sql = "CREATE TABLE arch_req_steps (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL, arch_req_id INTEGER NOT NULL, FOREIGN KEY (arch_req_id) REFERENCES arch_requests(id))"
        con.execute(sql)

    def get_local_db() -> sqlite3.Connection:
        """
        Get a connection to the local database and create it if it is not already there
        """
        db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
        if not os.path.exists(db_file):
            create_local_db()
        return sqlite3.connect(db_file)

    def load_test_group_to_db(test_group: TestGroup, con: sqlite3.Connection) -> None:
        """
        Load a single TestGroup object into the DB, decomposing to the TestGroup,
        ArchitectureRequest within that and ArchitectureRequestSteps within those
        """
        cur = con.cursor()
        sql = f'SELECT count(*) from test_groups where test_group ="{test_group.test_group}"'
        cur.execute(sql)
        tg_not_in_db = cur.fetchall()[0][0] == 0
        if tg_not_in_db:
            sql = f'INSERT into test_groups (test_group, start, end) VALUES ("{test_group.test_group}", {test_group.start}, {test_group.end})'
            tg_id = con.execute(sql).lastrowid

            for arr in test_group.arch_request_records:

                sql = f'INSERT INTO arch_requests (arch_name, request, response, response_len, start, end, comment, test_group_id) VALUES ("{arr.arch}", ?, ?, {arr.response_len}, {arr.start}, {arr.end}, "{arr.comment}", {tg_id})'
                arr_id = con.execute(sql, (arr.request, arr.response)).lastrowid

                for s in arr.steps:
                    sql= f'INSERT INTO arch_req_steps (name, start, end, arch_req_id) VALUES ("{s.name}", {s.start}, {s.end}, {arr_id})'
                    con.execute(sql)
            con.commit()
        else:
            print(f"Warning TestGroup {test_group.test_group} was not added to the DB as it already existed there")

    def load_all_test_groups_to_db(con: sqlite3.Connection) -> None:
        """
        Load a list of TestGroups to the DB, one at a time
        """
        TestGroup.load_all()
        for tg in TestGroup.all.values():
            load_test_group_to_db(tg, con)

    """
    Main control flow using utility nested functions above for better structure and readability
    """
    download_latest_json_file(hf_hub_token)
    conn = get_local_db()
    load_all_test_groups_to_db(conn)
    Architecture.wipe_trace(hf_hub_token)
    print("REMINDER: need to commit the local sqlite file to make it available to the server")


if __name__ == "__main__":
    # Expected to only be directly called for the json to db transfer - arg should be the HF token
    hf_token = sys.argv[1]
    move_test_records_to_db(hf_token)