analyzer_rnn1_tester.cc 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2018 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.

15
#include "paddle/fluid/inference/api/analysis_predictor.h"
L
luotao1 已提交
16
#include "paddle/fluid/inference/tests/api/tester_helper.h"
17

18 19
DEFINE_bool(with_precision_check, true, "turn on test");

20 21 22 23
namespace paddle {
namespace inference {

using namespace framework;  // NOLINT
24
using namespace contrib;    // NOLINT
25 26 27 28 29 30 31

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<size_t> lod1, lod2, lod3;
  std::vector<std::vector<float>> rnn_link_data, rnn_week_datas,
      rnn_minute_datas;
T
Tao Luo 已提交
32
  size_t num_samples;  // total number of samples
33 34 35
  size_t batch_iter{0};
  size_t batch_size{1};
  DataRecord() = default;
36

37 38 39 40
  explicit DataRecord(const std::string &path, int batch_size = 1)
      : batch_size(batch_size) {
    Load(path);
  }
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
  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.push_back(0);
      data.lod2.push_back(0);
      data.lod3.push_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.push_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.push_back(std::move(link_step_data));
      week_data_all.push_back(std::move(week_data));
      minute_data_all.push_back(std::move(minute_data));
    }
T
Tao Luo 已提交
107
    num_samples = num_lines;
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
void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
                   int batch_size) {
  PaddleTensor lod_attention_tensor, init_zero_tensor, lod_tensor_tensor,
      week_tensor, minute_tensor;
  lod_attention_tensor.name = "data_lod_attention";
  init_zero_tensor.name = "cell_init";
  lod_tensor_tensor.name = "data";
  week_tensor.name = "week";
  minute_tensor.name = "minute";
  auto one_batch = data->NextBatch();
  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())});
  lod_attention_tensor.shape.assign({1, 2});
  lod_attention_tensor.lod.assign({one_batch.lod1, one_batch.lod2});
  init_zero_tensor.shape.assign({batch_size, 15});
  init_zero_tensor.lod.assign({one_batch.lod3});
  lod_tensor_tensor.shape = rnn_link_data_shape;
  lod_tensor_tensor.lod.assign({one_batch.lod1});
  // clang-format off
  week_tensor.shape.assign(
      {static_cast<int>(one_batch.rnn_week_datas.size()),
       static_cast<int>(one_batch.rnn_week_datas.front().size())});
  week_tensor.lod.assign({one_batch.lod3});
  minute_tensor.shape.assign(
      {static_cast<int>(one_batch.rnn_minute_datas.size()),
       static_cast<int>(one_batch.rnn_minute_datas.front().size())});
  minute_tensor.lod.assign({one_batch.lod3});
  // clang-format on
  // assign data
  TensorAssignData<float>(&lod_attention_tensor,
                          std::vector<std::vector<float>>({{0, 0}}));
  std::vector<float> tmp_zeros(batch_size * 15, 0.);
  TensorAssignData<float>(&init_zero_tensor, {tmp_zeros});
  TensorAssignData<float>(&lod_tensor_tensor, one_batch.rnn_link_data);
  TensorAssignData<float>(&week_tensor, one_batch.rnn_week_datas);
  TensorAssignData<float>(&minute_tensor, one_batch.rnn_minute_datas);
  // Set inputs.
  auto init_zero_tensor1 = init_zero_tensor;
  init_zero_tensor1.name = "hidden_init";
  input_slots->assign({week_tensor, init_zero_tensor, minute_tensor,
                       init_zero_tensor1, lod_attention_tensor,
                       lod_tensor_tensor});
  for (auto &tensor : *input_slots) {
    tensor.dtype = PaddleDType::FLOAT32;
  }
}

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
void PrepareZeroCopyInputs(ZeroCopyTensor *lod_attention_tensor,
                           ZeroCopyTensor *cell_init_tensor,
                           ZeroCopyTensor *data_tensor,
                           ZeroCopyTensor *hidden_init_tensor,
                           ZeroCopyTensor *week_tensor,
                           ZeroCopyTensor *minute_tensor,
                           DataRecord *data_record, int batch_size) {
  auto one_batch = data_record->NextBatch();
  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())});
  lod_attention_tensor->Reshape({1, 2});
  lod_attention_tensor->SetLoD({one_batch.lod1, one_batch.lod2});

  cell_init_tensor->Reshape({batch_size, 15});
  cell_init_tensor->SetLoD({one_batch.lod3});

  hidden_init_tensor->Reshape({batch_size, 15});
  hidden_init_tensor->SetLoD({one_batch.lod3});

  data_tensor->Reshape(rnn_link_data_shape);
  data_tensor->SetLoD({one_batch.lod1});

  week_tensor->Reshape(
      {static_cast<int>(one_batch.rnn_week_datas.size()),
       static_cast<int>(one_batch.rnn_week_datas.front().size())});
  week_tensor->SetLoD({one_batch.lod3});

  minute_tensor->Reshape(
      {static_cast<int>(one_batch.rnn_minute_datas.size()),
       static_cast<int>(one_batch.rnn_minute_datas.front().size())});
  minute_tensor->SetLoD({one_batch.lod3});

  // assign data
  float arr0[] = {0, 0};
  std::vector<float> zeros(batch_size * 15, 0);
  std::copy_n(arr0, 2,
              lod_attention_tensor->mutable_data<float>(PaddlePlace::kCPU));
  std::copy_n(arr0, 2, data_tensor->mutable_data<float>(PaddlePlace::kCPU));
  std::copy_n(zeros.begin(), zeros.size(),
              cell_init_tensor->mutable_data<float>(PaddlePlace::kCPU));
  std::copy_n(zeros.begin(), zeros.size(),
              hidden_init_tensor->mutable_data<float>(PaddlePlace::kCPU));
  ZeroCopyTensorAssignData(data_tensor, one_batch.rnn_link_data);
  ZeroCopyTensorAssignData(week_tensor, one_batch.rnn_week_datas);
  ZeroCopyTensorAssignData(minute_tensor, one_batch.rnn_minute_datas);
}

