mem_profiler.py 1.21 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
4
from cgitb import enable
import torch
from .device import *

Nianchen Deng's avatar
sync    
Nianchen Deng committed
5

Nianchen Deng's avatar
Nianchen Deng committed
6
7
8
9
10
class MemProfiler:

    enable = False

    @staticmethod
Nianchen Deng's avatar
sync    
Nianchen Deng committed
11
12
    def print_memory_stats(prefix, last_allocated=None, device=None, enable_once=False):
        if not enable_once and not MemProfiler.enable:
Nianchen Deng's avatar
Nianchen Deng committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
            return
        if device is None:
            device = default()
        allocated = torch.cuda.memory_allocated(device)
        if last_allocated is not None:
            delta = (allocated - last_allocated) / 1024 / 1024
            delta_str = f'{-delta:.2f}MB is freed, ' if delta < 0 else f'{delta:.2f}MB is allocated, '
        else:
            delta_str = ''
        print(f'{prefix}: {delta_str}currently PyTorch allocates {torch.cuda.memory_allocated(device)/1024/1024:.2f}MB and '
            f'reserves {torch.cuda.memory_reserved(device)/1024/1024:.2f}MB memory')


    def __init__(self, name, device=None) -> None:
        self.name = name
        self.device = device or default()
        self.alloc0 = 0

    def __enter__(self):
        self.alloc0 = torch.cuda.memory_allocated(self.device)
        return self

    def __exit__(self, exc_type, exc_val, exc_traceback):
        MemProfiler.print_memory_stats(self.name, self.alloc0, self.device)