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

broadcast_axis optimization #17091

Merged
merged 4 commits into from
Dec 27, 2019
Merged

Conversation

TaoLv
Copy link
Member

@TaoLv TaoLv commented Dec 16, 2019

Description

Improve the performance of single axis broadcasting.

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

@TaoLv
Copy link
Member Author

TaoLv commented Dec 21, 2019

On my machine, before optimization:

broadcast (1,) to (128,), time: 0.06958 ms
broadcast (1, 128) to (128, 128), time: 0.07495 ms
broadcast (1, 256) to (256, 256), time: 0.07513 ms
broadcast (1, 512) to (512, 512), time: 0.13259 ms
broadcast (1, 1024) to (1024, 1024), time: 0.39795 ms
broadcast (1, 16383) to (24, 16383), time: 0.17657 ms
broadcast (1, 65535) to (24, 65535), time: 0.57813 ms
broadcast (1, 256, 256) to (24, 256, 256), time: 0.57402 ms
broadcast (256, 1, 256) to (256, 24, 256), time: 1.51982 ms

After optimization:

broadcast (1,) to (128,), time: 0.06901 ms
broadcast (1, 128) to (128, 128), time: 0.07477 ms
broadcast (1, 256) to (256, 256), time: 0.07464 ms
broadcast (1, 512) to (512, 512), time: 0.08006 ms
broadcast (1, 1024) to (1024, 1024), time: 0.07419 ms
broadcast (1, 16383) to (24, 16383), time: 0.07501 ms
broadcast (1, 65535) to (24, 65535), time: 0.08144 ms
broadcast (1, 256, 256) to (24, 256, 256), time: 0.08710 ms
broadcast (256, 1, 256) to (256, 24, 256), time: 0.07697 ms

Using below script:

import time
import mxnet as mx

src_shape = [(1, ), (1, 128), (1, 256), (1, 512), (1, 1024), (1, 16383), (1, 65535), (1, 256, 256), (256, 1, 256)]
dst_shape = [(128,), (128, 128), (256, 256), (512, 512), (1024, 1024), (24, 16383), (24, 65535), (24, 256, 256), (256, 24, 256)]

axes = [0, 0, 0, 0, 0, 0, 0, 0, 1]

for idx, sh in enumerate(src_shape):
    size = dst_shape[idx][axes[idx]]

    # mxnet
    x = mx.random.uniform(shape=sh)
    y = mx.nd.broadcast_axis(x, axis=axes[idx], size=size)
    y.wait_to_read()

    tic = time.time()
    for _ in range(3000):
        y = mx.nd.broadcast_axis(x, axis=axes[idx], size=size)
        y.wait_to_read()

    mx_time = time.time() - tic

    print("broadcast %s to %s, time: %.5f ms" %(sh, dst_shape[idx], mx_time*1000/3000.0))

@TaoLv
Copy link
Member Author

TaoLv commented Dec 21, 2019

@sxjscience Please take a look. Thanks!

const std::vector<TBlob>& outputs) {
using namespace mshadow;
const BroadcastAxesParam& param = nnvm::get<BroadcastAxesParam>(attrs.parsed);
if (param.axis.ndim() == 1 && inputs[0].shape_[param.axis[0]] == 1 && req[0] == kWriteTo) {
Copy link
Member

Choose a reason for hiding this comment

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

We should deal with negative value in param.axis.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems broadcast_axis doesn't support negative axis even before this change. See https://github.com/apache/incubator-mxnet/blob/master/src/operator/tensor/broadcast_reduce_op.h#L386.

import mxnet as mx

a = mx.random.uniform(shape=(4, 5, 1))
b = mx.nd.broadcast_axis(a, axis=-1, size=6)
print(b)

Will cause error:

Traceback (most recent call last):
  File "test_bcast.py", line 4, in <module>
    b = mx.nd.broadcast_axis(a, axis=-1, size=6)
  File "<string>", line 60, in broadcast_axis
  File "/home/lvtao/miniconda3/envs/mxnet/lib/python3.6/site-packages/mxnet/_ctypes/ndarray.py", line 107, in _imperative_invoke
    ctypes.byref(out_stypes)))
  File "/home/lvtao/miniconda3/envs/mxnet/lib/python3.6/site-packages/mxnet/base.py", line 278, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [18:53:34] include/mxnet/./tuple.h:206: Check failed: i >= 0 && i < ndim(): index = -1 must be in range [0, 3)

Copy link
Member

Choose a reason for hiding this comment

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

Okay, then LGTM.

@sxjscience sxjscience merged commit 2ad3ce4 into apache:master Dec 27, 2019
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.

2 participants