Skip to content
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

Closed
wants to merge 13 commits into from

Conversation

bpickrel
Copy link
Contributor

@bpickrel bpickrel commented Sep 20, 2023

… test.

@bpickrel bpickrel requested a review from pfultz2 September 20, 2023 23:55
@migraphx-bot
Copy link
Collaborator

migraphx-bot commented Sep 21, 2023

Test Batch Rate new
d9ad08
Rate old
fff12a
Diff Compare
torchvision-resnet50 64 2,835.60 nan nan%
torchvision-resnet50_fp16 64 6,502.33 6,505.90 -0.05%
torchvision-densenet121 32 2,096.61 2,080.03 0.80%
torchvision-densenet121_fp16 32 3,664.03 3,662.79 0.03%
torchvision-inceptionv3 32 1,597.74 1,597.57 0.01%
torchvision-inceptionv3_fp16 32 2,556.32 2,562.85 -0.25%
cadene-inceptionv4 16 722.35 722.13 0.03%
cadene-resnext64x4 16 689.74 692.38 -0.38%
slim-mobilenet 64 8,346.46 8,344.82 0.02%
slim-nasnetalarge 64 230.67 230.69 -0.01%
slim-resnet50v2 64 2,663.35 2,665.66 -0.09%
bert-mrpc-onnx 8 812.09 813.21 -0.14%
bert-mrpc-tf 1 388.13 387.28 0.22%
pytorch-examples-wlang-gru 1 302.32 302.68 -0.12%
pytorch-examples-wlang-lstm 1 314.52 318.19 -1.15%
torchvision-resnet50_1 1 607.37 605.99 0.23%
torchvision-inceptionv3_1 1 345.36 341.75 1.06%
cadene-dpn92_1 1 401.36 405.34 -0.98%
cadene-resnext101_1 1 324.08 328.21 -1.26%
slim-vgg16_1 1 459.79 nan nan%
slim-mobilenet_1 1 2,128.16 2,112.08 0.76%
slim-inceptionv4_1 1 214.31 213.80 0.24%
onnx-taau-downsample 1 305.60 305.91 -0.10%
dlrm-criteoterabyte 1 21.62 21.61 0.02%
dlrm-criteoterabyte_fp16 1 40.65 40.66 -0.02%
agentmodel 1 5,784.19 5,850.96 -1.14%
unet_fp16 2 54.69 54.74 -0.08%
resnet50v1_fp16 1 948.93 944.08 0.51%
bert_base_cased_fp16 64 924.70 924.89 -0.02%
bert_large_uncased_fp16 32 290.71 290.67 0.01%
bert_large_fp16 1 171.80 171.71 0.05%
distilgpt2_fp16 16 1,282.47 1,281.45 0.08%

This build is not recommended to merge 🔴

@migraphx-bot
Copy link
Collaborator

migraphx-bot commented Sep 21, 2023


     ✅ bert-mrpc-onnx: PASSED: MIGraphX meets tolerance

     ✅ bert-mrpc-tf: PASSED: MIGraphX meets tolerance

     ✅ pytorch-examples-wlang-gru: PASSED: MIGraphX meets tolerance

     ✅ pytorch-examples-wlang-lstm: PASSED: MIGraphX meets tolerance

     ✅ torchvision-resnet50_1: PASSED: MIGraphX meets tolerance

     ✅ torchvision-inceptionv3_1: PASSED: MIGraphX meets tolerance

     ✅ cadene-dpn92_1: PASSED: MIGraphX meets tolerance

     ✅ cadene-resnext101_1: PASSED: MIGraphX meets tolerance

     ✅ slim-vgg16_1: PASSED: MIGraphX meets tolerance

     ✅ slim-mobilenet_1: PASSED: MIGraphX meets tolerance

     ✅ slim-inceptionv4_1: PASSED: MIGraphX meets tolerance

     ✅ dlrm-criteoterabyte: PASSED: MIGraphX meets tolerance

     ✅ agentmodel: PASSED: MIGraphX meets tolerance

     ✅ unet: PASSED: MIGraphX meets tolerance

     ✅ resnet50v1: PASSED: MIGraphX meets tolerance

     ✅ bert_base_cased_fp16: PASSED: MIGraphX meets tolerance

     ✅ bert_large_uncased_fp16: PASSED: MIGraphX meets tolerance

     ✅ bert_large: PASSED: MIGraphX meets tolerance

🔴distilgpt2_fp16: FAILED: MIGraphX is not within tolerance - check verbose output

@bpickrel bpickrel marked this pull request as ready for review September 30, 2023 00:35
@codecov
Copy link

codecov bot commented Sep 30, 2023

Codecov Report

Merging #2224 (0bc7d59) into develop (4188c38) will decrease coverage by 0.04%.
Report is 22 commits behind head on develop.
The diff coverage is 85.71%.

❗ Current head 0bc7d59 differs from pull request most recent head 6befe93. Consider uploading reports for the commit 6befe93 to get more accurate results

@@             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     
Files Coverage Δ
src/compile_src.cpp 85.00% <100.00%> (ø)
src/cpp_generator.cpp 70.83% <100.00%> (ø)
src/include/migraphx/compile_src.hpp 100.00% <100.00%> (ø)
src/include/migraphx/op/allocate.hpp 100.00% <ø> (ø)
src/include/migraphx/op/convert.hpp 100.00% <100.00%> (ø)
src/include/migraphx/op/isnan.hpp 100.00% <100.00%> (ø)
src/onnx/parse_castlike.cpp 100.00% <100.00%> (ø)
src/onnx/parse_constant_of_shape.cpp 92.85% <100.00%> (+1.19%) ⬆️
src/onnx/parse_depthtospace.cpp 90.90% <100.00%> (-0.27%) ⬇️
src/onnx/parse_reshape.cpp 100.00% <100.00%> (ø)
... and 9 more

@bpickrel bpickrel linked an issue Oct 2, 2023 that may be closed by this pull request
void register_target(const target& t)
{
std::unique_lock<std::mutex> lock(target_mutex());
target_map()[t.name()] = t;
Copy link
Collaborator

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.

Copy link
Contributor Author

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);

Copy link
Collaborator

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

Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

@bpickrel bpickrel Oct 3, 2023

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
Copy link
Member

@umangyadav umangyadav Oct 3, 2023

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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");
Copy link
Member

@umangyadav umangyadav Oct 3, 2023

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
test/targets.cpp Outdated Show resolved Hide resolved
test/targets.cpp Outdated

// remove all existing targets, if any
std::vector<std::string> target_list = migraphx::get_targets();
for(auto tt : target_list)
Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@bpickrel bpickrel marked this pull request as draft October 5, 2023 20:55
for(auto i = 0u; i < n_threads; i++)
{
auto thread_body = [&target_name]() {
// TODO: remove all existing targets, if any.
Copy link
Contributor Author

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.

@bpickrel bpickrel marked this pull request as ready for review October 10, 2023 21:20
@bpickrel
Copy link
Contributor Author

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-commenter
Copy link

codecov-commenter commented Dec 29, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (fff12a2) 91.38% compared to head (d7657d7) 91.39%.
Report is 2 commits behind head on develop.

❗ Current head d7657d7 differs from pull request most recent head d9ad082. Consider uploading reports for the commit d9ad082 to get more accurate results

❗ 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.
📢 Have feedback on the report? Share it here.

@causten causten closed this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fixes a bug found in the code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make register_target methods thread-safe
8 participants