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
Integrate MKLDNN Conv1d and support 3d layout #13530
Merged
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
06901b8
add 3d layout support for MKLDNN Conv and Activation
xinyu-intel 49296fc
fix lint
xinyu-intel cc6ebf3
resolve conflict
xinyu-intel 2da1e2a
code refactor
xinyu-intel 8f70c99
Merge remote-tracking branch 'upstream/master' into conv1d
xinyu-intel 869ad9d
add testcase for group1 conv and skip quantization for conv1d
xinyu-intel 5e1a5f3
Merge remote-tracking branch 'upstream/master' into conv1d
xinyu-intel a3f843b
fix lint
xinyu-intel 25a0e4f
avoid conv1d quantization
xinyu-intel e20807f
Merge remote-tracking branch 'upstream/master' into conv1d
xinyu-intel b627592
Merge remote-tracking branch 'upstream/master' into conv1d
xinyu-intel 4d51c8f
code refactor and add activation ut
xinyu-intel 95bef09
del todo
xinyu-intel a13d87a
Merge remote-tracking branch 'upstream/master' into conv1d
xinyu-intel 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 |
---|---|---|
|
@@ -175,10 +175,11 @@ struct ConvolutionParam; | |
struct DeconvolutionParam; | ||
struct SoftmaxParam; | ||
bool SupportMKLDNNAct(const ActivationParam& param); | ||
bool SupportMKLDNNAct(const ActivationParam& param, const NDArray &input); | ||
bool SupportMKLDNNConv(const ConvolutionParam& params, const NDArray &input); | ||
bool SupportMKLDNNDeconv(const DeconvolutionParam& params, const NDArray &input); | ||
bool SupportMKLDNNSoftmax(const SoftmaxParam& param); | ||
} | ||
} // namespace op | ||
|
||
static int GetTypeSize(int dtype) { | ||
int size = -1; | ||
|
@@ -253,14 +254,23 @@ inline static mkldnn::memory::desc GetWeightDesc(const NDArray &arr, | |
if (num_groups == 1) { | ||
return GetMemDesc(arr); | ||
} else { | ||
CHECK_EQ(arr.shape().ndim(), 4U); | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{ num_groups, | ||
static_cast<int>(arr.shape()[0] / num_groups), | ||
static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2]), | ||
static_cast<int>(arr.shape()[3])}; | ||
return mkldnn::memory::desc{tz, get_mkldnn_type(arr.dtype()), | ||
mkldnn::memory::format::any}; | ||
CHECK((arr.shape().ndim() == 3) || (arr.shape().ndim() == 4)) | ||
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. Use a variable to save the value of |
||
<< "MKL-DNN weight currectly supports 3d and 4d layout"; | ||
const int N = 0, H = 2, W = 3, C = 1; | ||
if (arr.shape().ndim() == 3) { | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{ | ||
num_groups, static_cast<int>(arr.shape()[N] / num_groups), | ||
static_cast<int>(arr.shape()[C]), static_cast<int>(arr.shape()[H])}; | ||
return mkldnn::memory::desc{tz, get_mkldnn_type(arr.dtype()), | ||
mkldnn::memory::format::any}; | ||
} else { | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{ | ||
num_groups, static_cast<int>(arr.shape()[N] / num_groups), | ||
static_cast<int>(arr.shape()[C]), static_cast<int>(arr.shape()[H]), | ||
static_cast<int>(arr.shape()[W])}; | ||
return mkldnn::memory::desc{tz, get_mkldnn_type(arr.dtype()), | ||
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. This line is the common part of dim3 and dim4, right? |
||
mkldnn::memory::format::any}; | ||
} | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -238,39 +238,48 @@ const mkldnn::memory *GetWeights(const NDArray &arr, | |
return mem; | ||
|
||
mkldnn::memory::data_type type = get_mkldnn_type(arr.dtype()); | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{0}; | ||
mkldnn::memory::format format = mkldnn::memory::format::format_undef; | ||
auto engine = CpuEngine::Get()->get_engine(); | ||
if (arr.shape().ndim() == 2) { | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{ | ||
static_cast<int>(arr.shape()[0]), static_cast<int>(arr.shape()[1])}; | ||
mkldnn::memory::desc md = | ||
mkldnn::memory::desc{tz, type, mkldnn::memory::format::oi}; | ||
mkldnn::memory::primitive_desc pd = | ||
mkldnn::memory::primitive_desc{md, engine}; | ||
mem = arr.GetMKLDNNData(pd); | ||
} else if (arr.shape().ndim() == 4 && num_groups == 1) { | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{ | ||
static_cast<int>(arr.shape()[0]), static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2]), static_cast<int>(arr.shape()[3])}; | ||
mkldnn::memory::desc md = | ||
mkldnn::memory::desc{tz, type, mkldnn::memory::format::oihw}; | ||
mkldnn::memory::primitive_desc pd = | ||
mkldnn::memory::primitive_desc{md, engine}; | ||
mem = arr.GetMKLDNNData(pd); | ||
tz = mkldnn::memory::dims{static_cast<int>(arr.shape()[0]), | ||
static_cast<int>(arr.shape()[1])}; | ||
format = mkldnn::memory::format::oi; | ||
} else if (arr.shape().ndim() == 3) { | ||
tz = num_groups > 1 | ||
? mkldnn::memory::dims{num_groups, | ||
static_cast<int>(arr.shape()[0] / | ||
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. Use O, I, H, W instead of 0, 1, 2 ... |
||
num_groups), | ||
static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2])} | ||
: mkldnn::memory::dims{static_cast<int>(arr.shape()[0]), | ||
static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2])}; | ||
format = num_groups > 1 ? mkldnn::memory::format::goiw | ||
: mkldnn::memory::format::oiw; | ||
} else if (arr.shape().ndim() == 4) { | ||
mkldnn::memory::dims tz = mkldnn::memory::dims{ num_groups, | ||
static_cast<int>(arr.shape()[0] / num_groups), | ||
static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2]), | ||
static_cast<int>(arr.shape()[3])}; | ||
mkldnn::memory::desc md = | ||
mkldnn::memory::desc{tz, type, mkldnn::memory::format::goihw}; | ||
mkldnn::memory::primitive_desc pd = | ||
mkldnn::memory::primitive_desc{md, engine}; | ||
mem = arr.GetMKLDNNData(pd); | ||
tz = num_groups > 1 | ||
? mkldnn::memory::dims{num_groups, | ||
static_cast<int>(arr.shape()[0] / | ||
num_groups), | ||
static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2]), | ||
static_cast<int>(arr.shape()[3])} | ||
: mkldnn::memory::dims{static_cast<int>(arr.shape()[0]), | ||
static_cast<int>(arr.shape()[1]), | ||
static_cast<int>(arr.shape()[2]), | ||
static_cast<int>(arr.shape()[3])}; | ||
format = num_groups > 1 ? mkldnn::memory::format::goihw | ||
: mkldnn::memory::format::oihw; | ||
} else { | ||
LOG(FATAL) << "The weight array has an unsupported number of dimensions"; | ||
return nullptr; | ||
} | ||
mkldnn::memory::desc md = | ||
mkldnn::memory::desc{tz, type, format}; | ||
mkldnn::memory::primitive_desc pd = | ||
mkldnn::memory::primitive_desc{md, engine}; | ||
mem = arr.GetMKLDNNData(pd); | ||
if (mem == nullptr) | ||
mem = arr.GetMKLDNNDataReorder(target_pd); | ||
if (mem->get_primitive_desc() == target_pd) return mem; | ||
|
@@ -284,6 +293,7 @@ mkldnn_memory_format_t GetDefaultFormat(int num_dims) { | |
switch (num_dims) { | ||
case 1: return mkldnn_x; | ||
case 2: return mkldnn_nc; | ||
case 3: return mkldnn_ncw; | ||
case 4: return mkldnn_nchw; | ||
case 5: return mkldnn_goihw; | ||
default: | ||
|
@@ -300,6 +310,30 @@ mkldnn_memory_format_t GetDefaultFormat(const mkldnn::memory::desc &desc) { | |
return mkldnn_oi; | ||
else | ||
return desc.data.format; | ||
} else if (desc.data.ndims == 3) { | ||
switch (desc.data.format) { | ||
case mkldnn_ncw: | ||
case mkldnn_nwc: | ||
case mkldnn_nCw8c: | ||
case mkldnn_nCw16c: | ||
return mkldnn_ncw; | ||
case mkldnn_oiw: | ||
case mkldnn_wio: | ||
case mkldnn_Owi8o: | ||
case mkldnn_OIw8i8o: | ||
case mkldnn_OIw8o8i: | ||
case mkldnn_OIw16i16o: | ||
case mkldnn_OIw16o16i: | ||
case mkldnn_Oiw16o: | ||
case mkldnn_Owi16o: | ||
case mkldnn_OIw8i16o2i: | ||
case mkldnn_OIw8o16i2o: | ||
case mkldnn_IOw16o16i: | ||
return mkldnn_oiw; | ||
default: | ||
LOG(FATAL) << "Unknown MKLDNN format for 3 dimensions: " << desc.data.format; | ||
return mkldnn_format_undef; | ||
} | ||
} else if (desc.data.ndims == 4) { | ||
switch (desc.data.format) { | ||
case mkldnn_nchw: | ||
|
@@ -328,6 +362,18 @@ mkldnn_memory_format_t GetDefaultFormat(const mkldnn::memory::desc &desc) { | |
case mkldnn_Ohwi16o: | ||
case mkldnn_OhIw16o4i: | ||
return mkldnn_oihw; | ||
case mkldnn_goiw: | ||
case mkldnn_gOwi8o: | ||
case mkldnn_gOIw8o8i: | ||
case mkldnn_gOIw8i8o: | ||
case mkldnn_gOIw16i16o: | ||
case mkldnn_gOIw16o16i: | ||
case mkldnn_gOiw16o: | ||
case mkldnn_gOwi16o: | ||
case mkldnn_gOIw8i16o2i: | ||
case mkldnn_gOIw8o16i2o: | ||
case mkldnn_gIOw16o16i: | ||
return mkldnn_goiw; | ||
default: | ||
LOG(FATAL) << "Unknown MKLDNN format for 4 dimensions: " << desc.data.format; | ||
return mkldnn_format_undef; | ||
|
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.
Is there a performance difference between 3D and 4D implementation?