From e148bc15b0565404be4d880a0c8b00179da9a1a3 Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Thu, 15 Jun 2017 02:24:49 +0800 Subject: [PATCH] "remove unused tensor line" --- cmake/util.cmake | 1 + paddle/optimizer/CMakeLists.txt | 5 ++--- paddle/optimizer/Tensor.h | 6 +++++- paddle/optimizer/parameter_optimizer_test.cpp | 8 ++++---- paddle/optimizer/serialization.h | 4 +++- paddle/optimizer/serialization_test.cpp | 1 + 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cmake/util.cmake b/cmake/util.cmake index 8c914346222..87ad9d91d87 100644 --- a/cmake/util.cmake +++ b/cmake/util.cmake @@ -84,6 +84,7 @@ function(link_paddle_exe TARGET_NAME) paddle_parameter paddle_proto paddle_cuda + paddle_optimizer ${EXTERNAL_LIBS} ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS} diff --git a/paddle/optimizer/CMakeLists.txt b/paddle/optimizer/CMakeLists.txt index bafd8a9b97a..791be95efa9 100644 --- a/paddle/optimizer/CMakeLists.txt +++ b/paddle/optimizer/CMakeLists.txt @@ -9,9 +9,8 @@ set(OPITMIZER_SRCS sgd_optimizer.cc ) -add_library(optimizer STATIC ${OPITMIZER_SRCS}) -add_dependencies(optimizer gen_proto_cpp) +add_library(paddle_optimizer STATIC ${OPITMIZER_SRCS}) +add_dependencies(paddle_optimizer gen_proto_cpp) add_simple_unittest(serialization_test) add_simple_unittest(parameter_optimizer_test) -add_dependencies(parameter_optimizer_test optimizer) diff --git a/paddle/optimizer/Tensor.h b/paddle/optimizer/Tensor.h index b8f212e81ff..2eefdec3e5e 100644 --- a/paddle/optimizer/Tensor.h +++ b/paddle/optimizer/Tensor.h @@ -15,13 +15,17 @@ template class TensorT { public: TensorT(size_t size) : height_(1), width_(size) { data_ = new T[size]; } + TensorT(T* data, size_t size) : height_(1), width_(size), data_(data) {} - TensorT(T* data, size_t h, size_t w) : height_(h), width_(w), data_(data_) {} + + TensorT(T* data, size_t h, size_t w) : height_(h), width_(w), data_(data) {} + ~TensorT() { if (data_) delete data_; } T* get_buffer() { return this->data_; } + T& operator[](const size_t idx) { CHECK(idx >= 0 && idx < this->width_) << "out of index range"; return data_[idx]; diff --git a/paddle/optimizer/parameter_optimizer_test.cpp b/paddle/optimizer/parameter_optimizer_test.cpp index 8d3bfa9cf4b..afacd6d54a7 100644 --- a/paddle/optimizer/parameter_optimizer_test.cpp +++ b/paddle/optimizer/parameter_optimizer_test.cpp @@ -49,8 +49,8 @@ public: config_.set_lr_policy(OptimizerConfig::ConstLr); config_.mutable_const_lr()->set_learning_rate(0.1); - ParameterOptimizer* opt = - ParameterOptimizer::Create(config_.SerializeAsString(), parameter); + std::string str = config_.SerializeAsString(); + ParameterOptimizer* opt = ParameterOptimizer::Create(str, parameter); opts_.push_back(opt); opts_table_[opts_.size()] = OptimizerConfig::SGD; } @@ -64,8 +64,8 @@ public: config_.mutable_adam()->set_decay(0.0); config_.set_lr_policy(OptimizerConfig::ConstLr); config_.mutable_const_lr()->set_learning_rate(0.1); - ParameterOptimizer* opt = - ParameterOptimizer::Create(config_.SerializeAsString(), parameter); + std::string str = config_.SerializeAsString(); + ParameterOptimizer* opt = ParameterOptimizer::Create(str, parameter); opts_.push_back(opt); opts_table_[opts_.size()] = OptimizerConfig::Adam; } diff --git a/paddle/optimizer/serialization.h b/paddle/optimizer/serialization.h index 21de3259a88..92fbf65cc6b 100644 --- a/paddle/optimizer/serialization.h +++ b/paddle/optimizer/serialization.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -16,7 +17,7 @@ static void TensorToProto(const Tensor& tensor, TensorProto* proto) { for (size_t i = 0; i < tensor.size(); ++i) { os << tensor[i]; proto->add_content(os.str()); - os.clear(); + os.str(std::string()); } } @@ -25,6 +26,7 @@ static void ProtoToTensor(const TensorProto& proto, Tensor* tensor) { for (auto i = 0; i < proto.content_size(); ++i) { sin << proto.content(i); sin >> (*tensor)[i]; + sin.str(std::string()); sin.clear(); } } diff --git a/paddle/optimizer/serialization_test.cpp b/paddle/optimizer/serialization_test.cpp index 98fbdf5a5e2..d2454140dc2 100644 --- a/paddle/optimizer/serialization_test.cpp +++ b/paddle/optimizer/serialization_test.cpp @@ -10,6 +10,7 @@ TEST(TensorToProto, Case1) { t[i] = i; t1[i] = 0; } + TensorProto proto; TensorToProto(t, &proto); ProtoToTensor(proto, &t1); -- GitLab