Skip to content

Commit 1e7ccf9

Browse files
authored
Merge pull request #13126 from tensor-tang/fea/infer/ut/lac-new
add lac infer test
2 parents 3fe0575 + 9f02497 commit 1e7ccf9

File tree

5 files changed

+225
-16
lines changed

5 files changed

+225
-16
lines changed

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ function (inference_analysis_test TARGET)
2525
if(WITH_TESTING)
2626
set(options "")
2727
set(oneValueArgs "")
28-
set(multiValueArgs SRCS EXTRA_DEPS)
28+
set(multiValueArgs SRCS ARGS EXTRA_DEPS)
2929
cmake_parse_arguments(analysis_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
30-
3130
set(mem_opt "")
3231
if(WITH_GPU)
3332
set(mem_opt "--fraction_of_gpu_memory_to_use=0.5")
3433
endif()
3534
cc_test(${TARGET}
3635
SRCS "${analysis_test_SRCS}"
3736
DEPS analysis graph fc_fuse_pass graph_viz_pass infer_clean_graph_pass graph_pattern_detector pass ${analysis_test_EXTRA_DEPS}
38-
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model ${mem_opt})
37+
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model ${mem_opt} ${analysis_test_ARGS})
3938
set_tests_properties(${TARGET} PROPERTIES DEPENDS test_word2vec)
4039
endif(WITH_TESTING)
4140
endfunction(inference_analysis_test)
@@ -51,7 +50,7 @@ endfunction(inference_download_and_uncompress)
5150
set(DITU_RNN_MODEL_URL "http://paddle-inference-dist.bj.bcebos.com/ditu_rnn_fluid%2Fmodel.tar.gz")
5251
set(DITU_RNN_DATA_URL "http://paddle-inference-dist.bj.bcebos.com/ditu_rnn_fluid%2Fdata.txt.tar.gz")
5352
set(DITU_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo/ditu_rnn" CACHE PATH "Ditu RNN model and data root." FORCE)
54-
if (NOT EXISTS ${DITU_INSTALL_DIR})
53+
if (NOT EXISTS ${DITU_INSTALL_DIR} AND WITH_TESTING)
5554
inference_download_and_uncompress(${DITU_INSTALL_DIR} ${DITU_RNN_MODEL_URL} "ditu_rnn_fluid%2Fmodel.tar.gz")
5655
inference_download_and_uncompress(${DITU_INSTALL_DIR} ${DITU_RNN_DATA_URL} "ditu_rnn_fluid%2Fdata.txt.tar.gz")
5756
endif()
@@ -70,8 +69,7 @@ inference_analysis_test(test_analyzer SRCS analyzer_tester.cc
7069
attention_lstm_fuse_pass
7170
paddle_inference_api
7271
pass
73-
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model
74-
--infer_ditu_rnn_model=${DITU_INSTALL_DIR}/model
72+
ARGS --infer_ditu_rnn_model=${DITU_INSTALL_DIR}/model
7573
--infer_ditu_rnn_data=${DITU_INSTALL_DIR}/data.txt)
7674

7775
inference_analysis_test(test_data_flow_graph SRCS data_flow_graph_tester.cc)
@@ -88,13 +86,25 @@ inference_analysis_test(test_model_store_pass SRCS model_store_pass_tester.cc)
8886
set(CHINESE_NER_MODEL_URL "http://paddle-inference-dist.bj.bcebos.com/chinese_ner_model.tar.gz")
8987
set(CHINESE_NER_DATA_URL "http://paddle-inference-dist.bj.bcebos.com/chinese_ner-data.txt.tar.gz")
9088
set(CHINESE_NER_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo/chinese_ner" CACHE PATH "Chinese ner model and data root." FORCE)
91-
if (NOT EXISTS ${CHINESE_NER_INSTALL_DIR})
89+
if (NOT EXISTS ${CHINESE_NER_INSTALL_DIR} AND WITH_TESTING)
9290
inference_download_and_uncompress(${CHINESE_NER_INSTALL_DIR} ${CHINESE_NER_MODEL_URL} "chinese_ner_model.tar.gz")
9391
inference_download_and_uncompress(${CHINESE_NER_INSTALL_DIR} ${CHINESE_NER_DATA_URL} "chinese_ner-data.txt.tar.gz")
9492
endif()
9593

96-
inference_analysis_test(test_chinese_ner SRCS chinese_ner_tester.cc
94+
inference_analysis_test(test_analyzer_ner SRCS analyzer_ner_tester.cc
9795
EXTRA_DEPS paddle_inference_api paddle_fluid_api
98-
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model
99-
--infer_model=${CHINESE_NER_INSTALL_DIR}/model
96+
ARGS --infer_model=${CHINESE_NER_INSTALL_DIR}/model
10097
--infer_data=${CHINESE_NER_INSTALL_DIR}/data.txt)
98+
99+
set(LAC_MODEL_URL "http://paddle-inference-dist.bj.bcebos.com/lac_model.tar.gz")
100+
set(LAC_DATA_URL "http://paddle-inference-dist.bj.bcebos.com/lac_data.txt.tar.gz")
101+
set(LAC_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo/lac" CACHE PATH "LAC model and data root." FORCE)
102+
if (NOT EXISTS ${LAC_INSTALL_DIR} AND WITH_TESTING)
103+
inference_download_and_uncompress(${LAC_INSTALL_DIR} ${LAC_MODEL_URL} "lac_model.tar.gz")
104+
inference_download_and_uncompress(${LAC_INSTALL_DIR} ${LAC_DATA_URL} "lac_data.txt.tar.gz")
105+
endif()
106+
107+
inference_analysis_test(test_analyzer_lac SRCS analyzer_lac_tester.cc
108+
EXTRA_DEPS paddle_inference_api paddle_fluid_api
109+
ARGS --infer_model=${LAC_INSTALL_DIR}/model
110+
--infer_data=${LAC_INSTALL_DIR}/data.txt)
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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

paddle/fluid/inference/analysis/chinese_ner_tester.cc renamed to paddle/fluid/inference/analysis/analyzer_ner_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "paddle/fluid/inference/analysis/analyzer.h"
1516
#include <google/protobuf/text_format.h>
1617
#include <gtest/gtest.h>
1718
#include "paddle/fluid/framework/ir/pass.h"
18-
#include "paddle/fluid/inference/analysis/analyzer.h"
1919
#include "paddle/fluid/inference/analysis/ut_helper.h"
2020
#include "paddle/fluid/inference/api/helper.h"
2121
#include "paddle/fluid/inference/api/paddle_inference_api.h"

paddle/fluid/inference/analysis/analyzer_tester.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace paddle {
3434
namespace inference {
3535
namespace analysis {
3636

37-
using namespace framework;
37+
using namespace framework; // NOLINT
3838

3939
TEST(Analyzer, analysis_without_tensorrt) {
4040
FLAGS_IA_enable_tensorrt_subgraph_engine = false;
@@ -312,8 +312,8 @@ void TestDituRNNPrediction(const std::string &model_path,
312312
PADDLE_ENFORCE_GT(size, 0);
313313
float *data = static_cast<float *>(out.data.data());
314314
float *base_data = static_cast<float *>(base_out.data.data());
315-
for (size_t i = 0; i < size; i++) {
316-
EXPECT_NEAR(data[i], base_data[i], 1e-3);
315+
for (size_t j = 0; j < size; j++) {
316+
EXPECT_NEAR(data[j], base_data[j], 1e-3);
317317
}
318318
}
319319

paddle/fluid/inference/api/api_impl.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ void NativePaddlePredictor::PrepareFeedFetch() {
6262
for (auto *op : inference_program_->Block(0).AllOps()) {
6363
if (op->Type() == "feed") {
6464
int idx = boost::get<int>(op->GetAttr("col"));
65-
if (feeds_.size() <= (size_t)idx) {
65+
if (feeds_.size() <= static_cast<size_t>(idx)) {
6666
feeds_.resize(idx + 1);
6767
}
6868
feeds_[idx] = op;
6969
feed_names_[op->Output("Out")[0]] = idx;
7070
} else if (op->Type() == "fetch") {
7171
int idx = boost::get<int>(op->GetAttr("col"));
72-
if (fetchs_.size() <= (size_t)idx) {
72+
if (fetchs_.size() <= static_cast<size_t>(idx)) {
7373
fetchs_.resize(idx + 1);
7474
}
7575
fetchs_[idx] = op;

0 commit comments

Comments
 (0)