From 13e6b1b90831770d41efd506615373e6bb350271 Mon Sep 17 00:00:00 2001 From: Elham Date: Fri, 10 Apr 2026 01:41:15 -0400 Subject: [PATCH] [BugFix][CPU] Add CPU profiler summary file output (#38366) Signed-off-by: Elham Harirpoush --- vllm/profiler/wrapper.py | 48 +++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/vllm/profiler/wrapper.py b/vllm/profiler/wrapper.py index f3af993e7f..7cd4d8874d 100644 --- a/vllm/profiler/wrapper.py +++ b/vllm/profiler/wrapper.py @@ -240,6 +240,28 @@ class TorchProfilerWrapper(WorkerProfiler): 0, ) + def _build_profiler_table( + self, + sort_key: str, + row_limit: int | None = None, + ) -> str: + if row_limit is None: # use profiler default row limit of 100 + return self.profiler.key_averages().table(sort_by=sort_key) + return self.profiler.key_averages().table( + sort_by=sort_key, + row_limit=row_limit, + ) + + def _write_profiler_table(self, rank: int, table: str) -> None: + profiler_dir = self.profiler_config.torch_profiler_dir + + # Skip file write for URI paths (gs://, s3://, etc.) + # as standard file I/O doesn't work with URI schemes + if not _is_uri_path(profiler_dir): + profiler_out_file = f"{profiler_dir}/profiler_out_{rank}.txt" + with open(profiler_out_file, "w") as f: + print(table, file=f) + @override def _start(self) -> None: self.profiler.start() @@ -251,26 +273,22 @@ class TorchProfilerWrapper(WorkerProfiler): profiler_config = self.profiler_config rank = self.local_rank if profiler_config.torch_profiler_dump_cuda_time_total: - profiler_dir = profiler_config.torch_profiler_dir - sort_key = "self_cuda_time_total" - table = self.profiler.key_averages().table(sort_by=sort_key) - - # Skip file write for URI paths (gs://, s3://, etc.) - # as standard file I/O doesn't work with URI schemes - if not _is_uri_path(profiler_dir): - profiler_out_file = f"{profiler_dir}/profiler_out_{rank}.txt" - with open(profiler_out_file, "w") as f: - print(table, file=f) + table = self._build_profiler_table(sort_key="self_cuda_time_total") + self._write_profiler_table(rank, table) # only print profiler results on rank 0 if rank == 0: print(table) - if self.dump_cpu_time_total and rank == 0: - logger.info( - self.profiler.key_averages().table( - sort_by="self_cpu_time_total", row_limit=50 - ) + + if self.dump_cpu_time_total: + table = self._build_profiler_table( + sort_key="self_cpu_time_total", row_limit=50 ) + self._write_profiler_table(rank, table) + + # only print profiler results on rank 0 + if rank == 0: + print(table) @override def _profiler_step(self) -> bool: