Skip to content

Commit 696b025

Browse files
committed
Refine paddle/v2/fluid/profiler.py.
1 parent 623f62a commit 696b025

File tree

3 files changed

+30
-58
lines changed

3 files changed

+30
-58
lines changed

paddle/platform/cuda_profiler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ void CudaProfilerInit(std::string output_file, std::string output_mode,
2929
memcpy(buf.data(), tmpl.data(), tmpl.size());
3030
auto result = mktemp(buf.data());
3131
PADDLE_ENFORCE(strlen(result) != 0);
32-
std::string config = result;
32+
std::string config_file = result;
3333

3434
{
35-
std::ofstream ofs(config, std::ios::out | std::ios::trunc);
35+
std::ofstream ofs(config_file, std::ios::out | std::ios::trunc);
3636
PADDLE_ENFORCE(ofs.is_open(), "ofstream: ", ofs.rdstate());
3737
for (const auto& line : config_flags) {
3838
ofs << line << std::endl;
@@ -42,12 +42,12 @@ void CudaProfilerInit(std::string output_file, std::string output_mode,
4242
PADDLE_ENFORCE(output_mode == "kvp" || output_mode == "csv");
4343
cudaOutputMode_t mode = output_mode == "csv" ? cudaCSV : cudaKeyValuePair;
4444
PADDLE_ENFORCE(
45-
cudaProfilerInitialize(config.c_str(), output_file.c_str(), mode));
45+
cudaProfilerInitialize(config_file.c_str(), output_file.c_str(), mode));
4646
}
4747

4848
void CudaProfilerStart() { PADDLE_ENFORCE(cudaProfilerStart()); }
4949

50-
void CudaProfilerStop() { PADDLE_ENFORCE((cudaProfilerStop())); }
50+
void CudaProfilerStop() { PADDLE_ENFORCE(cudaProfilerStop()); }
5151

5252
} // namespace platform
5353
} // namespace paddle

python/paddle/v2/fluid/profiler.py

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import paddle.v2.fluid.core as core
2-
import subprocess
2+
from contextlib import contextmanager
33

44
__all__ = ['CudaProfiler']
55

6-
NV_FLAGS = [
6+
NVPROF_CONFIG = [
77
"gpustarttimestamp",
88
"gpuendtimestamp",
99
"gridsize3d",
@@ -14,61 +14,33 @@
1414
]
1515

1616

17-
def nvporf_init(output_file, output_mode=None, flags=None):
18-
"""
19-
Initialize the CUDA profiler.
20-
This methods must be called before nvprof_start.
21-
22-
:param output_file: The output file name.
23-
:type output_file: string
24-
:param output_mode: The output mode has Key-Value pair format and
25-
Comma separated values format.
26-
It should be 'kv' or 'csv'.
27-
:type output_mode: string
17+
@contextmanager
18+
def cuda_profiler(output_file, output_mode=None, config=None):
19+
"""The CUDA profiler.
20+
This fuctions is used to profile CUDA program by CUDA runtime application
21+
programming interface. The profiling result will be written into
22+
`output_file` with Key-Value pair format or Comma separated values format.
23+
The user can set the output mode by `output_mode` argument and set the
24+
counters/options for profiling by `config` argument. The default config
25+
caontains 'gpustarttimestamp', 'gpustarttimestamp', 'gridsize3d',
26+
'threadblocksize', 'streamid', 'enableonstart 0', 'conckerneltrace'.
27+
28+
Args:
29+
output_file (string) : The output file name, the result will be
30+
written into this file.
31+
output_mode (string) : The output mode has Key-Value pair format and
32+
Comma separated values format. It should be 'kv' or 'csv'.
33+
config (string) : The profiler options and counters can refer to
34+
"Compute Command Line Profiler User Guide".
2835
"""
2936
if output_mode is None:
3037
output_mode = 'csv'
3138
if output_mode not in ['kv', 'csv']:
3239
raise ValueError("The output mode must be 'key-value' or 'csv'.")
33-
flags = NV_FLAGS if flags is None else flags
34-
core.nvprof_init(output_file, output_mode, flags)
35-
36-
37-
def nvporf_start():
38-
"""
39-
Enables profiler collection by the active CUDA profiling tool.
40-
"""
40+
config = NVPROF_CONFIG if config is None else config
41+
core.nvprof_init(output_file, output_mode, config)
42+
# Enables profiler collection by the active CUDA profiling tool.
4143
core.nvprof_start()
42-
43-
44-
def nvporf_stop():
45-
"""
46-
Disables profiler collection.
47-
"""
44+
yield
45+
# Disables profiler collection.
4846
core.nvprof_stop()
49-
50-
51-
class CudaProfiler(object):
52-
def __init__(self, output_file, output_mode=None, flags=None, enabled=True):
53-
self.enabled = enabled
54-
if not self.enabled:
55-
return
56-
self.entered = False
57-
self.out_file = output_file
58-
nvporf_init(output_file, output_mode, flags)
59-
60-
def __enter__(self):
61-
if not self.enabled:
62-
return
63-
if self.entered:
64-
raise RuntimeError("The profiler traces are not reentrant")
65-
self.entered = True
66-
nvporf_start()
67-
return self
68-
69-
def __exit__(self, exc_type, exc_value, tb):
70-
if exc_value is not None:
71-
raise exc_value
72-
if not self.enabled:
73-
return
74-
nvporf_stop()

python/paddle/v2/fluid/tests/test_profiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_nvprof(self):
1818
exe = fluid.Executor(place)
1919
exe.run(fluid.default_startup_program())
2020

21-
with profiler.CudaProfiler("cuda_profiler.txt", 'csv') as nvprof:
21+
with profiler.cuda_profiler("cuda_profiler.txt", 'csv') as nvprof:
2222
for i in range(epoc):
2323
input = np.random.random(dshape).astype("float32")
2424
exe.run(fluid.default_main_program(), feed={'data': input})

0 commit comments

Comments
 (0)