metric.py 10.2 KB
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
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
#!/usr/bin/env python3
# Benchmark tool used to collect metrics.

import time
import os
from collections import OrderedDict
import numpy as np
import json

cur_dir = os.path.dirname(os.path.abspath(__file__))

adaptive_strategy_env = os.getenv('MLU_ADAPTIVE_STRATEGY_COUNT')
adaptive_cnt = int(adaptive_strategy_env) if (adaptive_strategy_env
                                              is not None) else 0


def load_metrics_from_config():
    required_metrics = {}
    with open(cur_dir + "/configs.json", 'r') as f:
        configs = json.load(f)
        for key, value in configs.items():
            if os.getenv(key) is None:
                continue
            required_metrics[key] = value
    return required_metrics


required_metrics = load_metrics_from_config()


def get_platform():
    import glob
    import fileinput
    files_driver = glob.glob("/proc/driver/*/*/*/information")
    if not len(files_driver):
        return "Unkonw"
    finput = fileinput.input(files_driver[0])
    for line in finput:
        # MLU device information
        if "Device name" in line:
            return line.split(":")[-1].strip()
        # GPU device information
        elif "Model" in line:
            return line.split(":")[-1].strip()
    finput.close()
    return "Unkonw"


cur_platform = get_platform()


def get_dataset():
    dataset = os.getenv("DATASET_NAME")
    if dataset is None:
        return "unknow"
    return dataset


cur_dataset = get_dataset()


class AggregatorMeter(object):

    def __init__(self, *args):
        self.meters = []
        for meter in args:
            self.meters.append(meter)

    def update(self, val):
        for meter in self.meters:
            meter.update(val)

    def result(self):
        results = [meter.result() for meter in self.meters]
        return results


class MaxMeter(object):

    def __init__(self, name):
        self.name = name
        self.max = None

    def reset(self):
        self.max = None

    def update(self, val):
        if self.max is None:
            self.max = val
        else:
            self.max = max(self.max, val)

    def result(self):
        return (self.name, self.max)

    def __str__(self):
        pass


class AverageMeter(object):

    def __init__(self, name):
        self.name = name
        self.reset()

    def reset(self):
        self.val = 0
        self.count = 0

    def update(self, val):
        self.count += 1
        self.val += val

    def result(self):
        if self.count == 0:
            return None
        return (self.name, round(self.val / self.count, 3))

    def __str__(self):
        pass


class VarianceMeter(object):

    def __init__(self, name):
        self.name = name
        self.reset()

    def reset(self):
        self.vals = []
        self.count = 0

    def update(self, val):
        self.count += 1
        self.vals += [val]

    def result(self):
        if self.count == 0:
            return None
        return (self.name, round(np.var(self.vals), 6))

    def __str__(self):
        pass


class ElapsedTimer(object):

    def __init__(self, count_down=0):
        self.meter = AggregatorMeter(AverageMeter("batch_time_avg"),
                                     VarianceMeter("batch_time_var"))
        self.count_down = count_down

    def place(self):
        self.last_time_stamp = time.time()

    def clock(self):
        now = time.time()
        elapsed_time = now - self.last_time_stamp
        return elapsed_time

    def record(self):
        elapsed_time = self.clock()
        if self.count_down > 0:
            self.count_down -= 1
            return
        self.meter.update(elapsed_time)

    def data(self):
        return self.meter.result()


class HardwareTimer(object):

    def __init__(self, count_down=0):
        import torch_mlu
        from torch_mlu.core.device.notifier import Notifier
        self.meter = AggregatorMeter(AverageMeter("hardware_time_avg"),
                                     VarianceMeter("hardware_time_var"))
        self.start_notifier = Notifier()
        self.end_notifier = Notifier()
        self.count_down = count_down

    def place(self):
        self.start_notifier.place()

    def clock(self):
        self.end_notifier.place()
        self.end_notifier.synchronize()
        # unit of hardware_time should be second.
        hardware_time = self.start_notifier.hardware_time(
            self.end_notifier) / 1000.0 / 1000.0
        return hardware_time

    def record(self):
        hardware_time = self.clock()
        if self.count_down > 0:
            self.count_down -= 1
            return
        self.meter.update(hardware_time)

    def data(self):
        return self.meter.result()


class MemoryProfiler(object):

    def __init__(self):
        raise Exception("Not Implemented")

    def record(self):
        raise Exception("Not Implemented")


