• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15import sys
16import re
17import json
18import os
19import time
20import openpyxl as opx
21
22
23def parse_arguments():
24    print(sys.argv)
25    me_report_path = sys.argv[1]
26    log_path = sys.argv[2]
27    n_iter = sys.argv[3]
28    out = sys.argv[4]
29    assert n_iter.isdigit()
30    return me_report_path, log_path, int(n_iter), out
31
32
33def extract_by_keyword(doc, keyword, pattern):
34    rst = []
35    for i, s in enumerate(doc):
36        if keyword in s:
37            p = re.findall(pattern, s)
38            print("L%d: extracted %s from '%s'" % (i, p, s.strip()))
39            rst.extend(p)
40    return rst
41
42
43def process_log(fname, log_path, n_iter, keyword, pattern):
44    rnt = {}
45    for i in range(1, 1+n_iter):
46        fname_path = os.path.join(log_path, fname % i)
47        with open(fname_path) as f:
48            print("\nLoading %s" % fname_path)
49            rst = extract_by_keyword(f, keyword, pattern)
50        rnt[fname % i] = rst
51    return rnt
52
53
54def summarize(func):
55    def wrapper(*args, **kwargs):
56        log = func(*args, **kwargs)
57        times = list(log.items())
58        times.sort(key=lambda x: x[1])
59        min_file, min_time = times[0]
60        avg = sum(map(lambda x: x[1], times)) / len(times)
61        log["min_time"] = min_time
62        log["min_file"] = min_file
63        log["avg_time"] = avg
64        return log
65    return wrapper
66
67
68@summarize
69def process_bert_log(log_path, n_iter):
70    fname = "bert%d.log"
71    total = process_log(fname, log_path, n_iter, "TotalTime", r"\d+.\d+")
72    task = process_log(fname, log_path, n_iter, "task_emit", r"\d+.\d+")
73    log = {}
74    for fname in total:
75        log[fname] = float(total[fname][0]) - float(task[fname][0])
76    return log
77
78
79@summarize
80def process_resnet_log(log_path, n_iter):
81    fname = "resnet%d.log"
82    total = process_log(fname, log_path, n_iter, "TotalTime", r"\d+.\d+")
83    task = process_log(fname, log_path, n_iter, "task_emit", r"\d+.\d+")
84    log = {}
85    for fname in total:
86        log[fname] = float(total[fname][0]) - float(task[fname][0])
87    return log
88
89
90@summarize
91def process_gpt_log(log_path, n_iter):
92    fname = "gpt%d.log"
93    total = process_log(fname, log_path, n_iter, "TotalTime", r"\d+.\d+")
94    task = process_log(fname, log_path, n_iter, "task_emit", r"\d+.\d+")
95    log = {}
96    for fname in total:
97        log[fname] = float(total[fname][0]) - float(task[fname][0])
98    return log
99
100
101@summarize
102def process_reid_log(log_path, n_iter):
103    log = {}
104    for i in range(8):
105        fname = "reid_%d_"+str(i)+".log"
106        total = process_log(fname, log_path, n_iter, "TotalTime", r"\d+.\d+")
107        task = process_log(fname, log_path, n_iter, "task_emit", r"\d+.\d+")
108        for fname in total:
109            log[fname] = float(total[fname][0]) - float(task[fname][0])
110    return log
111
112
113def write_to_me_report(log, me_report_path):
114    wb = opx.load_workbook(me_report_path)
115    sheet = wb["Sheet"]
116    idx = sheet.max_row + 1
117    date = time.strftime('%m%d', time.localtime())
118    sheet['A%d' % idx] = date
119    sheet['B%d' % idx] = round(log["reid"]["min_time"], 2)
120    sheet['C%d' % idx] = round(log["bert"]["min_time"], 2)
121    sheet['D%d' % idx] = round(log['resnet']["min_time"], 2)
122    sheet['E%d' % idx] = round(log['gpt']["min_time"], 2)
123    wb.save(me_report_path)
124
125
126def generate_report():
127    me_report_path, log_path, n_iter, out = parse_arguments()
128    log_data = {}
129    bert_log = process_bert_log(log_path, n_iter)
130    resnet_log = process_resnet_log(log_path, n_iter)
131    gpt_log = process_gpt_log(log_path, n_iter)
132    reid_log = process_reid_log(log_path, n_iter)
133    log_data["bert"] = bert_log
134    log_data["resnet"] = resnet_log
135    log_data["gpt"] = gpt_log
136    log_data["reid"] = reid_log
137    with open(out, "w") as f:
138        json.dump(log_data, f, indent=2)
139    write_to_me_report(log_data, me_report_path)
140
141
142if __name__ == "__main__":
143    generate_report()
144