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

Accelerate ROIPooling layer #14894

Merged
merged 13 commits into from
Jul 12, 2019
Merged

Conversation

arcadiaphy
Copy link
Member

@arcadiaphy arcadiaphy commented May 6, 2019

Description

As title. The major changes in implementation:

  1. Use index_t to store max_idx, avoiding possible value change when casting int to float.
  2. Remove assert in code, fix a bug in processing invalid box
  3. Store global index of input data blob in max_idx to simplify grad accumulation in backward pass.

Benchmarking script

import mxnet as mx
import numpy as np
np.random.seed(0)
import time

batch_size = 4
channel = 16
height = 256
width = 256
n_rois = 500
pooled_size = (5, 5)

def test(ctx, tries=5):
    s = 0.
    for idx in xrange(tries + 1):
        x = np.random.random((batch_size, channel, height, width))
        y = np.zeros((n_rois, 5))
        y[:, 0] += np.floor(np.random.random((n_rois)) * batch_size)
        y[:, 1] += np.random.random((n_rois)) * width
        y[:, 2] += np.random.random((n_rois)) * height
        y[:, 3] += np.minimum(y[:, 1] + np.random.random((n_rois)) * width, width)
        y[:, 4] += np.minimum(y[:, 2] + np.random.random((n_rois)) * height, height)

        with ctx:
            x = mx.nd.array(x)
            y = mx.nd.array(y)
            mx.nd.waitall()
            start = time.time()
            with mx.autograd.record():
                x.attach_grad()
                r = mx.nd.ROIPooling(data=x, rois=y,
                                     spatial_scale=1,
                                     pooled_size=pooled_size)
                r.backward(mx.nd.ones_like(r))
            mx.nd.waitall()
            if idx > 0:
                s += time.time() - start
    print 'time: {} s on {}'.format(s / tries, ctx)

if __name__ == '__main__':
    test(mx.cpu())
    test(mx.gpu())

Result

Before:

time: 13.2959038258 s on cpu(0)
time: 0.0346892356873 s on gpu(0)

After:

time: 0.6734582901 s on cpu(0)
time: 0.0038733959198 s on gpu(0)

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

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

@anirudhacharya
Copy link
Member

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

@marcoabreu marcoabreu added the pr-awaiting-review PR is waiting for code review label May 6, 2019
@karan6181
Copy link
Contributor

@arcadiaphy Thanks for your contribution!
@sxjscience , @KellenSunderland Could you please review this PR? Thanks!

@piyushghai
Copy link
Contributor

@sxjscience @KellenSunderland Gentle ping...

@vandanavk
Copy link
Contributor

@mxnet-label-bot add [Operator]

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 improvement!

@wkcn
Copy link
Member

wkcn commented Jul 10, 2019

@sxjscience @KellenSunderland
Could you please help take a review? Thank you!

@sxjscience
Copy link
Member

I think it should be good. One concern is it may be backward incompatible due to the atomicAdd. Although I feel it's reasonable to use the faster version, I need to confirm with the GluonCV team whether it will break the current training scripts.

@sxjscience sxjscience merged commit 9c5acb4 into apache:master Jul 12, 2019
@zhreshold
Copy link
Member

Would be nice to apply to ROIAlign similarly since GluonCV have transitioned to use ROIAlign in recent networks.

@larroy
Copy link
Contributor

larroy commented Jul 12, 2019

This is great, is the performance increase only due to type changes as in the description?

@wkcn
Copy link
Member

wkcn commented Jul 13, 2019

@larroy I think the main reason is that the indices of the maximum numbers are saved in this PR.

@arcadiaphy arcadiaphy deleted the pr_pooling_refactor branch July 15, 2019 02:39
@KellenSunderland
Copy link
Contributor

LGTM. Many thanks for the speedup / refactor.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Operator pr-awaiting-review PR is waiting for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.