-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add mkl multi-thread test cases in PR-CI-INFERENCE #34946
Merged
OliverLPH
merged 3 commits into
PaddlePaddle:develop
from
OliverLPH:add_mkl_multi_thread
Aug 17, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
paddle/fluid/inference/tests/infer_ut/test_ernie_text_cls.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "test_suite.h" // NOLINT | ||
|
||
DEFINE_string(modeldir, "", "Directory of the inference model."); | ||
|
||
namespace paddle_infer { | ||
|
||
template <typename T> | ||
T cRandom(int min, int max) { | ||
unsigned int seed = 100; | ||
return (min + | ||
static_cast<T>(max * rand_r(&seed) / static_cast<T>(RAND_MAX + 1))); | ||
} | ||
|
||
std::map<std::string, paddle::test::Record> PrepareInput(int batch_size) { | ||
// init input data | ||
int digit_length = 115; | ||
paddle::test::Record input_ids, segment_ids; | ||
int input_num = batch_size * digit_length; | ||
std::vector<int64_t> input_data(input_num, 1); | ||
std::vector<int64_t> segment_data(input_num, 0); | ||
srand((unsigned)time(NULL)); | ||
for (int x = 0; x < input_data.size(); x++) { | ||
input_data[x] = cRandom<int>(1, 100); | ||
} | ||
input_ids.data = std::vector<float>(input_data.begin(), input_data.end()); | ||
input_ids.shape = std::vector<int>{batch_size, digit_length}; | ||
input_ids.type = paddle::PaddleDType::INT64; | ||
|
||
segment_ids.data = | ||
std::vector<float>(segment_data.begin(), segment_data.end()); | ||
segment_ids.shape = std::vector<int>{batch_size, digit_length}; | ||
segment_ids.type = paddle::PaddleDType::INT64; | ||
|
||
std::map<std::string, paddle::test::Record> my_input_data_map; | ||
my_input_data_map.insert({"input_ids", input_ids}); | ||
my_input_data_map.insert({"token_type_ids", segment_ids}); | ||
|
||
return my_input_data_map; | ||
} | ||
|
||
TEST(test_ernie_text_cls, analysis_gpu_bz2_buffer) { | ||
// init input data | ||
auto my_input_data_map = PrepareInput(2); | ||
// init output data | ||
std::map<std::string, paddle::test::Record> infer_output_data, | ||
truth_output_data; | ||
// prepare groudtruth config | ||
paddle_infer::Config config, config_no_ir; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以后可以把每个测试case的config.Summary()打印出来,develop和今后的2.2支持 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 收到,下个PR就把 config summary打印出来 |
||
config_no_ir.SetModel(FLAGS_modeldir + "/inference.pdmodel", | ||
FLAGS_modeldir + "/inference.pdiparams"); | ||
config_no_ir.SwitchIrOptim(false); | ||
|
||
// prepare inference config from buffer | ||
std::string prog_file = FLAGS_modeldir + "/inference.pdmodel"; | ||
std::string params_file = FLAGS_modeldir + "/inference.pdiparams"; | ||
std::string prog_str = paddle::test::read_file(prog_file); | ||
std::string params_str = paddle::test::read_file(params_file); | ||
config.SetModelBuffer(prog_str.c_str(), prog_str.size(), params_str.c_str(), | ||
params_str.size()); | ||
// get groudtruth by disbale ir | ||
paddle_infer::services::PredictorPool pred_pool_no_ir(config_no_ir, 1); | ||
SingleThreadPrediction(pred_pool_no_ir.Retrive(0), &my_input_data_map, | ||
&truth_output_data, 1); | ||
// get infer results | ||
paddle_infer::services::PredictorPool pred_pool(config, 1); | ||
SingleThreadPrediction(pred_pool.Retrive(0), &my_input_data_map, | ||
&infer_output_data); | ||
// check outputs | ||
CompareRecord(&truth_output_data, &infer_output_data); | ||
std::cout << "finish test" << std::endl; | ||
} | ||
|
||
TEST(test_ernie_text_cls, multi_thread4_mkl_fp32_bz2) { | ||
int thread_num = 4; | ||
// init input data | ||
auto my_input_data_map = PrepareInput(2); | ||
// init output data | ||
std::map<std::string, paddle::test::Record> infer_output_data, | ||
truth_output_data; | ||
// prepare groudtruth config | ||
paddle_infer::Config config, config_no_ir; | ||
config_no_ir.SetModel(FLAGS_modeldir + "/inference.pdmodel", | ||
FLAGS_modeldir + "/inference.pdiparams"); | ||
config.DisableGpu(); | ||
config_no_ir.SwitchIrOptim(false); | ||
// prepare inference config | ||
config.SetModel(FLAGS_modeldir + "/inference.pdmodel", | ||
FLAGS_modeldir + "/inference.pdiparams"); | ||
config.DisableGpu(); | ||
config.EnableMKLDNN(); | ||
config.SetMkldnnCacheCapacity(10); | ||
config.SetCpuMathLibraryNumThreads(10); | ||
// get groudtruth by disbale ir | ||
paddle_infer::services::PredictorPool pred_pool_no_ir(config_no_ir, 1); | ||
SingleThreadPrediction(pred_pool_no_ir.Retrive(0), &my_input_data_map, | ||
&truth_output_data, 1); | ||
|
||
// get infer results from multi threads | ||
std::vector<std::thread> threads; | ||
services::PredictorPool pred_pool(config, thread_num); | ||
for (int i = 0; i < thread_num; ++i) { | ||
threads.emplace_back(paddle::test::SingleThreadPrediction, | ||
pred_pool.Retrive(i), &my_input_data_map, | ||
&infer_output_data, 2); | ||
} | ||
|
||
// thread join & check outputs | ||
for (int i = 0; i < thread_num; ++i) { | ||
LOG(INFO) << "join tid : " << i; | ||
threads[i].join(); | ||
CompareRecord(&truth_output_data, &infer_output_data); | ||
} | ||
|
||
std::cout << "finish multi-thread test" << std::endl; | ||
} | ||
|
||
} // namespace paddle_infer | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
::google::ParseCommandLineFlags(&argc, &argv, true); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cRandom method seems to always return 1 https://ideone.com/Al9qCY
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got a build error on CI so I looked at this