general_model.h 5.7 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 24 25
#include <string>
#include <vector>

G
guru4elephant 已提交
26 27 28 29
#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"
B
barrierye 已提交
30 31 32
#define BLOG(fmt, ...) \
  printf(              \
      "[%s:%s]:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
G
guru4elephant 已提交
33 34 35 36

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

37 38 39
DECLARE_bool(profile_client);
DECLARE_bool(profile_server);

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

B
barrierye 已提交
45
class ModelRes {
46
 public:
B
barrierye 已提交
47
  ModelRes() {}
B
barrierye 已提交
48 49 50 51 52 53 54 55 56 57 58 59
  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 已提交
60
  ~ModelRes() {}
M
MRXLT 已提交
61 62
  const std::vector<std::vector<int64_t>>& get_int64_by_name(
      const std::string& name) {
63 64
    return _int64_map[name];
  }
M
MRXLT 已提交
65 66
  const std::vector<std::vector<float>>& get_float_by_name(
      const std::string& name) {
67 68
    return _float_map[name];
  }
B
barrierye 已提交
69 70 71
  void set_engine_name(const std::string& engine_name) {
    _engine_name = engine_name;
  }
B
barrierye 已提交
72 73
  const std::string& engine_name() { return _engine_name; }
  ModelRes& operator=(ModelRes&& res) {
B
barrierye 已提交
74
    if (this != &res) {
B
barrierye 已提交
75 76 77 78 79
      _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 已提交
80 81 82
    }
    return *this;
  }
B
barrierye 已提交
83

B
barrierye 已提交
84
 public:
B
barrierye 已提交
85
  std::string _engine_name;
B
barrierye 已提交
86 87 88 89 90 91 92 93 94 95
  std::map<std::string, std::vector<std::vector<int64_t>>> _int64_map;
  std::map<std::string, std::vector<std::vector<float>>> _float_map;
};

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

 public:
B
barrierye 已提交
96 97
  void clear() {
    _models.clear();
B
barrierye 已提交
98
    _engine_names.clear();
B
barrierye 已提交
99
  }
B
barrierye 已提交
100 101 102 103 104 105 106 107
  const std::vector<std::vector<int64_t>>& get_int64_by_name(
      const int model_idx, const std::string& name) {
    return _models[model_idx].get_int64_by_name(name);
  }
  const std::vector<std::vector<float>>& get_float_by_name(
      const int model_idx, const std::string& name) {
    return _models[model_idx].get_float_by_name(name);
  }
B
barrierye 已提交
108 109
  void add_model_res(ModelRes&& res) {
    _engine_names.push_back(res.engine_name());
B
barrierye 已提交
110
    _models.emplace_back(std::move(res));
B
barrierye 已提交
111
  }
112 113 114 115
  void set_variant_tag(const std::string& variant_tag) {
    _variant_tag = variant_tag;
  }
  const std::string& variant_tag() { return _variant_tag; }
B
barrierye 已提交
116
  const std::vector<std::string>& get_engine_names() { return _engine_names; }
117 118

 private:
B
barrierye 已提交
119
  std::vector<ModelRes> _models;
120
  std::string _variant_tag;
B
barrierye 已提交
121
  std::vector<std::string> _engine_names;
122
};
G
guru4elephant 已提交
123 124 125 126 127 128

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

129 130
  void init_gflags(std::vector<std::string> argv);

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

M
MRXLT 已提交
133 134
  void set_predictor_conf(const std::string& conf_path,
                          const std::string& conf_file);
G
guru4elephant 已提交
135

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

G
guru4elephant 已提交
138
  int create_predictor();
139
  int destroy_predictor();
G
guru4elephant 已提交
140

141 142 143 144 145
  int predict(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,
M
MRXLT 已提交
146 147
              PredictorRes& predict_res,  // NOLINT
              const int& pid);
148

M
MRXLT 已提交
149 150 151 152 153 154
  int batch_predict(
      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,
      const std::vector<std::string>& fetch_name,
M
MRXLT 已提交
155
      PredictorRes& predict_res_batch,  // NOLINT
M
MRXLT 已提交
156
      const int& pid);
M
MRXLT 已提交
157

G
guru4elephant 已提交
158 159
 private:
  PredictorApi _api;
M
MRXLT 已提交
160
  Predictor* _predictor;
G
guru4elephant 已提交
161 162 163 164 165 166
  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;
167
  std::map<std::string, int> _fetch_name_to_type;
M
MRXLT 已提交
168
  std::vector<std::vector<int>> _shape;
G
guru4elephant 已提交
169
  std::vector<int> _type;
G
guru4elephant 已提交
170
  std::vector<int64_t> _last_request_ts;
G
guru4elephant 已提交
171 172 173 174 175 176 177
};

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

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