This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Fix a bug in getting MKLDNN memory #10731
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
081de9e
test inference multiple times.
zheng-da 42d64b4
Fix a bug in GetMKLDNNData().
zheng-da 3ff7475
Update comments.
zheng-da b746362
Handle all cases for GetMKLDNNDataReorder
zheng-da 60e9c6d
avoid unnecessary message.
zheng-da c1c68a1
Add C++ unit test for NDArray.
zheng-da 244149b
Fix a minor bug.
zheng-da 8de717e
Unit tests on GetMKLDNNDataReorder.
zheng-da 735f1d3
Fix lint error.
zheng-da 03a742f
Add more test cases.
zheng-da a21af4e
add comments for the test code.
zheng-da cd1a074
Reorganize test code.
zheng-da 1987430
Fix cpp tests.
zheng-da ff8bd94
test.
zheng-da 8835d77
Add a new Jenkins compile task.
zheng-da 150d38a
Update jenkins.
zheng-da afdae0a
update jenkins.
zheng-da cfc38e6
Fix a Jenkins.
zheng-da beb7c27
Fix jenkins.
zheng-da 859005c
Fix jenkins.
zheng-da 65917bc
Fix CMake for MKLDNN.
zheng-da 45683e2
Fix jenkins.
zheng-da 69b06d3
update jenkins.
zheng-da b9b360a
update CMake.
zheng-da a737d4d
Fix cmake.
zheng-da e8b604a
update CI.
zheng-da faafd08
add comment.
zheng-da 4d441e6
add comments.
zheng-da 01b37ce
cmake builds mkldnn with -mtune=generic by default.
zheng-da 1c5e15e
adjust comments.
zheng-da 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
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 |
---|---|---|
|
@@ -485,8 +485,8 @@ const mkldnn::memory *NDArray::GetMKLDNNData( | |
} | ||
|
||
const mkldnn::memory *NDArray::GetMKLDNNDataReorder( | ||
const mkldnn::memory::primitive_desc &desc) const { | ||
if (desc.get_size() != shape().Size() * GetTypeSize(dtype_)) { | ||
const mkldnn::memory::primitive_desc &new_pd) const { | ||
if (new_pd.get_size() != shape().Size() * GetTypeSize(dtype_)) { | ||
LOG(FATAL) << "The size of NDArray doesn't match the requested MKLDNN memory desc"; | ||
return nullptr; | ||
} | ||
|
@@ -495,24 +495,41 @@ const mkldnn::memory *NDArray::GetMKLDNNDataReorder( | |
const mkldnn::memory *mem = GetMKLDNNData(); | ||
// If the memory descriptor matches, it's easy. | ||
MKLDNNStream *stream = MKLDNNStream::Get(); | ||
if (mem->get_primitive_desc() == desc) { | ||
return GetMKLDNNExact(mem, desc); | ||
if (mem->get_primitive_desc() == new_pd) { | ||
return GetMKLDNNExact(mem, new_pd); | ||
} | ||
|
||
mkldnn::memory::primitive_desc _desc = desc; | ||
mkldnn::memory::primitive_desc _pd = new_pd; | ||
mkldnn::memory::desc desc1 = mem->get_primitive_desc().desc(); | ||
mkldnn::memory::desc desc2 = _pd.desc(); | ||
// Now we need to determine if we should reorder the memory. | ||
// If both use the default formats, we think we don't need to reorder. | ||
mkldnn::memory::desc desc1 = mem->get_primitive_desc().desc(); | ||
mkldnn::memory::desc desc2 = _desc.desc(); | ||
if (desc1.data.format == GetDefaultFormat(desc1) && | ||
desc2.data.format == GetDefaultFormat(desc2)) { | ||
mkldnn_mem_ptr ret(new mkldnn::memory(desc, mem->get_data_handle())); | ||
mkldnn_mem_ptr ret(new mkldnn::memory(new_pd, mem->get_data_handle())); | ||
stream->RegisterMem(ret); | ||
return ret.get(); | ||
} else { | ||
mkldnn::memory *ret = TmpMemMgr::Get()->Alloc(desc); | ||
} else if (same_shape(desc1, desc2)) { | ||
// If they have the same shape, we can reorder data directly. | ||
mkldnn::memory *ret = TmpMemMgr::Get()->Alloc(new_pd); | ||
stream->RegisterPrim(mkldnn::reorder(*mem, *ret)); | ||
return ret; | ||
} else { | ||
// If they have different shapes, we need to reshape the array first. | ||
// Since this method will only be used inside an operator, we can call | ||
// MKLDNNDataReshape to reshape an array. | ||
TShape required_shape(desc2.data.ndims); | ||
for (int i = 0; i < desc2.data.ndims; i++) | ||
required_shape[i] = desc2.data.dims[i]; | ||
NDArray reshaped = MKLDNNDataReshape(required_shape); | ||
const mkldnn::memory *ret = reshaped.GetMKLDNNData(); | ||
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. Where is ret being destroyed? 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. The memory is managed by MKLDNNStream. You can take a look at the implementation of GetMKLDNNData |
||
if (ret->get_primitive_desc() == new_pd) { | ||
return GetMKLDNNExact(ret, new_pd); | ||
} else { | ||
mkldnn::memory *ret2 = TmpMemMgr::Get()->Alloc(new_pd); | ||
stream->RegisterPrim(mkldnn::reorder(*ret, *ret2)); | ||
return ret2; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -566,10 +583,15 @@ void NDArray::MKLDNNDataReorderAsync(const mkldnn::memory::primitive_desc &desc) | |
|
||
const mkldnn::memory *NDArray::GetMKLDNNData() const { | ||
CHECK(storage_type() == kDefaultStorage); | ||
// If this array uses MKLDNN layout, we have to make sure it's not a view. | ||
// Otherwise, we'll have to change the layout inside the array. | ||
if (IsMKLDNNData()) | ||
if (IsMKLDNNData()) { | ||
// If this array uses MKLDNN layout, we have to make sure it's not a view. | ||
// Otherwise, we'll have to change the layout inside the array. | ||
CHECK(!IsView()); | ||
MKLDNNStream::Get()->RegisterMem(ptr_->mkl_mem_->GetMem()); | ||
// If this array uses MKLDNN format, we should return now. Otherwise, | ||
// SetMKLMem may mess up mkl_mem_. | ||
return ptr_->mkl_mem_->GetRaw(); | ||
} | ||
ptr_->SetMKLMem(IsView() ? ptr_->storage_shape : shape_, dtype_); | ||
MKLDNNStream::Get()->RegisterMem(ptr_->mkl_mem_->GetMem()); | ||
if (IsView()) { | ||
|
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
Oops, something went wrong.
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 is a no-op. Please elaborate
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 isn't no-op. I think my comment explain this. libmkldnn.so.0 is a link file. We need an actual binary file named libmkldnn.so.0. Jenkins can't pack a link file.
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 see, so you're making use of the fact that cp automatically resolves the symlink? I think a cleaner way would be to resolve the symlink explicitely and then override the symlink file with the original file rather than relying on implicit actions.
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.
If you know a cleaner way, I'm happy to do so.
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.
https://stackoverflow.com/questions/7665/how-to-resolve-symbolic-links-in-a-shell-script?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
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.
What we need here is to turn a link file to a regular file, instead of finding the path to the regular file. It's a different thing. Jenkins can't wrapper a link file and mxnet wants a library file named libmkldnn.so.0, instead of libmkldnn.so.0.13 (which is what libmkldnn.so.0 points to). If what you find can turn libmkldnn.so.0 (a link file) to a regular file with the same name with a single command, can you please provide the command?
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.
https://stackoverflow.com/a/13396140/3062895
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.
so the code will be something like this.
do you think this is a preferred way or less confusing way?
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.
To be honest, I actually prefer the existing solution over the one-liner.
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.
Alright, no strong feelings from my side