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

Fix reshape to add in-place back #14903

Merged
merged 13 commits into from
May 14, 2019
Merged

Conversation

TaoLv
Copy link
Member

@TaoLv TaoLv commented May 7, 2019

Description

Previously in-place option was removed when build with MKL-DNN. It makes reshape operator taking much time in mxnet-mkl package. This PR adds the in-pace option back and uses a temporal buffer for reordering when the input is a MKL-DNN layout.

Besides, this PR also adds a unit test from #14766

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

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

@pengzhao-intel
Copy link
Contributor

@TaoLv please share the performance data too

@pengzhao-intel pengzhao-intel added this to Review in progress in CPU Performance and Quantization May 7, 2019
@TaoLv
Copy link
Member Author

TaoLv commented May 7, 2019

I use the code snippet below for benchmarking. Transpose is added before and after reshape operator in order to make it in-placed.

import time
import mxnet as mx
from mxnet import Context

data = mx.symbol.Variable('data')
res = mx.symbol.transpose(data=data, axes=(1, 0, 2, 3))
res = mx.symbol.reshape(data=res, shape=(-1, 16, 16, 16))
res = mx.symbol.transpose(data=res, axes=(1, 0, 2, 3))

shape = (64, 4, 16, 16)
exe = res.simple_bind(Context.default_ctx, data=shape, grad_req='null')


tic = 0
for i in range(205):
    if i == 5:
        mx.profiler.set_config(profile_symbolic=True, profile_imperative=True, profile_api=False, profile_memory=False, aggregate_stats=True, filename='profile_output.json')
        mx.profiler.set_state('run')
        tic = time.time()
    q = exe.forward(is_train=False)
    q[0].wait_to_read()

toc = time.time()
print("total time: %f ms " % ((toc - tic) * 1000))
mx.profiler.set_state('stop')
print(mx.profiler.dumps())

With mxnet-mkl==1.5.0b20190506, profile as following:

Profile Statistics.
        Note that counter items are counter values and not time units.
operator
=================
Name                          Total Count        Time (ms)    Min Time (ms)    Max Time (ms)    Avg Time (ms)
----                          -----------        ---------    -------------    -------------    -------------
WaitForVar                            400           1.2550           0.0020           0.0080           0.0031
Reshape                               400           7.5040           0.0150           0.0320           0.0188
transpose                             800           7.0800           0.0070           0.0260           0.0088

With this PR, reshape time is reduce from 7.50 ms to 1.73 ms.

Profile Statistics.
        Note that counter items are counter values and not time units.
operator
=================
Name                          Total Count        Time (ms)    Min Time (ms)    Max Time (ms)    Avg Time (ms)
----                          -----------        ---------    -------------    -------------    -------------
WaitForVar                            400           1.2640           0.0020           0.0080           0.0032
Reshape                               400           1.7300           0.0030           0.0110           0.0043
transpose                             800           7.9200           0.0060           0.0330           0.0099

@TaoLv TaoLv requested a review from szha May 7, 2019 09:48
@vandanavk
Copy link
Contributor

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

@mseth10 for review

@marcoabreu marcoabreu added MKLDNN pr-awaiting-review PR is waiting for code review labels May 7, 2019
Copy link
Contributor

@pengzhao-intel pengzhao-intel left a comment

Choose a reason for hiding this comment

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

Minor comments.

prims_.push_back(mkldnn::reorder(*data_, *temp_));
prims_.push_back(mkldnn::reorder(*temp_, *out_));
needInvalidateInput = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the situation of else?

Copy link
Contributor

@pengzhao-intel pengzhao-intel left a comment

Choose a reason for hiding this comment

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

Thanks for the explanations. Will merge the PR after CI pass.

CPU Performance and Quantization automation moved this from Review in progress to Reviewer approved May 9, 2019
@pengzhao-intel
Copy link
Contributor

The PR is verified internally with performance and accuracy.
Merging now. Thanks for your contribution.

@pengzhao-intel pengzhao-intel merged commit 13d6ee6 into apache:master May 14, 2019
CPU Performance and Quantization automation moved this from Reviewer approved to Done May 14, 2019
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* fix reshape

* save

* fix reshape

* clean code

* fix memory allocation & lint

* add unit test

* req type

* add comments to describe the logic
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
MKLDNN pr-awaiting-review PR is waiting for code review
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants