From 50804464d14f27802893813f714f94b874788739 Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Mon, 6 Aug 2018 18:17:16 -0700 Subject: [PATCH 1/9] fallback when env set --- src/operator/nn/activation.cc | 8 ++++++++ src/operator/nn/batch_norm.cc | 3 +++ src/operator/nn/concat.cc | 8 ++++++++ src/operator/nn/convolution.cc | 8 ++++++-- src/operator/nn/deconvolution.cc | 8 ++++++-- src/operator/nn/fully_connected.cc | 9 +++++++++ src/operator/nn/lrn.cc | 19 +++++++++++-------- src/operator/nn/mkldnn/mkldnn_base-inl.h | 4 ++++ src/operator/nn/pooling.cc | 10 ++++++++-- src/operator/nn/softmax.cc | 4 +++- .../tensor/elemwise_binary_op_basic.cc | 8 ++++++-- 11 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/operator/nn/activation.cc b/src/operator/nn/activation.cc index 3404b5b700af..8f24db05aebd 100644 --- a/src/operator/nn/activation.cc +++ b/src/operator/nn/activation.cc @@ -116,6 +116,10 @@ inline static bool ActivationStorageType(const nnvm::NodeAttrs& attrs, if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNAct(param)) { *dispatch_mode = DispatchMode::kFComputeEx; } + if (!MKLDNNEnvSet()) { + *dispatch_mode = DispatchMode::kFComputeFallback; + return ret; + } #endif return ret; } @@ -158,6 +162,10 @@ inline static bool BackwardActStorageType(const nnvm::NodeAttrs& attrs, if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNAct(param)) { *dispatch_mode = DispatchMode::kFComputeEx; } + if (!MKLDNNEnvSet()) { + *dispatch_mode = DispatchMode::kFComputeFallback; + return ret; + } #endif return ret; } diff --git a/src/operator/nn/batch_norm.cc b/src/operator/nn/batch_norm.cc index 30fb665dd05a..c7b1b609990e 100644 --- a/src/operator/nn/batch_norm.cc +++ b/src/operator/nn/batch_norm.cc @@ -460,6 +460,9 @@ static inline bool BatchNormStorageType(const nnvm::NodeAttrs &attrs, dispatched = MKLDNNStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs, out_attrs); } + if (!MKLDNNEnvSet()) { + *dispatch_mode = DispatchMode::kFComputeFallback; + } #else for (int& v : *in_attrs) if (v == - 1) v = kDefaultStorage; diff --git a/src/operator/nn/concat.cc b/src/operator/nn/concat.cc index 7c7f403d6985..9df459e9224d 100644 --- a/src/operator/nn/concat.cc +++ b/src/operator/nn/concat.cc @@ -194,6 +194,10 @@ inline static bool ConcatForwardInferStorageType(const nnvm::NodeAttrs& attrs, if (!dispatched) { dispatched = dispatch_fallback(out_attrs, dispatch_mode); } +#if MXNET_USE_MKLDNN == 1 + if (!MKLDNNEnvSet()) + *dispatch_mode = DispatchMode::kFComputeFallback; +#endif return dispatched; } @@ -213,6 +217,10 @@ inline static bool BackwardConcatStorageType(const nnvm::NodeAttrs& attrs, else #endif wanted_mode = DispatchMode::kFCompute; +#if MXNET_USE_MKLDNN == 1 + if (!MKLDNNEnvSet()) + wanted_mode = DispatchMode::kFComputeFallback; +#endif return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, wanted_mode); } diff --git a/src/operator/nn/convolution.cc b/src/operator/nn/convolution.cc index ef70ccd6ec1e..9656e04ea215 100644 --- a/src/operator/nn/convolution.cc +++ b/src/operator/nn/convolution.cc @@ -300,7 +300,9 @@ inline static bool ConvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) + if (!MKLDNNEnvSet()) + wanted_mode = DispatchMode::kFComputeFallback; + else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; else #endif @@ -322,7 +324,9 @@ inline static bool BackwardConvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) + if (!MKLDNNEnvSet()) + wanted_mode = DispatchMode::kFComputeFallback; + else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; else #endif diff --git a/src/operator/nn/deconvolution.cc b/src/operator/nn/deconvolution.cc index 9e0a70121bf9..4b4c422cec62 100644 --- a/src/operator/nn/deconvolution.cc +++ b/src/operator/nn/deconvolution.cc @@ -268,7 +268,9 @@ inline static bool DeconvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) + if (!MKLDNNEnvSet()) + wanted_mode = DispatchMode::kFComputeFallback; + else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; else #endif @@ -289,7 +291,9 @@ inline static bool BackwardDeconvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) + if (!MKLDNNEnvSet()) + wanted_mode = DispatchMode::kFComputeFallback; + else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; else #endif diff --git a/src/operator/nn/fully_connected.cc b/src/operator/nn/fully_connected.cc index d1d84e975290..09a32fcb50ef 100644 --- a/src/operator/nn/fully_connected.cc +++ b/src/operator/nn/fully_connected.cc @@ -193,6 +193,11 @@ inline static bool FCStorageType(const nnvm::NodeAttrs& attrs, dispatched = storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeEx); } +#if MXNET_USE_MKLDNN == 1 + if (!MKLDNNEnvSet()) + *dispatch_mode = DispatchMode::kFComputeFallback; +#endif + if (!dispatched) { dispatched = dispatch_fallback(out_attrs, dispatch_mode); } @@ -223,6 +228,10 @@ inline static bool BackwardFCStorageType(const nnvm::NodeAttrs& attrs, dispatched = storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFCompute); } +#if MXNET_USE_MKLDNN == 1 + if (!MKLDNNEnvSet()) + *dispatch_mode = DispatchMode::kFComputeFallback; +#endif return dispatched; } diff --git a/src/operator/nn/lrn.cc b/src/operator/nn/lrn.cc index 6b3d7c818378..ce2cd075b4fd 100644 --- a/src/operator/nn/lrn.cc +++ b/src/operator/nn/lrn.cc @@ -88,10 +88,12 @@ bool LRNForwardInferStorageType(const nnvm::NodeAttrs& attrs, std::vector *out_attrs) { CHECK(!in_attrs->empty()); #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) { - storage_type_assign(out_attrs, mxnet::kDefaultStorage, + if (!MKLDNNEnvSet()) { + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, + dispatch_mode, DispatchMode::kFComputeFallback); + } else if (dev_mask == mshadow::cpu::kDevMask) { + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeEx); - return true; } #endif storage_type_assign(out_attrs, mxnet::kDefaultStorage, @@ -106,15 +108,16 @@ bool LRNBackwardInferStorageType(const nnvm::NodeAttrs& attrs, std::vector *out_attrs) { CHECK(!in_attrs->empty()); #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) { - storage_type_assign(out_attrs, mxnet::kDefaultStorage, + if (!MKLDNNEnvSet()) { + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, + dispatch_mode, DispatchMode::kFComputeFallback); + } else if (dev_mask == mshadow::cpu::kDevMask) { + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeEx); - return true; } #endif - storage_type_assign(out_attrs, mxnet::kDefaultStorage, + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFCompute); - return true; } #if MXNET_USE_MKLDNN == 1 diff --git a/src/operator/nn/mkldnn/mkldnn_base-inl.h b/src/operator/nn/mkldnn/mkldnn_base-inl.h index bbfb873ee86a..c1dabb134cf9 100644 --- a/src/operator/nn/mkldnn/mkldnn_base-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_base-inl.h @@ -137,6 +137,10 @@ static inline bool SupportMKLDNN(const NDArray &input) { && SupportStorageMKLDNN(input.storage_type()); } +static inline bool MKLDNNEnvSet() { + return dmlc::GetEnv("USE_MKLDNN", "ON") == "ON"; +} + /* * This is to align address to a certain alignment. */ diff --git a/src/operator/nn/pooling.cc b/src/operator/nn/pooling.cc index 9b6996d0feb0..4549eb2befd9 100644 --- a/src/operator/nn/pooling.cc +++ b/src/operator/nn/pooling.cc @@ -295,7 +295,10 @@ inline static bool PoolingStorageType(const nnvm::NodeAttrs &attrs, #if MXNET_USE_MKLDNN == 1 const PoolingParam ¶m = nnvm::get(attrs.parsed); - if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNPooling(param)) { + if (!MKLDNNEnvSet()) { + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, + dispatch_mode, DispatchMode::kFComputeFallback); + } else if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNPooling(param)) { return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeEx); } @@ -316,7 +319,10 @@ inline static bool BackwardPoolingStorageType(const nnvm::NodeAttrs &attrs, CHECK_EQ(out_attrs->size(), 1); #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNPooling(param)) { + if (!MKLDNNEnvSet()) { + return storage_type_assign(out_attrs, mxnet::kDefaultStorage, + dispatch_mode, DispatchMode::kFComputeFallback); + } else if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNPooling(param)) { return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeEx); } diff --git a/src/operator/nn/softmax.cc b/src/operator/nn/softmax.cc index e855608e7f28..f08eb530a59d 100644 --- a/src/operator/nn/softmax.cc +++ b/src/operator/nn/softmax.cc @@ -63,7 +63,9 @@ inline static bool SoftmaxStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 // We only run MKLDNN op if it runs on CPU. - if (dev_mask == mshadow::cpu::kDevMask) + if (!MKLDNNEnvSet()) + wanted_mode = DispatchMode::kFComputeFallback; + else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; else #endif diff --git a/src/operator/tensor/elemwise_binary_op_basic.cc b/src/operator/tensor/elemwise_binary_op_basic.cc index 6fc1ebb76cfd..49673df76f42 100644 --- a/src/operator/tensor/elemwise_binary_op_basic.cc +++ b/src/operator/tensor/elemwise_binary_op_basic.cc @@ -62,7 +62,9 @@ static inline bool ElemwiseAddStorageType(const nnvm::NodeAttrs& attrs, bool ret = ElemwiseBinaryOp::PreferDenseStorageType( attrs, dev_mask, dispatch_mode, in_attrs, out_attrs); #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask + if (!MKLDNNEnvSet()) { + *dispatch_mode = DispatchMode::kFComputeFallback; + } else if (dev_mask == mshadow::cpu::kDevMask && common::ContainsOnlyStorage(*in_attrs, kDefaultStorage) && out_attrs->at(0) == kDefaultStorage) { *dispatch_mode = DispatchMode::kFComputeEx; @@ -132,7 +134,9 @@ static inline bool ElemwiseAddBackwardStorageType(const nnvm::NodeAttrs& attrs, bool ret = ElemwiseStorageType<1, 2, true, true, true>(attrs, dev_mask, dispatch_mode, in_attrs, out_attrs); #if MXNET_USE_MKLDNN == 1 - if (dev_mask == mshadow::cpu::kDevMask) { + if (!MKLDNNEnvSet()) { + *dispatch_mode = DispatchMode::kFComputeFallback; + } else if (dev_mask == mshadow::cpu::kDevMask) { *dispatch_mode = DispatchMode::kFComputeEx; } #endif From 03bd210d181d471ac842a69490fbe4de33aa16c5 Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Mon, 6 Aug 2018 20:32:07 -0700 Subject: [PATCH 2/9] fix mkdnn default env --- src/operator/nn/mkldnn/mkldnn_base-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/operator/nn/mkldnn/mkldnn_base-inl.h b/src/operator/nn/mkldnn/mkldnn_base-inl.h index c1dabb134cf9..a610cd11ecbe 100644 --- a/src/operator/nn/mkldnn/mkldnn_base-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_base-inl.h @@ -138,7 +138,7 @@ static inline bool SupportMKLDNN(const NDArray &input) { } static inline bool MKLDNNEnvSet() { - return dmlc::GetEnv("USE_MKLDNN", "ON") == "ON"; + return dmlc::GetEnv("USE_MKLDNN", true); } /* From 3b8083664962a4242ce15beb91d977aab6383b33 Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Wed, 8 Aug 2018 12:38:25 -0700 Subject: [PATCH 3/9] update docs to include desc about MXNET_MKLDNN_ENABLED --- docs/faq/env_var.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/faq/env_var.md b/docs/faq/env_var.md index 6e9a3594168f..017592d99c08 100644 --- a/docs/faq/env_var.md +++ b/docs/faq/env_var.md @@ -155,6 +155,11 @@ When USE_PROFILER is enabled in Makefile or CMake, the following environments ca * MXNET_HOME - Data directory in the filesystem for storage, for example when downloading gluon models. - Default in *nix is .mxnet APPDATA/mxnet in windows. + +* MXNET_MKLDNN_ENABLED + - Values: 0, 1 ```(default=1)``` + - Flag to enable or disable MKLDNN accelerator. On by default. + - Only applies to mxnet that has been compiled with MKLDNN (```pip install mxnet-mkl``` or built from source with ```USE_MKLDNN=1```) Settings for Minimum Memory Usage --------------------------------- From 3ab08fa28de5cdde9dbfb4ccea4087ca60a24add Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Wed, 8 Aug 2018 12:38:58 -0700 Subject: [PATCH 4/9] update env name to MXNET_MKLDNN_ENABLED --- src/operator/nn/mkldnn/mkldnn_base-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/operator/nn/mkldnn/mkldnn_base-inl.h b/src/operator/nn/mkldnn/mkldnn_base-inl.h index a610cd11ecbe..b0e7d6989f97 100644 --- a/src/operator/nn/mkldnn/mkldnn_base-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_base-inl.h @@ -138,7 +138,7 @@ static inline bool SupportMKLDNN(const NDArray &input) { } static inline bool MKLDNNEnvSet() { - return dmlc::GetEnv("USE_MKLDNN", true); + return dmlc::GetEnv("MXNET_MKLDNN_ENABLED", true); } /* From 06b2a5ccf39688e701740a7dc39cc53689d01aef Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Wed, 8 Aug 2018 13:00:11 -0700 Subject: [PATCH 5/9] check dev_mask is cpu before fallback --- src/operator/nn/activation.cc | 4 ++-- src/operator/nn/convolution.cc | 4 ++-- src/operator/nn/deconvolution.cc | 4 ++-- src/operator/nn/lrn.cc | 4 ++-- src/operator/nn/pooling.cc | 4 ++-- src/operator/nn/softmax.cc | 2 +- src/operator/tensor/elemwise_binary_op_basic.cc | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/operator/nn/activation.cc b/src/operator/nn/activation.cc index 8f24db05aebd..277ca8e3013f 100644 --- a/src/operator/nn/activation.cc +++ b/src/operator/nn/activation.cc @@ -116,7 +116,7 @@ inline static bool ActivationStorageType(const nnvm::NodeAttrs& attrs, if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNAct(param)) { *dispatch_mode = DispatchMode::kFComputeEx; } - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { *dispatch_mode = DispatchMode::kFComputeFallback; return ret; } @@ -162,7 +162,7 @@ inline static bool BackwardActStorageType(const nnvm::NodeAttrs& attrs, if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNAct(param)) { *dispatch_mode = DispatchMode::kFComputeEx; } - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { *dispatch_mode = DispatchMode::kFComputeFallback; return ret; } diff --git a/src/operator/nn/convolution.cc b/src/operator/nn/convolution.cc index 9656e04ea215..681f3138dcb5 100644 --- a/src/operator/nn/convolution.cc +++ b/src/operator/nn/convolution.cc @@ -300,7 +300,7 @@ inline static bool ConvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) wanted_mode = DispatchMode::kFComputeFallback; else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; @@ -324,7 +324,7 @@ inline static bool BackwardConvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) wanted_mode = DispatchMode::kFComputeFallback; else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; diff --git a/src/operator/nn/deconvolution.cc b/src/operator/nn/deconvolution.cc index 4b4c422cec62..54b77aafda0f 100644 --- a/src/operator/nn/deconvolution.cc +++ b/src/operator/nn/deconvolution.cc @@ -268,7 +268,7 @@ inline static bool DeconvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) wanted_mode = DispatchMode::kFComputeFallback; else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; @@ -291,7 +291,7 @@ inline static bool BackwardDeconvStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) wanted_mode = DispatchMode::kFComputeFallback; else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; diff --git a/src/operator/nn/lrn.cc b/src/operator/nn/lrn.cc index ce2cd075b4fd..7159902a6df8 100644 --- a/src/operator/nn/lrn.cc +++ b/src/operator/nn/lrn.cc @@ -88,7 +88,7 @@ bool LRNForwardInferStorageType(const nnvm::NodeAttrs& attrs, std::vector *out_attrs) { CHECK(!in_attrs->empty()); #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeFallback); } else if (dev_mask == mshadow::cpu::kDevMask) { @@ -108,7 +108,7 @@ bool LRNBackwardInferStorageType(const nnvm::NodeAttrs& attrs, std::vector *out_attrs) { CHECK(!in_attrs->empty()); #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeFallback); } else if (dev_mask == mshadow::cpu::kDevMask) { diff --git a/src/operator/nn/pooling.cc b/src/operator/nn/pooling.cc index 4549eb2befd9..77445abc714a 100644 --- a/src/operator/nn/pooling.cc +++ b/src/operator/nn/pooling.cc @@ -295,7 +295,7 @@ inline static bool PoolingStorageType(const nnvm::NodeAttrs &attrs, #if MXNET_USE_MKLDNN == 1 const PoolingParam ¶m = nnvm::get(attrs.parsed); - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeFallback); } else if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNPooling(param)) { @@ -319,7 +319,7 @@ inline static bool BackwardPoolingStorageType(const nnvm::NodeAttrs &attrs, CHECK_EQ(out_attrs->size(), 1); #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { return storage_type_assign(out_attrs, mxnet::kDefaultStorage, dispatch_mode, DispatchMode::kFComputeFallback); } else if (dev_mask == mshadow::cpu::kDevMask && SupportMKLDNNPooling(param)) { diff --git a/src/operator/nn/softmax.cc b/src/operator/nn/softmax.cc index f08eb530a59d..c58f382bbade 100644 --- a/src/operator/nn/softmax.cc +++ b/src/operator/nn/softmax.cc @@ -63,7 +63,7 @@ inline static bool SoftmaxStorageType(const nnvm::NodeAttrs& attrs, DispatchMode wanted_mode; #if MXNET_USE_MKLDNN == 1 // We only run MKLDNN op if it runs on CPU. - if (!MKLDNNEnvSet()) + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) wanted_mode = DispatchMode::kFComputeFallback; else if (dev_mask == mshadow::cpu::kDevMask) wanted_mode = DispatchMode::kFComputeEx; diff --git a/src/operator/tensor/elemwise_binary_op_basic.cc b/src/operator/tensor/elemwise_binary_op_basic.cc index 49673df76f42..884a1ddad217 100644 --- a/src/operator/tensor/elemwise_binary_op_basic.cc +++ b/src/operator/tensor/elemwise_binary_op_basic.cc @@ -62,7 +62,7 @@ static inline bool ElemwiseAddStorageType(const nnvm::NodeAttrs& attrs, bool ret = ElemwiseBinaryOp::PreferDenseStorageType( attrs, dev_mask, dispatch_mode, in_attrs, out_attrs); #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { *dispatch_mode = DispatchMode::kFComputeFallback; } else if (dev_mask == mshadow::cpu::kDevMask && common::ContainsOnlyStorage(*in_attrs, kDefaultStorage) @@ -134,7 +134,7 @@ static inline bool ElemwiseAddBackwardStorageType(const nnvm::NodeAttrs& attrs, bool ret = ElemwiseStorageType<1, 2, true, true, true>(attrs, dev_mask, dispatch_mode, in_attrs, out_attrs); #if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) { + if (dev_mask == mshadow::cpu::kDevMask && !MKLDNNEnvSet()) { *dispatch_mode = DispatchMode::kFComputeFallback; } else if (dev_mask == mshadow::cpu::kDevMask) { *dispatch_mode = DispatchMode::kFComputeEx; From 3ad3ec3d46df5a93a5c0c0c3e42fcfc104ec099a Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Tue, 14 Aug 2018 22:29:43 -0700 Subject: [PATCH 6/9] log if flag is off --- src/executor/attach_op_execs_pass.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/executor/attach_op_execs_pass.cc b/src/executor/attach_op_execs_pass.cc index 72919d90c620..5c974fe4edfd 100644 --- a/src/executor/attach_op_execs_pass.cc +++ b/src/executor/attach_op_execs_pass.cc @@ -262,6 +262,12 @@ void CreateOpExecs(const Graph& g, OpExecVector* p_ret, size_t i) { const auto& vctx = g.GetAttr("context"); const auto& dispatch_modes = g.GetAttr("dispatch_mode"); +#if MXNET_USE_MKLDNN == 1 + if (!MKLDNNEnvSet()) common::LogOnce("MXNET_MKLDNN_ENABLED flag is off. " + "You can re-enable by setting MXNET_MKLDNN_ENABLED=1"); +#endif + + // get the graph const auto& idx = g.indexed_graph(); OpExecVector& ret = *p_ret; From 9e53fdcee693f947906f896d5a1f1d05994ad241 Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Wed, 15 Aug 2018 15:02:27 -0700 Subject: [PATCH 7/9] cache mkldnn check output --- src/operator/nn/mkldnn/mkldnn_base-inl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/operator/nn/mkldnn/mkldnn_base-inl.h b/src/operator/nn/mkldnn/mkldnn_base-inl.h index b0e7d6989f97..273afcd32dc7 100644 --- a/src/operator/nn/mkldnn/mkldnn_base-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_base-inl.h @@ -138,7 +138,8 @@ static inline bool SupportMKLDNN(const NDArray &input) { } static inline bool MKLDNNEnvSet() { - return dmlc::GetEnv("MXNET_MKLDNN_ENABLED", true); + static bool is_mkldnn_enabled = dmlc::GetEnv("MXNET_MKLDNN_ENABLED", true); + return is_mkldnn_enabled; } /* From 65184d3dee5841d609c40b9d96883a78b3e516eb Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Wed, 15 Aug 2018 15:03:16 -0700 Subject: [PATCH 8/9] move logonce inside fallback --- src/common/utils.h | 7 +++++++ src/executor/attach_op_execs_pass.cc | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/common/utils.h b/src/common/utils.h index 96949a047fba..96569b317351 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -46,6 +46,9 @@ #include #include "../operator/mxnet_op.h" +#if MXNET_USE_MKLDNN == 1 +#include "../operator/nn/mkldnn/mkldnn_base-inl.h" +#endif namespace mxnet { namespace common { @@ -468,6 +471,10 @@ inline void LogStorageFallback(const nnvm::NodeAttrs& attrs, "0 to suppress this warning."; os << "\nStorage type fallback detected:\n" << op_str << warning; LogOnce(os.str()); +#if MXNET_USE_MKLDNN == 1 + if (!MKLDNNEnvSet()) common::LogOnce("MXNET_MKLDNN_ENABLED flag is off. " + "You can re-enable by setting MXNET_MKLDNN_ENABLED=1"); +#endif } // heuristic to dermine number of threads per GPU diff --git a/src/executor/attach_op_execs_pass.cc b/src/executor/attach_op_execs_pass.cc index 5c974fe4edfd..c011c1d9ce03 100644 --- a/src/executor/attach_op_execs_pass.cc +++ b/src/executor/attach_op_execs_pass.cc @@ -261,13 +261,6 @@ void CreateOpExecs(const Graph& g, OpExecVector* p_ret, size_t i) { const auto& vshape = g.GetAttr("shape"); const auto& vctx = g.GetAttr("context"); const auto& dispatch_modes = g.GetAttr("dispatch_mode"); - -#if MXNET_USE_MKLDNN == 1 - if (!MKLDNNEnvSet()) common::LogOnce("MXNET_MKLDNN_ENABLED flag is off. " - "You can re-enable by setting MXNET_MKLDNN_ENABLED=1"); -#endif - - // get the graph const auto& idx = g.indexed_graph(); OpExecVector& ret = *p_ret; From ca3efb1c4f78ee7891156eb845c3b4c52f6ecf9e Mon Sep 17 00:00:00 2001 From: Alexander Zai Date: Wed, 15 Aug 2018 20:28:28 -0700 Subject: [PATCH 9/9] retrigger