general_model.cpp 5.2 KB
Newer Older
G
guru4elephant 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

B
barrierye 已提交
15
#include "general_model.h"  // NOLINT
G
guru4elephant 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <fstream>
#include "sdk-cpp/builtin_format.pb.h"
#include "sdk-cpp/include/common.h"
#include "sdk-cpp/include/predictor_sdk.h"

using baidu::paddle_serving::predictor::general_model::Request;
using baidu::paddle_serving::predictor::general_model::Response;
using baidu::paddle_serving::predictor::general_model::Tensor;
using baidu::paddle_serving::predictor::general_model::FeedInst;
using baidu::paddle_serving::predictor::general_model::FetchInst;

namespace baidu {
namespace paddle_serving {
namespace general_model {

B
barrierye 已提交
31
void PredictorClient::init(const std::string &conf_file) {
G
guru4elephant 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  _conf_file = conf_file;
  std::ifstream fin(conf_file);
  if (!fin) {
    LOG(ERROR) << "Your inference conf file can not be found";
    exit(-1);
  }
  _feed_name_to_idx.clear();
  _fetch_name_to_idx.clear();
  _shape.clear();
  int feed_var_num = 0;
  int fetch_var_num = 0;
  fin >> feed_var_num >> fetch_var_num;
  std::string name;
  std::string fetch_var_name;
  int shape_num = 0;
  int dim = 0;
  for (int i = 0; i < feed_var_num; ++i) {
    fin >> name;
    _feed_name_to_idx[name] = i;
    fin >> shape_num;
    std::vector<int> tmp_feed_shape;
    for (int j = 0; j < shape_num; ++j) {
      fin >> dim;
      tmp_feed_shape.push_back(dim);
    }
    _shape.push_back(tmp_feed_shape);
  }

  for (int i = 0; i < fetch_var_num; ++i) {
    fin >> name;
    fin >> fetch_var_name;
    _fetch_name_to_idx[name] = i;
    _fetch_name_to_var_name[name] = fetch_var_name;
  }
}

B
barrierye 已提交
68 69
void PredictorClient::set_predictor_conf(const std::string &conf_path,
                                         const std::string &conf_file) {
G
guru4elephant 已提交
70 71
  _predictor_path = conf_path;
  _predictor_conf = conf_file;
G
guru4elephant 已提交
72 73
}

G
guru4elephant 已提交
74 75 76 77 78 79 80 81
int PredictorClient::create_predictor() {
  if (_api.create(_predictor_path.c_str(), _predictor_conf.c_str()) != 0) {
    LOG(ERROR) << "Predictor Creation Failed";
    return -1;
  }
  _api.thrd_initialize();
}

B
barrierye 已提交
82 83 84 85 86 87
void PredictorClient::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,
                              FetchedMap *fetch_result) {
G
guru4elephant 已提交
88 89
  _api.thrd_clear();
  _predictor = _api.fetch_predictor("general_model");
G
guru4elephant 已提交
90 91
  Request req;
  std::vector<Tensor *> tensor_vec;
B
barrierye 已提交
92 93
  FeedInst *inst = req.add_insts();
  for (auto &name : float_feed_name) {
G
guru4elephant 已提交
94 95 96
    tensor_vec.push_back(inst->add_tensor_array());
  }

B
barrierye 已提交
97
  for (auto &name : int_feed_name) {
G
guru4elephant 已提交
98 99 100 101
    tensor_vec.push_back(inst->add_tensor_array());
  }

  int vec_idx = 0;
B
barrierye 已提交
102
  for (auto &name : float_feed_name) {
G
guru4elephant 已提交
103
    int idx = _feed_name_to_idx[name];
B
barrierye 已提交
104
    Tensor *tensor = tensor_vec[idx];
G
guru4elephant 已提交
105 106 107 108
    for (int j = 0; j < _shape[idx].size(); ++j) {
      tensor->add_shape(_shape[idx][j]);
    }
    tensor->set_elem_type(1);
G
guru4elephant 已提交
109
    for (int j = 0; j < float_feed[vec_idx].size(); ++j) {
B
barrierye 已提交
110 111
      tensor->add_data((char *)(&(float_feed[vec_idx][j])),  // NOLINT
                       sizeof(float));
G
guru4elephant 已提交
112
    }
G
guru4elephant 已提交
113 114 115 116
    vec_idx++;
  }

  vec_idx = 0;
B
barrierye 已提交
117
  for (auto &name : int_feed_name) {
G
guru4elephant 已提交
118
    int idx = _feed_name_to_idx[name];
B
barrierye 已提交
119
    Tensor *tensor = tensor_vec[idx];
G
guru4elephant 已提交
120 121 122 123
    for (int j = 0; j < _shape[idx].size(); ++j) {
      tensor->add_shape(_shape[idx][j]);
    }
    tensor->set_elem_type(0);
G
guru4elephant 已提交
124
    for (int j = 0; j < int_feed[vec_idx].size(); ++j) {
B
barrierye 已提交
125 126
      tensor->add_data((char *)(&(int_feed[vec_idx][j])),  // NOLINT
                       sizeof(int64_t));
G
guru4elephant 已提交
127 128
    }
    vec_idx++;
G
guru4elephant 已提交
129 130
  }

G
guru4elephant 已提交
131
  // std::map<std::string, std::vector<float> > result;
G
guru4elephant 已提交
132
  Response res;
G
guru4elephant 已提交
133 134

  res.Clear();
G
guru4elephant 已提交
135
  if (_predictor->inference(&req, &res) != 0) {
G
guru4elephant 已提交
136 137 138
    LOG(ERROR) << "failed call predictor with req: " << req.ShortDebugString();
    exit(-1);
  } else {
B
barrierye 已提交
139
    for (auto &name : fetch_name) {
G
guru4elephant 已提交
140
      int idx = _fetch_name_to_idx[name];
G
guru4elephant 已提交
141 142 143
      int len = res.insts(0).tensor_array(idx).data_size();
      (*fetch_result)[name].resize(len);
      for (int i = 0; i < len; ++i) {
B
barrierye 已提交
144 145
        (*fetch_result)[name][i] =
            *(const float *)res.insts(0).tensor_array(idx).data(i).c_str();
G
guru4elephant 已提交
146
      }
G
guru4elephant 已提交
147 148 149
    }
  }

G
guru4elephant 已提交
150
  return;
G
guru4elephant 已提交
151 152
}

G
guru4elephant 已提交
153
void PredictorClient::predict_with_profile(
B
barrierye 已提交
154 155 156 157 158 159
    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,
    FetchedMap *fetch_result) {
G
guru4elephant 已提交
160
  return;
G
guru4elephant 已提交
161 162 163 164 165
}

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