From dcd16ad1c9eccf4dc6dc6278ad2342907c92d065 Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Tue, 5 Feb 2019 23:12:50 +0000
Subject: [PATCH 01/10] Relax constexpr restriction

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 53998ac31919..515227de370e 100644
--- a/Makefile
+++ b/Makefile
@@ -89,7 +89,7 @@ CFLAGS = -DMSHADOW_FORCE_STREAM $(WARNFLAGS)
 
 ifeq ($(DEV), 1)
 	CFLAGS += -g -Werror
-	NVCCFLAGS += -Werror cross-execution-space-call
+	NVCCFLAGS += -Werror cross-execution-space-call --expt-relaxed-constexpr
 endif
 
 # CFLAGS for debug

From c0dcaf796676fc3229a313bed3f117f91406df28 Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Tue, 12 Feb 2019 23:05:39 +0000
Subject: [PATCH 02/10] Change imagenet_gen_qsym_mkldnn

---
 .../quantization/imagenet_gen_qsym_mkldnn.py  |  3 ++
 include/mxnet/c_api_error.h                   | 37 +++++++++++++++++--
 python/mxnet/base.py                          |  6 ++-
 src/c_api/c_api_error.cc                      |  1 +
 src/engine/threaded_engine.h                  |  4 ++
 tests/python/unittest/test_exc_handling.py    | 12 +++++-
 6 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/example/quantization/imagenet_gen_qsym_mkldnn.py b/example/quantization/imagenet_gen_qsym_mkldnn.py
index 2ef137273cca..3ce95cbc1afa 100644
--- a/example/quantization/imagenet_gen_qsym_mkldnn.py
+++ b/example/quantization/imagenet_gen_qsym_mkldnn.py
@@ -179,7 +179,9 @@ def save_params(fname, arg_params, aux_params, logger=None):
         prefix, epoch = download_model(model_name=args.model, logger=logger)
         sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
 
+    sym.save("model_pre_quantize.json")
     sym = sym.get_backend_symbol('MKLDNN')
+    sym.save("model_post_quantize.json")
 
     # get batch size
     batch_size = args.batch_size
@@ -302,6 +304,7 @@ def save_params(fname, arg_params, aux_params, logger=None):
                              % calib_mode)
         sym_name = '%s-symbol.json' % (prefix + suffix)
     qsym = qsym.get_backend_symbol('MKLDNN_POST_QUANTIZE')
+    qsym.save("model_post_post_quantize.json")
     save_symbol(sym_name, qsym, logger)
     param_name = '%s-%04d.params' % (prefix + '-quantized', epoch)
     save_params(param_name, qarg_params, aux_params, logger)
diff --git a/include/mxnet/c_api_error.h b/include/mxnet/c_api_error.h
index 0c6ea03fa459..9ff9b17224fc 100644
--- a/include/mxnet/c_api_error.h
+++ b/include/mxnet/c_api_error.h
@@ -31,9 +31,35 @@
  * and finishes with API_END() or API_END_HANDLE_ERROR()
  * The finally clause contains procedure to cleanup states when an error happens.
  */
