pybind_general_model.cpp 6.6 KB
Newer Older
M
MRXLT 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

G
guru4elephant 已提交
15 16
#include <Python.h>
#include <pybind11/pybind11.h>
M
MRXLT 已提交
17
#include <pybind11/stl.h>
G
guru4elephant 已提交
18
#include <unordered_map>
G
guru4elephant 已提交
19
#include "core/general-client/include/general_model.h"
G
guru4elephant 已提交
20 21 22 23 24 25 26 27 28 29

namespace py = pybind11;

namespace baidu {
namespace paddle_serving {
namespace general_model {

PYBIND11_MODULE(serving_client, m) {
  m.doc() = R"pddoc(this is a practice
       )pddoc";
30 31 32 33

  py::class_<PredictorRes>(m, "PredictorRes", py::buffer_protocol())
      .def(py::init())
      .def("get_int64_by_name",
B
barrierye 已提交
34
           [](PredictorRes &self, int model_idx, std::string &name) {
B
barrierye 已提交
35
             // see more: https://github.com/pybind/pybind11/issues/1042
B
barrierye 已提交
36 37 38 39 40 41 42
             std::vector<int64_t> *ptr = new std::vector<int64_t>(
                 std::move(self.get_int64_by_name_with_rv(model_idx, name)));
             auto capsule = py::capsule(ptr, [](void *p) {
               delete reinterpret_cast<std::vector<int64_t> *>(p);
             });
             return py::array(ptr->size(), ptr->data(), capsule);
           })
43
      .def("get_float_by_name",
B
barrierye 已提交
44
           [](PredictorRes &self, int model_idx, std::string &name) {
B
barrierye 已提交
45 46 47 48 49 50 51
             std::vector<float> *ptr = new std::vector<float>(
                 std::move(self.get_float_by_name_with_rv(model_idx, name)));
             auto capsule = py::capsule(ptr, [](void *p) {
               delete reinterpret_cast<std::vector<float> *>(p);
             });
             return py::array(ptr->size(), ptr->data(), capsule);
           })
52
      .def("get_shape",
B
barrierye 已提交
53
           [](PredictorRes &self, int model_idx, std::string &name) {
54 55 56 57 58 59 60
             std::vector<int> *ptr = new std::vector<int>(
                 std::move(self.get_shape_by_name_with_rv(model_idx, name)));
             auto capsule = py::capsule(ptr, [](void *p) {
               delete reinterpret_cast<std::vector<int> *>(p);
             });
             return py::array(ptr->size(), ptr->data(), capsule);
           })
61
      .def("get_lod",
B
barrierye 已提交
62
           [](PredictorRes &self, int model_idx, std::string &name) {
63 64 65 66 67 68 69
             std::vector<int> *ptr = new std::vector<int>(
                 std::move(self.get_lod_by_name_with_rv(model_idx, name)));
             auto capsule = py::capsule(ptr, [](void *p) {
               delete reinterpret_cast<std::vector<int> *>(p);
             });
             return py::array(ptr->size(), ptr->data(), capsule);
           })
B
barrierye 已提交
70 71 72
      .def("variant_tag", [](PredictorRes &self) { return self.variant_tag(); })
      .def("get_engine_names",
           [](PredictorRes &self) { return self.get_engine_names(); });
73

G
guru4elephant 已提交
74 75
  py::class_<PredictorClient>(m, "PredictorClient", py::buffer_protocol())
      .def(py::init())
76 77 78 79
      .def("init_gflags",
           [](PredictorClient &self, std::vector<std::string> argv) {
             self.init_gflags(argv);
           })
G
guru4elephant 已提交
80
      .def("init",
M
MRXLT 已提交
81
           [](PredictorClient &self, const std::string &conf) {
82
             return self.init(conf);
G
guru4elephant 已提交
83 84
           })
      .def("set_predictor_conf",
M
MRXLT 已提交
85 86 87
           [](PredictorClient &self,
              const std::string &conf_path,
              const std::string &conf_file) {
G
guru4elephant 已提交
88 89
             self.set_predictor_conf(conf_path, conf_file);
           })
G
guru4elephant 已提交
90
      .def("create_predictor_by_desc",
M
MRXLT 已提交
91 92 93
           [](PredictorClient &self, const std::string &sdk_desc) {
             self.create_predictor_by_desc(sdk_desc);
           })
G
guru4elephant 已提交
94
      .def("create_predictor",
M
MRXLT 已提交
95
           [](PredictorClient &self) { self.create_predictor(); })
96 97
      .def("destroy_predictor",
           [](PredictorClient &self) { self.destroy_predictor(); })
M
MRXLT 已提交
98
      .def("batch_predict",
M
MRXLT 已提交
99 100 101 102
           [](PredictorClient &self,
              const std::vector<std::vector<std::vector<float>>>
                  &float_feed_batch,
              const std::vector<std::string> &float_feed_name,
D
dongdaxiang 已提交
103
              const std::vector<std::vector<int>> &float_shape,
M
MRXLT 已提交
104 105 106
              const std::vector<std::vector<std::vector<int64_t>>>
                  &int_feed_batch,
              const std::vector<std::string> &int_feed_name,
D
dongdaxiang 已提交
107
              const std::vector<std::vector<int>> &int_shape,
M
MRXLT 已提交
108
              const std::vector<std::string> &fetch_name,
M
MRXLT 已提交
109
              PredictorRes &predict_res_batch,
110 111
              const int &pid,
              const uint64_t log_id) {
M
MRXLT 已提交
112 113
             return self.batch_predict(float_feed_batch,
                                       float_feed_name,
D
dongdaxiang 已提交
114
                                       float_shape,
M
MRXLT 已提交
115 116
                                       int_feed_batch,
                                       int_feed_name,
D
dongdaxiang 已提交
117
                                       int_shape,
M
MRXLT 已提交
118
                                       fetch_name,
M
MRXLT 已提交
119
                                       predict_res_batch,
120 121
                                       pid,
                                       log_id);
122 123
           },
           py::call_guard<py::gil_scoped_release>())
M
MRXLT 已提交
124 125 126 127 128 129 130 131 132 133 134 135
      .def("numpy_predict",
           [](PredictorClient &self,
              const std::vector<std::vector<py::array_t<float>>>
                  &float_feed_batch,
              const std::vector<std::string> &float_feed_name,
              const std::vector<std::vector<int>> &float_shape,
              const std::vector<std::vector<py::array_t<int64_t>>>
                  &int_feed_batch,
              const std::vector<std::string> &int_feed_name,
              const std::vector<std::vector<int>> &int_shape,
              const std::vector<std::string> &fetch_name,
              PredictorRes &predict_res_batch,
136 137
              const int &pid,
              const uint64_t log_id) {
M
MRXLT 已提交
138 139 140 141 142 143 144 145
             return self.numpy_predict(float_feed_batch,
                                       float_feed_name,
                                       float_shape,
                                       int_feed_batch,
                                       int_feed_name,
                                       int_shape,
                                       fetch_name,
                                       predict_res_batch,
146 147
                                       pid,
                                       log_id);
148 149
           },
           py::call_guard<py::gil_scoped_release>());
G
guru4elephant 已提交
150 151 152 153 154
}

}  // namespace general_model
}  // namespace paddle_serving
}  // namespace baidu