void SetConfig(AnalysisConfig *cfg) {
T
Tao Luo 已提交
208 209 210 211 212 213 214 215
  cfg->prog_file = FLAGS_infer_model + "/__model__";
  cfg->param_file = FLAGS_infer_model + "/param";
  cfg->use_gpu = false;
  cfg->device = 0;
  cfg->specify_input_name = true;
  cfg->enable_ir_optim = true;
  cfg->ir_passes.clear();  // Do not exclude any pass.
}
216

T
Tao Luo 已提交
217 218
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
  DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
219
  std::vector<PaddleTensor> input_slots;
T
Tao Luo 已提交
220 221 222 223 224 225 226
  int epoch = FLAGS_test_all_data ? data.num_samples / FLAGS_batch_size : 1;
  LOG(INFO) << "number of samples: " << epoch * FLAGS_batch_size;
  for (int bid = 0; bid < epoch; ++bid) {
    PrepareInputs(&input_slots, &data, FLAGS_batch_size);
    (*inputs).emplace_back(input_slots);
  }
}
227

T
Tao Luo 已提交
228 229
// Easy for profiling independently.
TEST(Analyzer_rnn1, profile) {
Y
Yan Chunwei 已提交
230
  contrib::AnalysisConfig cfg;
T
Tao Luo 已提交
231 232
  SetConfig(&cfg);
  std::vector<PaddleTensor> outputs;
233

L
luotao1 已提交
234
  std::vector<std::vector<PaddleTensor>> input_slots_all;
T
Tao Luo 已提交
235 236 237
  SetInput(&input_slots_all);
  TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
}
238

T
Tao Luo 已提交
239 240
// Check the fuse status
TEST(Analyzer_rnn1, fuse_statis) {
Y
Yan Chunwei 已提交
241
  contrib::AnalysisConfig cfg;
T
Tao Luo 已提交
242
  SetConfig(&cfg);
243

T
Tao Luo 已提交
244
  int num_ops;
245 246 247
  auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg);
  auto fuse_statis = GetFuseStatis(
      static_cast<AnalysisPredictor *>(predictor.get()), &num_ops);
T
Tao Luo 已提交
248 249 250 251 252 253 254
  ASSERT_TRUE(fuse_statis.count("fc_fuse"));
  EXPECT_EQ(fuse_statis.at("fc_fuse"), 1);
  EXPECT_EQ(fuse_statis.at("fc_nobias_lstm_fuse"), 2);  // bi-directional LSTM
  EXPECT_EQ(fuse_statis.at("seq_concat_fc_fuse"), 1);
  EXPECT_EQ(num_ops,
            13);  // After graph optimization, only 13 operators exists.
}
255

T
Tao Luo 已提交
256 257
// Compare result of NativeConfig and AnalysisConfig
TEST(Analyzer_rnn1, compare) {
Y
Yan Chunwei 已提交
258
  contrib::AnalysisConfig cfg;
T
Tao Luo 已提交
259 260 261 262 263
  SetConfig(&cfg);

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
  CompareNativeAndAnalysis(cfg, input_slots_all);
264 265
}

