Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Update CustomOp doc with changes for GPU support #17486

Merged
merged 9 commits into from
Feb 16, 2020

Conversation

rondogency
Copy link
Contributor

Description

Improve the custom operator example doc by adding how to write GPU custom operator.

This is a continuation of the CustomOp tutorial doc #17241

This PR is not

  • Updating docs on the MXNet website on extending MXNet
  • Consolidating operator related docs on the website

Those will be addressed in the coming PRs

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

example/extensions/lib_custom_op/README.md Outdated Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Outdated Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Outdated Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Outdated Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Show resolved Hide resolved
example/extensions/lib_custom_op/README.md Show resolved Hide resolved
@samskalicky
Copy link
Contributor

@rondogency please add the fix for the gpu example test program here from #17270 (comment)

@rondogency
Copy link
Contributor Author

@samskalicky the fix is in #17516 and you can review it now

Copy link
Contributor

@aaronmarkham aaronmarkham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronmarkham aaronmarkham self-requested a review February 6, 2020 18:51
@samskalicky
Copy link
Contributor

@aaronmarkham it looks fine to me here:
https://github.com/apache/incubator-mxnet/blob/9b93000c4a8f8da05e51547312bd605ec3798b3f/example/extensions/lib_custom_op/README.md

Looks like its just weird GitHub source-diff showing odd italics.

@a550461053
Copy link

Hi, @rondogency I am trying to use the dynamic operator and have some questions.
How can we use mxnet::NDArray or mshadow_op used in xxx-inl.h of mxnet-backend operator? And I want to know how to use the mutateInputs function like in BatchNorm-like operators.
Thank you very much.

@samskalicky
Copy link
Contributor

Hi, @rondogency I am trying to use the dynamic operator and have some questions.
How can we use mxnet::NDArray or mshadow_op used in xxx-inl.h of mxnet-backend operator? And I want to know how to use the mutateInputs function like in BatchNorm-like operators.
Thank you very much.

Hi @a550461053, MXNet custom operators have been designed not to integrate with any MXNet backend classes (mxnet::NDArray) to simplify building. We've designed MXTensor to be equivalent. Is there some other functionality you were wanting from NDArray thats not in MXTensor?

The mutateInputs API allows you to do the same as the backend APIs, so you can specify the index of the input that you want to be mutable to make a BatchNorm-like operator.

@a550461053
Copy link

Hi, @rondogency I am trying to use the dynamic operator and have some questions.
How can we use mxnet::NDArray or mshadow_op used in xxx-inl.h of mxnet-backend operator? And I want to know how to use the mutateInputs function like in BatchNorm-like operators.
Thank you very much.

Hi @a550461053, MXNet custom operators have been designed not to integrate with any MXNet backend classes (mxnet::NDArray) to simplify building. We've designed MXTensor to be equivalent. Is there some other functionality you were wanting from NDArray thats not in MXTensor?

The mutateInputs API allows you to do the same as the backend APIs, so you can specify the index of the input that you want to be mutable to make a BatchNorm-like operator.

Ok, thank you. I want to create a custom operator calling another operator which input NDArray. Both operator is async pushed to engine, I think this way is possible. Also, I am not clear on using mutateInputs API, how and when to use it? If you can provide a example of the mutateInputs API, I will be grateful to you~

@samskalicky
Copy link
Contributor

Ok, thank you. I want to create a custom operator calling another operator which input NDArray. Both operator is async pushed to engine, I think this way is possible. Also, I am not clear on using mutateInputs API, how and when to use it? If you can provide a example of the mutateInputs API, I will be grateful to you~

Hi @a550461053 currently the custom operator design focused on separating the custom operator from the MXNet backend source code complexity. This means that your custom operator can (must) be entirely separated from MXNet. So you cannot call a regular built-in MXNet operator from your custom operator. We have an item here #17006 for adding support in the future to be able to do this, but it is not implemented yet.

As for how to use mutateInputs, it works exactly as the doc describes:

This function allows you to mark some inputs to be mutable inputs. It is useful when using aux parameters for BatchNorm-like operators.

So lets say you have an operator with 5 inputs, you can mark the indices of the inputs that you want to be mutable like this (for example mark the last two inputs as mutable):

MXReturnValue batchNorm_mutateInputs(std::map<std::string, std::string> attrs,
                                     std::vector<int> &input_indices) {
  // mark mutable inputs                                                                        
  input_indices.push_back(3);
  input_indices.push_back(4);
  return MX_SUCCESS;
}

@rondogency
Copy link
Contributor Author

rondogency commented Feb 13, 2020

@a550461053 Thanks for the questions. Currently you cannot call MXNet built-in operators from a custom operator. We are making a plan for adding this support, but it cannot be easily implemented and may be a separate project. Also I added a snippet of how to use mutable input, hope it would help.

Copy link
Contributor

@samskalicky samskalicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @rondogency!

@a550461053
Copy link

Ok, thank you. I want to create a custom operator calling another operator which input NDArray. Both operator is async pushed to engine, I think this way is possible. Also, I am not clear on using mutateInputs API, how and when to use it? If you can provide a example of the mutateInputs API, I will be grateful to you~

Hi @a550461053 currently the custom operator design focused on separating the custom operator from the MXNet backend source code complexity. This means that your custom operator can (must) be entirely separated from MXNet. So you cannot call a regular built-in MXNet operator from your custom operator. We have an item here #17006 for adding support in the future to be able to do this, but it is not implemented yet.

As for how to use mutateInputs, it works exactly as the doc describes:

This function allows you to mark some inputs to be mutable inputs. It is useful when using aux parameters for BatchNorm-like operators.

So lets say you have an operator with 5 inputs, you can mark the indices of the inputs that you want to be mutable like this (for example mark the last two inputs as mutable):

MXReturnValue batchNorm_mutateInputs(std::map<std::string, std::string> attrs,
                                     std::vector<int> &input_indices) {
  // mark mutable inputs                                                                        
  input_indices.push_back(3);
  input_indices.push_back(4);
  return MX_SUCCESS;
}

Thank you and @rondogency . I am clear on use of mutateInputs API.
But now I can only build mxnet to create c++ customOp and I have a problem on compiling into python wheel at #17577

@wkcn wkcn added the pr-awaiting-merge Review and CI is complete. Ready to Merge label Feb 14, 2020
@samskalicky
Copy link
Contributor

samskalicky commented Feb 15, 2020

But now I can only build mxnet to create c++ customOp and I have a problem on compiling into python wheel at #17577

@a550461053 just to clarify, you do not need to recompile MXNet from source to use a custom operator, you can dynamically load your library into MXNet at runtime using one of the nightly builds from here: https://repo.mxnet.io/dist/index.html

@samskalicky
Copy link
Contributor

@aaronmarkham or @wkcn can we merge this please?

@wkcn wkcn merged commit 99d5773 into apache:master Feb 16, 2020
@wkcn
Copy link
Member

wkcn commented Feb 16, 2020

The PR has been merged. Thanks for your contribution! : )

@rondogency rondogency deleted the custom_op_doc_gpu branch February 17, 2020 01:46
zheyuye pushed a commit to zheyuye/incubator-mxnet that referenced this pull request Feb 19, 2020
* add gpu doc portion

* resolve sam comments

* add lib_api and resolve comments

* resolve sam comments

* try remove italic

* fix typo and add mutable example

* retrigger ci
anirudh2290 pushed a commit to anirudh2290/mxnet that referenced this pull request May 29, 2020
* add gpu doc portion

* resolve sam comments

* add lib_api and resolve comments

* resolve sam comments

* try remove italic

* fix typo and add mutable example

* retrigger ci
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants