int64tensor_format.cpp 4.6 KB
Newer Older
W
wangguibao 已提交
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.

W
wangguibao 已提交
15
#include <sys/stat.h>
W
wangguibao 已提交
16
#include <sys/types.h>
W
wangguibao 已提交
17 18 19
#include <unistd.h>

#include <fstream>
G
guru4elephant 已提交
20 21 22 23
#include "core/sdk-cpp/builtin_format.pb.h"
#include "core/sdk-cpp/include/common.h"
#include "core/sdk-cpp/include/predictor_sdk.h"
#include "core/sdk-cpp/int64tensor_service.pb.h"
W
wangguibao 已提交
24 25 26 27 28 29 30 31

using baidu::paddle_serving::sdk_cpp::Predictor;
using baidu::paddle_serving::sdk_cpp::PredictorApi;
using baidu::paddle_serving::predictor::int64tensor_service::Request;
using baidu::paddle_serving::predictor::int64tensor_service::Response;
using baidu::paddle_serving::predictor::format::Float32TensorPredictor;
using baidu::paddle_serving::predictor::format::Int64TensorInstance;

W
wangguibao 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
int create_req(Request& req) {  // NOLINT
  Int64TensorInstance* ins = req.mutable_instances()->Add();
  ins->add_data(1);
  ins->add_data(2);
  ins->add_data(3);
  ins->add_data(4);
  ins->add_shape(2);
  ins->add_shape(2);

  ins = req.mutable_instances()->Add();
  ins->add_data(5);
  ins->add_data(6);
  ins->add_data(7);
  ins->add_data(8);
  ins->add_data(9);
  ins->add_shape(5);
  ins->add_shape(1);
  return 0;
W
wangguibao 已提交
50 51
}

W
wangguibao 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
void print_res(const Request& req,
               const Response& res,
               std::string route_tag,
               uint64_t elapse_ms) {
  for (uint32_t i = 0; i < res.predictions_size(); ++i) {
    const Float32TensorPredictor& prediction = res.predictions(i);
    std::ostringstream oss1;
    for (uint32_t j = 0; j < prediction.data_size(); ++j) {
      oss1 << prediction.data(j) << " ";
    }

    std::ostringstream oss2;
    for (uint32_t j = 0; j < prediction.shape_size(); ++j) {
      oss2 << prediction.shape(j) << " ";
W
wangguibao 已提交
66
    }
W
wangguibao 已提交
67 68
    LOG(INFO) << "Receive result " << oss1.str() << ", shape " << oss2.str();
  }
W
wangguibao 已提交
69

W
wangguibao 已提交
70 71
  LOG(INFO) << "Succ call predictor[int64tensor_format], the tag is: "
            << route_tag << ", elapse_ms: " << elapse_ms;
W
wangguibao 已提交
72 73 74
}

int main(int argc, char** argv) {
W
wangguibao 已提交
75 76
  PredictorApi api;

W
wangguibao 已提交
77
// initialize logger instance
W
wangguibao 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#ifdef BCLOUD
  logging::LoggingSettings settings;
  settings.logging_dest = logging::LOG_TO_FILE;

  std::string filename(argv[0]);
  filename = filename.substr(filename.find_last_of('/') + 1);
  settings.log_file = (std::string("./log/") + filename + ".log").c_str();
  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  logging::InitLogging(settings);

  logging::ComlogSinkOptions cso;
  cso.process_name = filename;
  cso.enable_wf_device = true;
  logging::ComlogSink::GetInstance()->Setup(&cso);
#else
W
wangguibao 已提交
93 94 95 96 97 98 99 100
  struct stat st_buf;
  int ret = 0;
  if ((ret = stat("./log", &st_buf)) != 0) {
    mkdir("./log", 0777);
    ret = stat("./log", &st_buf);
    if (ret != 0) {
      LOG(WARNING) << "Log path ./log not exist, and create fail";
      return -1;
W
wangguibao 已提交
101
    }
W
wangguibao 已提交
102 103 104
  }
  FLAGS_log_dir = "./log";
  google::InitGoogleLogging(strdup(argv[0]));
W
wangguibao 已提交
105
#endif
W
wangguibao 已提交
106

W
wangguibao 已提交
107 108 109 110
  if (api.create("./conf", "predictors.prototxt") != 0) {
    LOG(ERROR) << "Failed create predictors api!";
    return -1;
  }
W
wangguibao 已提交
111

W
wangguibao 已提交
112 113
  Request req;
  Response res;
W
wangguibao 已提交
114

W
wangguibao 已提交
115
  api.thrd_initialize();
W
wangguibao 已提交
116

W
wangguibao 已提交
117 118 119
  while (true) {
    timeval start;
    gettimeofday(&start, NULL);
W
wangguibao 已提交
120

W
wangguibao 已提交
121
    api.thrd_clear();
W
wangguibao 已提交
122

W
wangguibao 已提交
123 124 125 126 127
    Predictor* predictor = api.fetch_predictor("int64tensor_service");
    if (!predictor) {
      LOG(ERROR) << "Failed fetch predictor: int64tensor_service";
      return -1;
    }
W
wangguibao 已提交
128

W
wangguibao 已提交
129 130
    req.Clear();
    res.Clear();
W
wangguibao 已提交
131

W
wangguibao 已提交
132 133 134 135 136 137 138 139 140
    if (create_req(req) != 0) {
      return -1;
    }

    butil::IOBufBuilder debug_os;
    if (predictor->debug(&req, &res, &debug_os) != 0) {
      LOG(ERROR) << "failed call predictor with req:" << req.ShortDebugString();
      return -1;
    }
W
wangguibao 已提交
141

W
wangguibao 已提交
142 143 144
    butil::IOBuf debug_buf;
    debug_os.move_to(debug_buf);
    LOG(INFO) << "Debug string: " << debug_buf;
W
wangguibao 已提交
145

W
wangguibao 已提交
146 147
    timeval end;
    gettimeofday(&end, NULL);
W
wangguibao 已提交
148

W
wangguibao 已提交
149 150
    uint64_t elapse_ms = (end.tv_sec * 1000 + end.tv_usec / 1000) -
                         (start.tv_sec * 1000 + start.tv_usec / 1000);
W
wangguibao 已提交
151

W
wangguibao 已提交
152 153
    print_res(req, res, predictor->tag(), elapse_ms);
    res.Clear();
W
wangguibao 已提交
154

W
wangguibao 已提交
155 156
    usleep(50);
  }  // while (true)
W
wangguibao 已提交
157

W
wangguibao 已提交
158 159
  api.thrd_finalize();
  api.destroy();
W
wangguibao 已提交
160

W
wangguibao 已提交
161
#ifndef BCLOUD
W
wangguibao 已提交
162
  google::ShutdownGoogleLogging();
W
wangguibao 已提交
163
#endif
W
wangguibao 已提交
164

W
wangguibao 已提交
165
  return 0;
W
wangguibao 已提交
166 167 168
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */