|
| 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 <google/protobuf/text_format.h> |
| 16 | +#include <gtest/gtest.h> |
| 17 | +#include "paddle/fluid/framework/ir/pass.h" |
| 18 | +#include "paddle/fluid/inference/analysis/analyzer.h" |
| 19 | +#include "paddle/fluid/inference/analysis/ut_helper.h" |
| 20 | +#include "paddle/fluid/inference/api/helper.h" |
| 21 | +#include "paddle/fluid/inference/api/paddle_inference_api.h" |
| 22 | +#include "paddle/fluid/platform/profiler.h" |
| 23 | + |
| 24 | +DEFINE_string(infer_model, "", "model path"); |
| 25 | +DEFINE_string(infer_data, "", "data path"); |
| 26 | +DEFINE_int32(batch_size, 10, "batch size."); |
| 27 | +DEFINE_int32(repeat, 1, "Running the inference program repeat times."); |
| 28 | + |
| 29 | +namespace paddle { |
| 30 | +namespace inference { |
| 31 | + |
| 32 | +struct DataRecord { |
| 33 | + std::vector<std::vector<int64_t>> word_data_all, mention_data_all; |
| 34 | + std::vector<std::vector<int64_t>> rnn_word_datas, rnn_mention_datas; |
| 35 | + std::vector<size_t> lod; // two inputs have the same lod info. |
| 36 | + size_t batch_iter{0}; |
| 37 | + size_t batch_size{1}; |
| 38 | + DataRecord() = default; |
| 39 | + explicit DataRecord(const std::string &path, int batch_size = 1) |
| 40 | + : batch_size(batch_size) { |
| 41 | + Load(path); |
| 42 | + } |
| 43 | + DataRecord NextBatch() { |
| 44 | + DataRecord data; |
| 45 | + size_t batch_end = batch_iter + batch_size; |
| 46 | + // NOTE skip the final batch, if no enough data is provided. |
| 47 | + if (batch_end <= word_data_all.size()) { |
| 48 | + data.word_data_all.assign(word_data_all.begin() + batch_iter, |
| 49 | + word_data_all.begin() + batch_end); |
| 50 | + data.mention_data_all.assign(mention_data_all.begin() + batch_iter, |
| 51 | + mention_data_all.begin() + batch_end); |
| 52 | + // Prepare LoDs |
| 53 | + data.lod.push_back(0); |
| 54 | + CHECK(!data.word_data_all.empty()); |
| 55 | + CHECK(!data.mention_data_all.empty()); |
| 56 | + CHECK_EQ(data.word_data_all.size(), data.mention_data_all.size()); |
| 57 | + for (size_t j = 0; j < data.word_data_all.size(); j++) { |
| 58 | + data.rnn_word_datas.push_back(data.word_data_all[j]); |
| 59 | + data.rnn_mention_datas.push_back(data.mention_data_all[j]); |
| 60 | + // calculate lod |
| 61 | + data.lod.push_back(data.lod.back() + data.word_data_all[j].size()); |
| 62 | + } |
| 63 | + } |
| 64 | + batch_iter += batch_size; |
| 65 | + return data; |
| 66 | + } |
| 67 | + void Load(const std::string &path) { |
| 68 | + std::ifstream file(path); |
| 69 | + std::string line; |
| 70 | + int num_lines = 0; |
| 71 | + while (std::getline(file, line)) { |
| 72 | + num_lines++; |
| 73 | + std::vector<std::string> data; |
| 74 | + split(line, ';', &data); |
| 75 | + // load word data |
| 76 | + std::vector<int64_t> word_data; |
| 77 | + split_to_int64(data[1], ' ', &word_data); |
| 78 | + // load mention data |
| 79 | + std::vector<int64_t> mention_data; |
| 80 | + split_to_int64(data[3], ' ', &mention_data); |
| 81 | + word_data_all.push_back(std::move(word_data)); |
| 82 | + mention_data_all.push_back(std::move(mention_data)); |
| 83 | + } |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data, |
| 88 | + int batch_size) { |
| 89 | + PaddleTensor lod_word_tensor, lod_mention_tensor; |
| 90 | + lod_word_tensor.name = "word"; |
| 91 | + lod_mention_tensor.name = "mention"; |
| 92 | + auto one_batch = data->NextBatch(); |
| 93 | + int size = one_batch.lod[one_batch.lod.size() - 1]; // token batch size |
| 94 | + lod_word_tensor.shape.assign({size, 1}); |
| 95 | + lod_word_tensor.lod.assign({one_batch.lod}); |
| 96 | + lod_mention_tensor.shape.assign({size, 1}); |
| 97 | + lod_mention_tensor.lod.assign({one_batch.lod}); |
| 98 | + // assign data |
| 99 | + TensorAssignData<int64_t>(&lod_word_tensor, one_batch.rnn_word_datas); |
| 100 | + TensorAssignData<int64_t>(&lod_mention_tensor, one_batch.rnn_mention_datas); |
| 101 | + // Set inputs. |
| 102 | + input_slots->assign({lod_word_tensor, lod_mention_tensor}); |
| 103 | + for (auto &tensor : *input_slots) { |
| 104 | + tensor.dtype = PaddleDType::INT64; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// the first inference result |
| 109 | +const int chinese_ner_result_data[] = {30, 45, 41, 48, 17, 26, |
| 110 | + 48, 39, 38, 16, 25}; |
| 111 | + |
| 112 | +void TestChineseNERPrediction() { |
| 113 | + NativeConfig config; |
| 114 | + config.prog_file = FLAGS_infer_model + "/__model__"; |
| 115 | + config.param_file = FLAGS_infer_model + "/param"; |
| 116 | + config.use_gpu = false; |
| 117 | + config.device = 0; |
| 118 | + config.specify_input_name = true; |
| 119 | + |
| 120 | + auto predictor = |
| 121 | + CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
| 122 | + std::vector<PaddleTensor> input_slots; |
| 123 | + DataRecord data(FLAGS_infer_data, FLAGS_batch_size); |
| 124 | + // Prepare inputs. |
| 125 | + PrepareInputs(&input_slots, &data, FLAGS_batch_size); |
| 126 | + std::vector<PaddleTensor> outputs; |
| 127 | + |
| 128 | + Timer timer; |
| 129 | + timer.tic(); |
| 130 | + for (int i = 0; i < FLAGS_repeat; i++) { |
| 131 | + predictor->Run(input_slots, &outputs); |
| 132 | + } |
| 133 | + LOG(INFO) << "===========profile result==========="; |
| 134 | + LOG(INFO) << "batch_size: " << FLAGS_batch_size |
| 135 | + << ", repeat: " << FLAGS_repeat |
| 136 | + << ", latency: " << timer.toc() / FLAGS_repeat << "ms"; |
| 137 | + LOG(INFO) << "====================================="; |
| 138 | + |
| 139 | + PADDLE_ENFORCE(outputs.size(), 1UL); |
| 140 | + auto &out = outputs[0]; |
| 141 | + size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1, |
| 142 | + [](int a, int b) { return a * b; }); |
| 143 | + PADDLE_ENFORCE_GT(size, 0); |
| 144 | + int64_t *result = static_cast<int64_t *>(out.data.data()); |
| 145 | + for (size_t i = 0; i < std::min(11UL, size); i++) { |
| 146 | + PADDLE_ENFORCE(result[i], chinese_ner_result_data[i]); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// Directly infer with the original model. |
| 151 | +TEST(Analyzer, Chinese_ner) { TestChineseNERPrediction(); } |
| 152 | + |
| 153 | +} // namespace inference |
| 154 | +} // namespace paddle |
0 commit comments