diff --git a/core/general-client/include/general_model.h b/core/general-client/include/general_model.h index 7e04ae11f2106bc8e03fb9045976abc2460e1864..8a4c5e8c3c15bdcb59fc5faa7461713bf291bab4 100644 --- a/core/general-client/include/general_model.h +++ b/core/general-client/include/general_model.h @@ -69,9 +69,15 @@ class ModelRes { const std::vector& get_int64_by_name(const std::string& name) { return _int64_value_map[name]; } + std::vector&& get_int64_by_name_with_rv(const std::string& name) { + return std::move(_int64_value_map[name]); + } const std::vector& get_float_by_name(const std::string& name) { return _float_value_map[name]; } + std::vector&& get_float_by_name_with_rv(const std::string& name) { + return std::move(_float_value_map[name]); + } const std::vector& get_shape(const std::string& name) { return _shape_map[name]; } @@ -121,10 +127,18 @@ class PredictorRes { const std::string& name) { return _models[model_idx].get_int64_by_name(name); } + std::vector&& get_int64_by_name_with_rv(const int model_idx, + const std::string& name) { + return std::move(_models[model_idx].get_int64_by_name_with_rv(name)); + } const std::vector& get_float_by_name(const int model_idx, const std::string& name) { return _models[model_idx].get_float_by_name(name); } + std::vector&& get_float_by_name_with_rv(const int model_idx, + const std::string& name) { + return std::move(_models[model_idx].get_float_by_name_with_rv(name)); + } const std::vector& get_shape(const int model_idx, const std::string& name) { return _models[model_idx].get_shape(name); diff --git a/core/general-client/src/pybind_general_model.cpp b/core/general-client/src/pybind_general_model.cpp index b0d1d2d624d616a1df3805364cf7802cc19fc46b..8c59357abe81e2b35dd476cab0477f59da95a3a1 100644 --- a/core/general-client/src/pybind_general_model.cpp +++ b/core/general-client/src/pybind_general_model.cpp @@ -32,14 +32,26 @@ PYBIND11_MODULE(serving_client, m) { .def(py::init()) .def("get_int64_by_name", [](PredictorRes &self, int model_idx, std::string &name) { - return self.get_int64_by_name(model_idx, name); - }, - py::return_value_policy::reference) + // https://github.com/pybind/pybind11/issues/1042 + std::vector *ptr = new std::vector( + std::move(self.get_int64_by_name_with_rv(model_idx, name))); + auto capsule = py::capsule(ptr, [](void *p) { + delete reinterpret_cast *>(p); + }); + return py::array(ptr->size(), ptr->data(), capsule); + }) .def("get_float_by_name", [](PredictorRes &self, int model_idx, std::string &name) { - return self.get_float_by_name(model_idx, name); - }, - py::return_value_policy::reference) + std::vector *ptr = new std::vector( + std::move(self.get_float_by_name_with_rv(model_idx, name))); + auto capsule = py::capsule(ptr, [](void *p) { + delete reinterpret_cast *>(p); + }); + return py::array(ptr->size(), ptr->data(), capsule); + // return self.get_float_by_name(model_idx, name); + }) + //, + // py::return_value_policy::reference) .def("get_shape", [](PredictorRes &self, int model_idx, std::string &name) { return self.get_shape(model_idx, name); diff --git a/python/paddle_serving_client/__init__.py b/python/paddle_serving_client/__init__.py index b68fb8f9cb91986c58b245a989b3792083d265c7..95d74303dce0d932806e02549ec7a56d2f562446 100644 --- a/python/paddle_serving_client/__init__.py +++ b/python/paddle_serving_client/__init__.py @@ -319,7 +319,7 @@ class Client(object): if self.fetch_names_to_type_[name] == int_type: result_map[name] = result_batch.get_int64_by_name(mi, name) shape = result_batch.get_shape(mi, name) - result_map[name] = np.array(result_map[name], dtype='int64') + # result_map[name] = np.array(result_map[name], dtype='int64') result_map[name].shape = shape if name in self.lod_tensor_set: result_map["{}.lod".format(name)] = np.array( @@ -327,8 +327,8 @@ class Client(object): elif self.fetch_names_to_type_[name] == float_type: result_map[name] = result_batch.get_float_by_name(mi, name) shape = result_batch.get_shape(mi, name) - result_map[name] = np.array( - result_map[name], dtype='float32') + # result_map[name] = np.array( + # result_map[name], dtype='float32') result_map[name].shape = shape if name in self.lod_tensor_set: result_map["{}.lod".format(name)] = np.array(