maprnn_dense.cpp 20.0 KB
Newer Older
W
sdk-cpp  
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
/***************************************************************************
 * 
 * Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
/**
 * @file demo.cpp
 * @author root(root@baidu.com)
 * @date 2018/07/09 20:12:44
 * @brief 
 *  
 **/
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <bthread.h>
#include "common.h"
#include "predictor_sdk.h"
#include "map_rnn.pb.h"
using baidu::paddle_serving::sdk_cpp::Predictor;
using baidu::paddle_serving::sdk_cpp::PredictorApi;
//using baidu::infinite::map_rnn::Tensor;
using baidu::infinite::map_rnn::DenseInstance;
using baidu::infinite::map_rnn::DensePrediction;
using baidu::infinite::map_rnn::Request;
using baidu::infinite::map_rnn::Response;
using baidu::infinite::map_rnn::MapRnnService;
static const uint32_t SELECT_VALID_UNIT = 1000;

int split(std::string source, char spliter, std::vector<std::string>& result)
{
    result.clear();
    std::string::size_type  pos;
    std::string::size_type  start = 0;
    while ((pos = source.find(spliter, start)) != std::string::npos)
    {
        result.insert(result.end(), source.substr(start, pos-start));
        start = pos+1;
    }
    result.insert(result.end(), source.substr(start));
    return (int)result.size();
}

int load_data(std::string data_file_name, std::vector<std::vector<float> >& data){
    std::ifstream data_file;
    std::vector<std::string> token;
    data_file.open(data_file_name, std::ios::in); 
    std::string input_line;
    while (std::getline(data_file, input_line)) {
        split(input_line, ',', token);
        std::vector<float> feature_one;
        for (size_t i = 0; i < token.size(); i++){
            feature_one.push_back(std::stof(token[i]));
        }
        data.push_back(feature_one);
    }
    return 0;
}

void split(const std::string &str, char sep, std::vector<std::string> *pieces) {
    pieces->clear();
    if (str.empty()) {
        return;
    }
    size_t pos = 0;
    size_t next = str.find(sep, pos);
    while (next != std::string::npos) {
        pieces->push_back(str.substr(pos, next - pos));
        pos = next + 1;
        next = str.find(sep, pos);
    }
    if (!str.substr(pos).empty()) {
        pieces->push_back(str.substr(pos));
    }
}

void split_to_float(const std::string &str, char sep, std::vector<float> *fs) {
    std::vector<std::string> pieces;
    split(str, sep, &pieces);
    std::transform(pieces.begin(), pieces.end(), std::back_inserter(*fs),
            [](const std::string &v) { 
                return std::stof(v); 
            });
}

// clang-format off
/*void TensorAssignData(paddle::PaddleTensor *tensor, const std::vector<std::vector<float>> &data) {
  // Assign buffer
  int dim = std::accumulate(tensor->shape.begin(), tensor->shape.end(), 1, [](int a, int b) { return a * b; });
  tensor->data.Resize(sizeof(float) * dim);
  int c = 0;
  for (const auto &f : data) {
    for (float v : f) { static_cast<float *>(tensor->data.data())[c++] = v; }
  }
}*/
// clang-format on

struct DataRecord {
    std::vector<std::vector<std::vector<float>>> link_step_data_all;
    std::vector<std::vector<float>> week_data_all, minute_data_all;
    std::vector<std::vector<std::vector<char>>> ch_link_step_data_all;  
    std::vector<std::vector<char>> ch_week_data_all, ch_minute_data_all;

    std::vector<size_t> lod1, lod2, lod3;
    std::vector<std::vector<float>> rnn_link_data, rnn_week_datas,
        rnn_minute_datas;

    size_t batch_iter{0};
    size_t batch_size{1};