-#define MX_API_BEGIN() try { on_enter_api(__FUNCTION__);
-#define MX_API_END() } catch(dmlc::Error &_except_) { on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0;  // NOLINT(*)
-#define MX_API_END_HANDLE_ERROR(Finalize) } catch(dmlc::Error &_except_) { Finalize; on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*)
+#define MX_API_BEGIN()                                                         \
+  try {                                                                        \
+    on_enter_api(__FUNCTION__);
+#define MX_API_END()                                                           \
+  }                                                                            \
+  catch (dmlc::Error &_except_) {                                              \
+    on_exit_api();                                                             \
+    return MXAPIHandleException(_except_);                                     \
+  }                                                                            \
+  catch (std::exception &_except_) {                                           \
+    on_exit_api();                                                             \
+    return MXAPIHandleException(_except_);                                     \
+  }                                                                            \
+  on_exit_api();                                                               \
+  return 0; // NOLINT(*)
+#define MX_API_END_HANDLE_ERROR(Finalize)                                      \
+  }                                                                            \
+  catch (dmlc::Error &_except_) {                                              \
+    Finalize;                                                                  \
+    on_exit_api();                                                             \
+    return MXAPIHandleException(_except_);                                     \
+  }                                                                            \
+  catch (std::exception &_except_) {                                           \
+    Finalize;                                                                  \
+    on_exit_api();                                                             \
+    return MXAPIHandleException(_except_);                                     \
+  }                                                                            \
+  on_exit_api();                                                               \
+  return 0; // NOLINT(*)
 /*!
  * \brief Set the last error message needed by C API
  * \param msg The error message to set.
@@ -49,6 +75,11 @@ inline int MXAPIHandleException(const dmlc::Error &e) {
   return -1;
 }
 
+inline int MXAPIHandleException(const std::exception &e) {
+  MXAPISetLastError(e.what());
+  return -1;
+}
+
 namespace mxnet {
 extern void on_enter_api(const char *function);
 extern void on_exit_api();
diff --git a/python/mxnet/base.py b/python/mxnet/base.py
index feb4d70b6533..679e978e458e 100644
--- a/python/mxnet/base.py
+++ b/python/mxnet/base.py
@@ -249,7 +249,11 @@ def check_call(ret):
         return value from API calls.
     """
     if ret != 0:
-        raise MXNetError(py_str(_LIB.MXGetLastError()))
+        # error code for std::exception
+        if ret == -2:
+            raise Exception(py_str(_LIB.MXGetLastError()))
+        else:
+            raise MXNetError(py_str(_LIB.MXGetLastError()))
 
 
 if sys.version_info[0] < 3:
diff --git a/src/c_api/c_api_error.cc b/src/c_api/c_api_error.cc
index 6dd4719281b6..b9975b050802 100644
--- a/src/c_api/c_api_error.cc
+++ b/src/c_api/c_api_error.cc
@@ -25,6 +25,7 @@
 #include <nnvm/c_api.h>
 #include "./c_api_common.h"
 
+
 const char *MXGetLastError() {
   return NNGetLastError();
 }
diff --git a/src/engine/threaded_engine.h b/src/engine/threaded_engine.h
index 3d2119d63291..b63c1416561e 100644
--- a/src/engine/threaded_engine.h
+++ b/src/engine/threaded_engine.h
@@ -384,6 +384,10 @@ class ThreadedEngine : public Engine {
           threaded_opr->opr_exception =
               std::make_shared<std::exception_ptr>(std::current_exception());
           callback();
+        } catch (std::exception& e) {
+          threaded_opr->opr_exception =
+              std::make_shared<std::exception_ptr>(std::current_exception());
+          callback();
         }
         if (debug_info) {
           LOG(INFO) << "Fin ExecuteOprFn ";
diff --git a/tests/python/unittest/test_exc_handling.py b/tests/python/unittest/test_exc_handling.py
index 5627ac50d26e..4a6d550997f0 100644
--- a/tests/python/unittest/test_exc_handling.py
+++ b/tests/python/unittest/test_exc_handling.py
@@ -24,6 +24,8 @@
 from mxnet.test_utils import assert_exception, default_context, set_default_context
 from nose.tools import assert_raises
 
+url = "https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/python/predict_image/cat.jpg?raw=true"
+
 @with_seed()
 def test_exc_imperative():
     def imperative(exec_numpy=True):
@@ -166,7 +168,6 @@ def test_multiple_waitalls():
     mx.nd.waitall()
 
 @with_seed()
-def test_exc_profiler():
     def run_training_iteration(data):
         output = net(data)
 
@@ -182,6 +183,15 @@ def run_training_iteration(data):
     mx.nd.waitall()
     mx.profiler.set_state("stop")
 
+@with_seed()
+def test_opencv_exception():
+    def check_resize():
+        fname = mx.test_utils.download(url)
+        img = mx.image.imread(fname)
+        img = mx.image.imresize(img, 320, 320, interp=-1)
+        img.asnumpy()
+    assert_raises(MXNetError, check_resize)
+
 
 if __name__ == '__main__':
     import nose

From aa6f3f2048044cee899c2e54aae2181c8b69acde Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Tue, 12 Mar 2019 02:08:27 +0000
Subject: [PATCH 03/10] Revert constexpr change

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 515227de370e..53998ac31919 100644
--- a/Makefile
+++ b/Makefile
@@ -89,7 +89,7 @@ CFLAGS = -DMSHADOW_FORCE_STREAM $(WARNFLAGS)
 
 ifeq ($(DEV), 1)
 	CFLAGS += -g -Werror
-	NVCCFLAGS += -Werror cross-execution-space-call --expt-relaxed-constexpr
+	NVCCFLAGS += -Werror cross-execution-space-call
 endif
 
 # CFLAGS for debug

From 9cc0f143a73865e55bd1ac5df286fcc36c8fceb6 Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Fri, 15 Mar 2019 17:03:54 +0000
Subject: [PATCH 04/10] Add dummy image for test

---
 tests/python/unittest/test_exc_handling.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tests/python/unittest/test_exc_handling.py b/tests/python/unittest/test_exc_handling.py
index 4a6d550997f0..6fd998099c2c 100644
--- a/tests/python/unittest/test_exc_handling.py
+++ b/tests/python/unittest/test_exc_handling.py
@@ -186,8 +186,7 @@ def run_training_iteration(data):
 @with_seed()
 def test_opencv_exception():
     def check_resize():
-        fname = mx.test_utils.download(url)
-        img = mx.image.imread(fname)
+        img = mx.nd.ones((1200, 1600, 3))
         img = mx.image.imresize(img, 320, 320, interp=-1)
         img.asnumpy()
     assert_raises(MXNetError, check_resize)

From df5bc6701ed995faaf461e181ae9b571027eb028 Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Fri, 15 Mar 2019 21:30:28 +0000
Subject: [PATCH 05/10] Fix unreverted change

---
 python/mxnet/base.py | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/python/mxnet/base.py b/python/mxnet/base.py
index 679e978e458e..feb4d70b6533 100644
--- a/python/mxnet/base.py
+++ b/python/mxnet/base.py
@@ -249,11 +249,7 @@ def check_call(ret):
         return value from API calls.
     """
     if ret != 0:
-        # error code for std::exception
-        if ret == -2:
-            raise Exception(py_str(_LIB.MXGetLastError()))
-        else:
-            raise MXNetError(py_str(_LIB.MXGetLastError()))
+        raise MXNetError(py_str(_LIB.MXGetLastError()))
 
 
 if sys.version_info[0] < 3:

From 798a5e0b794d05a40a8ac98fe839a419618edb1e Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Tue, 9 Apr 2019 21:25:54 +0000
Subject: [PATCH 06/10] Remove url

---
 src/c_api/c_api_error.cc                   | 1 -
 tests/python/unittest/test_exc_handling.py | 6 ++----
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/src/c_api/c_api_error.cc b/src/c_api/c_api_error.cc
index b9975b050802..6dd4719281b6 100644
--- a/src/c_api/c_api_error.cc
+++ b/src/c_api/c_api_error.cc
@@ -25,7 +25,6 @@
 #include <nnvm/c_api.h>
 #include "./c_api_common.h"
 
-
 const char *MXGetLastError() {
   return NNGetLastError();
 }
diff --git a/tests/python/unittest/test_exc_handling.py b/tests/python/unittest/test_exc_handling.py
index 6fd998099c2c..960a4e8d1364 100644
--- a/tests/python/unittest/test_exc_handling.py
+++ b/tests/python/unittest/test_exc_handling.py
@@ -24,8 +24,6 @@
 from mxnet.test_utils import assert_exception, default_context, set_default_context
 from nose.tools import assert_raises
 
-url = "https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/python/predict_image/cat.jpg?raw=true"
-
 @with_seed()
 def test_exc_imperative():
     def imperative(exec_numpy=True):
@@ -168,8 +166,8 @@ def test_multiple_waitalls():
     mx.nd.waitall()
 
 @with_seed()
-    def run_training_iteration(data):
-        output = net(data)
+def run_training_iteration(data):
+    output = net(data)
 
     net = gluon.nn.HybridSequential()
     with net.name_scope():

From d96ca1caad502c753cf8fc34287d1ddf0718153b Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Tue, 16 Apr 2019 19:01:28 +0000
Subject: [PATCH 07/10] Fix imagenet change

---
 example/quantization/imagenet_gen_qsym_mkldnn.py | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/example/quantization/imagenet_gen_qsym_mkldnn.py b/example/quantization/imagenet_gen_qsym_mkldnn.py
index 3ce95cbc1afa..2ef137273cca 100644
--- a/example/quantization/imagenet_gen_qsym_mkldnn.py
+++ b/example/quantization/imagenet_gen_qsym_mkldnn.py
@@ -179,9 +179,7 @@ def save_params(fname, arg_params, aux_params, logger=None):
         prefix, epoch = download_model(model_name=args.model, logger=logger)
         sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
 
-    sym.save("model_pre_quantize.json")
     sym = sym.get_backend_symbol('MKLDNN')
-    sym.save("model_post_quantize.json")
 
     # get batch size
     batch_size = args.batch_size
@@ -304,7 +302,6 @@ def save_params(fname, arg_params, aux_params, logger=None):
                              % calib_mode)
         sym_name = '%s-symbol.json' % (prefix + suffix)
     qsym = qsym.get_backend_symbol('MKLDNN_POST_QUANTIZE')
-    qsym.save("model_post_post_quantize.json")
     save_symbol(sym_name, qsym, logger)
     param_name = '%s-%04d.params' % (prefix + '-quantized', epoch)
     save_params(param_name, qarg_params, aux_params, logger)

From 514c282a24c3fe5f56c6eee89aa5b19b6626cc3e Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Tue, 16 Apr 2019 20:57:57 +0000
Subject: [PATCH 08/10] Catch only std::exception

---
 include/mxnet/c_api_error.h | 14 --------------
 1 file changed, 14 deletions(-)

diff --git a/include/mxnet/c_api_error.h b/include/mxnet/c_api_error.h
index 9ff9b17224fc..434160438dc3 100644
--- a/include/mxnet/c_api_error.h
+++ b/include/mxnet/c_api_error.h
@@ -36,10 +36,6 @@
     on_enter_api(__FUNCTION__);
 #define MX_API_END()                                                           \
   }                                                                            \
-  catch (dmlc::Error &_except_) {                                              \
-    on_exit_api();                                                             \
-    return MXAPIHandleException(_except_);                                     \
-  }                                                                            \
   catch (std::exception &_except_) {                                           \
     on_exit_api();                                                             \
     return MXAPIHandleException(_except_);                                     \
@@ -48,11 +44,6 @@
   return 0; // NOLINT(*)
 #define MX_API_END_HANDLE_ERROR(Finalize)                                      \
   }                                                                            \
-  catch (dmlc::Error &_except_) {                                              \
-    Finalize;                                                                  \
-    on_exit_api();                                                             \
-    return MXAPIHandleException(_except_);                                     \
-  }                                                                            \
   catch (std::exception &_except_) {                                           \
     Finalize;                                                                  \
     on_exit_api();                                                             \
@@ -70,11 +61,6 @@ void MXAPISetLastError(const char* msg);
  * \param e the exception
  * \return the return value of API after exception is handled
  */
