Skip to content

Commit 12843a3

Browse files
committed
Firt timeline version
1 parent fee90b5 commit 12843a3

File tree

14 files changed

+226
-22
lines changed

14 files changed

+226
-22
lines changed

cmake/inference_lib.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ copy(inference_lib DEPENDS paddle_fluid_shared
7272
)
7373

7474
set(module "platform")
75-
copy(platform_lib
75+
copy(platform_lib DEPS profiler_py_proto
7676
SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/dynload/*.h ${src_dir}/${module}/details/*.h
7777
DSTS ${dst_dir}/${module} ${dst_dir}/${module}/dynload ${dst_dir}/${module}/details
7878
)

paddle/fluid/platform/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
proto_library(profiler_proto SRCS profiler.proto)
2+
py_proto_compile(profiler_py_proto SRCS profiler.proto)
3+
4+
add_custom_target(profiler_py_proto_init ALL COMMAND ${CMAKE_COMMAND} -E touch __init__.py)
5+
6+
add_dependencies(profiler_py_proto profiler_py_proto_init)
7+
8+
add_custom_command(TARGET profiler_py_proto POST_BUILD
9+
COMMAND ${CMAKE_COMMAND} -E make_directory ${PADDLE_SOURCE_DIR}/python/paddle/fluid/proto/profiler
10+
COMMAND cp *.py ${PADDLE_SOURCE_DIR}/python/paddle/fluid/proto/profiler
11+
COMMENT "Copy generated python proto into directory paddle/fluid/proto/profiler."
12+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
213

314
if(WITH_GPU)
415
cc_library(enforce SRCS enforce.cc DEPS)

paddle/fluid/platform/device_tracer.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/platform/device_tracer.h"
16+
#include <google/protobuf/text_format.h>
17+
#include <fstream>
1618
#include <map>
1719
#include <mutex>
1820
#include "glog/logging.h"
@@ -177,7 +179,7 @@ class DeviceTracerImpl : public DeviceTracer {
177179
enabled_ = true;
178180
}
179181

180-
proto::Profile GenProfile() {
182+
proto::Profile GenProfile(const std::string &profile_path) {
181183
std::lock_guard<std::mutex> l(trace_mu_);
182184
proto::Profile profile_pb;
183185
profile_pb.set_start_ns(start_ns_);
@@ -196,13 +198,12 @@ class DeviceTracerImpl : public DeviceTracer {
196198
event->set_device_id(r.device_id);
197199
event_times[event->name()].push_back(r.end_ns - r.start_ns);
198200
}
199-
for (const auto &et : event_times) {
200-
fprintf(
201-
stderr, "%s: total: %fms invoked cuda kernels: %lu\n",
202-
et.first.c_str(),
203-
std::accumulate(et.second.begin(), et.second.end(), 0) / 1000000.0,
204-
et.second.size());
205-
}
201+
std::string profile_str;
202+
google::protobuf::TextFormat::PrintToString(profile_pb, &profile_str);
203+
std::ofstream profile_f;
204+
profile_f.open(profile_path, std::ios::out | std::ios::trunc);
205+
profile_f << profile_str;
206+
profile_f.close();
206207
return profile_pb;
207208
}
208209

@@ -259,7 +260,9 @@ class DeviceTracerDummy : public DeviceTracer {
259260

260261
void Enable() {}
261262

262-
proto::Profile GenProfile() { return proto::Profile(); }
263+
proto::Profile GenProfile(const std::string &profile_path) {
264+
return proto::Profile();
265+
}
263266

264267
void Disable() {}
265268
};

paddle/fluid/platform/device_tracer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class DeviceTracer {
5555
uint32_t correlation_id) = 0;
5656

5757
// Generate a proto after done (Disabled).
58-
virtual proto::Profile GenProfile() = 0;
58+
virtual proto::Profile GenProfile(const std::string& profile_path) = 0;
5959

6060
virtual bool IsEnabled() = 0;
6161
};

paddle/fluid/platform/profiler.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ std::vector<std::vector<Event>> GetAllEvents() {
199199
return result;
200200
}
201201

202-
void DisableProfiler(EventSortingKey sorted_key) {
202+
void DisableProfiler(EventSortingKey sorted_key,
203+
const std::string& profile_path) {
203204
PADDLE_ENFORCE(g_state != ProfilerState::kDisabled,
204205
"Can't disable profiling, since it's not starting.");
205206
// Mark the profiling stop.
@@ -209,7 +210,7 @@ void DisableProfiler(EventSortingKey sorted_key) {
209210
DeviceTracer* tracer = GetDeviceTracer();
210211
if (g_profiler_place == "All" && tracer && tracer->IsEnabled()) {
211212
tracer->Disable();
212-
tracer->GenProfile();
213+
tracer->GenProfile(profile_path);
213214
}
214215

215216
std::vector<std::vector<Event>> all_events = GetAllEvents();

paddle/fluid/platform/profiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ void EnableProfiler(ProfilerState state);
140140
// Clear the g_all_event_lists, which is total event lists of all threads.
141141
void ResetProfiler();
142142

143-
void DisableProfiler(EventSortingKey sorted_key);
143+
void DisableProfiler(EventSortingKey sorted_key,
144+
const std::string& profile_path);
144145

145146
// Parse the event list and output the profiling report
146147
void ParseEvents(std::vector<std::vector<Event>>&,

paddle/fluid/platform/profiler_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,5 @@ TEST(RecordEvent, RecordEvent) {
125125
EXPECT_EQ(start_profiler_count, 1);
126126

127127
// Will remove parsing-related code from test later
128-
DisableProfiler(EventSortingKey::kTotal);
128+
DisableProfiler(EventSortingKey::kTotal, "/tmp/profiler");
129129
}

python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ add_custom_command(OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp
5757
COMMAND ${CMAKE_COMMAND} -E touch ${PADDLE_PYTHON_BUILD_DIR}/.timestamp
5858
COMMAND ${CMAKE_COMMAND} -E remove_directory ${PADDLE_PYTHON_BUILD_DIR}/lib-python
5959
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PADDLE_PYTHON_BUILD_DIR}/lib* ${PADDLE_PYTHON_BUILD_DIR}/lib-python
60-
DEPENDS gen_proto_py copy_paddle_pybind framework_py_proto ${PY_FILES} ${external_project_dependencies} ${COPY_PADDLE_MASTER})
60+
DEPENDS gen_proto_py copy_paddle_pybind framework_py_proto profiler_py_proto ${PY_FILES} ${external_project_dependencies} ${COPY_PADDLE_MASTER})
6161

6262
set(paddle_python_deps ${PADDLE_PYTHON_BUILD_DIR}/.timestamp paddle_pserver_main paddle_trainer paddle_merge_model ${MKL_DEPENDS})
6363
if(WITH_SWIG_PY)

python/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

python/paddle/fluid/profiler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def reset_profiler():
7373

7474

7575
@contextmanager
76-
def profiler(state, sorted_key=None):
76+
def profiler(state, sorted_key=None, profile_path='/tmp/profile'):
7777
"""The profiler interface.
7878
Different from cuda_profiler, this profiler can be used to profile both CPU
7979
and GPU program. By defalut, it records the CPU and GPU operator kernels,
@@ -95,8 +95,9 @@ def profiler(state, sorted_key=None):
9595
The `max` means sorting by the maximum execution time.
9696
The `min` means sorting by the minimum execution time.
9797
The `ave` means sorting by the average execution time.
98+
profile_path (string) : If state == 'All', it will write a profile
99+
proto output file.
98100
"""
99-
100101
if state not in ['CPU', 'GPU', "All"]:
101102
raise ValueError("The state must be 'CPU' or 'GPU' or 'All'.")
102103
if state == "GPU":
@@ -122,4 +123,4 @@ def profiler(state, sorted_key=None):
122123
}
123124
# TODO(qingqing) : redirect C++ ostream to Python stream.
124125
# with core.ostream_redirect(stdout=True, stderr=True):
125-
core.disable_profiler(key_map[sorted_key])
126+
core.disable_profiler(key_map[sorted_key], profile_path)

0 commit comments

Comments
 (0)