mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Need a clear and thorough tutorial for writing custom operators in c++ with nnvm

Open x10000year opened this issue 9 years ago • 11 comments

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:

  1. 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?
  2. What does it mean by "Outputs can be made invisible to other operators"?
  3. 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.
  4. 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?
  5. 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?
  6. What's auxiliary states and how to use that?
  7. How to declare backward dependency and request resource like the legacy operator interface?
  8. 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.

x10000year avatar Aug 29 '17 10:08 x10000year

It's easier to use the legacy interface for complex operators.

@reminisce Let's update the tutorial later

piiswrong avatar Aug 30 '17 06:08 piiswrong

Yeah, I'm working on the tutorial pr right now. It is also expected to serve as a prerequisite tutorial for contributing sparse operators.

reminisce avatar Aug 30 '17 06:08 reminisce

@piiswrong but the operator interface is legacy. Will it be deprecated and removed in near future?

x10000year avatar Aug 30 '17 08:08 x10000year

@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

reminisce avatar Sep 10 '17 04:09 reminisce

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

YANG-H avatar Oct 24 '17 08:10 YANG-H

@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.

szha avatar Jan 23 '18 12:01 szha

@reminisce @cjolivier01?

nswamy avatar Mar 20 '18 22:03 nswamy

@zheng-da Can you describe how batch norm caches its aux values (running mean and variance) between forward and backward passes (see above)?

cjolivier01 avatar Mar 20 '18 22:03 cjolivier01

just saw the comment. I'll update the tutorial

zheng-da avatar Apr 20 '18 07:04 zheng-da

@zheng-da can we close out this issue now? thanks

sojiadeshina avatar Jul 30 '18 18:07 sojiadeshina

@sandeep-krishnamurthy @yzhliu this issue was resolved as part of https://github.com/apache/incubator-mxnet/pull/7828. Good to close, thanks!

thomelane avatar Aug 15 '18 22:08 thomelane