-
Notifications
You must be signed in to change notification settings - Fork 88
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
Added mutex locks in register_target.cpp and created a multithreading… #2224
Conversation
This build is not recommended to merge 🔴 |
🔴distilgpt2_fp16: FAILED: MIGraphX is not within tolerance - check verbose output |
Codecov Report
@@ Coverage Diff @@
## develop #2224 +/- ##
===========================================
- Coverage 91.49% 91.46% -0.04%
===========================================
Files 430 433 +3
Lines 16129 16184 +55
===========================================
+ Hits 14758 14803 +45
- Misses 1371 1381 +10
|
void register_target(const target& t) | ||
{ | ||
std::unique_lock<std::mutex> lock(target_mutex()); | ||
target_map()[t.name()] = t; |
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.
You should be unlocking here.
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.
With std::unique_lock, the destructor automatically unlocks it when it goes out of scope. This is supposed not only to be a coding convenience, but gives protection when things are interrupted by an exception. From cplusplus.com:
"This class guarantees an unlocked status on destruction (even if not called explicitly). Therefore it is especially useful as an object with automatic duration, as it guarantees the mutex object is properly unlocked in case an exception is thrown."
// search for match or return none | ||
std::unique_lock<std::mutex> lock(target_mutex()); | ||
const auto it = target_map().find(name); | ||
|
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.
Add unlock before each return
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.
Don't need to--see above
target make_target(const std::string& name) | ||
{ | ||
if(not contains(target_map(), name)) | ||
// no lock required here |
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.
Are we not concerned with something trying to reregister a target in another thread? If between find_target, we end up doing an additional find()
Is the intent to not expose find_target() outside of register_target? If so, why not just add the locking around here.
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.
The idea is that find_target does contain a lock, so make_target is already protected. The second thread would have to wait for the first thread to finish before trying to register it, then see that it's already there.
MIGRAPHX_THROW("Requested target '" + name + "' is not loaded or not supported"); | ||
} | ||
return it->second; | ||
// at this point we should always have a target |
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.
Why is it guaranteed to have a target at this point?
Let's say Target requested is not supported by MIGraphX e.g. some target named "TPU". TPU is not registered with MIGraphX and it will not be found or can not be loaded dynamically either.
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.
Agreed, we should check if (t == nullopt)
a second time and throw.
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.
Why is it guaranteed to have a target at this point? Let's say Target requested is not supported by MIGraphX e.g. some target named "TPU". TPU is not registered with MIGraphX and it will not be found or can not be loaded dynamically either.
Because the dynamic_loader
constructor will throw an exception. I added a line in the test for this.
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.
Agreed, we should check
if (t == nullopt)
a second time and throw.
That's redundant since find_target
contains that check.
test/targets.cpp
Outdated
for(auto i = 0u; i < 1000; i++) | ||
{ | ||
auto thread_body = []() { | ||
auto ref_target = migraphx::make_target("ref"); |
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.
target needs to be registered first before it can be made (or alternatively targets can by loaded dynamically). I think it works right now because it was already registered by other tests.
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.
Yes this should try to do make_target("gpu")
when gpu is enabled, and do make_target("cpu")
when cpu is enabled, and then call unregister_target
at the end.
There should also be threads calling get_targets
as well.
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.
target needs to be registered first before it can be made (or alternatively targets can by loaded dynamically). I think it works right now because it was already registered by other tests.
It is registered by auto_register.hpp during program initialization, before the test starts. I wouldn't have another way to make a target instance without calling make_target()
first.
Recall that the goal of this test is not to make targets correctly; it's to check thread safety.
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.
and then call
unregister_target
at the end.
That's not allowed. unregister_target
is called automatically on exit, and it throws an exception if it's called twice with the same target name. As it is, it's still possible in principle for one holder of a target to unregister it while others are still holding it.
Is there any use at all in having unregister_target
? It can only be used at program cleanup.
test/targets.cpp
Outdated
|
||
// remove all existing targets, if any | ||
std::vector<std::string> target_list = migraphx::get_targets(); | ||
for(auto tt : target_list) |
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 new loop breaks the test now. Multiple threads registering and unregistering a target with the same name is not safe. Apparently it's not the target_map that's a problem but the underlying handles. Trying to figure out how to protect against this, or if it's necessary.
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.
Followup: I disabled this extra test but when I tested the existing fixes with amdinfer, the Inference Server, the segmentation violation still occurs. I'm going to recommend we commit this PR as is but continue to search for the root cause of Issue #2208.
…. This code cannot pass the test.
for(auto i = 0u; i < n_threads; i++) | ||
{ | ||
auto thread_body = [&target_name]() { | ||
// TODO: remove all existing targets, if any. |
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.
Followup: I disabled this extra test but when I tested the existing fixes with amdinfer, the Inference Server, the segmentation violation still occurs. I'm going to recommend we commit this PR as is but continue to search for the root cause of Issue #2208.
I'm recommending that we approve this PR so we can get it into the next ROCm release, even though the underlying problem in the Inference Server still occurs. I can continue to look for the root cause. If we can agree that this code is correct as far as it goes then let's merge it. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## develop #2224 +/- ##
===========================================
+ Coverage 91.38% 91.39% +0.01%
===========================================
Files 456 456
Lines 17241 17252 +11
===========================================
+ Hits 15756 15768 +12
+ Misses 1485 1484 -1 ☔ View full report in Codecov by Sentry. |
… test.