-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Ascend NPU add topk #31596
Merged
Merged
Ascend NPU add topk #31596
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26e6e6e
add topk op
oyjxer b7d1b19
add cmake
oyjxer 8ccb0f9
update topk npu op
oyjxer 96bbb3f
refactor func
oyjxer 1827816
fix test not go npu TopKD bug
oyjxer 51dc4ca
NPUPlace(4) to NPUPlace(0)
oyjxer 2794740
update comment
oyjxer 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 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,89 @@ | ||
/* 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 <memory> | ||
#include <string> | ||
|
||
#include "paddle/fluid/operators/top_k_op.h" | ||
#include "paddle/fluid/operators/npu_op_runner.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
void gen_assist_seq(framework::Tensor* assit_tensor, | ||
int64_t dim, const framework::ExecutionContext& ctx) { | ||
const int64_t dimx2 = dim; | ||
std::vector<paddle::platform::float16> assit; | ||
assit.resize(2 * dimx2); | ||
for (int64_t i = 0; i < dimx2; i++) { | ||
// for i in range [0, dim] | ||
assit[i] = static_cast<paddle::platform::float16>(i); | ||
|
||
// for i in range [dim, dimx2] | ||
int64_t idx = static_cast<int64_t>( | ||
static_cast<paddle::platform::float16>(i)); | ||
int64_t gap = i - idx; | ||
assit[i + dim] = static_cast<paddle::platform::float16>(gap); | ||
} | ||
framework::TensorFromVector(assit, ctx.device_context(), assit_tensor); | ||
} | ||
|
||
|
||
template <typename DeviceContext, typename T> | ||
class TopkNPUKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
// read input | ||
auto* input = ctx.Input<framework::LoDTensor>("X"); | ||
auto* output = ctx.Output<framework::LoDTensor>("Out"); | ||
auto* indices = ctx.Output<framework::LoDTensor>("Indices"); | ||
|
||
size_t k = static_cast<int>(ctx.Attr<int>("k")); | ||
|
||
output->mutable_data<paddle::platform::float16>(ctx.GetPlace()); | ||
indices->mutable_data<int>(ctx.GetPlace()); | ||
|
||
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. The indices data type must be |
||
// prepare assit | ||
auto dim = input->dims().size(); | ||
framework::Tensor assist_seq_tensor; | ||
assist_seq_tensor.Resize({2 * dim}); | ||
assist_seq_tensor.mutable_data<paddle::platform::float16>(ctx.GetPlace()); | ||
gen_assist_seq(&assist_seq_tensor, dim, ctx); | ||
|
||
framework::NPUAttributeMap attr_input = {{"sorted", "true"}, | ||
{"k", static_cast<int>(k)}, | ||
{"dim", -1}, | ||
{"largest", true}}; | ||
|
||
// run ascend | ||
auto runner = NpuOpRunner("TopKD", | ||
{*input, assist_seq_tensor}, | ||
{*output, *indices}, | ||
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( | ||
top_k, | ||
Comment on lines
+86
to
+87
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. only fp16? |
||
ops::TopkNPUKernel<paddle::platform::NPUDeviceContext, | ||
paddle::platform::float16>); | ||
|
95 changes: 95 additions & 0 deletions
95
python/paddle/fluid/tests/unittests/npu/test_top_k_op_npu.py
This file contains 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,95 @@ | ||
# 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 | ||
|
||
paddle.enable_static() | ||
SEED = 2021 | ||
|
||
|
||
@unittest.skipIf(not paddle.is_compiled_with_npu(), | ||
"core is not compiled with NPU") | ||
class TestTopk(OpTest): | ||
def setUp(self): | ||
self.set_npu() | ||
self.place = paddle.NPUPlace(0) | ||
self.op_type = "top_k" | ||
self.init_dtype() | ||
|
||
x = np.array([[0.78104149, 0.88745828, 0.32362268], | ||
[0.82196718, 0.48763277, 0.42826136], | ||
[0.96527182, 0.34851612, 0.12959783]]).astype(self.dtype) | ||
|
||
self.inputs = {'X': x} | ||
np_out = np.array([[0.88745828], [0.82196718], [0.96527182]]).astype(self.dtype) | ||
np_indices = np.array([[1], [0], [0]]) | ||
|
||
self.attrs = {'k': 1, "axis": -1} | ||
self.outputs = {'Out': np_out, 'Indices':np_indices} | ||
|
||
def set_npu(self): | ||
self.__class__.use_npu = True | ||
self.__class__.no_need_check_grad = True | ||
|
||
def init_dtype(self): | ||
self.dtype = np.float16 | ||
|
||
def test_check_output(self): | ||
self.check_output_with_place(self.place, check_dygraph=False) | ||
|
||
|
||
@unittest.skipIf(not paddle.is_compiled_with_npu(), | ||
"core is not compiled with NPU") | ||
class TestTopkV2(OpTest): | ||
def setUp(self): | ||
self.set_npu() | ||
self.place = paddle.NPUPlace(0) | ||
self.op_type = "top_k" | ||
self.init_dtype() | ||
|
||
x = np.array([[0.78104149, 0.88745828, 0.32362268], | ||
[0.82196718, 0.48763277, 0.42826136], | ||
[0.96527182, 0.34851612, 0.12959783]]).astype(self.dtype) | ||
|
||
self.inputs = {'X': x} | ||
np_out = np.array([[0.88745828, 0.78104149], [0.82196718, 0.48763277], [0.96527182, 0.34851612]]).astype(self.dtype) | ||
np_indices = np.array([[1, 0], [0, 1], [0, 1]]) | ||
|
||
self.attrs = {'k': 2, "axis": -1} | ||
self.outputs = {'Out': np_out, 'Indices':np_indices} | ||
|
||
def set_npu(self): | ||
self.__class__.use_npu = True | ||
self.__class__.no_need_check_grad = True | ||
|
||
def init_dtype(self): | ||
self.dtype = np.float16 | ||
|
||
def test_check_output(self): | ||
self.check_output_with_place(self.place, check_dygraph=False) | ||
|
||
|
||
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.
mutable_data<paddle::platform::float16>
->mutable_data<T>