|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | #include "paddle/fluid/inference/analysis/analyzer.h"
|
16 |
| -#include <google/protobuf/text_format.h> |
17 | 16 | #include <gtest/gtest.h>
|
18 |
| -#include "paddle/fluid/framework/ir/pass.h" |
19 |
| -#include "paddle/fluid/inference/analysis/ut_helper.h" |
| 17 | +#include "paddle/fluid/framework/ir/fuse_pass_base.h" |
| 18 | +#include "paddle/fluid/inference/api/analysis_predictor.h" |
20 | 19 | #include "paddle/fluid/inference/api/helper.h"
|
21 |
| -#include "paddle/fluid/inference/api/paddle_inference_api.h" |
| 20 | +#include "paddle/fluid/inference/api/paddle_inference_pass.h" |
22 | 21 | #include "paddle/fluid/platform/profiler.h"
|
23 | 22 |
|
24 | 23 | DEFINE_string(infer_model, "", "model path");
|
@@ -112,19 +111,31 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
|
112 | 111 | const int chinese_ner_result_data[] = {30, 45, 41, 48, 17, 26,
|
113 | 112 | 48, 39, 38, 16, 25};
|
114 | 113 |
|
115 |
| -void TestChineseNERPrediction() { |
| 114 | +void TestChineseNERPrediction(bool use_analysis) { |
116 | 115 | NativeConfig config;
|
117 | 116 | config.prog_file = FLAGS_infer_model + "/__model__";
|
118 | 117 | config.param_file = FLAGS_infer_model + "/param";
|
119 | 118 | config.use_gpu = false;
|
120 | 119 | config.device = 0;
|
121 | 120 | config.specify_input_name = true;
|
122 | 121 |
|
123 |
| - auto predictor = |
124 |
| - CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
125 |
| - std::vector<PaddleTensor> input_slots; |
126 |
| - std::vector<PaddleTensor> outputs; |
| 122 | + std::vector<PaddleTensor> input_slots, outputs; |
| 123 | + std::unique_ptr<PaddlePredictor> predictor; |
127 | 124 | Timer timer;
|
| 125 | + if (use_analysis) { |
| 126 | + AnalysisConfig cfg; |
| 127 | + cfg.prog_file = FLAGS_infer_model + "/__model__"; |
| 128 | + cfg.param_file = FLAGS_infer_model + "/param"; |
| 129 | + cfg.use_gpu = false; |
| 130 | + cfg.device = 0; |
| 131 | + cfg.specify_input_name = true; |
| 132 | + cfg.enable_ir_optim = true; |
| 133 | + predictor = |
| 134 | + CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(cfg); |
| 135 | + } else { |
| 136 | + predictor = |
| 137 | + CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
| 138 | + } |
128 | 139 |
|
129 | 140 | if (FLAGS_test_all_data) {
|
130 | 141 | LOG(INFO) << "test all data";
|
@@ -165,10 +176,51 @@ void TestChineseNERPrediction() {
|
165 | 176 | for (size_t i = 0; i < std::min(11UL, size); i++) {
|
166 | 177 | PADDLE_ENFORCE(result[i], chinese_ner_result_data[i]);
|
167 | 178 | }
|
| 179 | + |
| 180 | + if (use_analysis) { |
| 181 | + // run once for comparion as reference |
| 182 | + auto ref_predictor = |
| 183 | + CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); |
| 184 | + std::vector<PaddleTensor> ref_outputs_slots; |
| 185 | + ref_predictor->Run(input_slots, &ref_outputs_slots); |
| 186 | + EXPECT_EQ(ref_outputs_slots.size(), outputs.size()); |
| 187 | + auto &ref_out = ref_outputs_slots[0]; |
| 188 | + size_t ref_size = |
| 189 | + std::accumulate(ref_out.shape.begin(), ref_out.shape.end(), 1, |
| 190 | + [](int a, int b) { return a * b; }); |
| 191 | + EXPECT_EQ(size, ref_size); |
| 192 | + int64_t *pdata_ref = static_cast<int64_t *>(ref_out.data.data()); |
| 193 | + for (size_t i = 0; i < size; ++i) { |
| 194 | + EXPECT_EQ(pdata_ref[i], result[i]); |
| 195 | + } |
| 196 | + |
| 197 | + AnalysisPredictor *analysis_predictor = |
| 198 | + dynamic_cast<AnalysisPredictor *>(predictor.get()); |
| 199 | + auto &fuse_statis = analysis_predictor->analysis_argument() |
| 200 | + .Get<std::unordered_map<std::string, int>>( |
| 201 | + framework::ir::kFuseStatisAttr); |
| 202 | + for (auto &item : fuse_statis) { |
| 203 | + LOG(INFO) << "fused " << item.first << " " << item.second; |
| 204 | + } |
| 205 | + int num_ops = 0; |
| 206 | + for (auto &node : |
| 207 | + analysis_predictor->analysis_argument().main_dfg->nodes.nodes()) { |
| 208 | + if (node->IsFunction()) { |
| 209 | + ++num_ops; |
| 210 | + } |
| 211 | + } |
| 212 | + LOG(INFO) << "has num ops: " << num_ops; |
| 213 | + ASSERT_TRUE(fuse_statis.count("fc_fuse")); |
| 214 | + ASSERT_TRUE(fuse_statis.count("fc_gru_fuse")); |
| 215 | + EXPECT_EQ(fuse_statis.at("fc_fuse"), 1); |
| 216 | + EXPECT_EQ(fuse_statis.at("fc_gru_fuse"), 2); |
| 217 | + EXPECT_EQ(num_ops, 14); |
| 218 | + } |
168 | 219 | }
|
169 | 220 |
|
170 |
| -// Directly infer with the original model. |
171 |
| -TEST(Analyzer, Chinese_ner) { TestChineseNERPrediction(); } |
| 221 | +TEST(Analyzer_Chinese_ner, native) { TestChineseNERPrediction(false); } |
| 222 | + |
| 223 | +TEST(Analyzer_Chinese_ner, analysis) { TestChineseNERPrediction(true); } |
172 | 224 |
|
173 | 225 | } // namespace inference
|
174 | 226 | } // namespace paddle
|
0 commit comments