azai91 closed pull request #11678: [MXNET-653] MKLDNN Fallback LRN
URL: https://github.com/apache/incubator-mxnet/pull/11678
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/operator/nn/lrn.cc b/src/operator/nn/lrn.cc
index 6b3d7c81837..bae6c17f00b 100644
--- a/src/operator/nn/lrn.cc
+++ b/src/operator/nn/lrn.cc
@@ -87,16 +87,25 @@ bool LRNForwardInferStorageType(const nnvm::NodeAttrs& 
attrs,
                                 std::vector<int> *in_attrs,
                                 std::vector<int> *out_attrs) {
   CHECK(!in_attrs->empty());
+  bool dispatched = false;
 #if MXNET_USE_MKLDNN == 1
-  if (dev_mask == mshadow::cpu::kDevMask) {
-    storage_type_assign(out_attrs, mxnet::kDefaultStorage,
-                        dispatch_mode, DispatchMode::kFComputeEx);
-    return true;
+  if (!dispatched) {
+    dispatched = MKLDNNStorageType(attrs, dev_mask, true, dispatch_mode,
+                                   in_attrs, out_attrs);
+  }
+#else
+  for (int& v : *in_attrs)
+    if (v == - 1) v = kDefaultStorage;
+  if (!dispatched && (common::ContainsStorageType(*in_attrs, 
kRowSparseStorage) ||
+      common::ContainsStorageType(*in_attrs, kCSRStorage))) {
+    dispatched = dispatch_fallback(out_attrs, dispatch_mode);
+  }
+  if (!dispatched) {
+    dispatched = storage_type_assign(out_attrs, kDefaultStorage,
+                                     dispatch_mode, DispatchMode::kFCompute);
   }
 #endif
-  storage_type_assign(out_attrs, mxnet::kDefaultStorage,
-                      dispatch_mode, DispatchMode::kFCompute);
-  return true;
+  return dispatched;
 }
 
 bool LRNBackwardInferStorageType(const nnvm::NodeAttrs& attrs,
@@ -105,16 +114,25 @@ bool LRNBackwardInferStorageType(const nnvm::NodeAttrs& 
attrs,
                                  std::vector<int> *in_attrs,
                                  std::vector<int> *out_attrs) {
   CHECK(!in_attrs->empty());
+  bool dispatched = false;
 #if MXNET_USE_MKLDNN == 1
-  if (dev_mask == mshadow::cpu::kDevMask) {
-    storage_type_assign(out_attrs, mxnet::kDefaultStorage,
-                        dispatch_mode, DispatchMode::kFComputeEx);
-    return true;
+  if (!dispatched) {
+    dispatched = MKLDNNStorageType(attrs, dev_mask, true, dispatch_mode,
+                                   in_attrs, out_attrs);
+  }
+#else
+  for (int& v : *in_attrs)
+    if (v == - 1) v = kDefaultStorage;
+  if (!dispatched && (common::ContainsStorageType(*in_attrs, 
kRowSparseStorage) ||
+      common::ContainsStorageType(*in_attrs, kCSRStorage))) {
+    dispatched = dispatch_fallback(out_attrs, dispatch_mode);
+  }
+  if (!dispatched) {
+    dispatched = storage_type_assign(out_attrs, kDefaultStorage,
+                                     dispatch_mode, DispatchMode::kFCompute);
   }
 #endif
-  storage_type_assign(out_attrs, mxnet::kDefaultStorage,
-                      dispatch_mode, DispatchMode::kFCompute);
-  return true;
+  return dispatched;
 }
 
 #if MXNET_USE_MKLDNN == 1
diff --git a/tests/python/mkl/test_mkldnn.py b/tests/python/mkl/test_mkldnn.py
index a6d7743e926..f27a097542d 100644
--- a/tests/python/mkl/test_mkldnn.py
+++ b/tests/python/mkl/test_mkldnn.py
@@ -241,6 +241,21 @@ def check_batchnorm_training(stype):
         check_batchnorm_training(stype)
 
 
+@with_seed()
+def test_lrn():
+    def check_lrn_training(stype):
+        shape = (2, 3, 2, 2)
+        data_tmp = np.random.normal(-0.1, 0.1, size=shape)
+        data = mx.symbol.Variable('data', stype=stype)
+        in_location = [mx.nd.array(data_tmp).tostype(stype)]
+        test = mx.symbol.LRN(data, nsize=3)
+        check_numeric_gradient(test, in_location, numeric_eps=1e-2, rtol=0.16, 
atol=1e-2)
+
+    stypes = ['row_sparse', 'default']
+    for stype in stypes:
+        check_lrn_training(stype)
+
+
 @with_seed()
 def test_fullyconnected():
     def check_fullyconnected_training(stype):


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to