"vscode:/vscode.git/clone" did not exist on "61a1c018f25f1735b2121ef850a170af55d282e7"
profiler.py 973 Bytes
Newer Older
1
2
3
import time
import torch
from contextlib import ContextDecorator
4
from lightx2v.utils.envs import *
root's avatar
root committed
5
from loguru import logger
6
7
8
9
10
11
12
13
14
15
16
17
18
19


class _ProfilingContext(ContextDecorator):
    def __init__(self, name):
        self.name = name

    def __enter__(self):
        torch.cuda.synchronize()
        self.start_time = time.perf_counter()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        torch.cuda.synchronize()
        elapsed = time.perf_counter() - self.start_time
root's avatar
root committed
20
        logger.info(f"[Profile] {self.name} cost {elapsed:.6f} seconds")
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        return False


class _NullContext(ContextDecorator):
    # Context manager without decision branch logic overhead
    def __init__(self, *args, **kwargs):
        pass

    def __enter__(self):
        return self

    def __exit__(self, *args):
        return False


ProfilingContext = _ProfilingContext
helloyongyang's avatar
helloyongyang committed
37
ProfilingContext4Debug = _ProfilingContext if CHECK_ENABLE_PROFILING_DEBUG() else _NullContext