general_infer_op.cpp 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2020 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_infer_op.h"
16 17 18 19 20 21 22
#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
#include "core/predictor/framework/infer.h"
#include "core/predictor/framework/memory.h"
#include "core/predictor/framework/resource.h"
23
#include "core/util/include/timer.h"
24 25 26 27 28

namespace baidu {
namespace paddle_serving {
namespace serving {

29
using baidu::paddle_serving::Timer;
30 31 32
using baidu::paddle_serving::predictor::MempoolWrapper;
using baidu::paddle_serving::predictor::general_model::Tensor;
using baidu::paddle_serving::predictor::general_model::Response;
33
using baidu::paddle_serving::predictor::general_model::Request;
34 35
using baidu::paddle_serving::predictor::general_model::FetchInst;
using baidu::paddle_serving::predictor::InferManager;
36
using baidu::paddle_serving::predictor::PaddleGeneralModelConfig;
37 38

int GeneralInferOp::inference() {
39
  VLOG(2) << "Going to run inference";
B
barrierye 已提交
40 41 42
  // const GeneralBlob *input_blob =
  // get_depend_argument<GeneralBlob>(pre_name());
  VLOG(2) << "try to get output_blob";
M
MRXLT 已提交
43
  GeneralBlob *output_blob = mutable_data<GeneralBlob>();
B
barrierye 已提交
44
  fprintf(stderr, "[output] blob address %x\n", output_blob);
45
  TensorVector *out = &output_blob->tensor_vector;
B
barrierye 已提交
46

B
barrierye 已提交
47 48
  const std::vector<std::string> pre_node_names = pre_names();
  VLOG(2) << "pre node names size: " << pre_node_names.size();
B
barrierye 已提交
49

B
barrierye 已提交
50 51 52
  TensorVector input;
  int batch_size = 0;
  const GeneralBlob *input_blob;
B
barrierye 已提交
53 54 55
  for (uint32_t i = 0; i < pre_node_names.size(); ++i) {
    VLOG(2) << "pre names[" << i << "]: " << pre_node_names[i];
    input_blob = get_depend_argument<GeneralBlob>(pre_node_names[i]);
B
barrierye 已提交
56
    if (!input_blob) {
B
barrierye 已提交
57 58
      LOG(ERROR) << "Failed mutable depended argument, op:"
                 << pre_node_names[i];
B
barrierye 已提交
59 60
      return -1;
    }
B
barrierye 已提交
61 62
    fprintf(stderr, "[input] blob address %x\n", input_blob);

B
barrierye 已提交
63 64
    batch_size = input_blob->GetBatchSize();
    VLOG(2) << "batch size of input: " << batch_size;
B
barrierye 已提交
65 66 67
    for (uint32_t j = 0; j < input_blob->tensor_vector.size(); ++j) {
      VLOG(2) << "input tensor[" << j
              << "]: " << input_blob->tensor_vector[j].name;
B
barrierye 已提交
68
      input.push_back(input_blob->tensor_vector[j]);
B
barrierye 已提交
69 70
      VLOG(2) << "add an input tensor name: "
              << input_blob->tensor_vector[j].name;
B
barrierye 已提交
71 72
    }
  }
73

B
barrierye 已提交
74
  const TensorVector *in = &input;
75

B
barrierye 已提交
76
  batch_size = 1;
77
  VLOG(2) << "infer batch size: " << batch_size;
B
barrierye 已提交
78
  output_blob->SetBatchSize(batch_size);
M
MRXLT 已提交
79

80
  Timer timeline;
G
guru4elephant 已提交
81
  int64_t start = timeline.TimeStampUS();
82
  timeline.Start();
M
MRXLT 已提交
83

B
barrierye 已提交
84
  VLOG(2) << "input of op " << op_name();
B
barrierye 已提交
85
  for (uint32_t i = 0; i < in->size(); ++i) {
B
barrierye 已提交
86 87 88 89
    VLOG(2) << in->at(i).name;
  }

  VLOG(2) << "get engine name: " << engine_name().c_str();
B
barrierye 已提交
90 91 92 93
  if (InferManager::instance().infer(
          GeneralInferOp::engine_name().c_str(), in, out, batch_size)) {
    LOG(ERROR) << "Failed do infer in fluid model: "
               << GeneralInferOp::engine_name();
94 95
    return -1;
  }
G
guru4elephant 已提交
96

B
barrierye 已提交
97
  VLOG(2) << "output of op " << op_name();
B
barrierye 已提交
98
  for (uint32_t i = 0; i < out->size(); ++i) {
B
barrierye 已提交
99 100 101
    VLOG(2) << out->at(i).name;
  }

G
guru4elephant 已提交
102 103 104 105
  int64_t end = timeline.TimeStampUS();
  CopyBlobInfo(input_blob, output_blob);
  AddBlobInfo(output_blob, start);
  AddBlobInfo(output_blob, end);
106 107 108 109 110 111 112
  return 0;
}
DEFINE_OP(GeneralInferOp);

}  // namespace serving
}  // namespace paddle_serving
}  // namespace baidu