class Dumper(object):

    def __init__(self, name, save_path, target):
        self.name = name
        self.save_path = save_path
        self.target = target

    def exception_handle(self, contents, exception):
        """exception method will be triggered when collected metrics 
           can not meet the requirements in the config.json"""
        if exception == "throughput":
            keys = contents.keys()
            if "batch_time_avg" not in keys or "cards" not in keys or "batch_size" not in keys:
                return "unknow"
            batch_time = contents["batch_time_avg"]
            cards = contents["cards"]
            batch_size = contents["batch_size"]
            return round(batch_size / batch_time * cards, 2)
        else:
            return "unknow"

    def dump(self, contents):
        from datetime import datetime
        date_now = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        output = {"date": date_now}
        for target in self.target:
            if target not in contents.keys():
                output[target] = self.exception_handle(contents, target)
            else:
                output[target] = contents[target]
        output["device"] = cur_platform
        output["dataset"] = cur_dataset
        with open(self.save_path, 'a') as f:
            json.dump(output, f, indent=4)
            f.write("\n")


class MetricCollector(object):

    def __init__(self,
                 enable=True,
                 enable_only_benchmark=False,
                 enable_only_avglog=False,
                 record_elapsed_time=False,
                 record_hardware_time=False,
                 profile_memory=False):

        self._enabled = self.check_enable(enable, enable_only_benchmark,
                                          enable_only_avglog)
        if not self._enabled:
            return
        
        self.record_elapsed_time = record_elapsed_time
        self.record_hardware_time = record_hardware_time
        self.profile_memory = profile_memory

        self._recorders = []
        self._init_recorders()
        
        self._dumpers = []
        self._init_dumpers()
        
        self._metrics = OrderedDict()
        self._insert_metrics = {}


    def check_enable(self, enable, enable_only_benchmark, enable_only_avglog):
        """For that envs like AVG_LOG not appear in user training script, the disablement 
           of MetricCollecotr should meet the conditions as follows."""
        if not enable:
            return False

        # At least on env(AVG_LOG or BENCHMARK_LOG) in the config.json exsits.
        if len(required_metrics) == 0:
            return False

        # If enable_only_benchmark equals True when construct MetricCollector, there must exists
        # env BENCHMARK_LOG, enable_only_avglog the same (hardcode like BENCHMARK_LOG and AVG_LOG is
        # not elegant to appear here, but...).
        if enable_only_benchmark and os.getenv("BENCHMARK_LOG") is None:
            return False
        if enable_only_avglog and os.getenv("AVG_LOG") is None:
            return False

        return True

    def _init_recorders(self):
        if self.record_elapsed_time:
            self._recorders.append(ElapsedTimer(count_down=adaptive_cnt))
        if self.record_hardware_time:
            self._recorders.append(HardwareTimer(count_down=adaptive_cnt))
        if self.profile_memory:
            self._recorders.append(MemoryProfiler())

    def _init_dumpers(self):
        for key, value in required_metrics.items():
            file_path = os.getenv(key)
            self._dumpers.append(Dumper(key, file_path, value))

    def place(self):
        """call all recorder's place method."""
        if not self._enabled:
            return
        for recorder in self._recorders:
            recorder.place()

    def record(self):
        """call all recorder's record method."""
        if not self._enabled:
            return
        for recorder in self._recorders:
            recorder.record()

    def __str__(self):
        return str(self.get_metrics())

    def insert_metrics(self, **kwargs):
        if self._enabled:
            self._insert_metrics.update(kwargs)

    def update_recorder_metrics(self):
        for recorder in self._recorders:
            result = recorder.data()
            if len(result) == 0:
                print(
                    "MetricCollector have not recorded any datas, please ensure \
                       you have called place() and record() method or check if iters \
                       lower than adaptive_cnt : {}.".format(adaptive_cnt))
                return

            if isinstance(result, tuple):
                name, val = result
                self._metrics[name] = val
            elif isinstance(result, list):
                for name, val in result:
                    self._metrics[name] = val
            else:
                raise "Unknow result type of recorder."

    def update_metrics(self):
        if not self._enabled:
            return
        self.update_recorder_metrics()
        for key, value in self._insert_metrics.items():
            self._metrics[key] = value

    def get_metrics(self):
        if not self._enabled:
            return OrderedDict()
        self.update_metrics()
        return self._metrics

    def dump(self):
        if not self._enabled:
            return
        self.update_metrics()
        for dumper in self._dumpers:
            dumper.dump(self._metrics)