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

add mkldnn softmax backward #17170

Merged
merged 4 commits into from
Feb 9, 2020
Merged

add mkldnn softmax backward #17170

merged 4 commits into from
Feb 9, 2020

Conversation

rongzha1
Copy link
Contributor

@rongzha1 rongzha1 commented Dec 25, 2019

Description

add mkldnn softmax backward implementation
unitest pass

Should fix #13365

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

@PatricZhao @TaoLv

Copy link
Member

@TaoLv TaoLv left a comment

Choose a reason for hiding this comment

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

Please take a look at #17152 and cache primitive for backward pass. Thanks!

src/operator/nn/mkldnn/mkldnn_ops-inl.h Outdated Show resolved Hide resolved
src/operator/nn/mkldnn/mkldnn_softmax.cc Outdated Show resolved Hide resolved
{ MKLDNN_ARG_DIFF_SRC, *out_mem.second },
};

stream->RegisterPrimArgs(bwd_pd, args);
Copy link
Member

Choose a reason for hiding this comment

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

pd?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will change it to cache primitive pd.

Copy link
Member

Choose a reason for hiding this comment

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

I mean here you need give a primitive not a primitive descriptor. Please check the definition of RegisterPrimArgs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, you're right.
Although it is a pd, it call implicit constructor
inline primitive::primitive(const primitive_desc &pd) : primitive(pd.get()) {}
to get a primitive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will make it more clearly by adding constructor before used.

const std::vector<NDArray>& inputs,
const std::vector<OpReqType>& req,
const std::vector<NDArray>& outputs) {
// It seems MKLDNN softmax doesn't support training.
Copy link
Member

Choose a reason for hiding this comment

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

Can you elaborate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove this out-dated comments

inline static bool SoftmaxStorageType(const nnvm::NodeAttrs& attrs,
const int dev_mask,
DispatchMode* dispatch_mode,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
const SoftmaxParam& param = nnvm::get<SoftmaxParam>(attrs.parsed);
CHECK_EQ(in_attrs->size(), (param.use_length.value()) ? 2U : 1U);
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 this?

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 check will result in backward check fail.
Will recover this check and add another function for backward.

@TaoLv TaoLv added the MKLDNN label Dec 25, 2019
@pengzhao-intel pengzhao-intel added this to In progress in CPU Performance and Quantization via automation Dec 26, 2019
@pengzhao-intel
Copy link
Contributor

@rongzha1 please retrigger the PR @TaoLv could you help review again?

std::shared_ptr<mkldnn::softmax_backward> bwd_;
};

typedef ParamOpSign<SoftmaxParam> MKLDNNSoftmaxSignature;
Copy link
Member

Choose a reason for hiding this comment

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

Same as L99?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

exactly same, add extra one when rebase, will remove this line Thanks

void MKLDNNSoftmaxBackward(const nnvm::NodeAttrs& attrs,
const OpContext &ctx,
const std::vector<NDArray> &in_data,
const std::vector<OpReqType>& req,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const std::vector<OpReqType>& req,
const std::vector<OpReqType> &req,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

auto data_mem = in_data[1].GetMKLDNNData();
auto bwd = GetSoftmaxBwd(param, axis, in_data, out_data);

auto out_mem = CreateMKLDNNMem(out_data[0], bwd.pd.diff_src_desc(), req[0]);
Copy link
Member

Choose a reason for hiding this comment

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

  1. Please check if you want to support req=kAddTo;
  2. softmax backward primitive should support in-place calculation so no need to create additional buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right, create mem is used to support kAddTo

Copy link
Member

Choose a reason for hiding this comment

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

Does the original softmax backward support kAddTo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, original softmax bwd support kAddTo : SoftmaxGradCompute-->SoftmaxGrad -->KERNEL_ASSIGN(igrad[base + j*sa], Req, final_result);

@pengzhao-intel
Copy link
Contributor

@TaoLv were all concerns resolved now?

CPU Performance and Quantization automation moved this from In progress to Reviewer approved Feb 9, 2020
@TaoLv TaoLv merged commit 9883b99 into apache:master Feb 9, 2020
CPU Performance and Quantization automation moved this from Reviewer approved to Done Feb 9, 2020
zheyuye pushed a commit to zheyuye/incubator-mxnet that referenced this pull request Feb 19, 2020
* add mkldnn softmax backward

* add primitive cache for softmax bwd

* fix preci failed test

* rm duplicate line
anirudh2290 pushed a commit to anirudh2290/mxnet that referenced this pull request May 29, 2020
* add mkldnn softmax backward

* add primitive cache for softmax bwd

* fix preci failed test

* rm duplicate line
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Missing MKLDNN implementation for backward softmax operator
3 participants