diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index 543dbb739e672a797719e9b226232f58929229bc..5ccc8c377fb568ecfc68dd3dcce14c7ea971fbdf 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"); @@ -381,11 +400,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) {