general_reader_op.cpp 7.5 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"
G
guru4elephant 已提交
23
#include "core/util/include/timer.h"
24 25 26 27 28

namespace baidu {
namespace paddle_serving {
namespace serving {

G
guru4elephant 已提交
29
using baidu::paddle_serving::Timer;
30 31 32 33
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::PaddleGeneralModelConfig;
H
HexToString 已提交
34
enum ProtoDataType { P_INT64, P_FLOAT32, P_INT32, P_STRING };
35 36

int GeneralReaderOp::inference() {
H
HexToString 已提交
37
  // read request from client
W
wangjiawei04 已提交
38
  const Request *req = dynamic_cast<const Request *>(get_request_message());
39 40 41 42 43
  if (!req) {
    LOG(ERROR) << "Failed get request message";
    return -1;
  }

W
wangjiawei04 已提交
44 45
  uint64_t log_id = req->log_id();
  int input_var_num = 0;
H
HexToString 已提交
46 47 48

  GeneralBlob *res = mutable_data<GeneralBlob>();
  if (!res) {
49 50
    LOG(ERROR) << "(logid=" << log_id << ") Failed get GeneralBlob";
    return -1;
H
HexToString 已提交
51 52
  }

53 54 55 56 57 58 59
  TensorVector *out = &(res->tensor_vector);
  if (!out) {
    LOG(ERROR) << "(logid=" << log_id << ") Failed get tensor_vector of res";
    return -1;
  }

  res->SetLogId(log_id);
H
HexToString 已提交
60 61
  Timer timeline;
  int64_t start = timeline.TimeStampUS();
62
  // var_num means the number of feed_var.
H
HexToString 已提交
63
  int var_num = req->tensor_size();
H
HexToString 已提交
64

H
HexToString 已提交
65
  VLOG(2) << "(logid=" << log_id << ") var num: " << var_num
H
HexToString 已提交
66 67
          << ") start to call load general model_conf op";

W
wangjiawei04 已提交
68 69
  baidu::paddle_serving::predictor::Resource &resource =
      baidu::paddle_serving::predictor::Resource::instance();
H
HexToString 已提交
70 71 72 73 74

  VLOG(2) << "(logid=" << log_id << ") get resource pointer done.";

  // package tensor
  // prepare basic information for input
H
HexToString 已提交
75 76 77
  // specify the memory needed for output tensor_vector
  // fill the data into output general_blob
  int data_len = 0;
H
HexToString 已提交
78 79 80
  int64_t elem_type = 0;
  int64_t elem_size = 0;
  int64_t databuf_size = 0;
W
wangjiawei04 已提交
81
  for (int i = 0; i < var_num; ++i) {
H
HexToString 已提交
82
    paddle::PaddleTensor paddleTensor;
H
HexToString 已提交
83
    const Tensor &tensor = req->tensor(i);
H
HexToString 已提交
84
    data_len = 0;
H
HexToString 已提交
85 86 87 88 89 90 91 92
    elem_type = 0;
    elem_size = 0;
    databuf_size = 0;
    elem_type = tensor.elem_type();
    VLOG(2) << "var[" << i << "] has elem type: " << elem_type;
    if (elem_type == P_INT64) {  // int64
      elem_size = sizeof(int64_t);
      paddleTensor.dtype = paddle::PaddleDType::INT64;
H
HexToString 已提交
93
      data_len = tensor.int64_data_size();
H
HexToString 已提交
94 95 96
    } else if (elem_type == P_FLOAT32) {
      elem_size = sizeof(float);
      paddleTensor.dtype = paddle::PaddleDType::FLOAT32;
H
HexToString 已提交
97
      data_len = tensor.float_data_size();
H
HexToString 已提交
98 99 100
    } else if (elem_type == P_INT32) {
      elem_size = sizeof(int32_t);
      paddleTensor.dtype = paddle::PaddleDType::INT32;
H
HexToString 已提交
101
      data_len = tensor.int_data_size();
H
HexToString 已提交
102
    } else if (elem_type == P_STRING) {
103
      // use paddle::PaddleDType::UINT8 as for String.
H
HexToString 已提交
104 105
      elem_size = sizeof(char);
      paddleTensor.dtype = paddle::PaddleDType::UINT8;
106 107
      // this is for vector<String>, cause the databuf_size !=
      // vector<String>.size()*sizeof(char);
H
HexToString 已提交
108 109
      // data_len should be +1 cause '\0'
      // now only support single string
H
HexToString 已提交
110
      for (int idx = 0; idx < tensor.data_size(); idx++) {
H
HexToString 已提交
111
        data_len += tensor.data()[idx].length() + 1;
H
HexToString 已提交
112
      }
H
HexToString 已提交
113 114
    }
    // implement lod tensor here
H
HexToString 已提交
115
    // only support 1-D lod
H
HexToString 已提交
116
    // TODO(HexToString): support 2-D lod
H
HexToString 已提交
117
    if (tensor.lod_size() > 0) {
H
HexToString 已提交
118
      VLOG(2) << "(logid=" << log_id << ") var[" << i << "] is lod_tensor";
H
HexToString 已提交
119
      paddleTensor.lod.resize(1);
H
HexToString 已提交
120
      for (int k = 0; k < tensor.lod_size(); ++k) {
H
HexToString 已提交
121
        paddleTensor.lod[0].push_back(tensor.lod(k));
W
wangjiawei04 已提交
122 123
      }
    }
H
HexToString 已提交
124

H
HexToString 已提交
125 126
    for (int k = 0; k < tensor.shape_size(); ++k) {
      int dim = tensor.shape(k);
127
      VLOG(2) << "(logid=" << log_id << ") shape for var[" << i << "]: " << dim;
H
HexToString 已提交
128
      paddleTensor.shape.push_back(dim);
H
HexToString 已提交
129
    }
H
HexToString 已提交
130
    paddleTensor.name = tensor.name();
H
HexToString 已提交
131
    out->push_back(paddleTensor);
H
HexToString 已提交
132

H
HexToString 已提交
133 134
    VLOG(2) << "(logid=" << log_id << ") tensor size for var[" << i
            << "]: " << data_len;
H
HexToString 已提交
135
    databuf_size = data_len * elem_size;
H
HexToString 已提交
136 137 138 139
    void *databuf_char = MempoolWrapper::instance().malloc(databuf_size);
    paddle::PaddleBuf paddleBuf(databuf_char, databuf_size);
    out->at(i).data = paddleBuf;
    // out->at(i).data.Resize(databuf_size);
H
HexToString 已提交
140 141 142 143
    if (out->at(i).lod.size() > 0) {
      VLOG(2) << "(logid=" << log_id << ") var[" << i
              << "] has lod_tensor and len=" << out->at(i).lod[0].back();
    }
H
HexToString 已提交
144
    if (elem_type == P_INT64) {
H
HexToString 已提交
145 146
      int64_t *dst_ptr = static_cast<int64_t *>(out->at(i).data.data());
      VLOG(2) << "(logid=" << log_id << ") first element data in var[" << i
H
HexToString 已提交
147
              << "] is " << tensor.int64_data(0);
H
HexToString 已提交
148
      if (!dst_ptr) {
H
HexToString 已提交
149
        LOG(ERROR) << "dst_ptr is nullptr";
150
        return -1;
H
HexToString 已提交
151
      }
H
HexToString 已提交
152
      memcpy(dst_ptr, tensor.int64_data().data(), databuf_size);
H
HexToString 已提交
153
      /*
H
HexToString 已提交
154
      int elem_num = tensor.int64_data_size();
W
wangjiawei04 已提交
155
      for (int k = 0; k < elem_num; ++k) {
H
HexToString 已提交
156
        dst_ptr[k] = tensor.int64_data(k);
W
wangjiawei04 已提交
157
      }
H
HexToString 已提交
158
      */
H
HexToString 已提交
159
    } else if (elem_type == P_FLOAT32) {
H
HexToString 已提交
160 161
      float *dst_ptr = static_cast<float *>(out->at(i).data.data());
      VLOG(2) << "(logid=" << log_id << ") first element data in var[" << i
H
HexToString 已提交
162
              << "] is " << tensor.float_data(0);
H
HexToString 已提交
163
      if (!dst_ptr) {
H
HexToString 已提交
164
        LOG(ERROR) << "dst_ptr is nullptr";
165
        return -1;
H
HexToString 已提交
166
      }
H
HexToString 已提交
167
      memcpy(dst_ptr, tensor.float_data().data(), databuf_size);
H
HexToString 已提交
168
      /*int elem_num = tensor.float_data_size();
W
wangjiawei04 已提交
169
      for (int k = 0; k < elem_num; ++k) {
H
HexToString 已提交
170
        dst_ptr[k] = tensor.float_data(k);
H
HexToString 已提交
171
      }*/
H
HexToString 已提交
172
    } else if (elem_type == P_INT32) {
H
HexToString 已提交
173 174
      int32_t *dst_ptr = static_cast<int32_t *>(out->at(i).data.data());
      VLOG(2) << "(logid=" << log_id << ") first element data in var[" << i
H
HexToString 已提交
175
              << "] is " << tensor.int_data(0);
H
HexToString 已提交
176
      if (!dst_ptr) {
H
HexToString 已提交
177
        LOG(ERROR) << "dst_ptr is nullptr";
178
        return -1;
H
HexToString 已提交
179
      }
H
HexToString 已提交
180 181 182
      memcpy(dst_ptr, tensor.int_data().data(), databuf_size);
    } else if (elem_type == P_STRING) {
      char *dst_ptr = static_cast<char *>(out->at(i).data.data());
H
HexToString 已提交
183
      VLOG(2) << "(logid=" << log_id << ") first element data in var[" << i
H
HexToString 已提交
184
              << "] is " << tensor.data(0);
H
HexToString 已提交
185 186
      if (!dst_ptr) {
        LOG(ERROR) << "dst_ptr is nullptr";
187
        return -1;
H
HexToString 已提交
188
      }
H
HexToString 已提交
189
      int elem_num = tensor.data_size();
H
HexToString 已提交
190
      int offset = 0;
W
wangjiawei04 已提交
191
      for (int k = 0; k < elem_num; ++k) {
H
HexToString 已提交
192 193 194 195
        memcpy(dst_ptr + offset,
               tensor.data(k).c_str(),
               strlen(tensor.data(k).c_str()) + 1);
        offset += strlen(tensor.data(k).c_str()) + 1;
W
wangjiawei04 已提交
196 197 198
      }
    }
  }
H
HexToString 已提交
199 200 201 202 203 204 205 206 207 208

  VLOG(2) << "(logid=" << log_id << ") output size: " << out->size();
  timeline.Pause();
  int64_t end = timeline.TimeStampUS();
  res->p_size = 0;
  res->_batch_size = 1;
  AddBlobInfo(res, start);
  AddBlobInfo(res, end);

  VLOG(2) << "(logid=" << log_id << ") read data from client success";
209 210 211 212 213
  return 0;
}
DEFINE_OP(GeneralReaderOp);
}  // namespace serving
}  // namespace paddle_serving
H
HexToString 已提交
214
}  // namespace baidu