client.h 7.5 KB
Newer Older
S
ShiningZhang 已提交
1
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
S
ShiningZhang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//
// 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 <string>
#include <vector>
#include <map>
#include <sstream>
20
#include <memory>
S
ShiningZhang 已提交
21 22 23

namespace baidu {
namespace paddle_serving {
24 25 26 27 28 29
namespace predictor {
  namespace general_model {
    class Request;
    class Response;
  }
}
S
ShiningZhang 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
namespace client {

class PredictorInputs;
class PredictorOutputs;

class ServingClient {
 public:
  ServingClient() {};

  virtual ~ServingClient() = default;

  int init(const std::vector<std::string>& client_conf,
           const std::string server_port);

  int load_client_config(const std::vector<std::string>& client_conf);

  virtual int connect(const std::string server_port) = 0;

  virtual int predict(const PredictorInputs& inputs,
                      PredictorOutputs& outputs,
S
ShiningZhang 已提交
50
                      const std::vector<std::string>& fetch_name,
S
ShiningZhang 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                      const uint64_t log_id) = 0;

 protected:
  std::map<std::string, int> _feed_name_to_idx;
  std::vector<std::string> _feed_name;
  std::map<std::string, int> _fetch_name_to_idx;
  std::map<std::string, std::string> _fetch_name_to_var_name;
  std::map<std::string, int> _fetch_name_to_type;
  std::vector<std::vector<int>> _shape;
  std::vector<int> _type;
  std::vector<int64_t> _last_request_ts;
};

class PredictorData {
 public:
  PredictorData() {};
  virtual ~PredictorData() {};

S
ShiningZhang 已提交
69 70 71
  void add_float_data(const std::vector<float>& data,
                      const std::string& name,
                      const std::vector<int>& shape,
72 73
                      const std::vector<int>& lod,
                      const int datatype = 1);
S
ShiningZhang 已提交
74

S
ShiningZhang 已提交
75 76 77
  void add_int64_data(const std::vector<int64_t>& data,
                      const std::string& name,
                      const std::vector<int>& shape,
78 79
                      const std::vector<int>& lod,
                      const int datatype = 0);
S
ShiningZhang 已提交
80

S
ShiningZhang 已提交
81 82 83
  void add_int32_data(const std::vector<int32_t>& data,
                      const std::string& name,
                      const std::vector<int>& shape,
84 85
                      const std::vector<int>& lod,
                      const int datatype = 2);
S
ShiningZhang 已提交
86

S
ShiningZhang 已提交
87 88 89
  void add_string_data(const std::string& data,
                       const std::string& name,
                       const std::vector<int>& shape,
90 91
                       const std::vector<int>& lod,
                       const int datatype = 3);
S
ShiningZhang 已提交
92

S
ShiningZhang 已提交
93
  const std::map<std::string, std::vector<float>>& float_data_map() const {
S
ShiningZhang 已提交
94 95 96
    return _float_data_map;
  };

S
ShiningZhang 已提交
97
  std::map<std::string, std::vector<float>>* mutable_float_data_map() {
S
ShiningZhang 已提交
98 99 100
    return &_float_data_map;
  };

S
ShiningZhang 已提交
101
  const std::map<std::string, std::vector<int64_t>>& int64_data_map() const {
S
ShiningZhang 已提交
102 103 104
    return _int64_data_map;
  };

S
ShiningZhang 已提交
105
  std::map<std::string, std::vector<int64_t>>* mutable_int64_data_map() {
S
ShiningZhang 已提交
106 107 108
    return &_int64_data_map;
  };

S
ShiningZhang 已提交
109
  const std::map<std::string, std::vector<int32_t>>& int_data_map() const {
S
ShiningZhang 已提交
110 111 112
    return _int32_data_map;
  };

S
ShiningZhang 已提交
113
  std::map<std::string, std::vector<int32_t>>* mutable_int_data_map() {
S
ShiningZhang 已提交
114 115 116
    return &_int32_data_map;
  };

S
ShiningZhang 已提交
117
  const std::map<std::string, std::string>& string_data_map() const {
S
ShiningZhang 已提交
118 119 120
    return _string_data_map;
  };

S
ShiningZhang 已提交
121
  std::map<std::string, std::string>* mutable_string_data_map() {
S
ShiningZhang 已提交
122 123 124
    return &_string_data_map;
  };

S
ShiningZhang 已提交
125
  const std::map<std::string, std::vector<int>>& shape_map() const {
S
ShiningZhang 已提交
126 127 128
    return _shape_map;
  };

S
ShiningZhang 已提交
129
  std::map<std::string, std::vector<int>>* mutable_shape_map() {
S
ShiningZhang 已提交
130 131 132
    return &_shape_map;
  };

S
ShiningZhang 已提交
133
  const std::map<std::string, std::vector<int>>& lod_map() const {
S
ShiningZhang 已提交
134 135 136
    return _lod_map;
  };

S
ShiningZhang 已提交
137
  std::map<std::string, std::vector<int>>* mutable_lod_map() {
S
ShiningZhang 已提交
138 139 140
    return &_lod_map;
  };

141 142
  int get_datatype(std::string name) const;

S
ShiningZhang 已提交
143
  std::string print();
S
ShiningZhang 已提交
144 145

