general_reader_op.cpp 6.1 KB
Newer Older
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.

M
MRXLT 已提交
15
#include "core/general-server/op/general_reader_op.h"
16 17 18 19
#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
20
#include "core/general-server/op/general_infer_helper.h"
21 22
#include "core/predictor/framework/infer.h"
#include "core/predictor/framework/memory.h"
W
wangjiawei04 已提交
23
#include "core/predictor/framework/resource.h"
G
guru4elephant 已提交
24
#include "core/util/include/timer.h"
25 26 27 28 29

namespace baidu {
namespace paddle_serving {
namespace serving {

G
guru4elephant 已提交
30
using baidu::paddle_serving::Timer;
31 32 33 34 35
using baidu::paddle_serving::predictor::MempoolWrapper;
using baidu::paddle_serving::predictor::general_model::Tensor;
using baidu::paddle_serving::predictor::general_model::Request;
using baidu::paddle_serving::predictor::general_model::FeedInst;
using baidu::paddle_serving::predictor::PaddleGeneralModelConfig;
W
wangjiawei04 已提交
36
using baidu::paddle_serving::predictor::InferManager;
37 38 39 40 41

int conf_check(const Request *req,
               const std::shared_ptr<PaddleGeneralModelConfig> &model_config) {
  int var_num = req->insts(0).tensor_array_size();
  if (var_num != model_config->_feed_type.size()) {
B
barriery 已提交
42 43 44
    LOG(ERROR) << "feed var number not match: model config["
               << model_config->_feed_type.size() << "] vs. actual[" << var_num
               << "]";
45 46
    return -1;
  }
47 48 49

  VLOG(2) << "fetch var num in reader op: " << req->fetch_var_names_size();

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  for (int i = 0; i < var_num; ++i) {
    if (model_config->_feed_type[i] !=
        req->insts(0).tensor_array(i).elem_type()) {
      LOG(ERROR) << "feed type not match.";
      return -1;
    }
    if (model_config->_feed_shape[i].size() ==
        req->insts(0).tensor_array(i).shape_size()) {
      for (int j = 0; j < model_config->_feed_shape[i].size(); ++j) {
        req->insts(0).tensor_array(i).shape(j);
        if (model_config->_feed_shape[i][j] !=
            req->insts(0).tensor_array(i).shape(j)) {
          LOG(ERROR) << "feed shape not match.";
          return -1;
        }
      }
    } else {
      LOG(ERROR) << "feed shape not match.";
      return -1;
    }
  }
  return 0;
}

int GeneralReaderOp::inference() {
  // reade request from client
W
wangjiawei04 已提交
76 77
  // TODO: only support one engine here
  std::string engine_name = "general_infer_0";
W
wangjiawei04 已提交
78
  const Request *req = dynamic_cast<const Request *>(get_request_message());
W
wangjiawei04 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  uint64_t log_id = req->log_id();
  int input_var_num = 0;
  std::vector<int64_t> elem_type;
  std::vector<int64_t> elem_size;
  std::vector<int64_t> capacity;
  int var_num = req->insts(0).tensor_array_size();
  baidu::paddle_serving::predictor::Resource &resource =
      baidu::paddle_serving::predictor::Resource::instance();
  std::shared_ptr<PaddleGeneralModelConfig> model_config =
      resource.get_general_model_config();
  elem_type.resize(var_num);
  elem_size.resize(var_num);
  capacity.resize(var_num);
  for (int i = 0; i < var_num; ++i) {
    std::string tensor_name = model_config->_feed_name[i];
W
wangjiawei04 已提交
94
    VLOG(2) << "(logid=" << log_id << ") get tensor name: " << tensor_name;
W
wangjiawei04 已提交
95 96
    auto lod_tensor = InferManager::instance().GetInputHandle(
        engine_name.c_str(), tensor_name.c_str());
W
wangjiawei04 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    std::vector<std::vector<size_t>> lod;
    std::vector<int> shape;
    // get lod info here
    if (req->insts(0).tensor_array(i).lod_size() > 0) {
      lod.resize(1);
      for (int k = 0; k < req->insts(0).tensor_array(i).lod_size(); ++k) {
        lod[0].push_back(req->insts(0).tensor_array(i).lod(k));
      }
      capacity[i] = 1;
      for (int k = 0; k < req->insts(0).tensor_array(i).shape_size(); ++k) {
        int dim = req->insts(0).tensor_array(i).shape(k);
        VLOG(2) << "(logid=" << log_id << ") shape for var[" << i
                << "]: " << dim;
        capacity[i] *= dim;
        shape.push_back(dim);
      }
      VLOG(2) << "(logid=" << log_id << ") var[" << i
              << "] is tensor, capacity: " << capacity[i];
W
wangjiawei04 已提交
115
    } else {
W
wangjiawei04 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      capacity[i] = 1;
      for (int k = 0; k < req->insts(0).tensor_array(i).shape_size(); ++k) {
        int dim = req->insts(0).tensor_array(i).shape(k);
        VLOG(2) << "(logid=" << log_id << ") shape for var[" << i
                << "]: " << dim;
        capacity[i] *= dim;
        shape.push_back(dim);
      }
      VLOG(2) << "(logid=" << log_id << ") var[" << i
              << "] is tensor, capacity: " << capacity[i];
    }
    lod_tensor->SetLoD(lod);
    lod_tensor->Reshape(shape);
    // insert data here
    if (req->insts(0).tensor_array(i).elem_type() == 0) {
      // TODO: Copy twice here, can optimize
      int elem_num = req->insts(0).tensor_array(i).int64_data_size();
      std::vector<int64_t> data(elem_num);
W
wangjiawei04 已提交
134
      int64_t *dst_ptr = data.data();
W
wangjiawei04 已提交
135 136 137 138
      for (int k = 0; k < elem_num; ++k) {
        dst_ptr[k] = req->insts(0).tensor_array(i).int64_data(k);
      }
      lod_tensor->CopyFromCpu(dst_ptr);
W
wangjiawei04 已提交
139
    } else if (req->insts(0).tensor_array(i).elem_type() == 1) {
W
wangjiawei04 已提交
140 141
      int elem_num = req->insts(0).tensor_array(i).float_data_size();
      std::vector<float> data(elem_num);
W
wangjiawei04 已提交
142
      float *dst_ptr = data.data();
W
wangjiawei04 已提交
143 144 145 146
      for (int k = 0; k < elem_num; ++k) {
        dst_ptr[k] = req->insts(0).tensor_array(i).float_data(k);
      }
      lod_tensor->CopyFromCpu(dst_ptr);
W
wangjiawei04 已提交
147
    } else if (req->insts(0).tensor_array(i).elem_type() == 2) {
W
wangjiawei04 已提交
148 149
      int elem_num = req->insts(0).tensor_array(i).int_data_size();
      std::vector<int32_t> data(elem_num);
W
wangjiawei04 已提交
150
      int32_t *dst_ptr = data.data();
W
wangjiawei04 已提交
151 152 153 154 155 156
      for (int k = 0; k < elem_num; ++k) {
        dst_ptr[k] = req->insts(0).tensor_array(i).int_data(k);
      }
      lod_tensor->CopyFromCpu(dst_ptr);
    }
  }
157 158 159 160 161 162
  return 0;
}
DEFINE_OP(GeneralReaderOp);
}  // namespace serving
}  // namespace paddle_serving
}  // namespace baidu