-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Added split op bf16/fp32 oneDNN kernel #33584
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 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d040922
base changes for split op
jakpiase e1e00f3
90% of split functionality added
jakpiase 50fd22c
full fp32 functionality
jakpiase a09a691
added bf16 test
jakpiase 1270057
added submemory caching
jakpiase 5afd5e3
added bf test to static mode whitelist
jakpiase e72f151
minor change
jakpiase 2bfbf62
enabled split op for inference
jakpiase e53f03e
Merge branch 'develop' into split_op
jakpiase d849d16
minor fix
jakpiase bdf98a1
minor fix
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,134 @@ | ||
| /* 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/operators/utils.h" | ||
| #include "paddle/fluid/platform/mkldnn_reuse.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| using paddle::framework::Tensor; | ||
|
|
||
| static inline std::vector<std::vector<int64_t>> CalculateOutsDims( | ||
| const framework::DDim& in_dims, const size_t num, | ||
| const std::vector<int>& sections, const size_t axis, | ||
| const int outs_number) { | ||
| std::vector<std::vector<int64_t>> outs_dims(outs_number, | ||
| framework::vectorize(in_dims)); | ||
|
|
||
| if (num > 0) { | ||
| PADDLE_ENFORCE_EQ(in_dims[axis] % num, 0, | ||
| platform::errors::InvalidArgument( | ||
| "The input's size along the split dimension " | ||
| "must be evenly divisible by Attr(num_or_sections). " | ||
| "But received Attr(num_or_sections) " | ||
| "= %d, input(X)'s shape = [%s], Attr(dim) = %d.", | ||
| num, in_dims, axis)); | ||
|
|
||
| const size_t out_axis_dim = in_dims[axis] / num; | ||
|
|
||
| for (auto& out_dim : outs_dims) out_dim[axis] = out_axis_dim; | ||
| } else { | ||
| for (size_t i = 0; i < outs_dims.size(); ++i) | ||
| outs_dims[i][axis] = sections[i]; | ||
| } | ||
| return outs_dims; | ||
| } | ||
|
|
||
| template <typename T> | ||
| class SplitMKLDNNKernel : 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>(); | ||
| const auto& onednn_engine = dev_ctx.GetEngine(); | ||
|
|
||
| const auto* x = ctx.Input<Tensor>("X"); | ||
| auto outs = ctx.MultiOutput<Tensor>("Out"); | ||
|
|
||
| int num = ctx.Attr<int>("num"); | ||
| auto sections = ctx.Attr<std::vector<int>>("sections"); | ||
| int axis = ctx.Attr<int>("axis"); | ||
| auto outs_number = outs.size(); | ||
| const auto x_dims = x->dims(); | ||
|
|
||
| bool need_resize = false; | ||
| if (ctx.HasInput("AxisTensor")) { | ||
| auto* axis_tensor = ctx.Input<Tensor>("AxisTensor"); | ||
| axis = GetDataFromTensor(axis_tensor)[0]; | ||
| need_resize = true; | ||
| } | ||
|
|
||
| auto sections_tensor_list = ctx.MultiInput<Tensor>("SectionsTensorList"); | ||
| if (sections_tensor_list.size() > 0) { | ||
| sections = GetDataFromTensorList(sections_tensor_list); | ||
| need_resize = true; | ||
| } | ||
|
|
||
| if (need_resize) { | ||
| const auto outs_dims = | ||
| CalculateOutsDims(x->dims(), num, sections, axis, outs_number); | ||
| for (size_t i = 0; i < outs.size(); ++i) { | ||
| outs[i]->Resize(framework::make_ddim(outs_dims[i])); | ||
| } | ||
| } | ||
|
|
||
| auto x_vec_dims = framework::vectorize(x_dims); | ||
|
|
||
| mkldnn::memory::data_type x_type = framework::ToMKLDNNDataType(x->type()); | ||
| std::string key = platform::CreateKey(dev_ctx, x_vec_dims, sections, | ||
| x->format(), x->format(), x_type); | ||
|
|
||
| auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); | ||
|
|
||
| std::vector<int64_t> offset(x_vec_dims.size(), 0); | ||
|
|
||
| platform::ReorderMKLDNNHandler reorder_handler( | ||
| x_vec_dims, x->type(), x_type, dev_ctx, onednn_engine, key); | ||
| auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( | ||
| x->format(), platform::to_void_cast(x->data<T>())); | ||
|
|
||
| for (size_t i = 0; i < outs_number; ++i) { | ||
| auto out_vec_dims = framework::vectorize(outs[i]->dims()); | ||
| const auto slice_md = reorder_src_memory_p->get_desc().submemory_desc( | ||
| out_vec_dims, {offset}); | ||
| auto slice_mem_p = std::make_shared<mkldnn::memory>( | ||
| slice_md, onednn_engine, reorder_src_memory_p->get_data_handle()); | ||
|
|
||
| auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( | ||
| outs[i], out_vec_dims, i, x->format(), ctx.GetPlace()); | ||
| auto reorder_p = | ||
| reorder_handler.AcquireReorder(reorder_dst_memory_p, slice_mem_p, i); | ||
|
|
||
| reorder_p->execute(astream, *slice_mem_p, *reorder_dst_memory_p); | ||
|
|
||
| offset[axis] += num > 0 ? x->dims()[axis] / num : sections[i]; | ||
|
|
||
| outs[i]->set_layout(framework::DataLayout::kMKLDNN); | ||
| outs[i]->set_format(platform::GetMKLDNNFormat(*reorder_dst_memory_p)); | ||
| } | ||
| astream.wait(); | ||
| } | ||
| }; | ||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
| REGISTER_OP_KERNEL(split, MKLDNN, paddle::platform::CPUPlace, | ||
| ops::SplitMKLDNNKernel<float>, | ||
| ops::SplitMKLDNNKernel<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
112 changes: 112 additions & 0 deletions
112
python/paddle/fluid/tests/unittests/mkldnn/test_split_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,112 @@ | ||
| # Copyright (c) 2018 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 as fluid | ||
| from paddle.fluid import compiler, Program, program_guard, core | ||
| from paddle.fluid.tests.unittests.op_test import OpTest | ||
|
|
||
|
|
||
| class TestSplitSectionsOneDNNOp(OpTest): | ||
| def init_data(self): | ||
| self.x = np.random.random((4, 5, 6)).astype("float32") | ||
| self.axis = 1 | ||
| self.sections = [2, 1, 2] | ||
| indices_or_sections = [2, 3] # sections | ||
| np_sections = [2, 3] | ||
| self.out = np.split(self.x, np_sections, self.axis) | ||
|
|
||
| def setUp(self): | ||
| self.op_type = "split" | ||
| self.axis_tensor = None | ||
| self.sections_tensor_list = None | ||
| self.num = 0 | ||
| self.init_data() | ||
| self.inputs = {'X': self.x} | ||
| self.attrs = {'use_mkldnn': True, 'num': self.num} | ||
|
|
||
| if self.axis is not None: | ||
| self.attrs['axis'] = self.axis | ||
| if self.sections is not None: | ||
| self.attrs['sections'] = self.sections | ||
| if self.axis_tensor is not None: | ||
| self.inputs['AxisTensor'] = self.axis_tensor | ||
| if self.sections_tensor_list is not None: | ||
| self.inputs['SectionsTensorList'] = self.sections_tensor_list | ||
|
|
||
| self.outputs = {'Out': [('out%d' % i, self.out[i]) \ | ||
| for i in range(len(self.out))]} | ||
|
|
||
| def test_check_output(self): | ||
| self.check_output() | ||
|
|
||
| def test_check_grad(self): | ||
| self.check_grad(['X'], ['out0', 'out1', 'out2']) | ||
|
|
||
|
|
||
| # test with attr(num) | ||
| class TestSplitNumOneDNNOp(TestSplitSectionsOneDNNOp): | ||
| def init_data(self): | ||
| self.x = np.random.random((4, 8, 5, 3)).astype("float32") | ||
| self.axis = 1 | ||
| self.sections = [] | ||
| self.num = 4 | ||
| indices_or_sections = 4 #indices | ||
| self.out = np.split(self.x, indices_or_sections, self.axis) | ||
|
|
||
| def test_check_grad(self): | ||
| self.check_grad(['X'], ['out0', 'out1', 'out2', 'out3']) | ||
|
|
||
|
|
||
| class TestSplitNumAxisTensorOneDNNOp(TestSplitSectionsOneDNNOp): | ||
| def init_data(self): | ||
| self.x = np.random.random((4, 5, 6)).astype("float32") | ||
| self.axis = None | ||
| self.sections = [] | ||
| self.num = 3 | ||
| indices_or_sections = 3 #indices | ||
| self.axis_tensor = np.array([2]).astype("int32") | ||
| self.out = np.split(self.x, indices_or_sections, 2) | ||
|
|
||
|
|
||
| # attr(sections) is list containing Tensor | ||
| class TestSplitSectionsTensorOneDNNOp(TestSplitSectionsOneDNNOp): | ||
| def init_data(self): | ||
| self.x = np.random.random((4, 5, 6)).astype("float32") | ||
| self.axis = 1 | ||
| self.sections = [2, 1, 2] | ||
| self.sections_tensor_list = [] | ||
| for index, ele in enumerate(self.sections): | ||
| self.sections_tensor_list.append(("x" + str(index), np.ones( | ||
| (1)).astype('int32') * ele)) | ||
| self.sections = [-1, -1, -1] | ||
| indices_or_sections = [2, 3] #sections | ||
| self.out = np.split(self.x, indices_or_sections, self.axis) | ||
|
|
||
|
|
||
| class TestSplitOpUnknownSectionOneDNNOp(TestSplitSectionsOneDNNOp): | ||
| def init_data(self): | ||
| self.x = np.random.random((4, 5, 6)).astype("float32") | ||
| self.axis = 2 | ||
| self.sections = [2, 2, -1] | ||
| indices_or_sections = [2, 4] #sections | ||
| self.out = np.split(self.x, indices_or_sections, self.axis) | ||
|
|
||
|
|
||
| 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
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.