hpuhelper.py 1.33 KB
Newer Older
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
import time
import habana_frameworks.torch as ht

class hpu_perf:
    def __init__(self, module, log=True, mark_step=True, memoryinfo=False, sync=False):
        if log:
            print(f" {module}: start")
        self.module = module
        self.stime = time.perf_counter()
        self.mark = mark_step
        self.mem = memoryinfo
        self.sync = sync
        self.log = log
        if self.mem:
            ht.hpu.reset_peak_memory_stats()
        self.prelog = None

    def checknow(self, log):
        if self.mark:
            ht.core.mark_step()
            if self.sync:
                ht.core.hpu.default_stream().synchronize()
        if self.mem:
            print(ht.hpu.memory_summary())

        tmp = time.perf_counter()
        if self.log:
            print(" {}: {} takes {:.2f} ms".format(self.module, log, (tmp - self.stime)*1000))
        self.stime = tmp

    def checkahead(self, log):
        if self.mark:
            ht.core.mark_step()
            if self.sync:
                ht.core.hpu.default_stream().synchronize()
        if self.mem:
            print(ht.hpu.memory_summary())

        tmp = time.perf_counter()
        if self.prelog is not None and self.log:
            print(" {}: {} takes {:.2f} ms".format(self.module, self.prelog, (tmp - self.stime)*1000))
        self.stime = tmp
        self.prelog = log