general_model.h 11.0 KB
Newer Older
G
guru4elephant 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2019 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.
#pragma once

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

M
MRXLT 已提交
20
#include <pybind11/numpy.h>
M
MRXLT 已提交
21
#include <algorithm>
G
guru4elephant 已提交
22
#include <fstream>
M
MRXLT 已提交
23
#include <map>
G
guru4elephant 已提交
24
#include <string>
25
#include <utility>  // move
G
guru4elephant 已提交
26
#include <vector>
G
guru4elephant 已提交
27 28 29 30
#include "core/sdk-cpp/builtin_format.pb.h"
#include "core/sdk-cpp/general_model_service.pb.h"
#include "core/sdk-cpp/include/common.h"
#include "core/sdk-cpp/include/predictor_sdk.h"
G
guru4elephant 已提交
31 32 33
using baidu::paddle_serving::sdk_cpp::Predictor;
using baidu::paddle_serving::sdk_cpp::PredictorApi;

34 35 36
DECLARE_bool(profile_client);
DECLARE_bool(profile_server);

G
guru4elephant 已提交
37
// given some input data, pack into pb, and send request
M
MRXLT 已提交
38
namespace py = pybind11;
G
guru4elephant 已提交
39 40 41 42
namespace baidu {
namespace paddle_serving {
namespace general_model {

B
barrierye 已提交
43
class ModelRes {
44
 public:
B
barrierye 已提交
45
  ModelRes() {}
B
barrierye 已提交
46 47
  ModelRes(const ModelRes& res) {
    _engine_name = res._engine_name;
B
barrierye 已提交
48 49 50 51
    _int64_value_map.insert(res._int64_value_map.begin(),
                            res._int64_value_map.end());
    _float_value_map.insert(res._float_value_map.begin(),
                            res._float_value_map.end());
M
MRXLT 已提交
52 53
    _int32_value_map.insert(res._int32_value_map.begin(),
                            res._int32_value_map.end());
B
fix bug  
barrierye 已提交
54 55
    _shape_map.insert(res._shape_map.begin(), res._shape_map.end());
    _lod_map.insert(res._lod_map.begin(), res._lod_map.end());
H
HexToString 已提交
56 57 58
    _tensor_alias_names.insert(_tensor_alias_names.end(),
                               res._tensor_alias_names.begin(),
                               res._tensor_alias_names.end());
B
barrierye 已提交
59 60 61
  }
  ModelRes(ModelRes&& res) {
    _engine_name = std::move(res._engine_name);
B
barrierye 已提交
62 63 64 65 66 67
    _int64_value_map.insert(
        std::make_move_iterator(std::begin(res._int64_value_map)),
        std::make_move_iterator(std::end(res._int64_value_map)));
    _float_value_map.insert(
        std::make_move_iterator(std::begin(res._float_value_map)),
        std::make_move_iterator(std::end(res._float_value_map)));
M
MRXLT 已提交
68 69 70
    _int32_value_map.insert(
        std::make_move_iterator(std::begin(res._int32_value_map)),
        std::make_move_iterator(std::end(res._int32_value_map)));
B
fix bug  
barrierye 已提交
71 72 73 74
    _shape_map.insert(std::make_move_iterator(std::begin(res._shape_map)),
                      std::make_move_iterator(std::end(res._shape_map)));
    _lod_map.insert(std::make_move_iterator(std::begin(res._lod_map)),
                    std::make_move_iterator(std::end(res._lod_map)));
H
HexToString 已提交
75 76 77 78
    _tensor_alias_names.insert(
        _tensor_alias_names.end(),
        std::make_move_iterator(std::begin(res._tensor_alias_names)),
        std::make_move_iterator(std::end(res._tensor_alias_names)));
B
barrierye 已提交
79
  }
B
barrierye 已提交
80
  ~ModelRes() {}
81 82
  const std::vector<int64_t>& get_int64_by_name(const std::string& name) {
    return _int64_value_map[name];
83
  }
B
barrierye 已提交
84 85 86
  std::vector<int64_t>&& get_int64_by_name_with_rv(const std::string& name) {
    return std::move(_int64_value_map[name]);
  }
87 88 89
  const std::vector<float>& get_float_by_name(const std::string& name) {
    return _float_value_map[name];
  }
B
barrierye 已提交
90 91 92
  std::vector<float>&& get_float_by_name_with_rv(const std::string& name) {
    return std::move(_float_value_map[name]);
  }
M
MRXLT 已提交
93 94 95 96 97 98
  const std::vector<int32_t>& get_int32_by_name(const std::string& name) {
    return _int32_value_map[name];
  }
  std::vector<int32_t>&& get_int32_by_name_with_rv(const std::string& name) {
    return std::move(_int32_value_map[name]);
  }
99
  const std::vector<int>& get_shape_by_name(const std::string& name) {
100 101
    return _shape_map[name];
  }
102 103 104 105
  std::vector<int>&& get_shape_by_name_with_rv(const std::string& name) {
    return std::move(_shape_map[name]);
  }
  const std::vector<int>& get_lod_by_name(const std::string& name) {
106
    return _lod_map[name];
107
  }
108 109 110
  std::vector<int>&& get_lod_by_name_with_rv(const std::string& name) {
    return std::move(_lod_map[name]);
  }
B
barrierye 已提交
111 112 113
  void set_engine_name(const std::string& engine_name) {
    _engine_name = engine_name;
  }
B
barrierye 已提交
114
  const std::string& engine_name() { return _engine_name; }
H
HexToString 已提交
115 116 117 118

