Skip to content

Commit 579449b

Browse files
committed
Update comments and revert pybind11.
1 parent d2a7024 commit 579449b

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

cmake/external/pybind11.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ExternalProject_Add(
2626
extern_pybind
2727
${EXTERNAL_PROJECT_LOG_ARGS}
2828
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
29-
GIT_TAG "v2.2.1"
29+
GIT_TAG "v2.1.1"
3030
PREFIX ${PYBIND_SOURCE_DIR}
3131
UPDATE_COMMAND ""
3232
CONFIGURE_COMMAND ""

paddle/pybind/protobuf.cc

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,74 @@ limitations under the License. */
2121
#include "paddle/framework/program_desc.h"
2222
#include "paddle/framework/var_desc.h"
2323

24-
using boost::variant;
25-
24+
// Cast boost::variant for PyBind.
25+
// Copy from
26+
// https://github.com/pybind/pybind11/issues/576#issuecomment-269563199
2627
namespace pybind11 {
2728
namespace detail {
2829

30+
// Can be replaced by a generic lambda in C++14
31+
struct variant_caster_visitor : public boost::static_visitor<handle> {
32+
return_value_policy policy;
33+
handle parent;
34+
35+
variant_caster_visitor(return_value_policy policy, handle parent)
36+
: policy(policy), parent(parent) {}
37+
38+
template <class T>
39+
handle operator()(T const &src) const {
40+
return make_caster<T>::cast(src, policy, parent);
41+
}
42+
};
43+
44+
template <class Variant>
45+
struct variant_caster;
46+
47+
template <template <class...> class V, class... Ts>
48+
struct variant_caster<V<Ts...>> {
49+
using Type = V<Ts...>;
50+
51+
template <typename T>
52+
typename std::enable_if<
53+
!std::is_same<T, boost::detail::variant::void_>::value, bool>::type
54+
try_load(handle src, bool convert) {
55+
auto caster = make_caster<T>();
56+
if (!load_success_ && caster.load(src, convert)) {
57+
load_success_ = true;
58+
value = cast_op<T>(caster);
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
template <typename T>
65+
typename std::enable_if<std::is_same<T, boost::detail::variant::void_>::value,
66+
bool>::type
67+
try_load(handle src, bool convert) {
68+
return false;
69+
}
70+
71+
bool load(handle src, bool convert) {
72+
auto unused = {false, try_load<Ts>(src, convert)...};
73+
(void)(unused);
74+
return load_success_;
75+
}
76+
77+
static handle cast(Type const &src, return_value_policy policy,
78+
handle parent) {
79+
variant_caster_visitor visitor(policy, parent);
80+
return boost::apply_visitor(visitor, src);
81+
}
82+
83+
PYBIND11_TYPE_CASTER(Type, _("Variant"));
84+
bool load_success_{false};
85+
};
86+
2987
// Add specialization for concrete variant type
3088
template <class... Args>
3189
struct type_caster<boost::variant<Args...>>
3290
: variant_caster<boost::variant<Args...>> {};
3391

34-
template <>
35-
struct visit_helper<boost::variant> {
36-
template <typename... Args>
37-
static auto call(Args &&... args) -> decltype(boost::apply_visitor(args...)) {
38-
return boost::apply_visitor(args...);
39-
}
40-
};
41-
4292
} // namespace detail
4393
} // namespace pybind11
4494

paddle/pybind/pybind.cc

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

1515
#include "paddle/pybind/protobuf.h"
16-
#include "pybind11/iostream.h"
1716

1817
#include <mutex> // for call_once
1918
#include <unordered_map>
@@ -62,8 +61,8 @@ bool IsCompileGPU() {
6261
#endif
6362
}
6463

65-
PYBIND11_MODULE(core, m) {
66-
m.doc() = "C++ core of PaddlePaddle";
64+
PYBIND11_PLUGIN(core) {
65+
py::module m("core", "C++ core of PaddlePaddle");
6766

6867
// using framework in this function. Since it is inside a function, it will
6968
// not cause namespace pollution.
@@ -501,8 +500,7 @@ All parameter, weight, gradient are variables in Paddle.
501500
m.def("enable_profiler", platform::EnableProfiler);
502501
m.def("disable_profiler", platform::DisableProfiler);
503502
m.def("reset_profiler", platform::ResetProfiler);
504-
505-
py::add_ostream_redirect(m, "ostream_redirect");
503+
return m.ptr();
506504
}
507505
} // namespace pybind
508506
} // namespace paddle

python/paddle/v2/fluid/profiler.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,30 @@ def cuda_profiler(output_file, output_mode=None, config=None):
5252

5353

5454
def reset_profiler():
55+
"""The profiler clear interface.
56+
reset_profiler will clear the previous time record.
57+
"""
5558
core.reset_profiler()
5659

5760

5861
@contextmanager
5962
def profiler(state, sorted_key=None):
6063
"""The profiler interface.
61-
Different from cuda_profiler, this fuction can be used to profile both CPU
62-
and GPU program.
64+
Different from cuda_profiler, this profiler can be used to profile both CPU
65+
and GPU program. By defalut, it records the CPU and GPU operator kernels,
66+
if you want to profile other program, you can refer the profiling tutorial
67+
to add more records.
6368
6469
Args:
65-
state (string) : The profiler state, It should be 'CPU' or 'GPU'.
66-
sorted_key (string) : If None, the profiler results will be printed
67-
without sorting. Otherwise, the profiler results will be sorted
68-
by the this flag. This flag should be one of 'calls', 'total',
69-
'max', 'min' or 'ave'.
70-
The `calls` means sorting by the calling counter.
70+
state (string) : The profiling state, It should be 'CPU' or 'GPU'.
71+
Although users may define CPUPlace or CUDAPlace when using Fluid,
72+
the profiler doesn't get the state based on this Place. Since the
73+
implementation is an independent part from the Fluid.
74+
sorted_key (string) : If None, the profiling results will be printed
75+
in the order of first end time of events. Otherwise, the profiling
76+
results will be sorted by the this flag. This flag should be one
77+
of 'calls', 'total', 'max', 'min' or 'ave'.
78+
The `calls` means sorting by the number of calls.
7179
The `total` means sorting by the total execution time.
7280
The `max` means sorting by the maximum execution time.
7381
The `min` means sorting by the minimum execution time.
@@ -92,5 +100,6 @@ def profiler(state, sorted_key=None):
92100
'min': core.EventSortingKey.kMin,
93101
'ave': core.EventSortingKey.kAve,
94102
}
95-
with core.ostream_redirect(stdout=True, stderr=True):
96-
core.disable_profiler(key_map[sorted_key])
103+
# TODO(qingqing) : redirect C++ ostream to Python stream.
104+
# with core.ostream_redirect(stdout=True, stderr=True):
105+
core.disable_profiler(key_map[sorted_key])

0 commit comments

Comments
 (0)