general_model.h 6.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 <algorithm>
G
guru4elephant 已提交
21
#include <fstream>
M
MRXLT 已提交
22
#include <map>
G
guru4elephant 已提交
23
#include <string>
24
#include <utility>  // move
G
guru4elephant 已提交
25 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 34

using baidu::paddle_serving::sdk_cpp::Predictor;
using baidu::paddle_serving::sdk_cpp::PredictorApi;

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

G
guru4elephant 已提交
38 39 40 41 42
// given some input data, pack into pb, and send request
namespace baidu {
namespace paddle_serving {
namespace general_model {

B
barrierye 已提交
43
class ModelRes {
44
 public:
B
barrierye 已提交
45
  ModelRes() {}
B
barrierye 已提交
46 47 48 49 50 51 52 53 54 55 56 57
  ModelRes(const ModelRes& res) {
    _engine_name = res._engine_name;
    _int64_map.insert(res._int64_map.begin(), res._int64_map.end());
    _float_map.insert(res._float_map.begin(), res._float_map.end());
  }
  ModelRes(ModelRes&& res) {
    _engine_name = std::move(res._engine_name);
    _int64_map.insert(std::make_move_iterator(std::begin(res._int64_map)),
                      std::make_move_iterator(std::end(res._int64_map)));
    _float_map.insert(std::make_move_iterator(std::begin(res._float_map)),
                      std::make_move_iterator(std::end(res._float_map)));
  }
B
barrierye 已提交
58
  ~ModelRes() {}
59 60
  const std::vector<int64_t>& get_int64_by_name(const std::string& name) {
    return _int64_value_map[name];
61
  }
62 63 64 65 66 67 68 69
  const std::vector<float>& get_float_by_name(const std::string& name) {
    return _float_value_map[name];
  }
  const std::vector<int>& get_shape(const std::string& name) {
    return _shape_map[name];
  }
  const std::vector<int>& get_lod(const std::string& name) {
    return _lod_map[name];
70
  }
B
barrierye 已提交
71 72 73
  void set_engine_name(const std::string& engine_name) {
    _engine_name = engine_name;
  }
B
barrierye 已提交
74 75
  const std::string& engine_name() { return _engine_name; }
  ModelRes& operator=(ModelRes&& res) {
B
barrierye 已提交
76
    if (this != &res) {
B
barrierye 已提交
77 78 79 80 81
      _engine_name = std::move(res._engine_name);
      _int64_map.insert(std::make_move_iterator(std::begin(res._int64_map)),
                        std::make_move_iterator(std::end(res._int64_map)));
      _float_map.insert(std::make_move_iterator(std::begin(res._float_map)),
                        std::make_move_iterator(std::end(res._float_map)));
B
barrierye 已提交
82 83 84
    }
    return *this;
  }
B
barrierye 已提交
85

B
barrierye 已提交
86
 public:
B
barrierye 已提交
87
  std::string _engine_name;
88 89 90 91
  std::map<std::string, std::vector<int64_t>> _int64_value_map;
  std::map<std::string, std::vector<float>> _float_value_map;
  std::map<std::string, std::vector<int>> _shape_map;
  std::map<std::string, std::vector<int>> _lod_map;
B
barrierye 已提交
92 93 94 95 96 97 98 99
};

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

 public:
B
barrierye 已提交
100 101
  void clear() {
    _models.clear();
B
barrierye 已提交
102
    _engine_names.clear();
B
barrierye 已提交
103
  }
B
barrierye 已提交
104 105
  const std::vector<int64_t>& get_int64_by_name(const int model_idx,
                                                const std::string& name) {
B
barrierye 已提交
106 107
    return _models[model_idx].get_int64_by_name(name);
  }
B
barrierye 已提交
108 109
  const std::vector<float>& get_float_by_name(const int model_idx,
                                              const std::string& name) {
B
barrierye 已提交
110 111
    return _models[model_idx].get_float_by_name(name);
  }
B
barrierye 已提交
112 113 114 115 116 117 118 119
  const std::vector<int>& get_shape(const int model_idx,
                                    const std::string& name) {
    return _models[model_idx].get_shape(name);
  }
  const std::vector<int>& get_lod(const int model_idx,
                                  const std::string& name) {
    return _models[model_idx].get_lod(name);
  }
B
barrierye 已提交
120 121
  void add_model_res(ModelRes&& res) {
    _engine_names.push_back(res.engine_name());
B
barrierye 已提交
122
    _models.emplace_back(std::move(res));
B
barrierye 已提交
123
  }
124 125 126 127
  void set_variant_tag(const std::string& variant_tag) {
    _variant_tag = variant_tag;
  }
  const std::string& variant_tag() { return _variant_tag; }
B
barrierye 已提交
128
  const std::vector<std::string>& get_engine_names() { return _engine_names; }
129 130

 private:
B
barrierye 已提交
131
  std::vector<ModelRes> _models;
132
  std::string _variant_tag;
B
barrierye 已提交
133
  std::vector<std::string> _engine_names;
134
};
G
guru4elephant 已提交
135 136 137 138 139 140

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

141 142
  void init_gflags(std::vector<std::string> argv);

143
  int init(const std::string& client_conf);
G
guru4elephant 已提交
144

M
MRXLT 已提交
145 146
  void set_predictor_conf(const std::string& conf_path,
                          const std::string& conf_file);
G
guru4elephant 已提交
147

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

G
guru4elephant 已提交
150 151
  int create_predictor();

152
  int destroy_predictor();
153

M
MRXLT 已提交
154 155 156
  int batch_predict(
      const std::vector<std::vector<std::vector<float>>>& float_feed_batch,
      const std::vector<std::string>& float_feed_name,
D
dongdaxiang 已提交
157
      const std::vector<std::vector<int>>& float_shape,
M
MRXLT 已提交
158 159
      const std::vector<std::vector<std::vector<int64_t>>>& int_feed_batch,
      const std::vector<std::string>& int_feed_name,
D
dongdaxiang 已提交
160
      const std::vector<std::vector<int>>& int_shape,
M
MRXLT 已提交
161
      const std::vector<std::string>& fetch_name,
M
MRXLT 已提交
162
      PredictorRes& predict_res_batch,  // NOLINT
M
MRXLT 已提交
163
      const int& pid);
M
MRXLT 已提交
164

G
guru4elephant 已提交
165 166
 private:
  PredictorApi _api;
M
MRXLT 已提交
167
  Predictor* _predictor;
G
guru4elephant 已提交
168 169 170 171 172 173
  std::string _predictor_conf;
  std::string _predictor_path;
  std::string _conf_file;
  std::map<std::string, int> _feed_name_to_idx;
  std::map<std::string, int> _fetch_name_to_idx;
  std::map<std::string, std::string> _fetch_name_to_var_name;
174
  std::map<std::string, int> _fetch_name_to_type;
M
MRXLT 已提交
175
  std::vector<std::vector<int>> _shape;
G
guru4elephant 已提交
176
  std::vector<int> _type;
G
guru4elephant 已提交
177
  std::vector<int64_t> _last_request_ts;
G
guru4elephant 已提交
178 179 180 181 182 183 184
};

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

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