Skip to content

Commit 63b38ca

Browse files
committed
add lac test
1 parent 663a11a commit 63b38ca

File tree

2 files changed

+211
-2
lines changed

2 files changed

+211
-2
lines changed

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ endfunction(inference_download_and_uncompress)
5050
set(DITU_RNN_MODEL_URL "http://paddle-inference-dist.bj.bcebos.com/ditu_rnn_fluid%2Fmodel.tar.gz")
5151
set(DITU_RNN_DATA_URL "http://paddle-inference-dist.bj.bcebos.com/ditu_rnn_fluid%2Fdata.txt.tar.gz")
5252
set(DITU_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo/ditu_rnn" CACHE PATH "Ditu RNN model and data root." FORCE)
53-
if (NOT EXISTS ${DITU_INSTALL_DIR})
53+
if (NOT EXISTS ${DITU_INSTALL_DIR} AND WITH_TESTING)
5454
inference_download_and_uncompress(${DITU_INSTALL_DIR} ${DITU_RNN_MODEL_URL} "ditu_rnn_fluid%2Fmodel.tar.gz")
5555
inference_download_and_uncompress(${DITU_INSTALL_DIR} ${DITU_RNN_DATA_URL} "ditu_rnn_fluid%2Fdata.txt.tar.gz")
5656
endif()
@@ -86,7 +86,7 @@ inference_analysis_test(test_model_store_pass SRCS model_store_pass_tester.cc)
8686
set(CHINESE_NER_MODEL_URL "http://paddle-inference-dist.bj.bcebos.com/chinese_ner_model.tar.gz")
8787
set(CHINESE_NER_DATA_URL "http://paddle-inference-dist.bj.bcebos.com/chinese_ner-data.txt.tar.gz")
8888
set(CHINESE_NER_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo/chinese_ner" CACHE PATH "Chinese ner model and data root." FORCE)
89-
if (NOT EXISTS ${CHINESE_NER_INSTALL_DIR})
89+
if (NOT EXISTS ${CHINESE_NER_INSTALL_DIR} AND WITH_TESTING)
9090
inference_download_and_uncompress(${CHINESE_NER_INSTALL_DIR} ${CHINESE_NER_MODEL_URL} "chinese_ner_model.tar.gz")
9191
inference_download_and_uncompress(${CHINESE_NER_INSTALL_DIR} ${CHINESE_NER_DATA_URL} "chinese_ner-data.txt.tar.gz")
9292
endif()
@@ -95,3 +95,16 @@ inference_analysis_test(test_analyzer_ner SRCS analyzer_ner_tester.cc
9595
EXTRA_DEPS paddle_inference_api paddle_fluid_api
9696
ARGS --infer_model=${CHINESE_NER_INSTALL_DIR}/model
9797
--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: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
DEFINE_string(infer_model, "", "model path for LAC");
23+
DEFINE_string(infer_data, "", "data file for LAC");
24+
DEFINE_int32(batch_size, 1, "batch size.");
25+
DEFINE_int32(burning, 0, "Burning before repeat.");
26+
DEFINE_int32(repeat, 1, "Running the inference program repeat times.");
27+
DEFINE_bool(test_all_data, false, "Test the all dataset in data file.");
28+
namespace paddle {
29+
namespace inference {
30+
namespace analysis {
31+
struct DataRecord {
32+
std::vector<int64_t> data;
33+
std::vector<size_t> lod;
34+
// for dataset and nextbatch
35+
size_t batch_iter{0};
36+
std::vector<std::vector<size_t>> batched_lods;
37+
std::vector<std::vector<int64_t>> batched_datas;
38+
std::vector<std::vector<int64_t>> datasets;
39+
DataRecord() = default;
40+
explicit DataRecord(const std::string &path, int batch_size = 1) {
41+
Load(path);
42+
Prepare(batch_size);
43+
batch_iter = 0;
44+
}
45+
void Load(const std::string &path) {
46+
std::ifstream file(path);
47+
std::string line;
48+
int num_lines = 0;
49+
datasets.resize(0);
50+
while (std::getline(file, line)) {
51+
num_lines++;
52+
std::vector<std::string> data;
53+
split(line, ';', &data);
54+
std::vector<int64_t> words_ids;
55+
split_to_int64(data[1], ' ', &words_ids);
56+
datasets.emplace_back(words_ids);
57+
}
58+
}
59+
void Prepare(int bs) {
60+
if (bs == 1) {
61+
batched_datas = datasets;
62+
for (auto one_sentence : datasets) {
63+
batched_lods.push_back({0, one_sentence.size()});
64+
}
65+
} else {
66+
std::vector<int64_t> one_batch;
67+
std::vector<size_t> lod{0};
68+
int bs_id = 0;
69+
for (auto one_sentence : datasets) {
70+
bs_id++;
71+
one_batch.insert(one_batch.end(), one_sentence.begin(),
72+
one_sentence.end());
73+
lod.push_back(lod.back() + one_sentence.size());
74+
if (bs_id == bs) {
75+
bs_id = 0;
76+
batched_datas.push_back(one_batch);
77+
batched_lods.push_back(lod);
78+
one_batch.clear();
79+
one_batch.resize(0);
80+
lod.clear();
81+
lod.resize(0);
82+
lod.push_back(0);
83+
}
84+
}
85+
if (one_batch.size() != 0) {
86+
batched_datas.push_back(one_batch);
87+
batched_lods.push_back(lod);
88+
}
89+
}
90+
}
91+
DataRecord NextBatch() {
92+
DataRecord data;
93+
data.data = batched_datas[batch_iter];
94+
data.lod = batched_lods[batch_iter];
95+
batch_iter++;
96+
if (batch_iter >= batched_datas.size()) {
97+
batch_iter = 0;
98+
}
99+
return data;
100+
}
101+
};
102+
void GetOneBatch(std::vector<PaddleTensor> *input_slots, DataRecord *data,
103+
int batch_size) {
104+
auto one_batch = data->NextBatch();
105+
PaddleTensor input_tensor;
106+
input_tensor.name = "word";
107+
input_tensor.shape.assign({static_cast<int>(one_batch.data.size()), 1});
108+
input_tensor.lod.assign({one_batch.lod});
109+
input_tensor.dtype = PaddleDType::INT64;
110+
TensorAssignData<int64_t>(&input_tensor, {one_batch.data});
111+
PADDLE_ENFORCE_EQ(batch_size, static_cast<int>(one_batch.lod.size() - 1));
112+
input_slots->assign({input_tensor});
113+
}
114+
static void PrintTime(const double latency, const int bs, const int repeat) {
115+
LOG(INFO) << "===========profile result===========";
116+
LOG(INFO) << "batch_size: " << bs << ", repeat: " << repeat
117+
<< ", avg latency: " << latency / repeat << "ms";
118+
LOG(INFO) << "=====================================";
119+
}
120+
void BenchAllData(const std::string &model_path, const std::string &data_file,
121+
const int batch_size, const int repeat) {
122+
NativeConfig config;
123+
config.model_dir = model_path;
124+
config.use_gpu = false;
125+
config.device = 0;
126+
config.specify_input_name = true;
127+
std::vector<PaddleTensor> input_slots, outputs_slots;
128+
DataRecord data(data_file, batch_size);
129+
auto predictor =
130+
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
131+
GetOneBatch(&input_slots, &data, batch_size);
132+
for (int i = 0; i < FLAGS_burning; i++) {
133+
predictor->Run(input_slots, &outputs_slots);
134+
}
135+
Timer timer;
136+
double sum = 0;
137+
for (int i = 0; i < repeat; i++) {
138+
for (size_t bid = 0; bid < data.batched_datas.size(); ++bid) {
139+
GetOneBatch(&input_slots, &data, batch_size);
140+
timer.tic();
141+
predictor->Run(input_slots, &outputs_slots);
142+
sum += timer.toc();
143+
}
144+
}
145+
PrintTime(sum, batch_size, repeat);
146+
}
147+
const int64_t lac_ref_data[] = {24, 25, 25, 25, 38, 30, 31, 14, 15, 44, 24, 25,
148+
25, 25, 25, 25, 44, 24, 25, 25, 25, 36, 42, 43,
149+
44, 14, 15, 44, 14, 15, 44, 14, 15, 44, 38, 39,
150+
14, 15, 44, 22, 23, 23, 23, 23, 23, 23, 23};
151+
void TestLACPrediction(const std::string &model_path,
152+
const std::string &data_file, const int batch_size,
153+
const int repeat, bool test_all_data) {
154+
if (test_all_data) {
155+
BenchAllData(model_path, data_file, batch_size, repeat);
156+
return;
157+
}
158+
NativeConfig config;
159+
config.model_dir = model_path;
160+
config.use_gpu = false;
161+
config.device = 0;
162+
config.specify_input_name = true;
163+
std::vector<PaddleTensor> input_slots, outputs_slots;
164+
DataRecord data(data_file, batch_size);
165+
GetOneBatch(&input_slots, &data, batch_size);
166+
auto predictor =
167+
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
168+
for (int i = 0; i < FLAGS_burning; i++) {
169+
predictor->Run(input_slots, &outputs_slots);
170+
}
171+
Timer timer;
172+
timer.tic();
173+
for (int i = 0; i < repeat; i++) {
174+
predictor->Run(input_slots, &outputs_slots);
175+
}
176+
PrintTime(timer.toc(), batch_size, repeat);
177+
EXPECT_EQ(outputs_slots.size(), 1UL);
178+
auto &out = outputs_slots[0];
179+
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,
180+
[](int a, int b) { return a * b; });
181+
size_t batch1_size = sizeof(lac_ref_data) / sizeof(int64_t);
182+
PADDLE_ENFORCE_GT(size, 0);
183+
EXPECT_GE(size, batch1_size);
184+
int64_t *pdata = static_cast<int64_t *>(out.data.data());
185+
for (size_t i = 0; i < batch1_size; ++i) {
186+
EXPECT_EQ(pdata[i], lac_ref_data[i]);
187+
}
188+
}
189+
TEST(Analyzer_LAC, native) {
190+
LOG(INFO) << "LAC with native";
191+
TestLACPrediction(FLAGS_infer_model, FLAGS_infer_data, FLAGS_batch_size,
192+
FLAGS_repeat, FLAGS_test_all_data);
193+
}
194+
} // namespace analysis
195+
} // namespace inference
196+
} // namespace paddle

0 commit comments

Comments
 (0)