 private:
S
ShiningZhang 已提交
146
  // used to print vector data map e.g. _float_data_map
S
ShiningZhang 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  template<typename T1, typename T2>
  std::string map2string(const std::map<T1, std::vector<T2>>& map) {
    std::ostringstream oss;
    oss.str("");
    oss.precision(6);
	  oss.setf(std::ios::fixed);
    std::string key_seg = ":";
    std::string val_seg = ",";
    std::string end_seg = "\n";
    typename std::map<T1, std::vector<T2>>::const_iterator it = map.begin();
    typename std::map<T1, std::vector<T2>>::const_iterator itEnd = map.end();
    for (; it != itEnd; it++) {
      oss << "{";
      oss << it->first << key_seg;
      const std::vector<T2>& v = it->second;
      for (size_t i = 0; i < v.size(); ++i) {
        if (i != v.size() - 1) {
          oss << v[i] << val_seg;
        }
        else {
          oss << v[i];
        }
      }
      oss << "}";
    }
    return oss.str();
  };

S
ShiningZhang 已提交
175
  // used to print data map without vector e.g. _string_data_map
S
ShiningZhang 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  template<typename T1, typename T2>
  std::string map2string(const std::map<T1, T2>& map) {
    std::ostringstream oss;
    oss.str("");
    std::string key_seg = ":";
    std::string val_seg = ",";
    std::string end_seg = "\n";
    typename std::map<T1, T2>::const_iterator it = map.begin();
    typename std::map<T1, T2>::const_iterator itEnd = map.end();
    for (; it != itEnd; it++) {
      oss << "{";
      oss << it->first << key_seg << it->second;
      oss << "}";
    }
    return oss.str();
  };

 protected:
  std::map<std::string, std::vector<float>> _float_data_map;
  std::map<std::string, std::vector<int64_t>> _int64_data_map;
  std::map<std::string, std::vector<int32_t>> _int32_data_map;
  std::map<std::string, std::string> _string_data_map;
  std::map<std::string, std::vector<int>> _shape_map;
  std::map<std::string, std::vector<int>> _lod_map;
200
  std::map<std::string, int> _datatype_map;
S
ShiningZhang 已提交
201 202 203 204 205 206
};

class PredictorInputs : public PredictorData {
 public:
  PredictorInputs() {};
  virtual ~PredictorInputs() {};
207

S
ShiningZhang 已提交
208 209 210
  // generate proto from inputs
  // feed_name_to_idx: mapping alias name to idx
  // feed_name: mapping idx to name
S
ShiningZhang 已提交
211
  static int GenProto(const PredictorInputs& inputs,
212 213 214
                      const std::map<std::string, int>& feed_name_to_idx,
                      const std::vector<std::string>& feed_name,
                      predictor::general_model::Request& req);
S
ShiningZhang 已提交
215 216 217 218 219 220 221 222 223 224 225 226
};

class PredictorOutputs {
 public:
  struct PredictorOutput {
    std::string engine_name;
    PredictorData data;
  };

  PredictorOutputs() {};
  virtual ~PredictorOutputs() {};

S
ShiningZhang 已提交
227
  const std::vector<std::shared_ptr<PredictorOutputs::PredictorOutput>>& datas() {
S
ShiningZhang 已提交
228 229 230
    return _datas;
  };

S
ShiningZhang 已提交
231
  std::vector<std::shared_ptr<PredictorOutputs::PredictorOutput>>* mutable_datas() {
S
ShiningZhang 已提交
232 233 234
    return &_datas;
  };

S
ShiningZhang 已提交
235
  void add_data(const std::shared_ptr<PredictorOutputs::PredictorOutput>& data) {
236
    _datas.push_back(data);
S
ShiningZhang 已提交
237 238
  };

S
ShiningZhang 已提交
239
  std::string print();
240

S
ShiningZhang 已提交
241
  void clear();
242

S
ShiningZhang 已提交
243 244 245
  // Parse proto to outputs
  // fetch_name: name of data to be output
  // fetch_name_to_type: mapping of fetch_name to datatype
S
ShiningZhang 已提交
246
  static int ParseProto(const predictor::general_model::Response& res,
247 248 249
                        const std::vector<std::string>& fetch_name,
                        std::map<std::string, int>& fetch_name_to_type,
                        PredictorOutputs& outputs);
S
ShiningZhang 已提交
250 251

 protected:
252
  std::vector<std::shared_ptr<PredictorOutputs::PredictorOutput>> _datas;
S
ShiningZhang 已提交
253 254 255 256 257
};

}  // namespace client
}  // namespace paddle_serving
}  // namespace baidu