general_infer_op.cpp 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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.

#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
19
#include "core/general-server/op/general_infer_helper.h"
20
#include "core/general-server/op/general_infer_op.h"
21 22 23
#include "core/predictor/framework/infer.h"
#include "core/predictor/framework/memory.h"
#include "core/predictor/framework/resource.h"
24
#include "core/util/include/timer.h"
25 26 27 28 29

namespace baidu {
namespace paddle_serving {
namespace serving {

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

int GeneralInferOp::inference() {
40 41
  const GeneralBlob * input_blob =
      get_depend_argument<GeneralBlob>(pre_name());
42

43 44 45
  if (!input_blob) {
    LOG(ERROR) << "Failed mutable depended argument, op:"
               << pre_name();
46 47 48
    return -1;
  }

49
  const TensorVector *in = &input_blob->tensor_vector;
50
  TensorVector *out = butil::get_object<TensorVector>();
51 52 53
  int batch_size = in->GetBatchSize();

  VLOG(2) << "infer batch size: " << batch_size;
54
  // infer
55 56 57
  Timer timeline;
  double infer_time = 0.0;
  timeline.Start();
58 59 60 61
  if (InferManager::instance().infer(GENERAL_MODEL_NAME, in, out, batch_size)) {
    LOG(ERROR) << "Failed do infer in fluid model: " << GENERAL_MODEL_NAME;
    return -1;
  }
62 63
  timeline.Pause();
  infer_time = timeline.ElapsedUS();
64 65 66 67 68 69 70 71

  return 0;
}
DEFINE_OP(GeneralInferOp);

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