    DataRecord() = default;
    DataRecord(const std::string &path, int batch_size = 1)
        : batch_size(batch_size) {
            Load(path);

        for (std::vector<std::vector<std::vector<float>>>::iterator it1 = link_step_data_all.begin();
                it1 != link_step_data_all.end(); ++it1) {
            std::vector<std::vector<char>> links;
            for (std::vector<std::vector<float>>::iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
                int len = it2->size() * sizeof(float);
                char* ch = (char*)malloc(len);
                memcpy(ch, it2->data(), len);
                std::vector<char> tmp(ch, ch + len);
                links.push_back(tmp);
                free(ch);
            }
            ch_link_step_data_all.push_back(links);
        }

        for (std::vector<std::vector<float>>::iterator it1 = week_data_all.begin(); it1 != week_data_all.end(); ++it1) {
            int len = it1->size() * sizeof(float);
            char* ch = (char*)malloc(len);
            memcpy(ch, it1->data(), len);
            std::vector<char> tmp(ch, ch + len);
            ch_week_data_all.push_back(tmp);
            free(ch);
        }

        for (std::vector<std::vector<float>>::iterator it1 = minute_data_all.begin(); it1 != minute_data_all.end(); ++it1) {
            int len = it1->size() * sizeof(float);
            char* ch = (char*)malloc(len);
            memcpy(ch, it1->data(), len);
            std::vector<char> tmp(ch, ch + len);
            ch_minute_data_all.push_back(tmp);
            free(ch);
        }
    }

    DataRecord NextBatch() {
        DataRecord data;
        size_t batch_end = batch_iter + batch_size;

        // NOTE skip the final batch, if no enough data is provided.
        if (batch_end <= link_step_data_all.size()) {
            data.link_step_data_all.assign(link_step_data_all.begin() + batch_iter,
                    link_step_data_all.begin() + batch_end);
            data.week_data_all.assign(week_data_all.begin() + batch_iter,
                    week_data_all.begin() + batch_end);
            data.minute_data_all.assign(minute_data_all.begin() + batch_iter,
                    minute_data_all.begin() + batch_end);

            // Prepare LoDs
            data.lod1.emplace_back(0);
            data.lod2.emplace_back(0);
            data.lod3.emplace_back(0);

            //CHECK(!data.link_step_data_all.empty()) << "empty";
            //CHECK(!data.week_data_all.empty());
            //CHECK(!data.minute_data_all.empty());
            //CHECK_EQ(data.link_step_data_all.size(), data.week_data_all.size());
            //CHECK_EQ(data.minute_data_all.size(), data.link_step_data_all.size());

            for (size_t j = 0; j < data.link_step_data_all.size(); j++) {
                for (const auto &d : data.link_step_data_all[j]) {
                    data.rnn_link_data.push_back(d);
                }
                data.rnn_week_datas.push_back(data.week_data_all[j]);
                data.rnn_minute_datas.push_back(data.minute_data_all[j]);
                // calculate lod
                data.lod1.push_back(data.lod1.back() +
                        data.link_step_data_all[j].size());
                data.lod3.push_back(data.lod3.back() + 1);
                for (size_t i = 1; i < data.link_step_data_all[j].size() + 1; i++) {
                    data.lod2.push_back(data.lod2.back() +
                            data.link_step_data_all[j].size());
                }
            }
        }

        batch_iter += batch_size;
        return data;
    }

    void Load(const std::string &path) {
        std::ifstream file(path);
        std::string line;

        int num_lines = 0;
        while (std::getline(file, line)) {
            num_lines++;
            std::vector<std::string> data;
            split(line, ':', &data);

            std::vector<std::vector<float>> link_step_data;
            std::vector<std::string> link_datas;
            split(data[0], '|', &link_datas);

            for (auto &step_data : link_datas) {
                std::vector<float> tmp;
                split_to_float(step_data, ',', &tmp);
                link_step_data.emplace_back(tmp);
            }

            // load week data
            std::vector<float> week_data;
            split_to_float(data[2], ',', &week_data);

            // load minute data
            std::vector<float> minute_data;
            split_to_float(data[1], ',', &minute_data);

            link_step_data_all.emplace_back(std::move(link_step_data));
            week_data_all.emplace_back(std::move(week_data));
            minute_data_all.emplace_back(std::move(minute_data));
        }
    }
};

