general_model.h 3.6 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"
G
guru4elephant 已提交
30 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 38 39 40 41
// given some input data, pack into pb, and send request
namespace baidu {
namespace paddle_serving {
namespace general_model {

42 43 44 45 46 47
class PredictorRes {
 public:
  PredictorRes() {}
  ~PredictorRes() {}

 public:
48 49
  const std::vector<int64_t>& get_int64_by_name(const std::string& name) {
    return _int64_value_map[name];
50
  }
51 52 53 54 55 56 57 58
  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];
59
  }
60 61 62 63
  void set_variant_tag(const std::string& variant_tag) {
    _variant_tag = variant_tag;
  }
  const std::string& variant_tag() { return _variant_tag; }
G
guru4elephant 已提交
64

65
 public:
66 67 68 69
  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;
70 71

 private:
72
  std::string _variant_tag;
73
};
G
guru4elephant 已提交
74 75 76 77 78 79

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

80 81
  void init_gflags(std::vector<std::string> argv);

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

M
MRXLT 已提交
84 85
  void set_predictor_conf(const std::string& conf_path,
                          const std::string& conf_file);
G
guru4elephant 已提交
86

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

G
guru4elephant 已提交
89 90
  int create_predictor();

91
  int destroy_predictor();
92

M
MRXLT 已提交
93 94 95
  int batch_predict(
      const std::vector<std::vector<std::vector<float>>>& float_feed_batch,
      const std::vector<std::string>& float_feed_name,
D
dongdaxiang 已提交
96
      const std::vector<std::vector<int>>& float_shape,
M
MRXLT 已提交
97 98
      const std::vector<std::vector<std::vector<int64_t>>>& int_feed_batch,
      const std::vector<std::string>& int_feed_name,
D
dongdaxiang 已提交
99
      const std::vector<std::vector<int>>& int_shape,
M
MRXLT 已提交
100
      const std::vector<std::string>& fetch_name,
M
MRXLT 已提交
101
      PredictorRes& predict_res_batch,  // NOLINT
M
MRXLT 已提交
102
      const int& pid);
M
MRXLT 已提交
103

G
guru4elephant 已提交
104 105
 private:
  PredictorApi _api;
M
MRXLT 已提交
106
  Predictor* _predictor;
G
guru4elephant 已提交
107 108 109 110 111 112
  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;
113
  std::map<std::string, int> _fetch_name_to_type;
M
MRXLT 已提交
114
  std::vector<std::vector<int>> _shape;
G
guru4elephant 已提交
115
  std::vector<int> _type;
G
guru4elephant 已提交
116
  std::vector<int64_t> _last_request_ts;
G
guru4elephant 已提交
117 118 119 120 121 122 123
};

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

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