Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-1357] Fix the cpp-examples to add exception handling #14441

Merged
merged 3 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpp-package/example/alexnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ int main(int argc, char const *argv[]) {
}
#endif

TRY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just add exception handling to cpp-package the way it is done in python. Every C API call is inside a check_call function which checks the return code and throws corresponding frontend exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is certainly possible to add exception handling in cpp-package API itself. However, it will require careful design and we may require to change the cpp-package API as well.
We will certainly consider this enhancement as a part of C++ API enhancement project.

/*net symbol*/
auto Net = AlexnetSymbol(10);

Expand Down Expand Up @@ -352,5 +353,6 @@ int main(int argc, char const *argv[]) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
3 changes: 3 additions & 0 deletions cpp-package/example/charRNN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <thread>
#include <chrono>
#include "mxnet-cpp/MxNetCpp.h"
#include "utils.h"

using namespace mxnet::cpp;

Expand Down Expand Up @@ -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;
Expand All @@ -746,5 +748,6 @@ int main(int argc, char** argv) {
}

MXNotifyShutdown();
CATCH
return 0;
}
2 changes: 2 additions & 0 deletions cpp-package/example/googlenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,5 +193,6 @@ int main(int argc, char const *argv[]) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
2 changes: 2 additions & 0 deletions cpp-package/example/inception_bn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -255,5 +256,6 @@ int main(int argc, char const *argv[]) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
3 changes: 3 additions & 0 deletions cpp-package/example/lenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <vector>
#include <cstdlib>
#include "mxnet-cpp/MxNetCpp.h"
#include "utils.h"

using namespace mxnet::cpp;

Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions cpp-package/example/lenet_with_mxdataiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ int main(int argc, char const *argv[]) {
}
#endif

TRY
auto lenet = LenetSymbol();
std::map<std::string, NDArray> args_map;

Expand Down Expand Up @@ -197,5 +198,6 @@ int main(int argc, char const *argv[]) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
3 changes: 3 additions & 0 deletions cpp-package/example/mlp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <vector>
#include <string>
#include "mxnet-cpp/MxNetCpp.h"
#include "utils.h"

using namespace mxnet::cpp;

Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions cpp-package/example/mlp_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -141,5 +142,6 @@ int main(int argc, char** argv) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
2 changes: 2 additions & 0 deletions cpp-package/example/mlp_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ int main(int argc, char** argv) {
.SetParam("shuffle", 0)
.CreateDataIter();

TRY
auto net = mlp(hidden_units);

Context ctx = Context::cpu();
Expand Down Expand Up @@ -269,5 +270,6 @@ int main(int argc, char** argv) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
2 changes: 2 additions & 0 deletions cpp-package/example/mlp_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -157,5 +158,6 @@ int main(int argc, char** argv) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
2 changes: 2 additions & 0 deletions cpp-package/example/resnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,5 +278,6 @@ int main(int argc, char const *argv[]) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return 0;
}
4 changes: 3 additions & 1 deletion cpp-package/example/test_score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -158,5 +159,6 @@ int main(int argc, char** argv) {
delete exec;
delete opt;
MXNotifyShutdown();
CATCH
return score >= MIN_SCORE ? 0 : 1;
}
9 changes: 9 additions & 0 deletions cpp-package/example/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@

using namespace mxnet::cpp;

#define TRY \
try {
#define CATCH \
} catch(dmlc::Error &err) { \
LG << "Status: FAIL";\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch only dmlc::Error. For non backend exceptions MXGetLastError will be empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

LG << "With Error: " << MXGetLastError(); \
return 1; \
}

bool isFileExists(const std::string &filename) {
std::ifstream fhandle(filename.c_str());
return fhandle.good();
Expand Down