  const std::vector<std::string>& tensor_alias_names() {
    return _tensor_alias_names;
  }
B
barrierye 已提交
119
  ModelRes& operator=(ModelRes&& res) {
B
barrierye 已提交
120
    if (this != &res) {
B
barrierye 已提交
121
      _engine_name = std::move(res._engine_name);
B
barrierye 已提交
122 123 124 125 126 127
      _int64_value_map.insert(
          std::make_move_iterator(std::begin(res._int64_value_map)),
          std::make_move_iterator(std::end(res._int64_value_map)));
      _float_value_map.insert(
          std::make_move_iterator(std::begin(res._float_value_map)),
          std::make_move_iterator(std::end(res._float_value_map)));
M
MRXLT 已提交
128 129 130
      _int32_value_map.insert(
          std::make_move_iterator(std::begin(res._int32_value_map)),
          std::make_move_iterator(std::end(res._int32_value_map)));
B
fix bug  
barrierye 已提交
131 132 133 134
      _shape_map.insert(std::make_move_iterator(std::begin(res._shape_map)),
                        std::make_move_iterator(std::end(res._shape_map)));
      _lod_map.insert(std::make_move_iterator(std::begin(res._lod_map)),
                      std::make_move_iterator(std::end(res._lod_map)));
H
HexToString 已提交
135 136 137 138
      _tensor_alias_names.insert(
          _tensor_alias_names.end(),
          std::make_move_iterator(std::begin(res._tensor_alias_names)),
          std::make_move_iterator(std::end(res._tensor_alias_names)));
B
barrierye 已提交
139 140 141
    }
    return *this;
  }
B
barrierye 已提交
142

B
barrierye 已提交
143
 public:
B
barrierye 已提交
144
  std::string _engine_name;
145 146
  std::map<std::string, std::vector<int64_t>> _int64_value_map;
  std::map<std::string, std::vector<float>> _float_value_map;
M
MRXLT 已提交
147
  std::map<std::string, std::vector<int32_t>> _int32_value_map;
148 149
  std::map<std::string, std::vector<int>> _shape_map;
  std::map<std::string, std::vector<int>> _lod_map;
H
HexToString 已提交
150
  std::vector<std::string> _tensor_alias_names;
B
barrierye 已提交
151 152 153 154 155 156 157 158
};

class PredictorRes {
 public:
  PredictorRes() {}
  ~PredictorRes() {}

 public:
B
barrierye 已提交
159 160
  void clear() {
    _models.clear();
B
barrierye 已提交
161
    _engine_names.clear();
B
barrierye 已提交
162
  }
B
barrierye 已提交
163 164
  const std::vector<int64_t>& get_int64_by_name(const int model_idx,
                                                const std::string& name) {
B
barrierye 已提交
165 166
    return _models[model_idx].get_int64_by_name(name);
  }
B
barrierye 已提交
167 168 169 170
  std::vector<int64_t>&& 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));
  }
B
barrierye 已提交
171 172
  const std::vector<float>& get_float_by_name(const int model_idx,
                                              const std::string& name) {
B
barrierye 已提交
173 174
    return _models[model_idx].get_float_by_name(name);
  }
B
barrierye 已提交
175 176 177 178
  std::vector<float>&& 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));
  }
M
MRXLT 已提交
179 180 181 182 183 184 185 186
  const std::vector<int32_t>& get_int32_by_name(const int model_idx,
                                                const std::string& name) {
    return _models[model_idx].get_int32_by_name(name);
  }
  std::vector<int32_t>&& get_int32_by_name_with_rv(const int model_idx,
                                                   const std::string& name) {
    return std::move(_models[model_idx].get_int32_by_name_with_rv(name));
  }
187 188 189 190 191 192 193
  const std::vector<int>& get_shape_by_name(const int model_idx,
                                            const std::string& name) {
    return _models[model_idx].get_shape_by_name(name);
  }
  const std::vector<int>&& get_shape_by_name_with_rv(const int model_idx,
                                                     const std::string& name) {
    return std::move(_models[model_idx].get_shape_by_name_with_rv(name));
B
barrierye 已提交
194
  }
195 196 197 198 199 200 201
  const std::vector<int>& get_lod_by_name(const int model_idx,
                                          const std::string& name) {
    return _models[model_idx].get_lod_by_name(name);
  }
  const std::vector<int>&& get_lod_by_name_with_rv(const int model_idx,
                                                   const std::string& name) {
    return std::move(_models[model_idx].get_lod_by_name_with_rv(name));
B
barrierye 已提交
202
  }
B
barrierye 已提交
203 204
  void add_model_res(ModelRes&& res) {
    _engine_names.push_back(res.engine_name());
B
barrierye 已提交
205
    _models.emplace_back(std::move(res));
B
barrierye 已提交
206
  }
207 208 209 210
  void set_variant_tag(const std::string& variant_tag) {
    _variant_tag = variant_tag;
  }
  const std::string& variant_tag() { return _variant_tag; }
B
barrierye 已提交
211
  const std::vector<std::string>& get_engine_names() { return _engine_names; }
H
HexToString 已提交
212 213 214 215
  const std::vector<std::string>& get_tensor_alias_names(const int model_idx) {
    _tensor_alias_names = _models[model_idx].tensor_alias_names();
    return _tensor_alias_names;
  }
216 217

 private:
B
barrierye 已提交
218
  std::vector<ModelRes> _models;
219
  std::string _variant_tag;
B
barrierye 已提交
220
  std::vector<std::string> _engine_names;
H
HexToString 已提交
221
  std::vector<std::string> _tensor_alias_names;
222
};
G
guru4elephant 已提交
223 224 225 226 227 228

class PredictorClient {
 public:
  PredictorClient() {}
  ~PredictorClient() {}

229 230
  void init_gflags(std::vector<std::string> argv);

H
HexToString 已提交
231
  int init(const std::vector<std::string>& client_conf);
G
guru4elephant 已提交
232

M
MRXLT 已提交
233 234
  void set_predictor_conf(const std::string& conf_path,
                          const std::string& conf_file);
G
guru4elephant 已提交
235

M
MRXLT 已提交
236
  int create_predictor_by_desc(const std::string& sdk_desc);
G
guru4elephant 已提交
237

G
guru4elephant 已提交
238 239
  int create_predictor();

240
  int destroy_predictor();
241

H
HexToString 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  int numpy_predict(const std::vector<py::array_t<float>>& float_feed,
                    const std::vector<std::string>& float_feed_name,
                    const std::vector<std::vector<int>>& float_shape,
                    const std::vector<std::vector<int>>& float_lod_slot_batch,
                    const std::vector<py::array_t<int64_t>>& int_feed,
                    const std::vector<std::string>& int_feed_name,
                    const std::vector<std::vector<int>>& int_shape,
                    const std::vector<std::vector<int>>& int_lod_slot_batch,
                    const std::vector<std::string>& string_feed,
                    const std::vector<std::string>& string_feed_name,
                    const std::vector<std::vector<int>>& string_shape,
                    const std::vector<std::vector<int>>& string_lod_slot_batch,
                    const std::vector<std::string>& fetch_name,
                    PredictorRes& predict_res_batch,  // NOLINT
                    const int& pid,
                    const uint64_t log_id);
M
MRXLT 已提交
258

G
guru4elephant 已提交
259 260
 private:
  PredictorApi _api;
M
MRXLT 已提交
261
  Predictor* _predictor;
G
guru4elephant 已提交
262 263 264 265
  std::string _predictor_conf;
  std::string _predictor_path;
  std::string _conf_file;
  std::map<std::string, int> _feed_name_to_idx;
H
HexToString 已提交
266
  std::vector<std::string> _feed_name;
G
guru4elephant 已提交
267 268
  std::map<std::string, int> _fetch_name_to_idx;
  std::map<std::string, std::string> _fetch_name_to_var_name;
269
  std::map<std::string, int> _fetch_name_to_type;
M
MRXLT 已提交
270
  std::vector<std::vector<int>> _shape;
G
guru4elephant 已提交
271
  std::vector<int> _type;
G
guru4elephant 已提交
272
  std::vector<int64_t> _last_request_ts;
G
guru4elephant 已提交
273 274 275 276 277 278 279
};

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

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */