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

[performance] Avoid uneccesary vector copies in imperative_utils.cc #14665

Merged
merged 1 commit into from
Apr 16, 2019

Conversation

larroy
Copy link
Contributor

@larroy larroy commented Apr 10, 2019

Description

see title

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 http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

@larroy larroy changed the title Avoid uneccesary vector copies in imperative_utils.cc [performance] Avoid uneccesary vector copies in imperative_utils.cc Apr 10, 2019
@larroy
Copy link
Contributor Author

larroy commented Apr 10, 2019

@mxnet-label-bot add [performance,pr-awaiting-review]

@marcoabreu marcoabreu added Performance pr-awaiting-review PR is waiting for code review labels Apr 10, 2019
@larroy
Copy link
Contributor Author

larroy commented Apr 10, 2019

@mxnet-label-bot add [backend,memory]

@marcoabreu marcoabreu added Backend Issues related to the backend of MXNet Memory labels Apr 10, 2019
inline std::vector<NDArray*> NodeInputs(const nnvm::IndexedGraph& idx,
const int node_idx,
const std::vector<NDArray*> arrays) {
std::vector<NDArray*> NodeInputs(const nnvm::IndexedGraph& idx,
Copy link
Member

Choose a reason for hiding this comment

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

Can we pass in the returned vector std::vector<NDArray*> so we can save another copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would be unnecessary and non idiomatic. Return value optimization (RVO) will kick in and eliminate any extra copies on return values.

https://shaharmike.com/cpp/rvo/

inline std::vector<NDArray*> NodeOutputs(const nnvm::IndexedGraph& idx,
const int node_idx,
const std::vector<NDArray*> arrays) {
std::vector<NDArray*> NodeOutputs(const nnvm::IndexedGraph& idx,
Copy link
Member

Choose a reason for hiding this comment

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

Can we pass in the returned vector std::vector<NDArray*> so we can save another copy?

Copy link
Member

Choose a reason for hiding this comment

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

I think there will be not any another copy because of the right-value reference of std::vector.

inline std::vector<OpReqType> NodeReq(const nnvm::IndexedGraph& idx,
const int node_idx,
const std::vector<OpReqType> array_reqs) {
std::vector<OpReqType> NodeReq(const nnvm::IndexedGraph& idx,
Copy link
Member

Choose a reason for hiding this comment

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

Can we pass in the returned vector std::vector<OpReqType> so we can save another copy?


inline std::vector<NDArray*> NodeInputs(const nnvm::IndexedGraph& idx,
Copy link
Member

Choose a reason for hiding this comment

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

Why remove inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the point of inline here? These are private functions so they are moved to anonymous namespace. Let the compiler do the inline as the compiler is better at this than us. We are abusing inline in the codebase.

https://stackoverflow.com/questions/1932311/when-to-use-inline-function-and-when-not-to-use-it

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we measure the impact here? Because this logic is part of core engine and gets exposed to every job, I was wondering if it would be good to measure the performance impact.

Copy link
Member

@yuxihu yuxihu left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@wkcn wkcn left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for the fix!

@larroy
Copy link
Contributor Author

larroy commented Apr 15, 2019

@mxnet-label-bot add [pr-awaiting-merge]

@marcoabreu marcoabreu added the pr-awaiting-merge Review and CI is complete. Ready to Merge label Apr 15, 2019
@larroy
Copy link
Contributor Author

larroy commented Apr 15, 2019

@mxnet-label-bot remove [pr-awaiting-review]

@marcoabreu marcoabreu removed the pr-awaiting-review PR is waiting for code review label Apr 15, 2019
@wkcn wkcn merged commit 413fe97 into apache:master Apr 16, 2019
@wkcn
Copy link
Member

wkcn commented Apr 16, 2019

Merged. Thank you!

@sandeep-krishnamurthy
Copy link
Contributor

@larroy - I had quick question on this PR - Can we measure the impact here?

kedarbellare pushed a commit to kedarbellare/incubator-mxnet that referenced this pull request Apr 20, 2019
@larroy
Copy link
Contributor Author

larroy commented Apr 22, 2019

@sandeep-krishnamurthy Do we have some default go-to performance metric for our countributions? I find this would be very useful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Backend Issues related to the backend of MXNet Memory Performance pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants