-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Support npu kernel for eye op #34543
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 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
16d0076
add eye npu op
yeliang2258 2632fad
remove useless headers
yeliang2258 067ef66
code style
yeliang2258 0c2d8a1
Update eye_op_npu.cc
yeliang2258 7e88219
Update eye_op_npu.cc
yeliang2258 aedd7eb
remove useless code in test file
yeliang2258 cbd6850
code style check
yeliang2258 dd2a6f9
change Copyright to 2021
yeliang2258 a79dd78
Merge branch 'eye_npu_op_dev' of https://github.com/yeliang2258/Paddl…
yeliang2258 16b15cc
add test case and do some fix
yeliang2258 97ecbc3
fix
yeliang2258 a6531aa
update code
yeliang2258 2cbf6c8
fix for CI
yeliang2258 43f7643
return
yeliang2258 c0c10bb
fix
yeliang2258 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* 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/eye_op.h" | ||
| #include "paddle/fluid/operators/npu_op_runner.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| using Tensor = framework::Tensor; | ||
|
|
||
| template <typename DeviceContext, typename T> | ||
| class EyeNPUKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const override { | ||
| auto num_rows = ctx.Attr<int64_t>("num_rows"); | ||
|
|
||
| auto d_nums = ctx.Attr<int>("dtype"); | ||
| auto dtype = | ||
| ConvertToNpuDtype(static_cast<framework::proto::VarType::Type>(d_nums)); | ||
|
|
||
| auto num_columns = ctx.Attr<int64_t>("num_columns"); | ||
| if (num_columns == -1) num_columns = num_rows; | ||
|
|
||
| framework::NPUAttributeMap attr_input = { | ||
| {"num_rows", num_rows}, {"num_columns", num_columns}, {"dtype", dtype}}; | ||
|
|
||
| auto* out = ctx.Output<framework::Tensor>("Out"); | ||
| out->mutable_data<T>(ctx.GetPlace()); | ||
|
|
||
| const auto& runner = NpuOpRunner("Eye", {}, {*out}, attr_input); | ||
| auto stream = | ||
| ctx.template device_context<paddle::platform::NPUDeviceContext>() | ||
| .stream(); | ||
| runner.Run(stream); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
|
|
||
| REGISTER_OP_NPU_KERNEL( | ||
| eye, ops::EyeNPUKernel<paddle::platform::NPUDeviceContext, float>, | ||
| ops::EyeNPUKernel<paddle::platform::NPUDeviceContext, int>, | ||
| ops::EyeNPUKernel<paddle::platform::NPUDeviceContext, | ||
| paddle::platform::float16>); | ||
195 changes: 195 additions & 0 deletions
195
python/paddle/fluid/tests/unittests/npu/test_eye_op_npu.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,195 @@ | ||
| # 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 numpy as np | ||
| import unittest | ||
| import sys | ||
| sys.path.append("..") | ||
| from op_test import OpTest | ||
| import paddle | ||
| import paddle.fluid as fluid | ||
| from paddle.fluid import core | ||
| import paddle.fluid.framework as framework | ||
|
|
||
| paddle.enable_static() | ||
| np.random.seed(10) | ||
|
|
||
|
|
||
| class TestEyeOp(OpTest): | ||
| def setUp(self): | ||
| ''' | ||
| Test eye op with specified shape | ||
| ''' | ||
| self.set_npu() | ||
| self.place = paddle.NPUPlace(0) | ||
| self.op_type = "eye" | ||
| self.inputs = {} | ||
|
|
||
| self.num_rows = 0 | ||
| self.num_columns = 0 | ||
| self.dtype = np.float32 | ||
|
|
||
| self.initTestCase() | ||
|
|
||
| if self.num_columns == 0: | ||
| self.attrs = { | ||
| 'num_rows': self.num_rows, | ||
| 'dtype': framework.convert_np_dtype_to_dtype_(self.dtype) | ||
| } | ||
| self.outputs = {'Out': np.eye(self.num_rows, dtype=self.dtype)} | ||
| else: | ||
| self.attrs = { | ||
| 'num_rows': self.num_rows, | ||
| 'num_columns': self.num_columns, | ||
| 'dtype': framework.convert_np_dtype_to_dtype_(self.dtype) | ||
| } | ||
| self.outputs = { | ||
| 'Out': np.eye(self.num_rows, self.num_columns, dtype=self.dtype) | ||
| } | ||
|
|
||
| def initTestCase(self): | ||
| self.num_rows = 219 | ||
| self.num_columns = 319 | ||
| self.dtype = np.int32 | ||
|
|
||
| def set_npu(self): | ||
| self.__class__.use_npu = True | ||
|
|
||
| def test_check_output(self): | ||
| self.check_output_with_place(self.place) | ||
|
|
||
|
|
||
| class TestEyeOp1(TestEyeOp): | ||
| def initTestCase(self): | ||
| self.num_rows = 50 | ||
|
|
||
|
|
||
| class TestEyeOp2(TestEyeOp): | ||
| def initTestCase(self): | ||
| self.num_rows = 50 | ||
| self.dtype = np.int32 | ||
|
|
||
|
|
||
| class TestEyeOp3(TestEyeOp): | ||
| def initTestCase(self): | ||
| self.num_rows = 50 | ||
| self.dtype = np.float16 | ||
|
|
||
|
|
||
| class TestEyeOp4(TestEyeOp): | ||
| def initTestCase(self): | ||
| self.num_rows = 1 | ||
| self.num_columns = 99 | ||
|
|
||
|
|
||
| class TestEyeOp5(TestEyeOp): | ||
| def initTestCase(self): | ||
| self.num_rows = 100 | ||
| self.num_columns = 100 | ||
|
|
||
|
|
||
|
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. 少了一个dtype是fp32的单测,另外参考 test_eye_op.py 的 class API_TestTensorEye(unittest.TestCase),增加关于静态图API和动态图API的测试。
Contributor
Author
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. |
||
| class TestEyeOp6(TestEyeOp): | ||
| def initTestCase(self): | ||
| self.num_rows = 100 | ||
| self.num_columns = 100 | ||
| self.dtype = np.float32 | ||
|
|
||
|
|
||
| class API_TestTensorEye(unittest.TestCase): | ||
| def test_out(self): | ||
| with paddle.static.program_guard(paddle.static.Program()): | ||
| data = paddle.eye(10) | ||
| place = paddle.NPUPlace(0) | ||
| exe = paddle.static.Executor(place) | ||
| result, = exe.run(fetch_list=[data]) | ||
| expected_result = np.eye(10, dtype="float32") | ||
| self.assertEqual((result == expected_result).all(), True) | ||
|
|
||
| with paddle.static.program_guard(paddle.static.Program()): | ||
| data = paddle.eye(10, num_columns=7, dtype="float16") | ||
| place = paddle.NPUPlace(0) | ||
| exe = paddle.static.Executor(place) | ||
| result, = exe.run(fetch_list=[data]) | ||
| expected_result = np.eye(10, 7, dtype="float16") | ||
| self.assertEqual((result == expected_result).all(), True) | ||
|
|
||
| with paddle.static.program_guard(paddle.static.Program()): | ||
| data = paddle.eye(10, dtype="int32") | ||
| place = paddle.NPUPlace(0) | ||
| exe = paddle.static.Executor(place) | ||
| result, = exe.run(fetch_list=[data]) | ||
| expected_result = np.eye(10, dtype="int32") | ||
| self.assertEqual((result == expected_result).all(), True) | ||
|
|
||
| paddle.disable_static(paddle.NPUPlace(0)) | ||
| out = paddle.eye(10, dtype="int32") | ||
| expected_result = np.eye(10, dtype="int32") | ||
| paddle.enable_static() | ||
| self.assertEqual((out.numpy() == expected_result).all(), True) | ||
|
|
||
| paddle.disable_static(paddle.NPUPlace(0)) | ||
| batch_shape = [2] | ||
| out = fluid.layers.eye(10, 10, dtype="int32", batch_shape=batch_shape) | ||
| result = np.eye(10, dtype="int32") | ||
| expected_result = [] | ||
| for index in reversed(batch_shape): | ||
| tmp_result = [] | ||
| for i in range(index): | ||
| tmp_result.append(result) | ||
| result = tmp_result | ||
| expected_result = np.stack(result, axis=0) | ||
| paddle.enable_static() | ||
| self.assertEqual(out.numpy().shape == np.array(expected_result).shape, | ||
| True) | ||
| self.assertEqual((out.numpy() == expected_result).all(), True) | ||
|
|
||
| paddle.disable_static(paddle.NPUPlace(0)) | ||
| batch_shape = [3, 2] | ||
| out = fluid.layers.eye(10, 10, dtype="int32", batch_shape=batch_shape) | ||
| result = np.eye(10, dtype="int32") | ||
| expected_result = [] | ||
| for index in reversed(batch_shape): | ||
| tmp_result = [] | ||
| for i in range(index): | ||
| tmp_result.append(result) | ||
| result = tmp_result | ||
| expected_result = np.stack(result, axis=0) | ||
| paddle.enable_static() | ||
| self.assertEqual(out.numpy().shape == np.array(expected_result).shape, | ||
| True) | ||
| self.assertEqual((out.numpy() == expected_result).all(), True) | ||
|
|
||
| def test_errors(self): | ||
| with paddle.static.program_guard(paddle.static.Program()): | ||
|
|
||
| def test_num_rows_type_check(): | ||
| paddle.eye(-1, dtype="int64") | ||
|
|
||
| self.assertRaises(TypeError, test_num_rows_type_check) | ||
|
|
||
| def test_num_columns_type_check(): | ||
| paddle.eye(10, num_columns=5.2, dtype="int64") | ||
|
|
||
| self.assertRaises(TypeError, test_num_columns_type_check) | ||
|
|
||
| def test_num_columns_type_check1(): | ||
| paddle.eye(10, num_columns=10, dtype="int8") | ||
|
|
||
| self.assertRaises(TypeError, test_num_columns_type_check1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
33-37行合并为以下2行就可以,因为在python端的def eye中输入的num_columns不是None,因此ctx.HasAttr("num_columns")在API调用端永远是true.
auto num_columns = ctx.Attr<int64_t>("num_columns");
if (num_columns == -1) num_columns = num_rows;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
明白了,done