-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Fix abort issue in cpu multi-threads #11233
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6840953
refine nlp multi-threads
tensor-tang 9b34f8d
fix abort issue in cpu multi-threads
tensor-tang 944bdee
Merge remote-tracking branch 'ups/develop' into multithreads
tensor-tang 4ae935e
refine the lock in scope
tensor-tang 64323b1
Merge remote-tracking branch 'ups/develop' into multithreads
tensor-tang 4b7b17a
fix conflcts
tensor-tang 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
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 |
---|---|---|
|
@@ -101,23 +101,22 @@ void SplitData( | |
} | ||
|
||
void ThreadRunInfer( | ||
const int tid, paddle::framework::Executor* executor, | ||
paddle::framework::Scope* scope, | ||
const std::unique_ptr<paddle::framework::ProgramDesc>& inference_program, | ||
const int tid, paddle::framework::Scope* scope, | ||
const std::vector<std::vector<const paddle::framework::LoDTensor*>>& jobs) { | ||
auto copy_program = std::unique_ptr<paddle::framework::ProgramDesc>( | ||
new paddle::framework::ProgramDesc(*inference_program)); | ||
// maybe framework:ProgramDesc is not thread-safe | ||
auto& sub_scope = scope->NewScope(); | ||
auto place = paddle::platform::CPUPlace(); | ||
auto executor = paddle::framework::Executor(place); | ||
auto inference_program = | ||
paddle::inference::Load(&executor, scope, FLAGS_model_path); | ||
|
||
std::string feed_holder_name = "feed_" + paddle::string::to_string(tid); | ||
std::string fetch_holder_name = "fetch_" + paddle::string::to_string(tid); | ||
copy_program->SetFeedHolderName(feed_holder_name); | ||
copy_program->SetFetchHolderName(fetch_holder_name); | ||
auto ctx = executor.Prepare(*inference_program, /*block_id*/ 0); | ||
executor.CreateVariables(*inference_program, &sub_scope, /*block_id*/ 0); | ||
|
||
const std::vector<std::string>& feed_target_names = | ||
copy_program->GetFeedTargetNames(); | ||
inference_program->GetFeedTargetNames(); | ||
const std::vector<std::string>& fetch_target_names = | ||
copy_program->GetFetchTargetNames(); | ||
inference_program->GetFetchTargetNames(); | ||
|
||
PADDLE_ENFORCE_EQ(fetch_target_names.size(), 1UL); | ||
std::map<std::string, paddle::framework::LoDTensor*> fetch_targets; | ||
|
@@ -131,9 +130,8 @@ void ThreadRunInfer( | |
auto start_ms = GetCurrentMs(); | ||
for (size_t i = 0; i < inputs.size(); ++i) { | ||
feed_targets[feed_target_names[0]] = inputs[i]; | ||
executor->Run(*copy_program, &sub_scope, &feed_targets, &fetch_targets, | ||
true /*create_local_scope*/, true /*create_vars*/, | ||
feed_holder_name, fetch_holder_name); | ||
executor.RunPreparedContext(ctx.get(), &sub_scope, &feed_targets, | ||
&fetch_targets, false /*create_local_scope*/); | ||
} | ||
auto stop_ms = GetCurrentMs(); | ||
scope->DeleteScope(&sub_scope); | ||
|
@@ -158,22 +156,10 @@ TEST(inference, nlp) { | |
LOG(INFO) << "Number of samples (seq_len<1024): " << datasets.size(); | ||
LOG(INFO) << "Total number of words: " << num_total_words; | ||
|
||
const bool model_combined = false; | ||
// 0. Call `paddle::framework::InitDevices()` initialize all the devices | ||
// 1. Define place, executor, scope | ||
auto place = paddle::platform::CPUPlace(); | ||
auto executor = paddle::framework::Executor(place); | ||
std::unique_ptr<paddle::framework::Scope> scope( | ||
new paddle::framework::Scope()); | ||
|
||
// 2. Initialize the inference_program and load parameters | ||
std::unique_ptr<paddle::framework::ProgramDesc> inference_program; | ||
inference_program = | ||
InitProgram(&executor, scope.get(), FLAGS_model_path, model_combined); | ||
if (FLAGS_use_mkldnn) { | ||
EnableMKLDNN(inference_program); | ||
} | ||
|
||
#ifdef PADDLE_WITH_MKLML | ||
// only use 1 thread number per std::thread | ||
omp_set_dynamic(0); | ||
|
@@ -189,21 +175,30 @@ TEST(inference, nlp) { | |
start_ms = GetCurrentMs(); | ||
for (int i = 0; i < FLAGS_num_threads; ++i) { | ||
threads.emplace_back( | ||
new std::thread(ThreadRunInfer, i, &executor, scope.get(), | ||
std::ref(inference_program), std::ref(jobs))); | ||
new std::thread(ThreadRunInfer, i, scope.get(), std::ref(jobs))); | ||
} | ||
for (int i = 0; i < FLAGS_num_threads; ++i) { | ||
threads[i]->join(); | ||
} | ||
stop_ms = GetCurrentMs(); | ||
} else { | ||
if (FLAGS_prepare_vars) { | ||
executor.CreateVariables(*inference_program, scope.get(), 0); | ||
// 1. Define place, executor, scope | ||
auto place = paddle::platform::CPUPlace(); | ||
auto executor = paddle::framework::Executor(place); | ||
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. same as above |
||
|
||
// 2. Initialize the inference_program and load parameters | ||
std::unique_ptr<paddle::framework::ProgramDesc> inference_program; | ||
inference_program = InitProgram(&executor, scope.get(), FLAGS_model_path, | ||
/*model combined*/ false); | ||
if (FLAGS_use_mkldnn) { | ||
EnableMKLDNN(inference_program); | ||
} | ||
// always prepare context | ||
std::unique_ptr<paddle::framework::ExecutorPrepareContext> ctx; | ||
ctx = executor.Prepare(*inference_program, 0); | ||
|
||
if (FLAGS_prepare_vars) { | ||
executor.CreateVariables(*inference_program, scope.get(), 0); | ||
} | ||
// preapre fetch | ||
const std::vector<std::string>& fetch_target_names = | ||
inference_program->GetFetchTargetNames(); | ||
|
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.
paddle::platform::CPUPlace place;
paddle::framework::Executor executor(place);