-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Added cast op oneDNN kernel for bf16/fp32 datatypes casting(FWD/BWD) #33056
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
85e5165
added op cast functionality for fp32/bf16
jakpiase d9403c0
added newline
jakpiase 8753181
added entries in static mode white list and unity build
jakpiase ce30ebb
fixed failing tests
jakpiase a37ca86
changes after review
jakpiase be9e734
added formatting
jakpiase ada7c31
upgraded tests file as reviewer suggested
jakpiase 5af7681
changes after review
jakpiase 7bcee8e
minor change
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,73 @@ | ||
| /* 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 paddle::framework::Tensor; | ||
|
|
||
| template <typename T> | ||
| class CastMKLDNNKernel : 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<platform::MKLDNNDeviceContext>(); | ||
|
|
||
| auto* x = ctx.Input<Tensor>("X"); | ||
| auto* out = ctx.Output<Tensor>("Out"); | ||
|
|
||
| int in_dtype = ctx.Attr<int>("in_dtype"); | ||
| int out_dtype = ctx.Attr<int>("out_dtype"); | ||
|
|
||
| auto x_paddle_type = framework::proto::VarType::Type(in_dtype); | ||
| auto out_paddle_type = framework::proto::VarType::Type(out_dtype); | ||
|
|
||
| mkldnn::memory::data_type x_type = | ||
| framework::ToMKLDNNDataType(x_paddle_type); | ||
| mkldnn::memory::data_type out_type = | ||
| framework::ToMKLDNNDataType(out_paddle_type); | ||
|
|
||
| auto x_tz = framework::vectorize(x->dims()); | ||
|
|
||
| std::string key = | ||
| platform::CreateKey(dev_ctx, x_tz, x->format(), x->format(), x_type); | ||
| platform::ReorderMKLDNNHandler reorder_handler( | ||
| x_tz, x_paddle_type, x_type, out_paddle_type, out_type, dev_ctx, | ||
| dev_ctx.GetEngine(), key); | ||
|
|
||
| auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( | ||
| x->format(), platform::to_void_cast(x->data<T>())); | ||
| auto reorder_dst_memory_p = | ||
| reorder_handler.AcquireDstMemory(out, x->format(), dev_ctx.GetPlace()); | ||
| auto reorder_p = reorder_handler.AcquireReorder(reorder_dst_memory_p, | ||
| reorder_src_memory_p); | ||
|
|
||
| auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); | ||
| reorder_p->execute(astream, *reorder_src_memory_p, *reorder_dst_memory_p); | ||
| astream.wait(); | ||
|
|
||
| out->set_layout(framework::DataLayout::kMKLDNN); | ||
| out->set_format(platform::GetMKLDNNFormat(*reorder_dst_memory_p)); | ||
| } | ||
| }; | ||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
| REGISTER_OP_KERNEL(cast, MKLDNN, paddle::platform::CPUPlace, | ||
| ops::CastMKLDNNKernel<float>, | ||
| ops::CastMKLDNNKernel<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
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
78 changes: 78 additions & 0 deletions
78
python/paddle/fluid/tests/unittests/mkldnn/test_cast_mkldnn_op.py
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,78 @@ | ||||||||||||||||||||||||||||||||
| # 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. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| from __future__ import print_function | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import unittest | ||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import paddle | ||||||||||||||||||||||||||||||||
| import paddle.fluid.core as core | ||||||||||||||||||||||||||||||||
| import paddle.fluid as fluid | ||||||||||||||||||||||||||||||||
| from paddle.fluid import compiler, Program, program_guard | ||||||||||||||||||||||||||||||||
| from paddle.fluid.tests.unittests.op_test import OpTest, convert_float_to_uint16 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @unittest.skipIf(not core.supports_bfloat16(), | ||||||||||||||||||||||||||||||||
| "place does not support BF16 evaluation") | ||||||||||||||||||||||||||||||||
| class TestCastBF16ToFP32MKLDNNOp(OpTest): | ||||||||||||||||||||||||||||||||
jakpiase marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
| def init_data(self): | ||||||||||||||||||||||||||||||||
| self.out = np.random.random(size=[10, 10]).astype("float32") | ||||||||||||||||||||||||||||||||
| self.x = convert_float_to_uint16(self.out) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def setUp(self): | ||||||||||||||||||||||||||||||||
| self.init_data() | ||||||||||||||||||||||||||||||||
| self.inputs = {'X': self.x} | ||||||||||||||||||||||||||||||||
| self.outputs = {'Out': self.out} | ||||||||||||||||||||||||||||||||
| prepare_dtype = lambda x: int(core.VarDesc.VarType.BF16 if x.dtype != np.float32 else core.VarDesc.VarType.FP32) | ||||||||||||||||||||||||||||||||
| self.attrs = { | ||||||||||||||||||||||||||||||||
| 'in_dtype': prepare_dtype(self.x), | ||||||||||||||||||||||||||||||||
| 'out_dtype': prepare_dtype(self.out), | ||||||||||||||||||||||||||||||||
| 'use_mkldnn': True | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| self.op_type = 'cast' | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def test_check_output(self): | ||||||||||||||||||||||||||||||||
| self.check_output(check_dygraph=False) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def test_check_grad(self): | ||||||||||||||||||||||||||||||||
| self.check_grad_with_place( | ||||||||||||||||||||||||||||||||
| core.CPUPlace(), ["X"], | ||||||||||||||||||||||||||||||||
| "Out", | ||||||||||||||||||||||||||||||||
| check_dygraph=False, | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paddle/python/paddle/fluid/tests/unittests/op_test.py Lines 1076 to 1090 in 394d8d4
since you add check_dygraph = False when bf16_op in check_output_with_place, could you add same code in check_grad_with_place? Thus, you can decrease one approve
@jakpiase @lidanqing-intel @jczaja @wzzju Please note this. |
||||||||||||||||||||||||||||||||
| user_defined_grads=[self.inputs['X']], | ||||||||||||||||||||||||||||||||
| user_defined_grad_outputs=[self.outputs['Out']]) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class TestCastFP32ToBF16MKLDNNOp(TestCastBF16ToFP32MKLDNNOp): | ||||||||||||||||||||||||||||||||
| def init_data(self): | ||||||||||||||||||||||||||||||||
| self.x = np.random.random(size=[2, 6]).astype("float32") | ||||||||||||||||||||||||||||||||
| self.out = convert_float_to_uint16(self.x) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class TestCastBF16ToBF16MKLDNNOp(TestCastBF16ToFP32MKLDNNOp): | ||||||||||||||||||||||||||||||||
| def init_data(self): | ||||||||||||||||||||||||||||||||
| self.x = np.random.random(size=[6, 13]).astype("uint16") | ||||||||||||||||||||||||||||||||
| self.out = self.x | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class TestCastFP32ToFP32MKLDNNOp(TestCastBF16ToFP32MKLDNNOp): | ||||||||||||||||||||||||||||||||
| def init_data(self): | ||||||||||||||||||||||||||||||||
| self.x = np.random.random(size=[7, 15]).astype("float32") | ||||||||||||||||||||||||||||||||
| self.out = self.x | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||||||
| paddle.enable_static() | ||||||||||||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||||||||||||
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
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.