From 17d93f4a04fa03517d84d20fd31829a7e02847b4 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Fri, 22 Sep 2017 10:38:04 -0700 Subject: [PATCH] Add Helper for Vector2Repeated --- paddle/pybind/pybind.cc | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index a6b583821f2..74f59040340 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -53,6 +53,25 @@ bool IsCompileGPU() { #endif } +template +inline std::vector RepeatedToVector( + const google::protobuf::RepeatedField &repeated_field) { + std::vector ret; + ret.reserve(repeated_field.size()); + std::copy( + repeated_field.begin(), repeated_field.end(), std::back_inserter(ret)); + return ret; +} + +template +inline void VectorToRepeated(const std::vector &vec, + RepeatedField *repeated_field) { + repeated_field->Reserve(vec.size()); + for (auto &elem : vec) { + *repeated_field->Add() = elem; + } +} + PYBIND11_PLUGIN(core) { py::module m("core", "C++ core of PaddlePaddle"); @@ -377,11 +396,7 @@ All parameter, weight, gradient are variables in Paddle. const std::string ¶meter, const std::vector &arguments) { var->set_parameter(parameter); - auto args = var->mutable_arguments(); - args->Reserve(static_cast(arguments.size())); - for (auto &arg : arguments) { - *args->Add() = arg; - } + VectorToRepeated(arguments, var->mutable_arguments()); }; auto op_desc_set_attr = [](OpDesc &desc, const std::string &name) { -- GitLab