simple_perf.py 910 Bytes
Newer Older
1
2
3
4
5
6
7
8
import torch.cuda


class SimplePerf(object):

    def __init__(self, enable, start = False) -> None:
        super().__init__()
        self.enable = enable
9
        self.start_event = None
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        if start:
            self.Start()
    
    def Start(self):
        if not self.enable:
            return
        if self.start_event == None:
            self.start_event = torch.cuda.Event(enable_timing=True)
            self.end_event = torch.cuda.Event(enable_timing=True)
        torch.cuda.synchronize()
        self.start_event.record()
    
    def Checkpoint(self, name: str, end: bool = False):
        if not self.enable:
            return
        self.end_event.record()
        torch.cuda.synchronize()
Nianchen Deng's avatar
sync    
Nianchen Deng committed
27
28
        duration = self.start_event.elapsed_time(self.end_event)
        print('%s: %.1fms' % (name, duration))
29
        if not end:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
30
31
            self.start_event.record()
        return duration