analyzer_vis_tester.cc 4.3 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#include <fstream>
#include <iostream>
T
tensor-tang 已提交
17
#include "paddle/fluid/inference/tests/api/tester_helper.h"
T
tensor-tang 已提交
18 19 20 21

namespace paddle {
namespace inference {
namespace analysis {
22
using contrib::AnalysisConfig;
T
tensor-tang 已提交
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

struct Record {
  std::vector<float> data;
  std::vector<int32_t> shape;
};

Record ProcessALine(const std::string &line) {
  VLOG(3) << "process a line";
  std::vector<std::string> columns;
  split(line, '\t', &columns);
  CHECK_EQ(columns.size(), 2UL)
      << "data format error, should be <data>\t<shape>";

  Record record;
  std::vector<std::string> data_strs;
  split(columns[0], ' ', &data_strs);
  for (auto &d : data_strs) {
    record.data.push_back(std::stof(d));
  }

  std::vector<std::string> shape_strs;
  split(columns[1], ' ', &shape_strs);
  for (auto &s : shape_strs) {
    record.shape.push_back(std::stoi(s));
  }
  VLOG(3) << "data size " << record.data.size();
  VLOG(3) << "data shape size " << record.shape.size();
  return record;
}

T
Tao Luo 已提交
53
void SetConfig(AnalysisConfig *cfg, bool _use_mkldnn = FLAGS_use_MKLDNN) {
T
Tao Luo 已提交
54 55 56 57 58 59
  cfg->param_file = FLAGS_infer_model + "/__params__";
  cfg->prog_file = FLAGS_infer_model + "/__model__";
  cfg->use_gpu = false;
  cfg->device = 0;
  cfg->enable_ir_optim = true;
  cfg->specify_input_name = true;
T
tensor-tang 已提交
60
  // TODO(TJ): fix fusion gru
T
Tao Luo 已提交
61
  cfg->ir_passes.push_back("fc_gru_fuse_pass");
T
Tao Luo 已提交
62
  cfg->_use_mkldnn = _use_mkldnn;
T
Tao Luo 已提交
63
}
T
tensor-tang 已提交
64

T
Tao Luo 已提交
65 66
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
  PADDLE_ENFORCE_EQ(FLAGS_test_all_data, 0, "Only have single batch of data.");
T
tensor-tang 已提交
67 68 69 70 71 72 73 74
  std::string line;
  std::ifstream file(FLAGS_infer_data);
  std::getline(file, line);
  auto record = ProcessALine(line);

  PaddleTensor input;
  input.shape = record.shape;
  input.dtype = PaddleDType::FLOAT32;
T
Tao Luo 已提交
75 76 77 78 79 80 81
  size_t input_size = record.data.size() * sizeof(float);
  input.data.Resize(input_size);
  memcpy(input.data.data(), record.data.data(), input_size);
  std::vector<PaddleTensor> input_slots;
  input_slots.assign({input});
  (*inputs).emplace_back(input_slots);
}
T
tensor-tang 已提交
82

T
Tao Luo 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
// Easy for profiling independently.
//  ocr, mobilenet and se_resnext50
TEST(Analyzer_vis, profile) {
  AnalysisConfig cfg;
  SetConfig(&cfg);
  std::vector<PaddleTensor> outputs;

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
  TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);

  if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) {
    const float ocr_result_data[] = {
        5.273636460856323538e-08, 3.296741795111302054e-07,
        1.873261190610264748e-08, 3.403730275408634043e-08,
        3.383312474625199684e-08};
    PADDLE_ENFORCE_EQ(outputs.size(), 1UL);
    size_t size = GetSize(outputs[0]);
    PADDLE_ENFORCE_GT(size, 0);
    float *result = static_cast<float *>(outputs[0].data.data());
    for (size_t i = 0; i < std::min(5UL, size); i++) {
      EXPECT_NEAR(result[i], ocr_result_data[i], 1e-3);
T
tensor-tang 已提交
105 106 107 108
    }
  }
}

T
Tao Luo 已提交
109 110 111 112 113
// Check the fuse status
TEST(Analyzer_vis, fuse_statis) {
  AnalysisConfig cfg;
  SetConfig(&cfg);
  int num_ops;
114 115
  auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg);
  GetFuseStatis(predictor.get(), &num_ops);
T
Tao Luo 已提交
116 117 118 119 120 121 122 123 124 125
}

// Compare result of NativeConfig and AnalysisConfig
TEST(Analyzer_vis, compare) {
  AnalysisConfig cfg;
  SetConfig(&cfg);

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
  CompareNativeAndAnalysis(cfg, input_slots_all);
T
Tao Luo 已提交
126 127 128
}

// Compare result of NativeConfig and AnalysisConfig with MKLDNN
T
Tao Luo 已提交
129
#ifdef PADDLE_WITH_MKLDNN
T
Tao Luo 已提交
130 131 132 133 134 135 136
TEST(Analyzer_vis, compare_mkldnn) {
  AnalysisConfig cfg;
  SetConfig(&cfg, true);

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
  CompareNativeAndAnalysis(cfg, input_slots_all);
T
tensor-tang 已提交
137
}
T
Tao Luo 已提交
138
#endif
T
tensor-tang 已提交
139 140 141 142

}  // namespace analysis
}  // namespace inference
}  // namespace paddle