Skip to content

Commit d6c9a23

Browse files
Superjomnhchings
authored andcommitted
make gc tracer singleton
Signed-off-by: Superjomn <[email protected]>
1 parent fac44cc commit d6c9a23

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

tensorrt_llm/_torch/pyexecutor/py_executor.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
from tensorrt_llm._torch.pyexecutor.resource_manager import (
2222
ResourceManagerType, request_context)
23-
from tensorrt_llm._utils import (customized_gc_thresholds, gc_nvtx_watcher,
24-
is_trace_enabled, mpi_disabled, nvtx_range,
25-
trace_func)
23+
from tensorrt_llm._utils import (customized_gc_thresholds, is_trace_enabled,
24+
mpi_disabled, nvtx_range, trace_func)
2625
from tensorrt_llm.bindings.executor import (DisServingRequestStats,
2726
FinishReason, InflightBatchingStats,
2827
IterationStats, KvCacheStats,
@@ -139,7 +138,6 @@ def __init__(self,
139138
# profile config
140139
self.profile_start_iters, self.profile_stop_iters = _load_iteration_indexes(
141140
PROFILE_START_STOP_ENV_VAR_NAME)
142-
self.gc_nvtx_watcher_handle = gc_nvtx_watcher()
143141

144142
# related modules
145143
self.resource_manager = resource_manager

tensorrt_llm/_utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,15 +1218,30 @@ class _GCNvtxHandle:
12181218
"""Handle object for GC NVTX watcher to keep it alive."""
12191219

12201220

1221-
def gc_nvtx_watcher() -> Optional[_GCNvtxHandle]:
1221+
# Singleton for the GC NVTX watcher handle.
1222+
_gc_watcher_handle: Optional[_GCNvtxHandle] = None
1223+
1224+
1225+
def _setup_gc_nvtx_profiling() -> Optional[_GCNvtxHandle]:
12221226
"""
1223-
Set up NVTX range markers for Python garbage collection events.
1227+
Set up NVTX range markers for Python garbage collection events (singleton).
12241228
This helps in profiling to visualize when GC occurs during execution.
12251229
1230+
This function is called automatically at module import time. The environment
1231+
variable TLLM_PROFILE_RECORD_GC must be set before importing this module.
1232+
1233+
This is an internal function and should not be called directly by users.
1234+
12261235
Returns:
12271236
_GCNvtxHandle or None: A handle object that keeps the GC callback alive,
12281237
or None if GC profiling is not enabled.
12291238
"""
1239+
global _gc_watcher_handle
1240+
1241+
# Return existing handle if already initialized
1242+
if _gc_watcher_handle is not None:
1243+
return _gc_watcher_handle
1244+
12301245
enabled = os.environ.get(PROFILE_RECORD_GC_ENV_VAR_NAME, None)
12311246
if not enabled:
12321247
return None
@@ -1253,4 +1268,10 @@ def gc_cleanup(callback):
12531268

12541269
handle = _GCNvtxHandle()
12551270
weakref.finalize(handle, gc_cleanup, gc_callback)
1271+
1272+
_gc_watcher_handle = handle
12561273
return handle
1274+
1275+
1276+
# Initialize GC NVTX profiling singleton at module import time
1277+
_setup_gc_nvtx_profiling()

tensorrt_llm/llmapi/llm.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from tensorrt_llm.llmapi import tracing
2222
from tensorrt_llm.metrics.enums import MetricNames
2323

24-
from .._utils import gc_nvtx_watcher, nvtx_range_debug
24+
from .._utils import nvtx_range_debug
2525
from ..bindings import executor as tllm
2626
from ..bindings import steady_clock_now
2727
from ..builder import EngineConfig
@@ -131,8 +131,6 @@ def __init__(self,
131131
self._orchestrator_type = kwargs.get("orchestrator_type", None)
132132
self._llm_id = None
133133

134-
self._gc_nvtx_watcher_handle = gc_nvtx_watcher()
135-
136134
log_level = logger.level
137135
logger.set_level("info") # force display the backend
138136

0 commit comments

Comments
 (0)