Files
ColossalAI/applications/ColossalChat/coati/distributed/profiling_utils.py
YeAnbang 594c2c6522 [feat[ Support one-behind to reduce bubble time. Add profiling code (#6353)
* support n_behind, add profiling

* fix bugs

* fix visualization

* fix behind

* fix loop issue

* add profiling

* fix update

* update assert

* remove assert

---------

Co-authored-by: Tong Li <tong.li35271158@gmail.com>
2025-08-05 14:04:10 +08:00

38 lines
1002 B
Python

import os
import time
class CustomProfiler:
def __init__(self, name, disabled=True):
self.disabled = disabled
if not disabled:
self.name = name
self.pid = os.getpid()
self.file = open(f"{name}.prof", "w")
def _log(self, message):
if self.disabled:
return
current_time = time.time()
self.file.write(f"{current_time} {self.name} {self.pid}:: {message}\n")
self.file.flush()
def log(self, message):
if self.disabled:
return
current_time = time.time()
self.file.write(f"[Log]: {current_time} {self.name} {self.pid}:: {message}\n")
self.file.flush()
def enter(self, event_name):
self._log(f"Enter {event_name}")
def exit(self, event_name):
self._log(f"Exit {event_name}")
def close(self):
if self.disabled:
return
self.file.close()
print(f"Profiler data written to {self.name}.prof")