This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
add mkldnn softmax_output #13699
Merged
Merged
add mkldnn softmax_output #13699
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0c81bf2
add mkldnn softmax_output
rongzha1 8cc2eec
fix gpu OP unittest error
rongzha1 ea81bbd
fix ci/jenkins/mxnet-validation/unix-gpu compiler error
rongzha1 403dc37
fix coding style
rongzha1 dbfff62
fix Tao comments
rongzha1 6e4e946
remove blank line, fix indentx
rongzha1 38ec708
modify according to sandeep's comments
rongzha1 37e9c11
change get CPU engine method, and pravate variable
rongzha1 d7af111
move macro MXNET_USE_MKLDNN to the head
rongzha1 58a23bd
modify according to Tao's comments
rongzha1 bbf11d7
make output layout as input
rongzha1 3420b61
change API of GetSoftmaxOutputForward
rongzha1 25162cb
add CommitOutput for mkldnn_softmax_output
rongzha1 171da54
trigger Jenkins re-test
rongzha1 c031f6b
add alias Softmax symbol for SoftmaxOutput OP
rongzha1 ee845f7
indent and remove blank line
rongzha1 0757669
Merge remote-tracking branch 'official/master' into mkldnn_softmax_ou…
rongzha1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file mkldnn_softmax_output.cc | ||
| * \brief integrate mkldnn softmax to softmax_output forward | ||
| * \author Zhang Rong A | ||
| */ | ||
|
|
||
| #if MXNET_USE_MKLDNN == 1 | ||
| #include "../../softmax_output-inl.h" | ||
| #include "./mkldnn_ops-inl.h" | ||
| #include "./mkldnn_base-inl.h" | ||
|
|
||
| namespace mxnet { | ||
| namespace op { | ||
|
|
||
| static mkldnn::softmax_forward::primitive_desc GetSoftmaxOutputFwdDescImpl( | ||
| const SoftmaxOutputParam& param, bool is_train, | ||
| const int axis, const mkldnn::memory &input_mem) { | ||
| mkldnn::memory::primitive_desc data_mpd = input_mem.get_primitive_desc(); | ||
| mkldnn::memory::desc data_md = data_mpd.desc(); | ||
| auto cpu_engine = CpuEngine::Get()->get_engine(); | ||
| auto prop = is_train ? mkldnn::prop_kind::forward_training | ||
| : mkldnn::prop_kind::forward_scoring; | ||
| auto desc = mkldnn::softmax_forward::desc(prop, data_md, axis); | ||
| return mkldnn::softmax_forward::primitive_desc(desc, cpu_engine); | ||
| } | ||
|
|
||
| typedef ParamOpSign<SoftmaxOutputParam> MKLDNNSoftmaxOuputSignature; | ||
|
|
||
| class MKLDNNSoftmaxOutputFwd { | ||
| std::shared_ptr<mkldnn::softmax_forward> fwd_; | ||
| std::shared_ptr<mkldnn::memory> data_; | ||
| std::shared_ptr<mkldnn::memory> out_; | ||
|
|
||
| public: | ||
| const mkldnn::softmax_forward::primitive_desc fwd_pd; | ||
|
|
||
| MKLDNNSoftmaxOutputFwd(const SoftmaxOutputParam& param, bool is_train, | ||
| const int axis, const mkldnn::memory &mem): fwd_pd( | ||
| GetSoftmaxOutputFwdDescImpl(param, is_train, axis, mem)) { | ||
| } | ||
|
|
||
| void SetNewMem(const mkldnn::memory &data, const mkldnn::memory &output) { | ||
| if (this->data_ == nullptr) | ||
| this->data_ = std::shared_ptr<mkldnn::memory>(new mkldnn::memory( | ||
| data.get_primitive_desc(), data.get_data_handle())); | ||
| else | ||
| this->data_->set_data_handle(data.get_data_handle()); | ||
|
|
||
| if (this->out_ == nullptr) | ||
| this->out_ = std::shared_ptr<mkldnn::memory>(new mkldnn::memory( | ||
| output.get_primitive_desc(), output.get_data_handle())); | ||
| else | ||
| this->out_->set_data_handle(output.get_data_handle()); | ||
|
|
||
| if (this->fwd_ == nullptr) { | ||
| this->fwd_ = std::shared_ptr<mkldnn::softmax_forward>( | ||
| new mkldnn::softmax_forward(fwd_pd, mkldnn::primitive::at(*this->data_), | ||
| *this->out_)); | ||
| } | ||
| } | ||
|
|
||
| const mkldnn::softmax_forward &GetFwd() const { | ||
| return *fwd_; | ||
| } | ||
| }; | ||
|
|
||
| static MKLDNNSoftmaxOutputFwd &GetSoftmaxOutputForward(const SoftmaxOutputParam& param, | ||
| const OpContext &ctx, | ||
| const NDArray &in_data) { | ||
| #if DMLC_CXX11_THREAD_LOCAL | ||
| static thread_local | ||
| std::unordered_map<MKLDNNSoftmaxOuputSignature, MKLDNNSoftmaxOutputFwd, OpHash> fwds; | ||
| #else | ||
| static MX_THREAD_LOCAL | ||
| std::unordered_map<MKLDNNSoftmaxOuputSignature, MKLDNNSoftmaxOutputFwd, OpHash> fwds; | ||
| #endif | ||
| MKLDNNSoftmaxOuputSignature key(param); | ||
| key.AddSign(ctx.is_train); | ||
| key.AddSign(in_data); | ||
|
|
||
| // softmax_output has no axis parameter, so use it as it original implement. | ||
| int axis = in_data.shape().ndim() - 1; | ||
|
|
||
| auto it = fwds.find(key); | ||
| if (it == fwds.end()) { | ||
| auto in_mem = *(in_data.GetMKLDNNData()); | ||
| MKLDNNSoftmaxOutputFwd fwd(param, ctx.is_train, axis, in_mem); | ||
| it = AddToCache(&fwds, key, fwd); | ||
| } | ||
| return it->second; | ||
| } | ||
|
|
||
| // This is only used for forward. For backward ,need double check compatibility | ||
| bool SupportMKLDNNSoftmaxOutput(const SoftmaxOutputParam ¶m) { | ||
| return param.multi_output ? false : true; | ||
| } | ||
|
|
||
| void MKLDNNSoftmaxOutputForward(const nnvm::NodeAttrs& attrs, | ||
| const OpContext &ctx, | ||
| const std::vector<NDArray> &in_data, | ||
| const std::vector<OpReqType> &req, | ||
| const std::vector<NDArray> &out_data) { | ||
| const SoftmaxOutputParam ¶m = nnvm::get<SoftmaxOutputParam>(attrs.parsed); | ||
|
|
||
| NDArray idata = in_data[softmaxout_enum::kData]; | ||
| NDArray odata = out_data[softmaxout_enum::kOut]; | ||
| if (in_data[softmaxout_enum::kData].IsView() && in_data[softmaxout_enum::kData].IsMKLDNNData()) { | ||
| idata = in_data[softmaxout_enum::kData].Reorder2Default(); | ||
| } | ||
|
|
||
| auto input_mem = idata.GetMKLDNNData(); | ||
| auto out_mem = CreateMKLDNNMem(out_data[softmaxout_enum::kOut], | ||
| input_mem->get_primitive_desc(), req[softmaxout_enum::kOut]); | ||
|
|
||
| MKLDNNSoftmaxOutputFwd &fwd = GetSoftmaxOutputForward(param, ctx, idata); | ||
| fwd.SetNewMem(*input_mem, *out_mem.second); | ||
|
|
||
| MKLDNNStream *stream = MKLDNNStream::Get(); | ||
| stream->RegisterPrim(fwd.GetFwd()); | ||
|
|
||
| CommitOutput(out_data[softmaxout_enum::kOut], out_mem); | ||
| stream->Submit(); | ||
| } | ||
| } // namespace op | ||
| } // namespace mxnet | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.