-inline int MXAPIHandleException(const dmlc::Error &e) {
-  MXAPISetLastError(e.what());
-  return -1;
-}
-
 inline int MXAPIHandleException(const std::exception &e) {
   MXAPISetLastError(e.what());
   return -1;

From f2593d8c8ef2afee26dec6a1e08c75543dbe99b4 Mon Sep 17 00:00:00 2001
From: Anirudh Subramanian <anirudh2290@ufl.edu>
Date: Mon, 22 Apr 2019 23:10:06 +0000
Subject: [PATCH 09/10] Fix const, remove dmlc::Error catch

---
 include/mxnet/c_api_error.h  | 4 ++--
 src/engine/threaded_engine.h | 6 +-----
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/include/mxnet/c_api_error.h b/include/mxnet/c_api_error.h
index 434160438dc3..e76a2c99f8d3 100644
--- a/include/mxnet/c_api_error.h
+++ b/include/mxnet/c_api_error.h
@@ -36,7 +36,7 @@
     on_enter_api(__FUNCTION__);
 #define MX_API_END()                                                           \
   }                                                                            \
-  catch (std::exception &_except_) {                                           \
+  catch (const std::exception &_except_) {                                     \
     on_exit_api();                                                             \
     return MXAPIHandleException(_except_);                                     \
   }                                                                            \
@@ -44,7 +44,7 @@
   return 0; // NOLINT(*)
 #define MX_API_END_HANDLE_ERROR(Finalize)                                      \
   }                                                                            \
-  catch (std::exception &_except_) {                                           \
+  catch (const std::exception &_except_) {                                     \
     Finalize;                                                                  \
     on_exit_api();                                                             \
     return MXAPIHandleException(_except_);                                     \
diff --git a/src/engine/threaded_engine.h b/src/engine/threaded_engine.h
index b63c1416561e..7cf181ec3781 100644
--- a/src/engine/threaded_engine.h
+++ b/src/engine/threaded_engine.h
@@ -380,11 +380,7 @@ class ThreadedEngine : public Engine {
           } else {
             callback();
           }
-        } catch (dmlc::Error& e) {
-          threaded_opr->opr_exception =
-              std::make_shared<std::exception_ptr>(std::current_exception());
-          callback();
-        } catch (std::exception& e) {
+        } catch (const std::exception& e) {
           threaded_opr->opr_exception =
               std::make_shared<std::exception_ptr>(std::current_exception());
           callback();

From a42ec4641275224030130e1ba77b19242f0b1d31 Mon Sep 17 00:00:00 2001
From: JackieWu <wkcn@live.cn>
Date: Sun, 5 May 2019 09:55:40 +0800
Subject: [PATCH 10/10] Retrigger CI