|
| 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/tests/api/tester_helper.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace inference { |
| 19 | +using contrib::AnalysisConfig; |
| 20 | +#define MAX_TURN_NUM 9 |
| 21 | +#define MAX_TURN_LEN 50 |
| 22 | +static std::vector<float> result_data; |
| 23 | + |
| 24 | +struct DataRecord { |
| 25 | + std::vector<std::vector<int64_t>> |
| 26 | + turns[MAX_TURN_NUM]; // turns data : MAX_TURN_NUM |
| 27 | + std::vector<std::vector<float>> |
| 28 | + turns_mask[MAX_TURN_NUM]; // turns mask data : MAX_TURN_NUM |
| 29 | + std::vector<std::vector<int64_t>> response; // response data : 1 |
| 30 | + std::vector<std::vector<float>> response_mask; // response mask data : 1 |
| 31 | + size_t batch_iter{0}; |
| 32 | + size_t batch_size{1}; |
| 33 | + size_t num_samples; // total number of samples |
| 34 | + DataRecord() = default; |
| 35 | + explicit DataRecord(const std::string &path, int batch_size = 1) |
| 36 | + : batch_size(batch_size) { |
| 37 | + Load(path); |
| 38 | + } |
| 39 | + DataRecord NextBatch() { |
| 40 | + DataRecord data; |
| 41 | + size_t batch_end = batch_iter + batch_size; |
| 42 | + // NOTE skip the final batch, if no enough data is provided. |
| 43 | + if (batch_end <= response.size()) { |
| 44 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 45 | + data.turns[i].assign(turns[i].begin() + batch_iter, |
| 46 | + turns[i].begin() + batch_end); |
| 47 | + } |
| 48 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 49 | + data.turns_mask[i].assign(turns_mask[i].begin() + batch_iter, |
| 50 | + turns_mask[i].begin() + batch_end); |
| 51 | + } |
| 52 | + data.response.assign(response.begin() + batch_iter, |
| 53 | + response.begin() + batch_end); |
| 54 | + data.response_mask.assign(response_mask.begin() + batch_iter, |
| 55 | + response_mask.begin() + batch_end); |
| 56 | + CHECK(!data.response.empty()); |
| 57 | + CHECK(!data.response_mask.empty()); |
| 58 | + CHECK_EQ(data.response.size(), data.response_mask.size()); |
| 59 | + } |
| 60 | + batch_iter += batch_size; |
| 61 | + return data; |
| 62 | + } |
| 63 | + void Load(const std::string &path) { |
| 64 | + std::ifstream file(path); |
| 65 | + std::string line; |
| 66 | + size_t num_lines = 0; |
| 67 | + result_data.clear(); |
| 68 | + while (std::getline(file, line)) { |
| 69 | + num_lines++; |
| 70 | + std::vector<std::string> data; |
| 71 | + split(line, ',', &data); |
| 72 | + CHECK_EQ(data.size(), 2 * MAX_TURN_NUM + 3); |
| 73 | + // load turn data |
| 74 | + std::vector<int64_t> turns_tmp[MAX_TURN_NUM]; |
| 75 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 76 | + split_to_int64(data[i], ' ', &turns_tmp[i]); |
| 77 | + turns[i].push_back(std::move(turns_tmp[i])); |
| 78 | + } |
| 79 | + // load turn_mask data |
| 80 | + std::vector<float> turns_mask_tmp[MAX_TURN_NUM]; |
| 81 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 82 | + split_to_float(data[MAX_TURN_NUM + i], ' ', &turns_mask_tmp[i]); |
| 83 | + turns_mask[i].push_back(std::move(turns_mask_tmp[i])); |
| 84 | + } |
| 85 | + // load response data |
| 86 | + std::vector<int64_t> response_tmp; |
| 87 | + split_to_int64(data[2 * MAX_TURN_NUM], ' ', &response_tmp); |
| 88 | + response.push_back(std::move(response_tmp)); |
| 89 | + // load response_mask data |
| 90 | + std::vector<float> response_mask_tmp; |
| 91 | + split_to_float(data[2 * MAX_TURN_NUM + 1], ' ', &response_mask_tmp); |
| 92 | + response_mask.push_back(std::move(response_mask_tmp)); |
| 93 | + // load result data |
| 94 | + float result_tmp; |
| 95 | + result_tmp = std::stof(data[2 * MAX_TURN_NUM + 2]); |
| 96 | + result_data.push_back(result_tmp); |
| 97 | + } |
| 98 | + num_samples = num_lines; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data, |
| 103 | + int batch_size) { |
| 104 | + PaddleTensor turns_tensor[MAX_TURN_NUM]; |
| 105 | + PaddleTensor turns_mask_tensor[MAX_TURN_NUM]; |
| 106 | + PaddleTensor response_tensor; |
| 107 | + PaddleTensor response_mask_tensor; |
| 108 | + std::string turn_pre = "turn_"; |
| 109 | + std::string turn_mask_pre = "turn_mask_"; |
| 110 | + |
| 111 | + auto one_batch = data->NextBatch(); |
| 112 | + int size = one_batch.response[0].size(); |
| 113 | + CHECK_EQ(size, MAX_TURN_LEN); |
| 114 | + // turn tensor assignment |
| 115 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 116 | + turns_tensor[i].name = turn_pre + std::to_string(i); |
| 117 | + turns_tensor[i].shape.assign({batch_size, size, 1}); |
| 118 | + turns_tensor[i].dtype = PaddleDType::INT64; |
| 119 | + TensorAssignData<int64_t>(&turns_tensor[i], one_batch.turns[i]); |
| 120 | + } |
| 121 | + // turn mask tensor assignment |
| 122 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 123 | + turns_mask_tensor[i].name = turn_mask_pre + std::to_string(i); |
| 124 | + turns_mask_tensor[i].shape.assign({batch_size, size, 1}); |
| 125 | + turns_mask_tensor[i].dtype = PaddleDType::FLOAT32; |
| 126 | + TensorAssignData<float>(&turns_mask_tensor[i], one_batch.turns_mask[i]); |
| 127 | + } |
| 128 | + // response tensor assignment |
| 129 | + response_tensor.name = "response"; |
| 130 | + response_tensor.shape.assign({batch_size, size, 1}); |
| 131 | + response_tensor.dtype = PaddleDType::INT64; |
| 132 | + TensorAssignData<int64_t>(&response_tensor, one_batch.response); |
| 133 | + // response mask tensor assignment |
| 134 | + response_mask_tensor.name = "response_mask"; |
| 135 | + response_mask_tensor.shape.assign({batch_size, size, 1}); |
| 136 | + response_mask_tensor.dtype = PaddleDType::FLOAT32; |
| 137 | + TensorAssignData<float>(&response_mask_tensor, one_batch.response_mask); |
| 138 | + |
| 139 | + // Set inputs. |
| 140 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 141 | + input_slots->push_back(std::move(turns_tensor[i])); |
| 142 | + } |
| 143 | + for (int i = 0; i < MAX_TURN_NUM; ++i) { |
| 144 | + input_slots->push_back(std::move(turns_mask_tensor[i])); |
| 145 | + } |
| 146 | + input_slots->push_back(std::move(response_tensor)); |
| 147 | + input_slots->push_back(std::move(response_mask_tensor)); |
| 148 | +} |
| 149 | + |
| 150 | +void SetConfig(contrib::AnalysisConfig *cfg) { |
| 151 | + cfg->prog_file = FLAGS_infer_model + "/__model__"; |
| 152 | + cfg->param_file = FLAGS_infer_model + "/param"; |
| 153 | + cfg->use_gpu = false; |
| 154 | + cfg->device = 0; |
| 155 | + cfg->specify_input_name = true; |
| 156 | + cfg->enable_ir_optim = true; |
| 157 | +} |
| 158 | + |
| 159 | +void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) { |
| 160 | + DataRecord data(FLAGS_infer_data, FLAGS_batch_size); |
| 161 | + std::vector<PaddleTensor> input_slots; |
| 162 | + int test_batch_num = |
| 163 | + FLAGS_test_all_data ? data.num_samples / FLAGS_batch_size : 1; |
| 164 | + LOG(INFO) << "The number of samples to be test: " |
| 165 | + << test_batch_num * FLAGS_batch_size; |
| 166 | + for (int bid = 0; bid < test_batch_num; ++bid) { |
| 167 | + input_slots.clear(); |
| 168 | + PrepareInputs(&input_slots, &data, FLAGS_batch_size); |
| 169 | + (*inputs).emplace_back(input_slots); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// Easy for profiling independently. |
| 174 | +TEST(Analyzer_dam, profile) { |
| 175 | + contrib::AnalysisConfig cfg; |
| 176 | + SetConfig(&cfg); |
| 177 | + |
| 178 | + std::vector<PaddleTensor> outputs; |
| 179 | + std::vector<std::vector<PaddleTensor>> input_slots_all; |
| 180 | + SetInput(&input_slots_all); |
| 181 | + TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads); |
| 182 | + |
| 183 | + if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) { |
| 184 | + PADDLE_ENFORCE_GT(outputs.size(), 0); |
| 185 | + size_t size = GetSize(outputs[0]); |
| 186 | + PADDLE_ENFORCE_GT(size, 0); |
| 187 | + float *result = static_cast<float *>(outputs[0].data.data()); |
| 188 | + for (size_t i = 0; i < size; i++) { |
| 189 | + EXPECT_NEAR(result[i], result_data[i], 1e-3); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// Check the fuse status |
| 195 | +TEST(Analyzer_dam, fuse_statis) { |
| 196 | + contrib::AnalysisConfig cfg; |
| 197 | + SetConfig(&cfg); |
| 198 | + |
| 199 | + if (FLAGS_use_analysis) { |
| 200 | + int num_ops; |
| 201 | + auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg); |
| 202 | + auto fuse_statis = GetFuseStatis( |
| 203 | + static_cast<AnalysisPredictor *>(predictor.get()), &num_ops); |
| 204 | + ASSERT_TRUE(fuse_statis.count("fc_fuse")); |
| 205 | + EXPECT_EQ(fuse_statis.at("fc_fuse"), 317); |
| 206 | + EXPECT_EQ(num_ops, 2020); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +// Compare result of NativeConfig and AnalysisConfig |
| 211 | +TEST(Analyzer_dam, compare) { |
| 212 | + contrib::AnalysisConfig cfg; |
| 213 | + SetConfig(&cfg); |
| 214 | + |
| 215 | + std::vector<std::vector<PaddleTensor>> input_slots_all; |
| 216 | + SetInput(&input_slots_all); |
| 217 | + |
| 218 | + if (FLAGS_use_analysis) { |
| 219 | + CompareNativeAndAnalysis(cfg, input_slots_all); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +} // namespace inference |
| 224 | +} // namespace paddle |
0 commit comments