eric-haibin-lin commented on a change in pull request #10081: [MXNET-82] [WIP] Sparse op tutorial for developers URL: https://github.com/apache/incubator-mxnet/pull/10081#discussion_r175203077
########## File path: docs/how_to/add_sparse_op_in_backend.md ########## @@ -0,0 +1,429 @@ +# A Guide to Implementing Sparse Operators in MXNet Backend + +## Prerequisites +- Basic knowledge of [how to implement a dense operator in MXNet backend](https://mxnet.incubator.apache.org/versions/master/how_to/add_op_in_backend.html) +- Basic knowledge of [CSRNDArray](http://mxnet.incubator.apache.org/tutorials/sparse/csr.html) and [RowSparseNDArray](http://mxnet.incubator.apache.org/tutorials/sparse/row_sparse.html) in MXNet + +## Introduction +In the [previous tutorial](https://mxnet.incubator.apache.org/versions/master/how_to/add_op_in_backend.html), +we went through the steps to implementing an operator using C++ in the MXNet backend. +In this tutorial, we will cover how sparse operators are implemented +in the backend. Specifically, we will practice adding CSRNDArray support to the forward function of the `quadratic` operator. + +## Implementation +### A Sparse Operator Example + +Let's consider the quadratic function `f(x) = ax^2+bx+c` when x is a CSRNDArray. +Notice that if the input x is sparse and c is 0.0, the output is also sparse. +If c is non-zero, the output is dense. In MXNet frontend, the operator works like this: + +```python +>>> x = mx.nd.array([[0,1],[2,0]).tostype('csr') +>>> x +<CSRNDArray 2x2 @cpu(0)> +>>> y = mx.nd.sparse.quadratic(x, a=1, b=2, c=0) +>>> y +<CSRNDArray 2x2 @cpu(0)> +>>> z = mx.nd.quadratic(x, a=1, b=2, c=3) +>>> z +[[ 3. 6.] + [ 11. 3.]] +<NDArray 2x2 @cpu(0)> +``` + +The statement `z = mx.nd.quadratic(x, a=1, b=2, c=3)` generates a warning message which says +the sparse input is converted to dense storage, and the dense operator is used to compute the dense output. +This is the "storage fallback" mechanism in MXNet, where a dense operator is automatically used for Review comment: The concept was introduced in the previous two sparse tutorials (in pre-requisite). I'll shorten the description and refer to the previous tutorials if the readers don't understand. ---------------------------------------------------------------- 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