analyzer_text_classification_tester.cc 4.5 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.

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

namespace paddle {
L
luotao1 已提交
18
namespace inference {
19

20
struct DataReader {
L
luotao1 已提交
21 22
  explicit DataReader(const std::string &path)
      : file(new std::ifstream(path)) {}
23

L
luotao1 已提交
24
  bool NextBatch(std::vector<PaddleTensor> *input, int batch_size) {
25 26
    PADDLE_ENFORCE_EQ(batch_size, 1);
    std::string line;
L
luotao1 已提交
27 28 29
    PaddleTensor tensor;
    tensor.dtype = PaddleDType::INT64;
    tensor.lod.emplace_back(std::vector<size_t>({0}));
30 31 32 33 34 35
    std::vector<int64_t> data;

    for (int i = 0; i < batch_size; i++) {
      if (!std::getline(*file, line)) return false;
      inference::split_to_int64(line, ' ', &data);
    }
L
luotao1 已提交
36
    tensor.lod.front().push_back(data.size());
37

L
luotao1 已提交
38
    tensor.data.Resize(data.size() * sizeof(int64_t));
F
flame 已提交
39 40
    CHECK(tensor.data.data() != nullptr);
    CHECK(data.data() != nullptr);
L
luotao1 已提交
41 42 43 44
    memcpy(tensor.data.data(), data.data(), data.size() * sizeof(int64_t));
    tensor.shape.push_back(data.size());
    tensor.shape.push_back(1);
    input->assign({tensor});
45
    return true;
46 47
  }

48 49 50
  std::unique_ptr<std::ifstream> file;
};

T
Tao Luo 已提交
51
void SetConfig(AnalysisConfig *cfg) {
52 53 54 55
  cfg->SetModel(FLAGS_infer_model);
  cfg->DisableGpu();
  cfg->SwitchSpecifyInputNames();
  cfg->SwitchIrOptim();
T
Tao Luo 已提交
56
}
57

T
Tao Luo 已提交
58 59
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
  std::vector<PaddleTensor> input_slots;
L
luotao1 已提交
60
  DataReader reader(FLAGS_infer_data);
T
Tao Luo 已提交
61 62 63 64 65
  int num_batches = 0;
  while (reader.NextBatch(&input_slots, FLAGS_batch_size)) {
    (*inputs).emplace_back(input_slots);
    ++num_batches;
    if (!FLAGS_test_all_data) return;
66
  }
T
Tao Luo 已提交
67 68
  LOG(INFO) << "total number of samples: " << num_batches * FLAGS_batch_size;
}
L
luotao1 已提交
69

T
Tao Luo 已提交
70 71 72 73
// Easy for profiling independently.
TEST(Analyzer_Text_Classification, profile) {
  AnalysisConfig cfg;
  SetConfig(&cfg);
Y
Yan Chunwei 已提交
74
  cfg.SwitchIrDebug();
75
  std::vector<std::vector<PaddleTensor>> outputs;
76

T
Tao Luo 已提交
77 78
  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
79 80
  TestPrediction(reinterpret_cast<const PaddlePredictor::Config *>(&cfg),
                 input_slots_all, &outputs, FLAGS_num_threads);
81

T
Tao Luo 已提交
82 83
  if (FLAGS_num_threads == 1) {
    // Get output
84 85 86
    PADDLE_ENFORCE_GT(outputs.size(), 0);
    LOG(INFO) << "get outputs " << outputs.back().size();
    for (auto &output : outputs.back()) {
T
Tao Luo 已提交
87 88 89 90 91
      LOG(INFO) << "output.shape: " << to_string(output.shape);
      // no lod ?
      CHECK_EQ(output.lod.size(), 0UL);
      LOG(INFO) << "output.dtype: " << output.dtype;
      std::stringstream ss;
F
flame 已提交
92 93 94 95 96 97
      int num_data = 1;
      for (auto i : output.shape) {
        num_data *= i;
      }

      for (int i = 0; i < num_data; i++) {
T
Tao Luo 已提交
98 99 100 101
        ss << static_cast<float *>(output.data.data())[i] << " ";
      }
      LOG(INFO) << "output.data summary: " << ss.str();
      // one batch ends
102 103 104 105
    }
  }
}

T
Tao Luo 已提交
106 107 108 109
// Compare result of NativeConfig and AnalysisConfig
TEST(Analyzer_Text_Classification, compare) {
  AnalysisConfig cfg;
  SetConfig(&cfg);
Y
Yan Chunwei 已提交
110
  cfg.EnableMemoryOptim();
T
Tao Luo 已提交
111 112 113

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
114 115
  CompareNativeAndAnalysis(
      reinterpret_cast<const PaddlePredictor::Config *>(&cfg), input_slots_all);
T
Tao Luo 已提交
116
}
117

L
luotao1 已提交
118 119 120 121 122 123 124 125 126 127 128
// Compare Deterministic result
TEST(Analyzer_Text_Classification, 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);
}

129 130 131 132
TEST(Analyzer_Text_Classification, compare_against_embedding_fc_lstm_fused) {
  AnalysisConfig cfg;
  SetConfig(&cfg);
  // Enable embedding_fc_lstm_fuse_pass (disabled by default)
133
  cfg.pass_builder()->InsertPass(2, "embedding_fc_lstm_fuse_pass");
134 135 136

  std::vector<std::vector<PaddleTensor>> input_slots_all;
  SetInput(&input_slots_all);
137 138
  CompareNativeAndAnalysis(
      reinterpret_cast<const PaddlePredictor::Config *>(&cfg), input_slots_all);
139 140
}

L
luotao1 已提交
141
}  // namespace inference
142
}  // namespace paddle