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

[MXNET-323] Improve performance of broadcast ops backward pass #11252

Merged
merged 11 commits into from
Jul 13, 2018

Conversation

anirudh2290
Copy link
Member

Description

This PR tries to improve the performance of broadcast ops backward pass, by caching the intermediate computations and using LaunchEx. The speedup for both forward and backward pass combined for the broadcast_add is around 1.4X. The experiments have been run on p2.8xlarge.

The below numbers are for broadcasting a tensor of shape (1,) to a tensor of shape destination shape as given below.

Destination tensor shape Before the Change After the Change
100, 100 0.017 0.013
250, 250 0.085 0.065
500, 500 0.35 0.25
1000, 1000 1.34 0.98
2000, 2000 5.6 3.9
12000, 12000 188 144
2**17, 10, 10 18 12
import numpy as np
import mxnet as mx
import time

a = mx.sym.var('a')
b = mx.sym.var('b')

a_ = mx.nd.ones((2**17, 10, 10))
b_ = mx.nd.ones((1))

func2 = mx.sym.broadcast_add(a, b).bind(mx.cpu(), args={'a': a_, 'b': b_}, args_grad = {'a': mx.nd.ones((2**17, 10, 10)), 'b': mx.nd.ones((1))})

for _ in range(2):
    # boadcast_add(array, array)
    start = time.time()
    for i in range(100):
        out = func2.forward(is_train=True)[0]
        func2.backward(mx.nd.ones((2**17, 10, 10)))
    mx.nd.waitall()
    print("func2: {}".format(time.time() - start))

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:https://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

@anirudh2290 anirudh2290 changed the title [WIP] [MXNET-323] Improve performance of broadcast ops backward pass [MXNET-323] Improve performance of broadcast ops backward pass Jun 21, 2018
@anirudh2290
Copy link
Member Author

@piiswrong can you help take a look ?

@anirudh2290
Copy link
Member Author

@@ -602,6 +602,11 @@ void Reduce(Stream<gpu> *s, const TBlob& small, const OpReqType req,
ReduceImpl<Reducer, ndim, DType, OP>(stream, small, req, big, workspace, config);
}

template <typename Reducer, int ndim, typename DType, typename OP>
void ReduceWithExtraMem(Stream<cpu>* s, const TBlob& small, const OpReqType req,
const Tensor<cpu, 1, char>& workspace, const TBlob& big) {};
Copy link
Member

Choose a reason for hiding this comment

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

Empty implementation?

Copy link
Member Author

Choose a reason for hiding this comment

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

ReduceWithExtraMem is only used for the cpu implementation and prevents the build from failing.

Copy link
Member

Choose a reason for hiding this comment

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

binary_broadcast_op.h already included broadcast_reduce-inl.h. Why is it necessary to add this function here?

Copy link
Member Author

Choose a reason for hiding this comment

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

broadcast_reduce-inl.h includes the code in broadcast_reduce-inl.cuh or some part of code (including ReduceWithExtraMem) in broadcast_reduce-inl.h based on if CUDACC is defined. https://github.com/apache/incubator-mxnet/blob/master/src/operator/tensor/broadcast_reduce-inl.h#L171. This causes build to fail when omitting ReduceWithExtraMem in broadcast_reduce-inl.cuh.

template<typename xpu, typename LOP, typename ROP>
inline typename std::enable_if<std::is_same<xpu, gpu>::value, void>::type
BinaryBroadcastBackwardUseNone(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
Copy link
Member

Choose a reason for hiding this comment

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

nit: indentation

const Tensor<cpu, 1, char>& workspace, const TBlob& big) {
if (req == kNullOp) return;
Shape<ndim> rshape, rstride;
diff(small.shape_.get<ndim>(), big.shape_.get<ndim>(), &rshape, &rstride);
int N = small.shape_.Size(), M = rshape.Size();
seq_reduce_compute<Reducer, ndim, DType, OP>(
N, M, req == kAddTo, big.dptr<DType>(), small.dptr<DType>(),
big.shape_.get<ndim>(), small.shape_.get<ndim>(), rshape, rstride);
Copy link
Contributor

@haojin2 haojin2 Jul 3, 2018

Choose a reason for hiding this comment

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

nit: indentation should be 2 spaces?

template<typename xpu, typename LOP, typename ROP>
inline typename std::enable_if<std::is_same<xpu, gpu>::value, void>::type
BinaryBroadcastBackwardUseNone(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: alignment of lines

@@ -544,20 +545,25 @@ void BinaryBroadcastBackwardUseNone(const nnvm::NodeAttrs& attrs,
const TBlob out = inputs[0].reshape(new_oshape);
Copy link
Member

Choose a reason for hiding this comment

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

since this implementation is only for cpu, is it better to replace xpu with cpu inside?

@@ -602,6 +602,11 @@ void Reduce(Stream<gpu> *s, const TBlob& small, const OpReqType req,
ReduceImpl<Reducer, ndim, DType, OP>(stream, small, req, big, workspace, config);
}

template <typename Reducer, int ndim, typename DType, typename OP>
void ReduceWithExtraMem(Stream<cpu>* s, const TBlob& small, const OpReqType req,
const Tensor<cpu, 1, char>& workspace, const TBlob& big) {};
Copy link
Member

Choose a reason for hiding this comment

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

binary_broadcast_op.h already included broadcast_reduce-inl.h. Why is it necessary to add this function here?

@anirudh2290
Copy link
Member Author

@eric-haibin-lin I have addressed your comments.

@piiswrong piiswrong merged commit 32d298b into apache:master Jul 13, 2018
XinYao1994 pushed a commit to XinYao1994/incubator-mxnet that referenced this pull request Aug 29, 2018
…e#11252)

* Fix cached broadcast

* Fix

* Use seq_reduce_compute logic for stable sum

* Fix lint

* Add declarations

* Add elemwise binary broadcast op cuh file

* Add license for elemwise_binary_broadcast_op-inl.cuh

* Fix broadcast

* Fix indentation

* Use cpu and gpu instead of xpu
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants