Skip to content

Commit 35bfb51

Browse files
lwxFormers Bot
authored andcommitted
Make profiler pre-extract CSV files needed by find_slowest (fairinternal/xformers#1413)
__original_commit__ = fairinternal/xformers@000d958
1 parent 8ed0992 commit 35bfb51

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

xformers/profiler/find_slowest.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def sort_nccl_events(
7777
]
7878

7979

80-
def parse_one_file(profile_trace_path):
80+
def read_one_file(profile_trace_path: str) -> pd.DataFrame:
81+
if profile_trace_path.endswith(".csv"):
82+
return pd.read_csv(profile_trace_path, names=["name", "dur"])
83+
8184
jq_pipe = '.traceEvents[] | select(.cat == "kernel") | [.name, .dur] | @csv'
8285
if profile_trace_path.endswith(".gz"):
8386
cmd = (
@@ -97,6 +100,11 @@ def parse_one_file(profile_trace_path):
97100
finally:
98101
assert subp.wait() == 0
99102

103+
return kernel_events
104+
105+
106+
def parse_one_file(profile_trace_path: str) -> tuple[pd.DataFrame, pd.DataFrame]:
107+
kernel_events = read_one_file(profile_trace_path)
100108
kernel_events["log_name"] = os.path.basename(profile_trace_path)
101109

102110
communication_kernels = kernel_events[kernel_events.name.str.startswith("nccl")]
@@ -106,9 +114,13 @@ def parse_one_file(profile_trace_path):
106114

107115

108116
def print_profiling_info(cuda_profile_dir: str):
109-
cuda_profile_path_name = f"{cuda_profile_dir}/*trace.json.gz"
117+
cuda_profile_path_name = f"{cuda_profile_dir}/kernels_*.csv"
110118
profile_files = glob.glob(cuda_profile_path_name)
111119

120+
if len(profile_files) == 0:
121+
cuda_profile_path_name = f"{cuda_profile_dir}/*.pt.trace.json.gz"
122+
profile_files = glob.glob(cuda_profile_path_name)
123+
112124
if len(profile_files) == 0:
113125
cuda_profile_path_name = f"{cuda_profile_dir}/*.json"
114126
profile_files = glob.glob(cuda_profile_path_name)

xformers/profiler/profiler.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# LICENSE file in the root directory of this source tree.
55

66

7+
import csv
78
import logging
89
import os
910
import queue
@@ -86,12 +87,29 @@ def _on_trace(self, prof: torch.profiler.profiler.profile) -> None:
8687
os.makedirs(dir_name, exist_ok=True)
8788
file_name = f"{worker_name}.{time.time_ns()}.pt.trace.json.gz"
8889
prof.export_chrome_trace(os.path.join(dir_name, file_name))
90+
csv_file_name = f"kernels_{worker_name}.{time.time_ns()}.csv"
91+
self._preprocess_trace(prof, os.path.join(dir_name, csv_file_name))
8992
try:
9093
self._analyze_trace(prof)
9194
except Exception as exc:
9295
self.main_profiler.summary.append(("TraceAnalysis", "Error"))
9396
logger.warning("Exception analyzing kineto trace", exc_info=exc)
9497

98+
def _preprocess_trace(
99+
self, prof: torch.profiler.profiler.profile, file_name: str
100+
) -> None:
101+
if prof.profiler is None or prof.profiler.kineto_results is None:
102+
return
103+
with open(file_name, "w", newline="") as file:
104+
writer = csv.writer(file)
105+
for e in prof.profiler.kineto_results.events():
106+
if (
107+
e.device_type().name == "CUDA"
108+
and not e.is_user_annotation()
109+
and e.duration_ns() > 0
110+
):
111+
writer.writerow([e.name(), f"{e.duration_ns() / 1_000}"])
112+
95113
def _analyze_trace(self, prof: torch.profiler.profiler.profile) -> None:
96114
if prof.profiler is None or prof.profiler.kineto_results is None:
97115
return

0 commit comments

Comments
 (0)