sparse_format.cpp 3.9 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>
W
wangguibao 已提交
20
#include "predictor/builtin_format.pb.h"
W
wangguibao 已提交
21 22 23
#include "sdk-cpp/include/common.h"
#include "sdk-cpp/include/predictor_sdk.h"
#include "sdk-cpp/sparse_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::sparse_service::Request;
using baidu::paddle_serving::predictor::sparse_service::Response;
using baidu::paddle_serving::predictor::format::SparsePrediction;
using baidu::paddle_serving::predictor::format::SparseInstance;

W
wangguibao 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
int create_req(Request& req) {  // NOLINT
  SparseInstance* ins = req.mutable_instances()->Add();
  ins->add_keys(26);
  ins->add_keys(182);
  ins->add_keys(232);
  ins->add_shape(2000);
  ins->add_values(1);
  ins->add_values(1);
  ins->add_values(1);

  ins = req.mutable_instances()->Add();
  ins->add_keys(0);
  ins->add_keys(182);
  ins->add_keys(232);
  ins->add_keys(299);
  ins->add_shape(2000);
  ins->add_values(13);
  ins->add_values(1);
  ins->add_values(1);
  ins->add_values(1);
  return 0;
W
wangguibao 已提交
53 54
}

W
wangguibao 已提交
55 56 57 58 59 60 61 62 63
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 SparsePrediction& prediction = res.predictions(i);
    std::ostringstream oss;
    for (uint32_t j = 0; j < prediction.categories_size(); ++j) {
      oss << prediction.categories(j) << " ";
W
wangguibao 已提交
64
    }
W
wangguibao 已提交
65 66
    LOG(INFO) << "Receive result " << oss.str();
  }
W
wangguibao 已提交
67

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

int main(int argc, char** argv) {
W
wangguibao 已提交
73 74 75 76 77 78 79 80 81 82 83
  PredictorApi api;

  // initialize logger instance
  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 已提交
84
    }
W
wangguibao 已提交
85 86 87
  }
  FLAGS_log_dir = "./log";
  google::InitGoogleLogging(strdup(argv[0]));
W
wangguibao 已提交
88

W
wangguibao 已提交
89 90 91 92
  if (api.create("./conf", "predictors.prototxt") != 0) {
    LOG(ERROR) << "Failed create predictors api!";
    return -1;
  }
W
wangguibao 已提交
93

W
wangguibao 已提交
94 95
  Request req;
  Response res;
W
wangguibao 已提交
96

W
wangguibao 已提交
97
  api.thrd_initialize();
W
wangguibao 已提交
98

W
wangguibao 已提交
99 100 101
  while (true) {
    timeval start;
    gettimeofday(&start, NULL);
W
wangguibao 已提交
102

W
wangguibao 已提交
103
    api.thrd_clear();
W
wangguibao 已提交
104

W
wangguibao 已提交
105 106 107 108 109 110 111 112
    Predictor* predictor = api.fetch_predictor("sparse_service");
    if (!predictor) {
      LOG(ERROR) << "Failed fetch predictor: sparse_service";
      return -1;
    }

    req.Clear();
    res.Clear();
W
wangguibao 已提交
113

W
wangguibao 已提交
114 115 116
    if (create_req(req) != 0) {
      return -1;
    }
W
wangguibao 已提交
117

W
wangguibao 已提交
118 119 120 121 122
    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 已提交
123

W
wangguibao 已提交
124 125 126
    butil::IOBuf debug_buf;
    debug_os.move_to(debug_buf);
    LOG(INFO) << "Debug string: " << debug_buf;
W
wangguibao 已提交
127

W
wangguibao 已提交
128 129
    timeval end;
    gettimeofday(&end, NULL);
W
wangguibao 已提交
130

W
wangguibao 已提交
131 132
    uint64_t elapse_ms = (end.tv_sec * 1000 + end.tv_usec / 1000) -
                         (start.tv_sec * 1000 + start.tv_usec / 1000);
W
wangguibao 已提交
133

W
wangguibao 已提交
134 135
    print_res(req, res, predictor->tag(), elapse_ms);
    res.Clear();
W
wangguibao 已提交
136

W
wangguibao 已提交
137 138
    usleep(50);
  }  // while (true)
W
wangguibao 已提交
139

W
wangguibao 已提交
140 141
  api.thrd_finalize();
  api.destroy();
W
wangguibao 已提交
142

W
wangguibao 已提交
143
  google::ShutdownGoogleLogging();
W
wangguibao 已提交
144

W
wangguibao 已提交
145
  return 0;
W
wangguibao 已提交
146 147 148
}

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