|
| 1 | +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/inference/analysis/analyzer.h" |
| 16 | + |
| 17 | +#include <google/protobuf/text_format.h> |
| 18 | +#include <gtest/gtest.h> |
| 19 | +#include <thread> // NOLINT |
| 20 | +#include "paddle/fluid/framework/ir/fuse_pass_base.h" |
| 21 | +#include "paddle/fluid/framework/ir/pass.h" |
| 22 | +#include "paddle/fluid/inference/analysis/ut_helper.h" |
| 23 | +#include "paddle/fluid/inference/api/analysis_predictor.h" |
| 24 | +#include "paddle/fluid/inference/api/helper.h" |
| 25 | +#include "paddle/fluid/inference/api/paddle_inference_api.h" |
| 26 | +#include "paddle/fluid/inference/api/paddle_inference_pass.h" |
| 27 | + |
| 28 | +DEFINE_string(infer_model, "", "model path"); |
| 29 | +DEFINE_string(infer_data, "", "data path"); |
| 30 | +DEFINE_int32(batch_size, 1, "batch size."); |
| 31 | +DEFINE_int32(repeat, 1, "Running the inference program repeat times."); |
| 32 | +DEFINE_int32(num_threads, 1, "Running the inference program in multi-threads."); |
| 33 | + |
| 34 | +namespace paddle { |
| 35 | +namespace inference { |
| 36 | + |
| 37 | +using namespace framework; // NOLINT |
| 38 | + |
| 39 | +struct DataRecord { |
| 40 | + std::vector<std::vector<std::vector<float>>> link_step_data_all; |
| 41 | + std::vector<size_t> lod; |
| 42 | + std::vector<std::vector<float>> rnn_link_data; |
| 43 | + std::vector<float> result_data; |
| 44 | + size_t batch_iter{0}; |
| 45 | + size_t batch_size{1}; |
| 46 | + DataRecord() = default; |
| 47 | + explicit DataRecord(const std::string &path, int batch_size = 1) |
| 48 | + : batch_size(batch_size) { |
| 49 | + Load(path); |
| 50 | + } |
| 51 | + DataRecord NextBatch() { |
| 52 | + DataRecord data; |
| 53 | + size_t batch_end = batch_iter + batch_size; |
| 54 | + // NOTE skip the final batch, if no enough data is provided. |
| 55 | + if (batch_end <= link_step_data_all.size()) { |
| 56 | + data.link_step_data_all.assign(link_step_data_all.begin() + batch_iter, |
| 57 | + link_step_data_all.begin() + batch_end); |
| 58 | + // Prepare LoDs |
| 59 | + data.lod.push_back(0); |
| 60 | + CHECK(!data.link_step_data_all.empty()) << "empty"; |
| 61 | + for (size_t j = 0; j < data.link_step_data_all.size(); j++) { |
| 62 | + for (const auto &d : data.link_step_data_all[j]) { |
| 63 | + data.rnn_link_data.push_back(d); |
| 64 | + // calculate lod |
| 65 | + data.lod.push_back(data.lod.back() + 11); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + batch_iter += batch_size; |
| 70 | + return data; |
| 71 | + } |
| 72 | + void Load(const std::string &path) { |
| 73 | + std::ifstream file(path); |
| 74 | + std::string line; |
| 75 | + int num_lines = 0; |
| 76 | + while (std::getline(file, line)) { |
| 77 | + num_lines++; |
| 78 | + std::vector<std::string> data; |
| 79 | + split(line, ':', &data); |
| 80 | + if (num_lines % 2) { // feature |
| 81 | + std::vector<std::string> feature_data; |
| 82 | + split(data[1], ' ', &feature_data); |
| 83 | + std::vector<std::vector<float>> link_step_data; |
| 84 | + int feature_count = 1; |
| 85 | + std::vector<float> feature; |
| 86 | + for (auto &step_data : feature_data) { |
| 87 | + std::vector<float> tmp; |
| 88 | + split_to_float(step_data, ',', &tmp); |
| 89 | + feature.insert(feature.end(), tmp.begin(), tmp.end()); |
| 90 | + if (feature_count % 11 == 0) { // each sample has 11 features |
| 91 | + link_step_data.push_back(feature); |
| 92 | + feature.clear(); |
| 93 | + } |
| 94 | + feature_count++; |
| 95 | + } |
| 96 | + link_step_data_all.push_back(std::move(link_step_data)); |
| 97 | + } else { // result |
| 98 | + std::vector<float> tmp; |
| 99 | + split_to_float(data[1], ',', &tmp); |
| 100 | + result_data.insert(result_data.end(), tmp.begin(), tmp.end()); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +}; |
| 105 | +void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data, |
| 106 | + int batch_size) { |
| 107 | + PaddleTensor feed_tensor; |
| 108 | + feed_tensor.name = "feed"; |
| 109 | + auto one_batch = data->NextBatch(); |
| 110 | + int token_size = one_batch.rnn_link_data.size(); |
| 111 | + // each token has 11 features, each feature's dim is 54. |
| 112 | + std::vector<int> rnn_link_data_shape({token_size * 11, 54}); |
| 113 | + feed_tensor.shape = rnn_link_data_shape; |
| 114 | + feed_tensor.lod.assign({one_batch.lod}); |
| 115 | + feed_tensor.dtype = PaddleDType::FLOAT32; |
| 116 | + TensorAssignData<float>(&feed_tensor, one_batch.rnn_link_data); |
| 117 | + // Set inputs. |
| 118 | + input_slots->assign({feed_tensor}); |
| 119 | +} |
| 120 | + |
| 121 | +void CompareResult(const std::vector<PaddleTensor> &outputs, |
| 122 | + const std::vector<float> &base_result) { |
| 123 | + PADDLE_ENFORCE_GT(outputs.size(), 0); |
| 124 | + for (size_t i = 0; i < outputs.size(); i++) { |
| 125 | + auto &out = outputs[i]; |
| 126 | + size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1, |
| 127 | + [](int a, int b) { return a * b; }); |
| 128 | + PADDLE_ENFORCE_GT(size, 0); |
| 129 | + float *data = static_cast<float *>(out.data.data()); |
| 130 | + for (size_t i = 0; i < size; i++) { |
| 131 | + EXPECT_NEAR(data[i], base_result[i], 1e-3); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +// Test with a really complicate model. |
| 136 | +void TestRNN2Prediction() { |
| 137 | + AnalysisConfig config; |
| 138 | + config.prog_file = FLAGS_infer_model + "/__model__"; |
| 139 | + config.param_file = FLAGS_infer_model + "/param"; |
| 140 | + config.use_gpu = false; |
| 141 | + config.device = 0; |
| 142 | + config.specify_input_name = true; |
| 143 | + config.enable_ir_optim = true; |
| 144 | + PADDLE_ENFORCE(config.ir_mode == |
| 145 | + AnalysisConfig::IrPassMode::kExclude); // default |
| 146 | + |
| 147 | + int batch_size = FLAGS_batch_size; |
| 148 | + int num_times = FLAGS_repeat; |
| 149 | + |
| 150 | + auto base_predictor = |
| 151 | + CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
| 152 | + auto predictor = |
| 153 | + CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>( |
| 154 | + config); |
| 155 | + std::vector<PaddleTensor> input_slots; |
| 156 | + DataRecord data(FLAGS_infer_data, batch_size); |
| 157 | + PrepareInputs(&input_slots, &data, batch_size); |
| 158 | + std::vector<PaddleTensor> outputs, base_outputs; |
| 159 | + |
| 160 | + Timer timer1; |
| 161 | + timer1.tic(); |
| 162 | + for (int i = 0; i < num_times; i++) { |
| 163 | + base_predictor->Run(input_slots, &base_outputs); |
| 164 | + } |
| 165 | + PrintTime(batch_size, num_times, 1, 0, timer1.toc() / num_times); |
| 166 | + |
| 167 | + Timer timer2; |
| 168 | + timer2.tic(); |
| 169 | + for (int i = 0; i < num_times; i++) { |
| 170 | + predictor->Run(input_slots, &outputs); |
| 171 | + } |
| 172 | + PrintTime(batch_size, num_times, 1, 0, timer2.toc() / num_times); |
| 173 | + |
| 174 | + CompareResult(base_outputs, data.result_data); |
| 175 | + CompareResult(outputs, data.result_data); |
| 176 | +} |
| 177 | + |
| 178 | +TEST(Analyzer, rnn2) { TestRNN2Prediction(); } |
| 179 | + |
| 180 | +} // namespace inference |
| 181 | +} // namespace paddle |
0 commit comments