analyzer_vis_tester.cc 4.9 KB
Newer Older
T
tensor-tang 已提交
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. */

Y
Yan Chunwei 已提交
15
#include <gtest/gtest.h>
16

T
tensor-tang 已提交
17 18
#include <fstream>
#include <iostream>
19

T
tensor-tang 已提交
20
#include "paddle/fluid/inference/tests/api/tester_helper.h"
T
tensor-tang 已提交
21 22 23 24 25 26 27 28 29 30 31

namespace paddle {
namespace inference {
namespace analysis {

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

Record ProcessALine(const std::string &line) {
M
minqiyang 已提交
32
  VLOG(3) << "process a line";
T
tensor-tang 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  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));
  }
M
minqiyang 已提交
50 51
  VLOG(3) << "data size " << record.data.size();
  VLOG(3) << "data shape size " << record.shape.size();
T
tensor-tang 已提交
52 53 54
  return record;
}

T
Tao Luo 已提交
55
void SetConfig(AnalysisConfig *cfg) {
56 57 58 59
  cfg->SetModel(FLAGS_infer_model + "/__model__",
                FLAGS_infer_model + "/__params__");
  cfg->DisableGpu();
  cfg->SwitchIrDebug();
Y
Yan Chunwei 已提交
60
  cfg->SwitchSpecifyInputNames(false);
T
Tao Luo 已提交
61
}
T
tensor-tang 已提交
62

T
Tao Luo 已提交
63
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
64
  PADDLE_ENFORCE_EQ(
65 66
      FLAGS_test_all_data,
      0,
67
      paddle::platform::errors::Fatal("Only have single batch of data."));
T
tensor-tang 已提交
68 69 70 71 72 73 74 75
  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 已提交
76 77 78 79 80 81 82
  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 已提交
83

T
Tao Luo 已提交
84 85
// Easy for profiling independently.
//  ocr, mobilenet and se_resnext50
T
Tao Luo 已提交
86
void profile(bool use_mkldnn = false) {
T
Tao Luo 已提交
87 88
  AnalysisConfig cfg;
  SetConfig(&cfg);
89 90 91
  if (use_mkldnn) {
    cfg.EnableMKLDNN();
  }
Y
Yan Chunwei 已提交
92
  // cfg.pass_builder()->TurnOnDebug();
93
  std::vector<std::vector<PaddleTensor>> outputs;
T
Tao Luo 已提交
94 95 96

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
97
  TestPrediction(reinterpret_cast<const PaddlePredictor::Config *>(&cfg),
98 99 100
                 input_slots_all,
                 &outputs,
                 FLAGS_num_threads);
T
Tao Luo 已提交
101 102 103 104 105 106 107
  if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) {
    std::string line;
    std::ifstream file(FLAGS_refer_result);
    std::getline(file, line);
    auto refer = ProcessALine(line);
    file.close();

108 109
    PADDLE_ENFORCE_GT(outputs.size(),
                      0,
110 111
                      paddle::platform::errors::Fatal(
                          "The size of output should be greater than 0."));
112
    auto &output = outputs.back().front();
T
Tao Luo 已提交
113 114 115
    size_t numel = output.data.length() / PaddleDtypeSize(output.dtype);
    CHECK_EQ(numel, refer.data.size());
    for (size_t i = 0; i < numel; ++i) {
116 117
      EXPECT_NEAR(
          static_cast<float *>(output.data.data())[i], refer.data[i], 1e-5);
T
Tao Luo 已提交
118 119
    }
  }
T
tensor-tang 已提交
120 121
}

T
Tao Luo 已提交
122 123 124 125 126 127
TEST(Analyzer_vis, profile) { profile(); }

#ifdef PADDLE_WITH_MKLDNN
TEST(Analyzer_vis, profile_mkldnn) { profile(true /* use_mkldnn */); }
#endif

T
Tao Luo 已提交
128 129 130 131 132
// Check the fuse status
TEST(Analyzer_vis, fuse_statis) {
  AnalysisConfig cfg;
  SetConfig(&cfg);
  int num_ops;
133 134
  auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg);
  GetFuseStatis(predictor.get(), &num_ops);
T
Tao Luo 已提交
135 136 137
}

// Compare result of NativeConfig and AnalysisConfig
T
Tao Luo 已提交
138
void compare(bool use_mkldnn = false) {
T
Tao Luo 已提交
139 140
  AnalysisConfig cfg;
  SetConfig(&cfg);
141 142 143
  if (use_mkldnn) {
    cfg.EnableMKLDNN();
  }
T
Tao Luo 已提交
144 145 146

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
147 148
  CompareNativeAndAnalysis(
      reinterpret_cast<const PaddlePredictor::Config *>(&cfg), input_slots_all);
T
Tao Luo 已提交
149 150
}

T
Tao Luo 已提交
151
TEST(Analyzer_vis, compare) { compare(); }
T
Tao Luo 已提交
152
#ifdef PADDLE_WITH_MKLDNN
T
Tao Luo 已提交
153
TEST(Analyzer_vis, compare_mkldnn) { compare(true /* use_mkldnn */); }
T
Tao Luo 已提交
154
#endif
T
tensor-tang 已提交
155

L
luotao1 已提交
156 157 158 159 160 161 162 163 164 165 166
// Compare Deterministic result
TEST(Analyzer_vis, compare_determine) {
  AnalysisConfig cfg;
  SetConfig(&cfg);

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
  CompareDeterministic(reinterpret_cast<const PaddlePredictor::Config *>(&cfg),
                       input_slots_all);
}

T
tensor-tang 已提交
167 168 169
}  // namespace analysis
}  // namespace inference
}  // namespace paddle