Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit faeddb5

Browse files
authored
[misc] Add Torch profiler support for CPU-only devices (vllm-project#7806)
1 parent fc5ebbd commit faeddb5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

vllm/worker/cpu_worker.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ def __init__(
179179
self.cache_engine: List[CPUCacheEngine]
180180
self.cpu_cache: List[List[torch.Tensor]]
181181

182+
# Torch profiler. Enabled and configured through env vars:
183+
# VLLM_TORCH_PROFILER_DIR=/path/to/save/trace
184+
if envs.VLLM_TORCH_PROFILER_DIR:
185+
torch_profiler_trace_dir = envs.VLLM_TORCH_PROFILER_DIR
186+
logger.info("Profiling enabled. Traces will be saved to: %s",
187+
torch_profiler_trace_dir)
188+
self.profiler = torch.profiler.profile(
189+
activities=[
190+
torch.profiler.ProfilerActivity.CPU,
191+
],
192+
with_stack=True,
193+
on_trace_ready=torch.profiler.tensorboard_trace_handler(
194+
torch_profiler_trace_dir, use_gzip=True))
195+
else:
196+
self.profiler = None
197+
198+
def start_profile(self):
199+
if self.profiler is None:
200+
raise RuntimeError("Profiler is not enabled.")
201+
self.profiler.start()
202+
203+
def stop_profile(self):
204+
if self.profiler is None:
205+
raise RuntimeError("Profiler is not enabled.")
206+
self.profiler.stop()
207+
182208
def init_device(self) -> None:
183209
if self.local_omp_cpuid != "all":
184210
torch.ops._C_utils.init_cpu_threads_env(self.local_omp_cpuid)

0 commit comments

Comments
 (0)