pybind_general_model.cpp 4.4 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 35
           [](PredictorRes &self, int model_idx, std::string &name) {
             return self.get_int64_by_name(model_idx, name);
M
MRXLT 已提交
36 37
           },
           py::return_value_policy::reference)
38
      .def("get_float_by_name",
B
barrierye 已提交
39 40
           [](PredictorRes &self, int model_idx, std::string &name) {
             return self.get_float_by_name(model_idx, name);
M
MRXLT 已提交
41
           },
42
           py::return_value_policy::reference)
43
      .def("variant_tag",
B
barrierye 已提交
44
           [](PredictorRes &self) { return self.variant_tag(); })
B
barrierye 已提交
45 46
      .def("model_num",
           [](PredictorRes &self) {return self.model_num(); });
47

G
guru4elephant 已提交
48 49
  py::class_<PredictorClient>(m, "PredictorClient", py::buffer_protocol())
      .def(py::init())
50 51 52 53
      .def("init_gflags",
           [](PredictorClient &self, std::vector<std::string> argv) {
             self.init_gflags(argv);
           })
G
guru4elephant 已提交
54
      .def("init",
M
MRXLT 已提交
55
           [](PredictorClient &self, const std::string &conf) {
56
             return self.init(conf);
G
guru4elephant 已提交
57 58
           })
      .def("set_predictor_conf",
M
MRXLT 已提交
59 60 61
           [](PredictorClient &self,
              const std::string &conf_path,
              const std::string &conf_file) {
G
guru4elephant 已提交
62 63
             self.set_predictor_conf(conf_path, conf_file);
           })
G
guru4elephant 已提交
64
      .def("create_predictor_by_desc",
M
MRXLT 已提交
65 66 67
           [](PredictorClient &self, const std::string &sdk_desc) {
             self.create_predictor_by_desc(sdk_desc);
           })
G
guru4elephant 已提交
68
      .def("create_predictor",
M
MRXLT 已提交
69
           [](PredictorClient &self) { self.create_predictor(); })
70 71
      .def("destroy_predictor",
           [](PredictorClient &self) { self.destroy_predictor(); })
72 73
      .def("predict",
           [](PredictorClient &self,
M
MRXLT 已提交
74 75 76 77 78 79 80
              const std::vector<std::vector<float>> &float_feed,
              const std::vector<std::string> &float_feed_name,
              const std::vector<std::vector<int64_t>> &int_feed,
              const std::vector<std::string> &int_feed_name,
              const std::vector<std::string> &fetch_name,
              PredictorRes &predict_res,
              const int &pid) {
81 82 83 84 85
             return self.predict(float_feed,
                                 float_feed_name,
                                 int_feed,
                                 int_feed_name,
                                 fetch_name,
M
MRXLT 已提交
86 87
                                 predict_res,
                                 pid);
88
           })
M
MRXLT 已提交
89
      .def("batch_predict",
M
MRXLT 已提交
90 91 92 93 94 95 96
           [](PredictorClient &self,
              const std::vector<std::vector<std::vector<float>>>
                  &float_feed_batch,
              const std::vector<std::string> &float_feed_name,
              const std::vector<std::vector<std::vector<int64_t>>>
                  &int_feed_batch,
              const std::vector<std::string> &int_feed_name,
M
MRXLT 已提交
97
              const std::vector<std::string> &fetch_name,
M
MRXLT 已提交
98
              PredictorRes &predict_res_batch,
M
MRXLT 已提交
99
              const int &pid) {
M
MRXLT 已提交
100 101 102 103
             return self.batch_predict(float_feed_batch,
                                       float_feed_name,
                                       int_feed_batch,
                                       int_feed_name,
M
MRXLT 已提交
104
                                       fetch_name,
M
MRXLT 已提交
105
                                       predict_res_batch,
M
MRXLT 已提交
106
                                       pid);
G
guru4elephant 已提交
107 108 109 110 111 112
           });
}

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