Need a clear and thorough tutorial for writing custom operators in c++ with nnvm
This page gives a very simple tutorial for creating operators in c++. After reading, however, I'm still unclear how to write a complicated operator in c++.
Compared to other frameworks (like caffe, pytorch), learning how to create operators in c++ is indeed much harder for mxnet/nnvm. A lot of new concepts, classes and macros are involved, and not explained in detail:
- For Attribute Parser, what's NodeAttrs? What's the type of attrs->parsed? What field types are supported by DMLC_DECLARE_FIELD and how? Why I cannot find ParamParser and other classes in the C++ API?
- What does it mean by "Outputs can be made invisible to other operators"?
- For FGradient, what's NodePtr and NodeEntry? Why the prototype is so different from FCompute? No example is given for how to write a FGradient function and how to access NodePtr and NodeEntry.
- For FCompute, what's TBlob? What is the difference of TBlob and NDArray/mshadow::Tensor? How can I know which input blobs need gradient and how can I tell which output blobs need gradient?
- The existence of req argument in FCompute makes writing custom operators more difficult, although it may have some benefit for performance. Is it possible that I only handle the case of kWriteTo and the framework handle the other cases for me?
- What's auxiliary states and how to use that?
- How to declare backward dependency and request resource like the legacy operator interface?
- For python, what's the lifetime of an instance of operator? Will an instance of operator be called more than once in a forward pass of a graph? For c++, how can I implement a stateful operator that has some internal states (possibly a very complicated c++ data structure in cpu memory) that are accessed across many forward and backward passes (but do not need to be saved)? And how to initialize those states?
Overall, the nnvm c++ API for creating custom operators is really complicated, especially compared to caffe. A thorough tutorial is required. Or better, simplify the API and make it easier to understand.
Thank you.
It's easier to use the legacy interface for complex operators.
@reminisce Let's update the tutorial later
Yeah, I'm working on the tutorial pr right now. It is also expected to serve as a prerequisite tutorial for contributing sparse operators.
@piiswrong but the operator interface is legacy. Will it be deprecated and removed in near future?
@x10000year I have submitted a PR for the tutorial of implementing operators in MXNet backend. Hope it could answer some your questions. Your comments and suggestions are welcome on the PR. Thanks. https://github.com/apache/incubator-mxnet/pull/7828
Hi, I'm writing a custom stateful op in c++ using the new interface, but I can't find how to share the state of a forward op with its gradient partner. Any tips for solving this? Thanks
@apache/mxnet-committers: This issue has been inactive for the past 90 days. It has no label and needs triage.
For general "how-to" questions, our user forum (and Chinese version) is a good place to get help.
@reminisce @cjolivier01?
@zheng-da Can you describe how batch norm caches its aux values (running mean and variance) between forward and backward passes (see above)?
just saw the comment. I'll update the tutorial
@zheng-da can we close out this issue now? thanks
@sandeep-krishnamurthy @yzhliu this issue was resolved as part of https://github.com/apache/incubator-mxnet/pull/7828. Good to close, thanks!