T
Tao Luo 已提交
266 267
// Test Multi-Thread.
TEST(Analyzer_rnn1, multi_thread) {
Y
Yan Chunwei 已提交
268
  contrib::AnalysisConfig cfg;
T
Tao Luo 已提交
269 270
  SetConfig(&cfg);
  std::vector<PaddleTensor> outputs;
271

T
Tao Luo 已提交
272 273
  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
T
Tao Luo 已提交
274
  TestPrediction(cfg, input_slots_all, &outputs, 4 /* multi_thread */);
275 276
}

T
Tao Luo 已提交
277 278
bool CompareTensors(const framework::Scope &a_scope,
                    const framework::Scope &b_scope,
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
                    const std::vector<std::string> &tensors) {
  for (auto &x : tensors) {
    auto *a_var = a_scope.FindVar(x);
    auto *b_var = b_scope.FindVar(x);
    if (a_var && b_var) {
      if (a_var->Type() == typeid(framework::LoDTensor) ||
          a_var->Type() == typeid(framework::Tensor)) {
        LOG(INFO) << "comparing tensor " << x;
        auto &a_t = a_var->Get<framework::LoDTensor>();
        auto &b_t = b_var->Get<framework::LoDTensor>();
        if (!inference::CompareTensor(a_t, b_t)) {
          LOG(ERROR) << string::Sprintf("tensor %s not match in two scopes", x);
        }
      } else {
        LOG(INFO) << "skip no tensor " << x;
      }
    } else {
      LOG(INFO) << "skip tensor " << x;
    }
  }
  return true;
}

// Validate that the AnalysisPredictor + ZeroCopyTensor really works by testing
// on the complex RNN1 model.
TEST(Analyzer_rnn1, ZeroCopy) {
  AnalysisConfig config;
  SetConfig(&config);
  config.use_feed_fetch_ops = false;

  PaddlePlace place;
  int output_size{0};

  auto predictor =
      CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(
          config);

  config.use_feed_fetch_ops = true;
  auto native_predictor =
      CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);

  config.use_feed_fetch_ops = true;  // the analysis predictor needs feed/fetch.
  auto analysis_predictor =
      CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(
          config);

