diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h index 4b5a2ae523f2f7fde5445f0534cd99969ad9d59e..f040c09c089ec75c9773d752685be5e232e8f4b7 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; @@ -165,12 +168,6 @@ class Tensor { /*! points to dimensions of memory block. */ DDim dims_; - /** - * A cache of the number of elements in a tensor. - * Would be 0 for an uninitialized tensor. - */ - int64_t numel_; - /** * @brief A PlaceHolder may be shared by more than one tensor. * diff --git a/paddle/framework/tensor_impl.h b/paddle/framework/tensor_impl.h index 6d2c14f4c47afb755b1c74f6dc4dd10ab25ed191..a5405f9c31543b5733f9db923c2a6f8b968cfc2d 100644 --- a/paddle/framework/tensor_impl.h +++ b/paddle/framework/tensor_impl.h @@ -147,13 +147,12 @@ inline Tensor Tensor::Slice(const int& begin_idx, const int& end_idx) const { inline Tensor& Tensor::Resize(const DDim& dims) { dims_ = dims; - numel_ = product(dims_); return *this; } inline const DDim& Tensor::dims() const { return dims_; } -inline int64_t Tensor::numel() const { return numel_; } +inline int64_t Tensor::numel() const { return product(dims_); } template inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) { diff --git a/paddle/operators/elementwise_add_op.h b/paddle/operators/elementwise_add_op.h index 42a7a29e991adcce98c74c62bb359a2cc4a7c30d..9e9f1ffba6fb23f5394713c67aa4363b85717f50 100644 --- a/paddle/operators/elementwise_add_op.h +++ b/paddle/operators/elementwise_add_op.h @@ -12,6 +12,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +#pragma once + #include "paddle/operators/elementwise_op.h" namespace paddle { diff --git a/paddle/operators/elementwise_div_op.h b/paddle/operators/elementwise_div_op.h index 6ef60cdf8dfc01f7cf51bb68f18900effb444fca..9bd7c8ea548c46ec9b4c5a085e4e70d5dd162f3a 100644 --- a/paddle/operators/elementwise_div_op.h +++ b/paddle/operators/elementwise_div_op.h @@ -12,6 +12,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +#pragma once + #include "paddle/operators/elementwise_op.h" namespace paddle { diff --git a/paddle/operators/elementwise_sub_op.h b/paddle/operators/elementwise_sub_op.h index faa38cf4014e98b2a28f51b32585ca5c10289841..f6bc66cd0e1594a8bc7070e2f182401b92d1c88e 100644 --- a/paddle/operators/elementwise_sub_op.h +++ b/paddle/operators/elementwise_sub_op.h @@ -12,6 +12,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#pragma once #include "paddle/operators/elementwise_op.h" namespace paddle { diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index c7009a604f60cda11434ad33b6c7d7caee1befdd..fbe074188e5870de4b00fa4fff733035739974ea 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); }) @@ -107,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); @@ -118,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); @@ -132,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()); @@ -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 95171acf729a513e5c92d1e0cba15cb12b38561a..bcfba84a1aa6e646cf255dc4612dfda42169fc44 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 {