/*void PrepareInputs(std::vector<paddle::PaddleTensor> *input_slots, DataRecord *data,
                   int batch_size) {
  // DataRecord data(FLAGS_datapath, batch_size);

  paddle::PaddleTensor lod_attention_tensor, init_zero_tensor, lod_tensor_tensor,
      week_tensor, minute_tensor;
  lod_attention_tensor.name = "lod_attention";
  init_zero_tensor.name = "init_zero";
  lod_tensor_tensor.name = "lod_tensor";
  week_tensor.name = "week";
  minute_tensor.name = "minute";

  auto one_batch = data->NextBatch();

    printf("rnn_link_data.size:%lu,\n", one_batch.rnn_link_data.size());
    printf("rnn_link_data.front().size:%lu\n", one_batch.rnn_link_data.front().size());

  // clang-format off
  std::vector<int> rnn_link_data_shape
      ({static_cast<int>(one_batch.rnn_link_data.size()), static_cast<int>(one_batch.rnn_link_data.front().size())});
  //LOG(INFO) << "set 1";
  lod_attention_tensor.shape.assign({1, 2});
  lod_attention_tensor.lod.assign({one_batch.lod1, one_batch.lod2});
  //LOG(INFO) << "set 1";
  init_zero_tensor.shape.assign({batch_size, 15});
  init_zero_tensor.lod.assign({one_batch.lod3});
  //LOG(INFO) << "set 1";
  lod_tensor_tensor.shape = rnn_link_data_shape;
  lod_tensor_tensor.lod.assign({one_batch.lod1});
  //LOG(INFO) << "set 1";
  week_tensor.shape.assign({(int) one_batch.rnn_week_datas.size(), (int) one_batch.rnn_week_datas.front().size()});
  week_tensor.lod.assign({one_batch.lod3});
  //LOG(INFO) << "set 1";
  minute_tensor.shape.assign({(int) one_batch.rnn_minute_datas.size(),
                              (int) one_batch.rnn_minute_datas.front().size()});
  minute_tensor.lod.assign({one_batch.lod3});

  // assign data
  TensorAssignData(&lod_attention_tensor, std::vector<std::vector<float>>({{0, 0}}));
  std::vector<float> tmp_zeros(batch_size * 15, 0.);
  TensorAssignData(&init_zero_tensor, {tmp_zeros});
  TensorAssignData(&lod_tensor_tensor, one_batch.rnn_link_data);
  TensorAssignData(&week_tensor, one_batch.rnn_week_datas);
  TensorAssignData(&minute_tensor, one_batch.rnn_minute_datas);
  // clang-format on

  input_slots->assign({lod_tensor_tensor, lod_attention_tensor,
                       init_zero_tensor, init_zero_tensor, week_tensor,
                       minute_tensor});

  for (auto &tensor : *input_slots) {
    tensor.dtype = paddle::PaddleDType::FLOAT32;
    // LOG(INFO) << DescribeTensor(tensor);
  }
}*/

class InputData {
public:
    InputData() {}
    ~InputData() {}
    int create(const std::string file_name, size_t buf_size, 
            size_t batch_size, int qps) {
        bthread_mutex_init(&_mutex, NULL);
        std::string datapath = "./data/test_features_sys";
        DataRecord data(datapath, batch_size);
        _data_record = data;
        /*FILE* fp = fopen(file_name.c_str(), "r");
        if (!fp) {
298
            LOG(ERROR) << "Failed open data file: " 
W
sdk-cpp  
wangguibao 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                << file_name;
            return -1;
        } 
        _data.clear();
        char buffer[2048000];
        std::vector<std::string> tokens;
        while (fgets(buffer, sizeof(buffer), fp) != NULL) {
            tokens.clear();
            baidu::paddle_serving::sdk_cpp::str_split(
                    buffer, ",", &tokens);
            std::vector<float> feature_one;
            for (size_t i = 0; i < tokens.size(); i++){
                feature_one.push_back(
                        strtof(tokens[i].c_str(), NULL));
            }
            _data.push_back(feature_one);
        }
        printf("succ load data, size:%ld\n", _data.size());
        */
        for (size_t ri = 0; ri < buf_size; ri++) {
            Request* req = new Request();
            if (generate_one_req(*req, batch_size) != 0) {
321
                LOG(ERROR) << "Failed generate req at: " << ri;
W
sdk-cpp  
wangguibao 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                //fclose(fp);
                return -1;
            }
            _req_list.push_back(req);
        }
        //fclose(fp);
        _current = 0;
        _waitingtm = 0;
        _lasttm.tv_sec = _lasttm.tv_usec = 0;
        if (qps == 0) {
            _interval = 0;
        } else if (qps < 1) {
            _interval = 1000 * 1000;
        } else {
            _interval = 1000 * 1000 / qps;
        }
        LOG(INFO) << "Succ create req, size: " << buf_size
            << ", batch_size: " << batch_size;
        return 0;
    }
    void destroy() {
        size_t ds = _data.size();
        for (size_t di = 0; di < ds; di++) {
            _data[di].clear();
        }
        _data.clear();
        size_t rs = _req_list.size();
        for (size_t ri = 0; ri < rs; ri++) {
            delete _req_list[ri];
        }
        _req_list.clear();
    }
    Request* next_req() {
        bthread_mutex_lock(&_mutex);
        if (_interval != 0)
        {
            if (_lasttm.tv_sec == 0 && _lasttm.tv_usec == 0)
            {
                gettimeofday(&_lasttm, NULL);
            }
            else
            {
                timeval curtm;
                gettimeofday(&curtm, NULL);
                long elapse = 
                    ((curtm.tv_sec - _lasttm.tv_sec) * 1000*1000 +
                    (curtm.tv_usec - _lasttm.tv_usec));
                _waitingtm += _interval - elapse;
                _lasttm = curtm;
                if (_waitingtm >= SELECT_VALID_UNIT) // select的最小响应单位
                {
                    long tm_unit 
                        = _waitingtm / SELECT_VALID_UNIT * SELECT_VALID_UNIT;
                    timeval tmp_tm = {tm_unit / 1000000, tm_unit % 1000000};
                    select(1, NULL, NULL, NULL, &tmp_tm); //延时以控制压力speed
                }
                else if (_waitingtm <= SELECT_VALID_UNIT * (-2))
                {
                    _waitingtm = -SELECT_VALID_UNIT;
                }
            }
        }
        size_t rs = _req_list.size();
        Request* req = _req_list[(_current++) % rs];
        bthread_mutex_unlock(&_mutex);
        return req;
    }
    int generate_one_req(Request& req, int batch) {
        int batch_size = batch;
        
        int i = 0;
        DenseInstance* ins = req.add_instances();
        ins->set_batch_size(batch_size);        
        for (std::vector<std::vector<std::vector<char>>>::iterator it1 = _data_record.ch_link_step_data_all.begin();
                it1 != _data_record.ch_link_step_data_all.end(); ++it1) {
            ::baidu::infinite::map_rnn::Lines* step_data = ins->add_step_data();
            for (std::vector<std::vector<char>>::iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
                ::baidu::infinite::map_rnn::Line* line = step_data->add_line();        
                line->set_value(it2->data(), it2->size());
            }
            if (++i == batch_size) {
                break;
            }
        }
        i = 0;
        ::baidu::infinite::map_rnn::Lines* week_data = ins->mutable_week_data();
        for (std::vector<std::vector<char>>::iterator it1 = _data_record.ch_week_data_all.begin(); 
                it1 != _data_record.ch_week_data_all.end(); ++it1) {
            ::baidu::infinite::map_rnn::Line* line = week_data->add_line();
            line->set_value(it1->data(), it1->size());
            if (++i == batch_size) {
                break;
            }
        }
        i = 0;
        ::baidu::infinite::map_rnn::Lines* minute_data = ins->mutable_minute_data();
        for (std::vector<std::vector<char>>::iterator it1 = _data_record.ch_minute_data_all.begin(); 
                it1 != _data_record.ch_minute_data_all.end(); ++it1) {
            ::baidu::infinite::map_rnn::Line* line = minute_data->add_line();
            line->set_value(it1->data(), it1->size());
            if (++i == batch_size) {
                break;
            }
        }

        /*for (int fi = 0; fi < _data.size(); ++fi) {
            Tensor* tensor = ins->add_tensors();
            tensor->set_name(tensor_names[fi]);
            int len = 1;
            for (int si = 0; si < shapes[fi].size(); ++si) {
                len *= shapes[fi][si];
            }
            for (int si = 0; si < shapes[fi].size(); ++si) {
                tensor->add_shape(shapes[fi][si]);
            }
            tensor->set_features(&(_data[fi][0]), len * sizeof(float));
        }*/
        return 0;
    }
private:
    DataRecord _data_record;
    std::vector<std::vector<float> > _data;
    std::vector<Request*> _req_list;
    bthread_mutex_t _mutex;
    long _waitingtm;
    long _interval;
    timeval _lasttm;
    int _current;
};
void print_res(
        const Request* req, 
        const Response& res, 
        std::string route_tag,
        uint64_t elapse_ms) {
    uint32_t sample_size = res.predictions_size();
    LOG(INFO) 
        << "Succ call predictor[wasq], res sample size: " 
        << sample_size << ", the tag is: " 
        << route_tag << ", elapse_ms: " << elapse_ms;
}

