szha commented on a change in pull request #9869: Exception handling documentation URL: https://github.com/apache/incubator-mxnet/pull/9869#discussion_r170358313
########## File path: docs/tutorials/basic/exception_handling.md ########## @@ -0,0 +1,122 @@ +# Exception Handling in MXNet + +This tutorial explains the exception handling support in MXNet, +and provides examples on how to throw and handle exceptions when in a multithreaded context. +Although, the examples are in Python, they can be easily extended to MXNet +language bindings. + +MXNet exceptions can be thrown from two areas: +- MXNet main thread. For eg. Infershape and InferType. +- Spawned threads: + * By dependency engine for operator execution in parallel + * By the iterators, during the data loading, text parsing phase etc. + +In the first case, the exception is thrown and can be handled in the main thread. +In the second case, the exception is thrown in a spawned thread, caught and transported to the +main thread, where it is rethrown. This tutorial will give more explanation and examples on how +to handle exceptions for the second case. + +## Prerequisites + +To complete this tutorial, we need: +- MXNet [7b24137](https://github.com/apache/incubator-mxnet/commit/7b24137ed45df605defa4ce72ec91554f6e445f0). See Instructions in [Setup and Installation](http://mxnet.io/install/index.html). + +## Exception Handling for Iterators + +The below example shows how to handle exceptions for iterators. In this example, +we populate a data and a labels file with lesser number of labels compared to the +number of samples. This should throw an exception. + +The CSVIter uses the PrefetcherIter for loading and parsing data. +The PrefetcherIter spawns a producer thread in the background which prefetches +the data while the main thread consumes the data. The exception is thrown in the spawned +producer thread during the prefetching, when the label is not found corresponding to a specific sample. + +The exception is transported to the main thread, where it is rethrown when Next is +called as part of the following line: `for batch in iter(data_train)`. + +In general, Exception may be rethrown as part of `Next` and `BeforeFirst` calls which correspond to `reset()` and `next()` methods in `MXDataIter` for Python language bindings. + +```python +import os +import mxnet as mx + +cwd = os.getcwd() +data_path = os.path.join(cwd, "data.csv") +label_path = os.path.join(cwd, "label.csv") + +with open(data_path, "w") as fout: + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") + fout.write("1,2,3,4,5,6,7,8,9,10\n") Review comment: for loop? ---------------------------------------------------------------- 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