analyzer_vis_tester.cc 4.1 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 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

namespace paddle {
namespace inference {
namespace analysis {

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;
}

/*
 * Use the native and analysis fluid engine to inference the demo.
 * ocr, mobilenet and se_resnext50
 */
T
tensor-tang 已提交
56
void TestVisualPrediction(bool use_mkldnn) {
T
tensor-tang 已提交
57 58 59 60 61
  std::unique_ptr<PaddlePredictor> predictor;
  AnalysisConfig cfg;
  cfg.param_file = FLAGS_infer_model + "/__params__";
  cfg.prog_file = FLAGS_infer_model + "/__model__";
  cfg.use_gpu = false;
T
tensor-tang 已提交
62
  cfg._use_mkldnn = use_mkldnn;
T
tensor-tang 已提交
63 64
  cfg.device = 0;
  cfg.enable_ir_optim = true;
T
tensor-tang 已提交
65
  // TODO(TJ): fix fusion gru
T
tensor-tang 已提交
66
  cfg.ir_passes.push_back("fc_gru_fuse_pass");
T
tensor-tang 已提交
67 68 69 70
#ifdef PADDLE_WITH_MKLDNN
  // disable mkldnn fuse since it should have some bugs
  cfg.ir_passes.push_back("conv_relu_mkldnn_fuse_pass");
#endif
T
tensor-tang 已提交
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
  predictor =
      CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(cfg);

  // Only have single batch of data.
  std::string line;
  std::ifstream file(FLAGS_infer_data);
  std::getline(file, line);
  auto record = ProcessALine(line);
  file.close();

  // Inference.
  PaddleTensor input;
  input.shape = record.shape;
  input.data =
      PaddleBuf(record.data.data(), record.data.size() * sizeof(float));
  input.dtype = PaddleDType::FLOAT32;

  std::vector<PaddleTensor> outputs_slots;
  Timer timer;
  timer.tic();
  for (int i = 0; i < FLAGS_repeat; i++) {
    predictor->Run({input}, &outputs_slots);
  }
  PrintTime(/*batch size*/ 1, FLAGS_repeat, /*num threads*/ 1, /*thread id*/ 0,
            timer.toc() / FLAGS_repeat);

  VLOG(3) << "output.size " << outputs_slots.size();

  // run native as reference
  auto ref_predictor =
T
tensor-tang 已提交
101
      CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(cfg);
T
tensor-tang 已提交
102 103
  std::vector<PaddleTensor> ref_outputs_slots;
  ref_predictor->Run({input}, &ref_outputs_slots);
T
tensor-tang 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  CompareResult(outputs_slots, ref_outputs_slots);
  // print what are fused
  AnalysisPredictor *analysis_predictor =
      dynamic_cast<AnalysisPredictor *>(predictor.get());
  auto &fuse_statis = analysis_predictor->analysis_argument()
                          .Get<std::unordered_map<std::string, int>>(
                              framework::ir::kFuseStatisAttr);
  for (auto &item : fuse_statis) {
    LOG(INFO) << "fused " << item.first << " " << item.second;
  }
  int num_ops = 0;
  for (auto &node :
       analysis_predictor->analysis_argument().main_dfg->nodes.nodes()) {
    if (node->IsFunction()) {
      ++num_ops;
T
tensor-tang 已提交
119 120
    }
  }
T
tensor-tang 已提交
121
  LOG(INFO) << "has num ops: " << num_ops;
T
tensor-tang 已提交
122 123
}

T
tensor-tang 已提交
124
TEST(Analyzer_vis, analysis) { TestVisualPrediction(/*use_mkldnn*/ false); }
T
tensor-tang 已提交
125
#ifdef PADDLE_WITH_MKLDNN
T
tensor-tang 已提交
126 127 128
TEST(Analyzer_vis, analysis_mkldnn) {
  TestVisualPrediction(/*use_mkldnn*/ true);
}
T
tensor-tang 已提交
129
#endif
T
tensor-tang 已提交
130 131 132 133

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