[MXNET-82] Sparse op tutorial for developers#10081
[MXNET-82] Sparse op tutorial for developers#10081eric-haibin-lin merged 1 commit intoapache:masterfrom
Conversation
| const nnvm::dim_t num_rows = output.shape()[0]; // 37 | ||
| output.CheckAndAlloc({Shape1(num_rows + 1), Shape1(nnz)}); // 38 | ||
| MSHADOW_TYPE_SWITCH(output.dtype(), DType, { // 39 | ||
| MSHADOW_TYPE_SWITCH(output.aux_type(kIdx), CType, { // 40 |
There was a problem hiding this comment.
Good catch! I'll update it
aaronmarkham
left a comment
There was a problem hiding this comment.
Some suggestions provided.
| infers the output storage type based on operator arguments and inputs types. Then we implement the forward | ||
| function for the case where c is 0.0 and x is a CSRNDArray. | ||
|
|
||
| Next, we are going to |
There was a problem hiding this comment.
Describe what we're generally going to do with the next bullets. Should you summarize the rest of the doc and include the unit test section?
"The next steps will go into detail on how to create a sparse operator in C++:"
There was a problem hiding this comment.
Good point. I'll add that
| @@ -0,0 +1,429 @@ | |||
| # A Guide to Implementing Sparse Operators in MXNet Backend | |||
There was a problem hiding this comment.
in C++ ?
Shouldn't you also add this to the tutorials page under the C++ section?
There was a problem hiding this comment.
I only found one tutorial under docs/tutorials/c++/basic.md. My understanding is that basic.md is for the cpp-package, one of the language bindings for MXNet, which belongs to the frontend. The readers would only care about how to use MXNet.
The tutorial I wrote is, however, for developers to get into the backend of MXNet. They build MXNet / add features to MXNet. I don't think it's reasonable to move it there.
If it has to be moved, I would move to some folder of tutorials for MXNet developers.
There was a problem hiding this comment.
oh, but anywhere than faq... the broad bucket makes it hard to find... it should go in a logical folder... even if you have to make a backend folder...
| <NDArray 2x2 @cpu(0)> | ||
| ``` | ||
|
|
||
| The statement `z = mx.nd.quadratic(x, a=1, b=2, c=3)` generates a warning message which says |
There was a problem hiding this comment.
Your output above didn't have a warning message...
There was a problem hiding this comment.
Yeah I could add that ...
|
|
||
| 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 |
There was a problem hiding this comment.
Reword? De-complicate.
There was a problem hiding this 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.
| # 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) |
There was a problem hiding this comment.
This moved:
https://mxnet.incubator.apache.org/faq/add_op_in_backend.html
And isn't really titled with "dense", but maybe it should be. Maybe rephrase to say that previously, backend ops were dense.
Also, I think you might want to update that page cross-referencing this one and introduce that you now have this dense v sparse option for ops. And also retitle it?
As a prerequisite I'm reading that as well and noted that it needs some updates too. I'll circle back on those as a comment to this PR.
There was a problem hiding this comment.
I would prefer updating the title of previous tutorial to be "dense operator" instead of "operator". @reminisce is that okay?
| const std::vector<OpReqType>& req, | ||
| const std::vector<NDArray>& outputs); | ||
| ``` | ||
| where the vectors of TBlobs are replaced with vectors of NDArrays. Now, let's go through a few important methods in the NDArray class. |
There was a problem hiding this comment.
Start a new sentence.
| ``` | ||
|
|
||
| ### Storage Type Inference | ||
| Storage type inference is the process of deducing storage types of `NDArray`s |
| python frontend so that `quadratic` is accessible from both `mx.symbol.sparse` | ||
| and `mx.ndarray.sparse`. | ||
| - .set_attr<FInferStorageType>("FInferStorageType", QuadraticOpStorageType) | ||
| This line register the storage type inference attribute of the operator. |
There was a problem hiding this comment.
Good catch. Thanks
| - .set_attr<FInferStorageType>("FInferStorageType", QuadraticOpStorageType) | ||
| This line register the storage type inference attribute of the operator. | ||
| - .set_attr<FComputeEx>("FComputeEx<cpu>", QuadraticOpForwardEx<cpu>) | ||
| This line register the `FComputeEx` attribute of the operator. |
| So far, only the forward operator supports sparse inputs. To add sparse support to the | ||
| backward operator, you also need to register these two attributes to `_backward_quadratic`: | ||
| - `FComputeEx` for sparse operator implementation | ||
| - `FInferStorage` for storage tyep inference in backward |
There was a problem hiding this comment.
fix type; add period or use semicolon before Due
in backward "what?"... the backward operator?
There was a problem hiding this comment.
Fixed type; added period. Updated to "backward computation"
|
Prerequisite add op in backend doc updates:
For my review on the tutorial for sparse ops, it looks like it was outdated before I finished it, so please take a look at all of my "outdated" comments! |
|
All how_to are moved to faq and we should not use how_to anymore. Is there a reason for updating how_to ? |
|
@aaronmarkham made updates according to your comments. Please review the most recent commit. I had a paragraph explaining the why the FCompute interface is limited and cannot be used for sparse ops. Does it make sense?
|
|
@thinksanky moved to faq now |
docs/faq/add_sparse_op_in_backend.md
Outdated
|
|
||
| Note that the statement `z = mx.nd.quadratic(x, a=1, b=2, c=3)` generates a warning message, saying that | ||
| a dense operator is used when the sparse operator doesn't support the above case. If you are not | ||
| familiar with the storag fallback mechanism, please revisit the tutorials for |
There was a problem hiding this comment.
Typo in "storag" -> "storage"
docs/faq/add_sparse_op_in_backend.md
Outdated
| ### Operator Registration | ||
| Finally let's extend the operator registration logic to expose `sparse.quadratic` | ||
| to frontend. Below is the extended registration code in `quadratic_op.cc`: | ||
| ```cpp |
There was a problem hiding this comment.
Having line numbers here will you to reference to them later, and help readers to understand the content.
There was a problem hiding this comment.
Sure I'll add it
docs/faq/add_sparse_op_in_backend.md
Outdated
| backward operator, you also need to register these two attributes to `_backward_quadratic`: | ||
| - `FComputeEx` for sparse operator implementation | ||
| - `FInferStorage` for storage type inference in the backward computation. | ||
| Due to length constraint, this is left as an exercise for readers. |
There was a problem hiding this comment.
Maybe write another tutorial on that? Having more ground truth implementations may increase number of contributions.
16f8d82 to
0306717
Compare
d7e8772 to
5f835b7
Compare
|
Guide added to the confluence page: |
This reverts commit a2c4b0a.
Description
A guide to implement sparse ops
requires #10091
Checklist
Essentials
make lint)Changes
Comments