-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Added PRelu BF16/FP32 FWD/BWD kernels #33878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b03f27d
added prelu bf16/fp32 fwd/bwd kernel
jakpiase b261b53
formated one file
jakpiase fce46aa
removed check_dygraph line
jakpiase a3c5d35
added check_dygraph line
jakpiase 6397227
added skipping if bf16 or cuda
jakpiase f690e65
minor change
jakpiase 62f2da9
minor change
jakpiase 5eb7bd0
added avoiding BWD pd creation in inference
jakpiase ae8dddf
minor change
jakpiase 890a2e6
added case for skipping dygraph check
jakpiase 23e93b7
added formatting
jakpiase 0d39c00
implemented reviewer's changes
jakpiase 3079690
minor change
jakpiase 3587da0
fixed dims
jakpiase cc699af
added skipping grad UT in cuda
jakpiase a602507
minor change
jakpiase b003a52
minor cchange
jakpiase 9dd9a21
minor change
jakpiase 3494fe7
Revert "minor change"
jakpiase 58c501f
temporarily disabled BF16 grad UT
jakpiase 3e24473
added more tests
jakpiase 6c377fb
minor change
jakpiase c98f76d
minor change
jakpiase be6c201
added back bf16 tests
jakpiase 01a06ee
disabed bf16 tests
jakpiase 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
| Licensed 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. */ | ||
|
|
||
| #include "paddle/fluid/platform/mkldnn_reuse.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| using dnnl::memory; | ||
| using framework::Tensor; | ||
| using platform::GetMKLDNNFormat; | ||
| using platform::MKLDNNDeviceContext; | ||
| using platform::MKLDNNGetDataType; | ||
| using platform::to_void_cast; | ||
|
|
||
| template <typename T> | ||
| class PReluMKLDNNHandler | ||
jakpiase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : public platform::MKLDNNHandlerT<T, dnnl::prelu_forward, | ||
| dnnl::prelu_backward> { | ||
| public: | ||
| PReluMKLDNNHandler(const MKLDNNDeviceContext& dev_ctx, | ||
| const mkldnn::engine engine, platform::Place cpu_place, | ||
| const Tensor* x, const Tensor* weights, | ||
| const std::string& uniq_name, bool is_test = false) | ||
| : platform::MKLDNNHandlerT<T, dnnl::prelu_forward, dnnl::prelu_backward>( | ||
| dev_ctx, engine, cpu_place, | ||
| platform::CreateKey(dev_ctx, framework::vectorize(x->dims()), | ||
| uniq_name)) { | ||
| if (!this->isCached()) { | ||
| auto x_md = memory::desc(framework::vectorize(x->dims()), | ||
| MKLDNNGetDataType<T>(), x->format()); | ||
| auto weights_md = | ||
| memory::desc(framework::vectorize(weights->dims()), | ||
| MKLDNNGetDataType<T>(), memory::format_tag::any); | ||
jakpiase marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| this->AcquireForwardPrimitiveDescriptor(dnnl::prop_kind::forward_training, | ||
| x_md, weights_md); | ||
| if (!is_test) | ||
| this->AcquireBackwardPrimitiveDescriptor(x_md, weights_md, x_md, | ||
| weights_md); | ||
| } | ||
| } | ||
|
|
||
| std::shared_ptr<memory> AcquireWeightsMemoryWithReorder(const Tensor* input, | ||
| const bool is_test) { | ||
| const T* input_data = input->data<T>(); | ||
| auto user_weights_md = | ||
| memory::desc(framework::vectorize(input->dims()), | ||
jakpiase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| MKLDNNGetDataType<T>(), input->format()); | ||
| return this->AcquireMemoryWithReorder( | ||
| user_weights_md, this->fwd_pd_->weights_desc(), | ||
| to_void_cast<T>(input_data), "@alpha_mem_p", is_test); | ||
| } | ||
|
|
||
| std::shared_ptr<memory> AcquireDiffWeightsMemory(Tensor* output) { | ||
| T* output_data = output->mutable_data<T>( | ||
| this->place_, this->bwd_pd_->diff_weights_desc().get_size()); | ||
| return this->AcquireMemoryFromPrimitive(this->bwd_pd_->diff_weights_desc(), | ||
| output_data, "@diff_weights_mem_p"); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class PReluMKLDNNKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const override { | ||
| this->RunKernel(ctx); | ||
| } | ||
|
|
||
| void RunKernel(const framework::ExecutionContext& ctx) const { | ||
jakpiase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const auto& dev_ctx = ctx.template device_context<MKLDNNDeviceContext>(); | ||
| const auto& onednn_engine = dev_ctx.GetEngine(); | ||
|
|
||
| const auto* x = ctx.Input<Tensor>("X"); | ||
| const auto* alpha = ctx.Input<Tensor>("Alpha"); | ||
| auto* out = ctx.Output<Tensor>("Out"); | ||
| const bool is_test = ctx.Attr<bool>("is_test"); | ||
|
|
||
| PReluMKLDNNHandler<T> handler(dev_ctx, onednn_engine, ctx.GetPlace(), x, | ||
| alpha, ctx.InputName("X"), is_test); | ||
|
|
||
| auto src_memory_p = handler.AcquireSrcMemory(x); | ||
| auto weights_memory_p = | ||
| handler.AcquireWeightsMemoryWithReorder(alpha, is_test); | ||
| auto dst_memory_p = handler.AcquireDstMemory(out); | ||
| auto prelu_p = handler.AcquireForwardPrimitive(); | ||
|
|
||
| auto& astream = MKLDNNDeviceContext::tls().get_stream(); | ||
| prelu_p->execute(astream, {{DNNL_ARG_SRC, *src_memory_p}, | ||
| {DNNL_ARG_WEIGHTS, *weights_memory_p}, | ||
| {DNNL_ARG_DST, *dst_memory_p}}); | ||
| astream.wait(); | ||
|
|
||
| out->set_layout(framework::DataLayout::kMKLDNN); | ||
| out->set_format(GetMKLDNNFormat(*dst_memory_p)); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class PReluGradMKLDNNKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const override { | ||
| this->RunKernel(ctx); | ||
| } | ||
|
|
||
| void RunKernel(const framework::ExecutionContext& ctx) const { | ||
| const auto& dev_ctx = ctx.template device_context<MKLDNNDeviceContext>(); | ||
| const auto& onednn_engine = dev_ctx.GetEngine(); | ||
|
|
||
| auto* x = ctx.Input<Tensor>("X"); | ||
| auto* dx = ctx.Output<Tensor>(framework::GradVarName("X")); | ||
| auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out")); | ||
| auto* dalpha = ctx.Output<Tensor>(framework::GradVarName("Alpha")); | ||
| auto* alpha = ctx.Input<Tensor>("Alpha"); | ||
| const bool is_test = ctx.Attr<bool>("is_test"); | ||
|
|
||
| PReluMKLDNNHandler<T> handler(dev_ctx, onednn_engine, ctx.GetPlace(), x, | ||
| alpha, framework::GradVarName("X")); | ||
|
|
||
| auto src_memory_p = handler.AcquireSrcMemory(x); | ||
| auto weights_memory_p = | ||
| handler.AcquireWeightsMemoryWithReorder(alpha, is_test); | ||
| auto diff_src_memory_p = handler.AcquireDiffSrcMemory(dx); | ||
| auto diff_weights_memory_p = handler.AcquireDiffWeightsMemory(dalpha); | ||
| auto diff_dst_memory_p = handler.AcquireDiffDstMemory(dout); | ||
| auto prelu_p = handler.AcquireBackwardPrimitive(); | ||
|
|
||
| auto& astream = MKLDNNDeviceContext::tls().get_stream(); | ||
| prelu_p->execute(astream, | ||
| {{DNNL_ARG_SRC, *src_memory_p}, | ||
| {DNNL_ARG_WEIGHTS, *weights_memory_p}, | ||
| {DNNL_ARG_DIFF_DST, *diff_dst_memory_p}, | ||
| {DNNL_ARG_DIFF_SRC, *diff_src_memory_p}, | ||
| {DNNL_ARG_DIFF_WEIGHTS, *diff_weights_memory_p}}); | ||
| astream.wait(); | ||
|
|
||
| dx->set_layout(framework::DataLayout::kMKLDNN); | ||
| dx->set_format(GetMKLDNNFormat(*diff_src_memory_p)); | ||
| } | ||
| }; | ||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
| REGISTER_OP_KERNEL(prelu, MKLDNN, paddle::platform::CPUPlace, | ||
| ops::PReluMKLDNNKernel<float>, | ||
| ops::PReluMKLDNNKernel<paddle::platform::bfloat16>); | ||
|
|
||
| REGISTER_OP_KERNEL(prelu_grad, MKLDNN, paddle::platform::CPUPlace, | ||
| ops::PReluGradMKLDNNKernel<float>, | ||
| ops::PReluGradMKLDNNKernel<paddle::platform::bfloat16>); | ||
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.