struct Arg {
    PredictorApi* api;
    InputData* input;
};

void* work(void* p) {
    Arg* arg = (Arg*) p;
    InputData* input = arg->input;
    if (PredictorApi::instance().thrd_initialize() != 0) {
472
        LOG(ERROR) << "Failed create bthread local predictor"; 
W
sdk-cpp  
wangguibao 已提交
473 474 475 476 477 478
        return NULL;
    }
    Response res;
    LOG(WARNING) << "Thread entry!";
    while (true) {
        if (PredictorApi::instance().thrd_clear() != 0) {
479
            LOG(ERROR) << "Failed thrd clear predictor"; 
W
sdk-cpp  
wangguibao 已提交
480 481 482 483
            return NULL;
        }
        Predictor* predictor = PredictorApi::instance().fetch_predictor("wasq");
        if (!predictor) {
484
            LOG(ERROR) << "Failed fetch predictor: wasq"; 
W
sdk-cpp  
wangguibao 已提交
485 486 487 488 489 490 491
            return NULL;
        }
        Request* req = input->next_req();
        res.Clear();
        timeval start;
        gettimeofday(&start, NULL);
        if (predictor->inference(req, &res) != 0) {
492
            LOG(ERROR) << "failed call predictor with req:"
W
sdk-cpp  
wangguibao 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                << req->ShortDebugString();
            return NULL;
        }
        timeval end;
        gettimeofday(&end, NULL);
        uint64_t elapse_ms = (end.tv_sec * 1000 + end.tv_usec / 1000)
            - (start.tv_sec * 1000 + start.tv_usec / 1000);
        print_res(req, res, predictor->tag(), elapse_ms);
        if (PredictorApi::instance().free_predictor(predictor) != 0) {
            printf("failed free predictor\n");
        }
        //break;
        //printf("done\n");
    }
    if (PredictorApi::instance().thrd_finalize() != 0) {
508
        LOG(ERROR) << "Failed thrd finalize predictor api";
W
sdk-cpp  
wangguibao 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522
    }
    LOG(WARNING) << "Thread exit!";
    return NULL;
}
int main(int argc, char** argv) {
    if (argc != 5) {
        printf("Usage: demo req_buf_size batch_size threads qps\n");
        return -1;
    }
    int req_buffer = atoi(argv[1]);
    int batch_size = atoi(argv[2]);
    int thread_num = atoi(argv[3]);
    int qps = atoi(argv[4]);
    if (PredictorApi::instance().create("./conf", "predictors.conf") != 0) {
523
        LOG(ERROR) << "Failed create predictors api!"; 
W
sdk-cpp  
wangguibao 已提交
524 525 526 527 528
        return -1;
    }
    InputData data;
    if (data.create(
                "./data/test_features_sys", req_buffer, batch_size, qps) != 0) {
529
        LOG(ERROR) << "Failed create inputdata!";
W
sdk-cpp  
wangguibao 已提交
530 531 532 533 534
        return -1;
    }
    Arg arg = {NULL, &data};
    bthread_t* threads = new bthread_t[thread_num];
    if (!threads) {
535
        LOG(ERROR) << "Failed create threads, num:" << thread_num;
W
sdk-cpp  
wangguibao 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548
        return -1;
    }
    for (int i = 0; i < thread_num; ++i) {
        bthread_start_background(threads + i, NULL, work, &arg);
    }
    for (int i = 0; i < thread_num; ++i) {
        bthread_join(threads[i], NULL);
    }
    delete[] threads;
    data.destroy();
    return 0;
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */