|
| 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 | +#include "paddle/fluid/inference/analysis/analyzer.h" |
| 15 | +#include <google/protobuf/text_format.h> |
| 16 | +#include <gtest/gtest.h> |
| 17 | +#include "paddle/fluid/framework/ir/pass.h" |
| 18 | +#include "paddle/fluid/inference/analysis/ut_helper.h" |
| 19 | +#include "paddle/fluid/inference/api/helper.h" |
| 20 | +#include "paddle/fluid/inference/api/paddle_inference_api.h" |
| 21 | +#include "paddle/fluid/platform/profiler.h" |
| 22 | + |
| 23 | +DEFINE_string(infer_model, "", "model path for LAC"); |
| 24 | +DEFINE_string(infer_data, "", "data file for LAC"); |
| 25 | +DEFINE_int32(batch_size, 1, "batch size."); |
| 26 | +DEFINE_int32(burning, 0, "Burning before repeat."); |
| 27 | +DEFINE_int32(repeat, 1, "Running the inference program repeat times."); |
| 28 | +DEFINE_bool(test_all_data, false, "Test the all dataset in data file."); |
| 29 | + |
| 30 | +namespace paddle { |
| 31 | +namespace inference { |
| 32 | +namespace analysis { |
| 33 | + |
| 34 | +struct DataRecord { |
| 35 | + std::vector<int64_t> data; |
| 36 | + std::vector<size_t> lod; |
| 37 | + // for dataset and nextbatch |
| 38 | + size_t batch_iter{0}; |
| 39 | + std::vector<std::vector<size_t>> batched_lods; |
| 40 | + std::vector<std::vector<int64_t>> batched_datas; |
| 41 | + std::vector<std::vector<int64_t>> datasets; |
| 42 | + DataRecord() = default; |
| 43 | + explicit DataRecord(const std::string &path, int batch_size = 1) { |
| 44 | + Load(path); |
| 45 | + Prepare(batch_size); |
| 46 | + batch_iter = 0; |
| 47 | + } |
| 48 | + void Load(const std::string &path) { |
| 49 | + std::ifstream file(path); |
| 50 | + std::string line; |
| 51 | + int num_lines = 0; |
| 52 | + datasets.resize(0); |
| 53 | + while (std::getline(file, line)) { |
| 54 | + num_lines++; |
| 55 | + std::vector<std::string> data; |
| 56 | + split(line, ';', &data); |
| 57 | + std::vector<int64_t> words_ids; |
| 58 | + split_to_int64(data[1], ' ', &words_ids); |
| 59 | + datasets.emplace_back(words_ids); |
| 60 | + } |
| 61 | + } |
| 62 | + void Prepare(int bs) { |
| 63 | + if (bs == 1) { |
| 64 | + batched_datas = datasets; |
| 65 | + for (auto one_sentence : datasets) { |
| 66 | + batched_lods.push_back({0, one_sentence.size()}); |
| 67 | + } |
| 68 | + } else { |
| 69 | + std::vector<int64_t> one_batch; |
| 70 | + std::vector<size_t> lod{0}; |
| 71 | + int bs_id = 0; |
| 72 | + for (auto one_sentence : datasets) { |
| 73 | + bs_id++; |
| 74 | + one_batch.insert(one_batch.end(), one_sentence.begin(), |
| 75 | + one_sentence.end()); |
| 76 | + lod.push_back(lod.back() + one_sentence.size()); |
| 77 | + if (bs_id == bs) { |
| 78 | + bs_id = 0; |
| 79 | + batched_datas.push_back(one_batch); |
| 80 | + batched_lods.push_back(lod); |
| 81 | + one_batch.clear(); |
| 82 | + one_batch.resize(0); |
| 83 | + lod.clear(); |
| 84 | + lod.resize(0); |
| 85 | + lod.push_back(0); |
| 86 | + } |
| 87 | + } |
| 88 | + if (one_batch.size() != 0) { |
| 89 | + batched_datas.push_back(one_batch); |
| 90 | + batched_lods.push_back(lod); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + DataRecord NextBatch() { |
| 95 | + DataRecord data; |
| 96 | + data.data = batched_datas[batch_iter]; |
| 97 | + data.lod = batched_lods[batch_iter]; |
| 98 | + batch_iter++; |
| 99 | + if (batch_iter >= batched_datas.size()) { |
| 100 | + batch_iter = 0; |
| 101 | + } |
| 102 | + return data; |
| 103 | + } |
| 104 | +}; |
| 105 | +void GetOneBatch(std::vector<PaddleTensor> *input_slots, DataRecord *data, |
| 106 | + int batch_size) { |
| 107 | + auto one_batch = data->NextBatch(); |
| 108 | + PaddleTensor input_tensor; |
| 109 | + input_tensor.name = "word"; |
| 110 | + input_tensor.shape.assign({static_cast<int>(one_batch.data.size()), 1}); |
| 111 | + input_tensor.lod.assign({one_batch.lod}); |
| 112 | + input_tensor.dtype = PaddleDType::INT64; |
| 113 | + TensorAssignData<int64_t>(&input_tensor, {one_batch.data}); |
| 114 | + PADDLE_ENFORCE_EQ(batch_size, static_cast<int>(one_batch.lod.size() - 1)); |
| 115 | + input_slots->assign({input_tensor}); |
| 116 | +} |
| 117 | +static void PrintTime(const double latency, const int bs, const int repeat) { |
| 118 | + LOG(INFO) << "===========profile result==========="; |
| 119 | + LOG(INFO) << "batch_size: " << bs << ", repeat: " << repeat |
| 120 | + << ", avg latency: " << latency / repeat << "ms"; |
| 121 | + LOG(INFO) << "====================================="; |
| 122 | +} |
| 123 | +void BenchAllData(const std::string &model_path, const std::string &data_file, |
| 124 | + const int batch_size, const int repeat) { |
| 125 | + NativeConfig config; |
| 126 | + config.model_dir = model_path; |
| 127 | + config.use_gpu = false; |
| 128 | + config.device = 0; |
| 129 | + config.specify_input_name = true; |
| 130 | + std::vector<PaddleTensor> input_slots, outputs_slots; |
| 131 | + DataRecord data(data_file, batch_size); |
| 132 | + auto predictor = |
| 133 | + CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
| 134 | + GetOneBatch(&input_slots, &data, batch_size); |
| 135 | + for (int i = 0; i < FLAGS_burning; i++) { |
| 136 | + predictor->Run(input_slots, &outputs_slots); |
| 137 | + } |
| 138 | + Timer timer; |
| 139 | + double sum = 0; |
| 140 | + for (int i = 0; i < repeat; i++) { |
| 141 | + for (size_t bid = 0; bid < data.batched_datas.size(); ++bid) { |
| 142 | + GetOneBatch(&input_slots, &data, batch_size); |
| 143 | + timer.tic(); |
| 144 | + predictor->Run(input_slots, &outputs_slots); |
| 145 | + sum += timer.toc(); |
| 146 | + } |
| 147 | + } |
| 148 | + PrintTime(sum, batch_size, repeat); |
| 149 | +} |
| 150 | +const int64_t lac_ref_data[] = {24, 25, 25, 25, 38, 30, 31, 14, 15, 44, 24, 25, |
| 151 | + 25, 25, 25, 25, 44, 24, 25, 25, 25, 36, 42, 43, |
| 152 | + 44, 14, 15, 44, 14, 15, 44, 14, 15, 44, 38, 39, |
| 153 | + 14, 15, 44, 22, 23, 23, 23, 23, 23, 23, 23}; |
| 154 | +void TestLACPrediction(const std::string &model_path, |
| 155 | + const std::string &data_file, const int batch_size, |
| 156 | + const int repeat, bool test_all_data) { |
| 157 | + if (test_all_data) { |
| 158 | + BenchAllData(model_path, data_file, batch_size, repeat); |
| 159 | + return; |
| 160 | + } |
| 161 | + NativeConfig config; |
| 162 | + config.model_dir = model_path; |
| 163 | + config.use_gpu = false; |
| 164 | + config.device = 0; |
| 165 | + config.specify_input_name = true; |
| 166 | + std::vector<PaddleTensor> input_slots, outputs_slots; |
| 167 | + DataRecord data(data_file, batch_size); |
| 168 | + GetOneBatch(&input_slots, &data, batch_size); |
| 169 | + auto predictor = |
| 170 | + CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
| 171 | + for (int i = 0; i < FLAGS_burning; i++) { |
| 172 | + predictor->Run(input_slots, &outputs_slots); |
| 173 | + } |
| 174 | + Timer timer; |
| 175 | + timer.tic(); |
| 176 | + for (int i = 0; i < repeat; i++) { |
| 177 | + predictor->Run(input_slots, &outputs_slots); |
| 178 | + } |
| 179 | + PrintTime(timer.toc(), batch_size, repeat); |
| 180 | + EXPECT_EQ(outputs_slots.size(), 1UL); |
| 181 | + auto &out = outputs_slots[0]; |
| 182 | + size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1, |
| 183 | + [](int a, int b) { return a * b; }); |
| 184 | + size_t batch1_size = sizeof(lac_ref_data) / sizeof(int64_t); |
| 185 | + PADDLE_ENFORCE_GT(size, 0); |
| 186 | + EXPECT_GE(size, batch1_size); |
| 187 | + int64_t *pdata = static_cast<int64_t *>(out.data.data()); |
| 188 | + for (size_t i = 0; i < batch1_size; ++i) { |
| 189 | + EXPECT_EQ(pdata[i], lac_ref_data[i]); |
| 190 | + } |
| 191 | +} |
| 192 | +TEST(Analyzer_LAC, native) { |
| 193 | + LOG(INFO) << "LAC with native"; |
| 194 | + TestLACPrediction(FLAGS_infer_model, FLAGS_infer_data, FLAGS_batch_size, |
| 195 | + FLAGS_repeat, FLAGS_test_all_data); |
| 196 | +} |
| 197 | +} // namespace analysis |
| 198 | +} // namespace inference |
| 199 | +} // namespace paddle |
0 commit comments