This is an automated email from the ASF dual-hosted git repository. anirudh2290 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push: new 9e4ee99 [MXNET-1357] Fix the cpp-examples to add exception handling (#14441) 9e4ee99 is described below commit 9e4ee992f90e7da6cf52fb2664f7aaeffa8ccb62 Author: Amol Lele <19983848+lelea...@users.noreply.github.com> AuthorDate: Tue Apr 2 16:23:54 2019 -0700 [MXNET-1357] Fix the cpp-examples to add exception handling (#14441) * Adding exception handling to the cpp-package examples. * Updating exception handling to catch dmlc::error. * Removed the catch all exception. --- cpp-package/example/alexnet.cpp | 2 ++ cpp-package/example/charRNN.cpp | 3 +++ cpp-package/example/googlenet.cpp | 2 ++ cpp-package/example/inception_bn.cpp | 2 ++ cpp-package/example/lenet.cpp | 3 +++ cpp-package/example/lenet_with_mxdataiter.cpp | 2 ++ cpp-package/example/mlp.cpp | 3 +++ cpp-package/example/mlp_cpu.cpp | 2 ++ cpp-package/example/mlp_csv.cpp | 2 ++ cpp-package/example/mlp_gpu.cpp | 2 ++ cpp-package/example/resnet.cpp | 2 ++ cpp-package/example/test_score.cpp | 4 +++- cpp-package/example/utils.h | 9 +++++++++ 13 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cpp-package/example/alexnet.cpp b/cpp-package/example/alexnet.cpp index 2b2d7b4..21029da 100644 --- a/cpp-package/example/alexnet.cpp +++ b/cpp-package/example/alexnet.cpp @@ -228,6 +228,7 @@ int main(int argc, char const *argv[]) { } #endif + TRY /*net symbol*/ auto Net = AlexnetSymbol(10); @@ -352,5 +353,6 @@ int main(int argc, char const *argv[]) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/charRNN.cpp b/cpp-package/example/charRNN.cpp index 8951580..ac5faa4 100644 --- a/cpp-package/example/charRNN.cpp +++ b/cpp-package/example/charRNN.cpp @@ -42,6 +42,7 @@ #include <thread> #include <chrono> #include "mxnet-cpp/MxNetCpp.h" +#include "utils.h" using namespace mxnet::cpp; @@ -721,6 +722,7 @@ int main(int argc, char** argv) { TIME_MAJOR = task.find("TimeMajor") != std::string::npos; std::cout << "use BuiltIn cuDNN RNN: " << builtIn << std::endl << "use data as TimeMajor: " << TIME_MAJOR << std::endl; + TRY if (task.find("train") == 0) { std::cout << "train batch size: " << argv[3] << std::endl << "train max epoch: " << argv[4] << std::endl; @@ -746,5 +748,6 @@ int main(int argc, char** argv) { } MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/googlenet.cpp b/cpp-package/example/googlenet.cpp index 26ba510..9cf1834 100644 --- a/cpp-package/example/googlenet.cpp +++ b/cpp-package/example/googlenet.cpp @@ -124,6 +124,7 @@ int main(int argc, char const *argv[]) { ctx = Context::cpu();; #endif + TRY auto googlenet = GoogleNetSymbol(10); std::map<std::string, NDArray> args_map; std::map<std::string, NDArray> aux_map; @@ -192,5 +193,6 @@ int main(int argc, char const *argv[]) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/inception_bn.cpp b/cpp-package/example/inception_bn.cpp index a29ef2d..caf858a 100644 --- a/cpp-package/example/inception_bn.cpp +++ b/cpp-package/example/inception_bn.cpp @@ -172,6 +172,7 @@ int main(int argc, char const *argv[]) { } #endif + TRY auto inception_bn_net = InceptionSymbol(10); std::map<std::string, NDArray> args_map; std::map<std::string, NDArray> aux_map; @@ -255,5 +256,6 @@ int main(int argc, char const *argv[]) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/lenet.cpp b/cpp-package/example/lenet.cpp index 4259454..a52efd8 100644 --- a/cpp-package/example/lenet.cpp +++ b/cpp-package/example/lenet.cpp @@ -25,6 +25,7 @@ #include <vector> #include <cstdlib> #include "mxnet-cpp/MxNetCpp.h" +#include "utils.h" using namespace mxnet::cpp; @@ -257,8 +258,10 @@ class Lenet { }; int main(int argc, char const *argv[]) { + TRY Lenet lenet; lenet.Run(argc > 1 ? strtol(argv[1], NULL, 10) : 100000); MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/lenet_with_mxdataiter.cpp b/cpp-package/example/lenet_with_mxdataiter.cpp index fac624b..69067d5 100644 --- a/cpp-package/example/lenet_with_mxdataiter.cpp +++ b/cpp-package/example/lenet_with_mxdataiter.cpp @@ -94,6 +94,7 @@ int main(int argc, char const *argv[]) { } #endif + TRY auto lenet = LenetSymbol(); std::map<std::string, NDArray> args_map; @@ -197,5 +198,6 @@ int main(int argc, char const *argv[]) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/mlp.cpp b/cpp-package/example/mlp.cpp index c3760fd..ee09bf8 100644 --- a/cpp-package/example/mlp.cpp +++ b/cpp-package/example/mlp.cpp @@ -24,6 +24,7 @@ #include <vector> #include <string> #include "mxnet-cpp/MxNetCpp.h" +#include "utils.h" using namespace mxnet::cpp; @@ -173,7 +174,9 @@ void MLP(int max_epoch) { int main(int argc, char** argv) { int max_epoch = argc > 1 ? strtol(argv[1], NULL, 10) : 15000; + TRY MLP(max_epoch); MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/mlp_cpu.cpp b/cpp-package/example/mlp_cpu.cpp index 5d46d40..7ea6946 100644 --- a/cpp-package/example/mlp_cpu.cpp +++ b/cpp-package/example/mlp_cpu.cpp @@ -72,6 +72,7 @@ int main(int argc, char** argv) { return 1; } + TRY auto net = mlp(layers); Context ctx = Context::cpu(); // Use CPU for training @@ -141,5 +142,6 @@ int main(int argc, char** argv) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/mlp_csv.cpp b/cpp-package/example/mlp_csv.cpp index f12b7c1..0d91037 100644 --- a/cpp-package/example/mlp_csv.cpp +++ b/cpp-package/example/mlp_csv.cpp @@ -156,6 +156,7 @@ int main(int argc, char** argv) { .SetParam("shuffle", 0) .CreateDataIter(); + TRY auto net = mlp(hidden_units); Context ctx = Context::cpu(); @@ -269,5 +270,6 @@ int main(int argc, char** argv) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/mlp_gpu.cpp b/cpp-package/example/mlp_gpu.cpp index f606020..5265de7 100644 --- a/cpp-package/example/mlp_gpu.cpp +++ b/cpp-package/example/mlp_gpu.cpp @@ -72,6 +72,7 @@ int main(int argc, char** argv) { return 1; } + TRY auto net = mlp(layers); Context ctx = Context::gpu(); // Use GPU for training @@ -157,5 +158,6 @@ int main(int argc, char** argv) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/resnet.cpp b/cpp-package/example/resnet.cpp index 29071bd..f59f606 100644 --- a/cpp-package/example/resnet.cpp +++ b/cpp-package/example/resnet.cpp @@ -172,6 +172,7 @@ int main(int argc, char const *argv[]) { float learning_rate = 1e-4; float weight_decay = 1e-4; + TRY auto resnet = ResNetSymbol(10); std::map<std::string, NDArray> args_map; std::map<std::string, NDArray> aux_map; @@ -277,5 +278,6 @@ int main(int argc, char const *argv[]) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return 0; } diff --git a/cpp-package/example/test_score.cpp b/cpp-package/example/test_score.cpp index 687683f..0ccdf65 100644 --- a/cpp-package/example/test_score.cpp +++ b/cpp-package/example/test_score.cpp @@ -62,6 +62,7 @@ int main(int argc, char** argv) { const int max_epoch = 10; const float learning_rate = 0.1; const float weight_decay = 1e-2; + float score = 0; std::vector<std::string> data_files = { "./data/mnist_data/train-images-idx3-ubyte", "./data/mnist_data/train-labels-idx1-ubyte", @@ -79,6 +80,7 @@ int main(int argc, char** argv) { return 1; } + TRY auto net = mlp(layers); Context ctx = Context::gpu(); // Use GPU for training @@ -111,7 +113,6 @@ int main(int argc, char** argv) { auto *exec = net.SimpleBind(ctx, args); auto arg_names = net.ListArguments(); - float score = 0; // Start training for (int iter = 0; iter < max_epoch; ++iter) { int samples = 0; @@ -158,5 +159,6 @@ int main(int argc, char** argv) { delete exec; delete opt; MXNotifyShutdown(); + CATCH return score >= MIN_SCORE ? 0 : 1; } diff --git a/cpp-package/example/utils.h b/cpp-package/example/utils.h index 020d1ec..8784770 100644 --- a/cpp-package/example/utils.h +++ b/cpp-package/example/utils.h @@ -27,6 +27,15 @@ using namespace mxnet::cpp; +#define TRY \ + try { +#define CATCH \ + } catch(dmlc::Error &err) { \ + LG << "Status: FAIL";\ + LG << "With Error: " << MXGetLastError(); \ + return 1; \ + } + bool isFileExists(const std::string &filename) { std::ifstream fhandle(filename.c_str()); return fhandle.good();