#define NEW_TENSOR(name__) \
  auto name__##_tensor = predictor->GetInputTensor(#name__);
  NEW_TENSOR(data_lod_attention);
  NEW_TENSOR(cell_init);
  NEW_TENSOR(data);
  NEW_TENSOR(week);
  NEW_TENSOR(minute);
  NEW_TENSOR(hidden_init);

  // Prepare data for AnalysisPredictor
  DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
  PrepareZeroCopyInputs(data_lod_attention_tensor.get(), cell_init_tensor.get(),
                        data_tensor.get(), hidden_init_tensor.get(),
                        week_tensor.get(), minute_tensor.get(), &data,
                        FLAGS_batch_size);

  // Prepare data for NativePredictor
  std::vector<std::vector<PaddleTensor>> native_inputs;
  SetInput(&native_inputs);
  std::vector<PaddleTensor> native_outputs;
  std::vector<PaddleTensor> analysis_outputs;

  auto output_tensor = predictor->GetOutputTensor("final_output.tmp_1");
  // Run analysis predictor

  int num_ops;
  auto fuse_statis = GetFuseStatis(predictor.get(), &num_ops);
  ASSERT_TRUE(fuse_statis.count("fc_fuse"));
  ASSERT_EQ(fuse_statis.at("fc_fuse"), 1);
  ASSERT_EQ(fuse_statis.at("fc_nobias_lstm_fuse"), 2);  // bi-directional LSTM
  ASSERT_EQ(fuse_statis.at("seq_concat_fc_fuse"), 1);
  ASSERT_EQ(num_ops,
            13);  // After graph optimization, only 13 operators exists.

  Timer timer;
  double total_time{0};
  double native_total_time{0};
  double analysis_total_time{0.};

  for (int i = 0; i < FLAGS_repeat; i++) {
    timer.tic();
    predictor->ZeroCopyRun();
    total_time += timer.toc();
  }

  auto *output_data = output_tensor->data<float>(&place, &output_size);
  ASSERT_GT(output_size, 0);  // more than one output!

  for (int i = 0; i < FLAGS_repeat; i++) {
    // Run native predictor.
    timer.tic();
    ASSERT_TRUE(native_predictor->Run(native_inputs.front(), &native_outputs));
    native_total_time += timer.toc();
  }

  for (int i = 0; i < FLAGS_repeat; i++) {
    timer.tic();
    ASSERT_TRUE(
        analysis_predictor->Run(native_inputs.front(), &analysis_outputs));
    analysis_total_time += timer.toc();
  }

  if (!FLAGS_with_precision_check) {
    return;
  }
  int native_output_size = VecReduceToInt(native_outputs.front().shape);

  EXPECT_EQ(native_output_size, output_size);

  // Compare tensors between analysis and zerocopy
  auto *p0 = static_cast<AnalysisPredictor *>(predictor.get());
  auto *p1 = static_cast<AnalysisPredictor *>(analysis_predictor.get());
  auto *p2 = static_cast<NativePaddlePredictor *>(native_predictor.get());

  std::vector<std::string> tensor_names;
  for (auto &var_desc : p0->program().Block(0).AllVars()) {
    tensor_names.push_back(var_desc->Name());
  }

  LOG(INFO) << "Comparing tensors";
  ASSERT_TRUE(
      CompareTensors(*p0->scope(), *p1->scope(), {"final_output.tmp_1"}));
  ASSERT_TRUE(
      CompareTensors(*p0->scope(), *p2->scope(), {"final_output.tmp_1"}));

  LOG(INFO) << "output1 " << inference::LoDTensorSummary<float>(
                                 p0->scope()
                                     ->FindVar("final_output.tmp_1")
                                     ->Get<framework::LoDTensor>());
  LOG(INFO) << "output2 " << inference::LoDTensorSummary<float>(
                                 p1->scope()
                                     ->FindVar("final_output.tmp_1")
                                     ->Get<framework::LoDTensor>());
  LOG(INFO) << "output3 " << inference::LoDTensorSummary<float>(
                                 p2->scope()
                                     ->FindVar("final_output.tmp_1")
                                     ->Get<framework::LoDTensor>());

  for (int i = 0; i < output_size; i++) {
    LOG(INFO) << output_data[i] << " "
              << static_cast<float *>(native_outputs.front().data.data())[i]
              << " "
              << static_cast<float *>(analysis_outputs.front().data.data())[i];
    EXPECT_NEAR(output_data[i],
                static_cast<float *>(native_outputs.front().data.data())[i],
                1e-3);
  }

  LOG(INFO) << "batch_size: " << FLAGS_batch_size;

  LOG(INFO) << "zero average time: "
            << total_time / (FLAGS_repeat * FLAGS_batch_size);
  LOG(INFO) << "analysis average time: "
            << analysis_total_time / (FLAGS_repeat * FLAGS_batch_size);
  LOG(INFO) << "native average time: "
            << native_total_time / (FLAGS_repeat * FLAGS_batch_size);
}

TEST(Analyzer_rnn1, ZeroCopyMultiThread) {
  AnalysisConfig config;
  SetConfig(&config);
  config.use_feed_fetch_ops = false;

#define NEW_TENSOR(name__) \
  auto name__##_tensor = predictor->GetInputTensor(#name__);

  auto base_predictor = CreatePaddlePredictor<AnalysisConfig>(config);
  double total_time_of_threads{0};
  std::vector<std::thread> threads;
  std::vector<std::unique_ptr<PaddlePredictor>> predictors;
  for (int tid = 0; tid < FLAGS_num_threads; tid++) {
    predictors.emplace_back(CreatePaddlePredictor<AnalysisConfig>(config));
  }

  for (int tid = 0; tid < FLAGS_num_threads; tid++) {
    threads.emplace_back([config, &total_time_of_threads, &predictors, tid] {
      // auto predictor = base_predictor->Clone();
      auto &predictor = predictors[tid];
      NEW_TENSOR(data_lod_attention);
      NEW_TENSOR(cell_init);
      NEW_TENSOR(data);
      NEW_TENSOR(week);
      NEW_TENSOR(minute);
      NEW_TENSOR(hidden_init);

      // Prepare data for AnalysisPredictor
      DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
      Timer timer;
      double total_time{0};

      for (int i = 0; i < FLAGS_repeat; i++) {
        PrepareZeroCopyInputs(data_lod_attention_tensor.get(),
                              cell_init_tensor.get(), data_tensor.get(),
                              hidden_init_tensor.get(), week_tensor.get(),
                              minute_tensor.get(), &data, FLAGS_batch_size);

        timer.tic();
        predictor->ZeroCopyRun();
        total_time += timer.toc();
      }

      total_time_of_threads += total_time;

      LOG(INFO) << "thread time: " << total_time / FLAGS_repeat;
    });
  }

  for (auto &t : threads) {
    t.join();
  }

  LOG(INFO) << "average time: "
            << total_time_of_threads / FLAGS_num_threads / FLAGS_repeat;
498 499 500 501
}

}  // namespace inference
}  // namespace paddle