|
2 | 2 | import collections
|
3 | 3 | import contextlib
|
4 | 4 | import copy
|
5 |
| -import cProfile |
6 | 5 | import dataclasses
|
7 | 6 | import datetime
|
8 | 7 | import dis
|
|
16 | 15 | import math
|
17 | 16 | import operator
|
18 | 17 | import os
|
19 |
| -import pstats |
20 | 18 | import re
|
21 |
| -import subprocess |
22 | 19 | import sys
|
23 | 20 | import textwrap
|
24 | 21 | import threading
|
|
28 | 25 | import weakref
|
29 | 26 | from contextlib import contextmanager
|
30 | 27 | from functools import lru_cache, wraps
|
31 |
| -from pathlib import Path |
32 | 28 | from types import MethodWrapperType
|
33 | 29 | from typing import (
|
34 | 30 | Any,
|
|
50 | 46 | ValuesView,
|
51 | 47 | )
|
52 | 48 |
|
53 |
| -from torch._utils_internal import maybe_upload_prof_stats_to_manifold |
54 |
| - |
55 | 49 | from ..utils.hooks import RemovableHandle
|
56 | 50 |
|
57 | 51 | try:
|
@@ -135,63 +129,6 @@ def tabulate(rows, headers):
|
135 | 129 | )
|
136 | 130 |
|
137 | 131 |
|
138 |
| -def maybe_cprofile(func): |
139 |
| - if config.cprofile: |
140 |
| - return cprofile_wrapper(func) |
141 |
| - return func |
142 |
| - |
143 |
| - |
144 |
| -def cprofile_wrapper(func): |
145 |
| - @wraps(func) |
146 |
| - def profile_wrapper(*args, **kwargs): |
147 |
| - global timer_counter |
148 |
| - profile_cnt = next(timer_counter) |
149 |
| - profile_path = Path("/tmp/" + func.__name__ + f"{profile_cnt}.profile") |
150 |
| - prof = cProfile.Profile() |
151 |
| - prof.enable() |
152 |
| - start_ts = time.time() |
153 |
| - retval = prof.runcall(func, *args, **kwargs) |
154 |
| - profile_latency = time.time() - start_ts |
155 |
| - prof.disable() |
156 |
| - print( |
157 |
| - f"### Cprofile for {func.__name__} iter {profile_cnt} took {profile_latency:.3f} seconds ###" |
158 |
| - ) |
159 |
| - ps = pstats.Stats(prof) |
160 |
| - prof.dump_stats(profile_path) |
161 |
| - svg_path = profile_path.with_suffix(".svg") |
162 |
| - try: |
163 |
| - gprof2dot_process = subprocess.Popen( |
164 |
| - [ |
165 |
| - "gprof2dot", |
166 |
| - "-f", |
167 |
| - "pstats", |
168 |
| - "--node-label=total-time-percentage", |
169 |
| - "--node-label=self-time-percentage", |
170 |
| - "--node-label=total-time", |
171 |
| - str(profile_path), |
172 |
| - ], |
173 |
| - stdout=subprocess.PIPE, |
174 |
| - ) |
175 |
| - subprocess.check_call( |
176 |
| - ["dot", "-Tsvg", "-o", str(svg_path)], |
177 |
| - stdin=gprof2dot_process.stdout, |
178 |
| - ) |
179 |
| - print(f"Generated SVG from profile at {str(svg_path)}") |
180 |
| - except FileNotFoundError: |
181 |
| - print( |
182 |
| - "Failed to generate SVG from profile -- dumping stats instead." |
183 |
| - "Try installing gprof2dot and dot for a better visualization" |
184 |
| - ) |
185 |
| - ps.sort_stats(pstats.SortKey.TIME).print_stats(20) |
186 |
| - ps.sort_stats(pstats.SortKey.CUMULATIVE).print_stats(20) |
187 |
| - |
188 |
| - maybe_upload_prof_stats_to_manifold(str(profile_path)) # fb-only |
189 |
| - |
190 |
| - return retval |
191 |
| - |
192 |
| - return profile_wrapper |
193 |
| - |
194 |
| - |
195 | 132 | curr_frame = 0
|
196 | 133 |
|
197 | 134 |
|
|
0 commit comments