From f0cd5142afbde12182ba532fbbb89c3b4e491f34 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Fri, 22 Sep 2017 11:13:42 -0700 Subject: [PATCH 1/2] Change namespace of pybind.cc to pybind --- paddle/framework/tensor.h | 7 +++++-- paddle/pybind/pybind.cc | 15 +++++++-------- paddle/pybind/tensor_py.h | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h index 4b5a2ae523..6a14cfde4b 100644 --- a/paddle/framework/tensor.h +++ b/paddle/framework/tensor.h @@ -29,16 +29,19 @@ limitations under the License. */ namespace paddle { -namespace framework { +namespace pybind { namespace details { template struct CastToPyBufferImpl; } +} // namespace pybind + +namespace framework { class Tensor { public: template - friend struct details::CastToPyBufferImpl; + friend struct pybind::details::CastToPyBufferImpl; template friend struct EigenTensor; diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index c7009a604f..9f5451340d 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -34,12 +34,7 @@ limitations under the License. */ namespace py = pybind11; namespace paddle { -namespace framework { - -using Tensor = framework::Tensor; -using LoDTensor = framework::LoDTensor; -using LoD = framework::LoD; - +namespace pybind { static size_t UniqueIntegerGenerator() { static std::atomic generator; return generator.fetch_add(1); @@ -56,6 +51,10 @@ bool IsCompileGPU() { PYBIND11_PLUGIN(core) { py::module m("core", "C++ core of PaddlePaddle"); + // using framework in this function. Since it is inside a function, it will + // not cause namespace pollution. + using namespace paddle::framework; // NOLINT + py::class_(m, "Tensor", py::buffer_protocol()) .def_buffer( [](Tensor &self) -> py::buffer_info { return CastToPyBuffer(self); }) @@ -63,7 +62,7 @@ PYBIND11_PLUGIN(core) { [](const Tensor &self) { return vectorize(self.dims()); }) .def("set_dims", [](Tensor &self, const std::vector &dim) { - self.Resize(make_ddim(dim)); + self.Resize(framework::make_ddim(dim)); }) .def("alloc_float", [](Tensor &self, paddle::platform::GPUPlace &place) { @@ -317,5 +316,5 @@ All parameter, weight, gradient are variables in Paddle. return m.ptr(); } -} // namespace framework +} // namespace pybind } // namespace paddle diff --git a/paddle/pybind/tensor_py.h b/paddle/pybind/tensor_py.h index 95171acf72..bcfba84a1a 100644 --- a/paddle/pybind/tensor_py.h +++ b/paddle/pybind/tensor_py.h @@ -23,7 +23,7 @@ namespace py = pybind11; namespace paddle { -namespace framework { +namespace pybind { namespace details { From b5bb6c2cab12e1c10f95b8a31470a71853a41df2 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Fri, 22 Sep 2017 11:30:55 -0700 Subject: [PATCH 2/2] Remove namespace framework in pybind --- paddle/pybind/pybind.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index 9f5451340d..fbe074188e 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -62,7 +62,7 @@ PYBIND11_PLUGIN(core) { [](const Tensor &self) { return vectorize(self.dims()); }) .def("set_dims", [](Tensor &self, const std::vector &dim) { - self.Resize(framework::make_ddim(dim)); + self.Resize(make_ddim(dim)); }) .def("alloc_float", [](Tensor &self, paddle::platform::GPUPlace &place) { @@ -106,7 +106,7 @@ PYBIND11_PLUGIN(core) { #ifdef PADDLE_ONLY_CPU new (&instance) LoDTensor(lod); #else - paddle::framework::LoD new_lod; + LoD new_lod; new_lod.reserve(lod.size()); std::copy(lod.begin(), lod.end(), std::back_inserter(new_lod)); new (&instance) LoDTensor(new_lod); @@ -117,7 +117,7 @@ PYBIND11_PLUGIN(core) { #ifdef PADDLE_ONLY_CPU self.set_lod(lod); #else - paddle::framework::LoD new_lod; + LoD new_lod; new_lod.reserve(lod.size()); std::copy(lod.begin(), lod.end(), std::back_inserter(new_lod)); self.set_lod(new_lod); @@ -131,7 +131,7 @@ PYBIND11_PLUGIN(core) { std::vector> new_lod; new_lod.reserve(lod.size()); std::transform(lod.begin(), lod.end(), std::back_inserter(new_lod), - [](paddle::framework::Vector item) -> + [](Vector item) -> std::vector { std::vector v; v.reserve(item.size());