-
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
add log_softmax_op_npu #35006
add log_softmax_op_npu #35006
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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/log_softmax_op.h" | ||
#include "paddle/fluid/operators/npu_op_runner.h" | ||
namespace paddle { | ||
namespace operators { | ||
template <typename DeviceContext, typename T> | ||
class LogSoftmaxNPUKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
auto* X = ctx.Input<framework::Tensor>("X"); | ||
auto* Out = ctx.Output<framework::Tensor>("Out"); | ||
const int rank = X->dims().size(); | ||
const int axis = CanonicalAxis(ctx.Attr<int>("axis"), rank); | ||
std::vector<int> axes; | ||
axes.push_back(axis); | ||
framework::NPUAttributeMap attr_input = {{"axes", axes}}; | ||
Out->mutable_data<T>(ctx.GetPlace()); | ||
const auto& runner = NpuOpRunner("LogSoftmaxV2", {*X}, {*Out}, attr_input); | ||
auto stream = | ||
ctx.template device_context<paddle::platform::NPUDeviceContext>() | ||
.stream(); | ||
runner.Run(stream); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
namespace ops = paddle::operators; | ||
namespace plat = paddle::platform; | ||
|
||
REGISTER_OP_NPU_KERNEL( | ||
log_softmax, | ||
ops::LogSoftmaxNPUKernel<paddle::platform::NPUDeviceContext, float>); |
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 | ||
import paddle.nn.functional as F | ||
from test_log_softmax import ref_log_softmax, test_log_softmax | ||
paddle.enable_static() | ||
np.random.seed(10) | ||
|
||
|
||
class TestLogSoftmaxNPUOp(OpTest): | ||
def setUp(self): | ||
self.set_npu() | ||
self.place = paddle.NPUPlace(0) | ||
self.op_type = "log_softmax" | ||
self.dtype = np.float32 | ||
self.shape = [2, 3, 4, 5] | ||
self.axis = -1 | ||
self.set_attrs() | ||
self.set_dtype() | ||
x = np.random.uniform(0.1, 1., self.shape).astype(self.dtype) | ||
out = np.apply_along_axis(ref_log_softmax, self.axis, x) | ||
self.x_grad = ref_log_softmax_grad(x, self.axis) | ||
self.inputs = {'X': x} | ||
self.outputs = {'Out': out} | ||
self.attrs = {'axis': self.axis} | ||
|
||
def set_npu(self): | ||
self.__class__.use_npu = True | ||
self.__class__.no_need_check_grad = True | ||
|
||
def set_attrs(self): | ||
pass | ||
|
||
def set_dtype(self): | ||
pass | ||
|
||
def test_check_output(self): | ||
self.check_output_with_place(self.place) | ||
|
||
def test_check_grad(self): | ||
pass | ||
|
||
|
||
def test_class(op_type, typename): | ||
class TestLogSoftmaxShape(TestLogSoftmaxNPUOp): | ||
def set_attrs(self): | ||
self.shape = [12, 10] | ||
|
||
def set_dtype(self): | ||
self.dtype = typename | ||
|
||
cls_name = "{0}_{1}_1".format(op_type, typename) | ||
TestLogSoftmaxShape.__name__ = cls_name | ||
globals()[cls_name] = TestLogSoftmaxShape | ||
|
||
|
||
def test_class2(op_type, typename): | ||
class TestLogSoftmaxAxis(TestLogSoftmaxNPUOp): | ||
def set_attrs(self): | ||
self.axis = 0 | ||
|
||
def set_dtype(self): | ||
self.dtype = typename | ||
|
||
cls_name = "{0}_{1}_2".format(op_type, typename) | ||
|
||
TestLogSoftmaxAxis.__name__ = cls_name | ||
globals()[cls_name] = TestLogSoftmaxAxis | ||
|
||
|
||
for _typename in {'float32'}: | ||
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. 可以支持fp64和fp16的情况下,这里增加float64 / float16的数据类型的单测 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. float64 / float16精度有问题,这里只注册了fp32算子 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. 已将注册部分的fp16/fp64删去。 |
||
test_class("logsoftmax", _typename) | ||
test_class2("logsoftmax", _typename) | ||
if __name__ == '__main__': | ||
unittest.main() |
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.
16行的tensor_util.h不需要
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