demo.cpp 4.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
// 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.

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <thread>  // NOLINT
#include "elastic-ctr/client/api/elastic_ctr_api.h"

using baidu::paddle_serving::elastic_ctr::ElasticCTRPredictorApi;
using baidu::paddle_serving::elastic_ctr::Prediction;

DEFINE_int32(batch_size, 10, "Infernce batch_size");

DEFINE_string(test_file, "", "test file");

const int VARIABLE_NAME_LEN = 256;
const int CTR_EMBEDDING_TABLE_SIZE = 400000001;

struct Sample {
  std::map<std::string, std::vector<uint64_t>> slots;
};

std::vector<Sample> samples;

int read_samples(const char* file) {
  std::ifstream fs(file);

  for (std::string line; std::getline(fs, line);) {
    std::vector<std::string> tokens;
    std::stringstream ss(line);
    std::string token;

    Sample sample;

    while (std::getline(ss, token, ' ')) {
      tokens.push_back(token);
    }

    if (tokens.size() <= 3) {
      continue;
    }

    for (std::size_t i = 2; i < tokens.size(); ++i) {
      std::size_t pos = tokens[i].find(':');
      if (pos == std::string::npos) {
        continue;
      }

      uint64_t x = std::strtoull(tokens[i].substr(0, pos).c_str(), NULL, 10);
      std::string slot_name = tokens[i].substr(pos + 1);

      if (sample.slots.find(slot_name) == sample.slots.end()) {
        std::vector<uint64_t> values;
        values.push_back(x % 400000001);
        sample.slots[slot_name] = values;
      } else {
        auto it = sample.slots.find(slot_name);
        it->second.push_back(x);
      }
    }

    samples.push_back(sample);
  }

  LOG(INFO) << "Samples size = " << samples.size();

#if 1
  for (std::size_t i = 0; i < samples.size(); ++i) {
    LOG(INFO) << "=============Sample " << i << "=========";
    for (auto slot : samples[i].slots) {
      LOG(INFO) << "slot_name: " << slot.first.c_str();
      for (auto x : slot.second) {
        LOG(INFO) << x;
      }
    }
    LOG(INFO) << "========================================";
  }
#endif
  return 0;
}

int main(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);

  ElasticCTRPredictorApi api;

#ifdef BCLOUD
  logging::LoggingSettings settings;
  settings.logging_dest = logging::LOG_TO_FILE;
  std::string log_filename(argv[0]);
  log_filename = log_filename.substr(log_filename.find_last_of('/') + 1);
  settings.log_file = (std::string("./log/") + log_filename + ".log").c_str();
  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  logging::InitLogging(settings);
  logging::ComlogSinkOptions cso;
  cso.process_name = log_filename;
  cso.enable_wf_device = true;
  logging::ComlogSink::GetInstance()->Setup(&cso);
#else
  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;
    }
  }
  FLAGS_log_dir = "./log";
  google::InitGoogleLogging(strdup(argv[0]));
  FLAGS_logbufsecs = 0;
  FLAGS_logbuflevel = -1;
#endif
  // predictor conf
  if (api.init("./conf", "slot.conf", "predictors.prototxt") != 0) {
    LOG(ERROR) << "Failed create predictors api!";
    return -1;
  }

  api.thrd_initialize();

  ret = read_samples(FLAGS_test_file.c_str());

  std::size_t index = 0;
  while (index < samples.size()) {
    api.thrd_clear();

    for (int i = 0; i < FLAGS_batch_size && index < samples.size(); ++i) {
      ReqInstance* ins = api.add_instance();
      if (!ins) {
        LOG(ERROR) << "Failed create req instance";
        return -1;
      }

      for (auto slot : samples[index].slots) {
        for (auto x : slot.second) {
          api.add_slot(ins, slot.first.c_str(), x);
        }
      }

      ++index;
    }

W
wangguibao 已提交
161 162
    std::vector<std::vector<float>> results_vec;
    if (api.inference(results_vec) != 0) {
W
wangguibao 已提交
163 164 165 166 167
      LOG(ERROR) << "failed call predictor";
      return -1;
    }

#if 1
W
wangguibao 已提交
168 169 170
    for (std::size_t i = 0; i < results_vec.size(); ++i) {
      LOG(INFO) << "sample " << i << ": [" << results_vec[i].at(0) << ", "
                << results_vec[i].at(1) << "]";
W
wangguibao 已提交
171 172 173 174 175 176 177 178 179
    }
#endif
  }  // end while

  api.thrd_finalize();

  api.destroy();
  return 0;
}