未验证 提交 e8df3226 编写于 作者: Z zyfncg 提交者: GitHub

Support npu op fill_any_like (#34518)

* Support npu kernel for fill_any_like op

* modify the description of exception

* remove useless template element

* remove useless decorator

* fix the code format error
上级 ed2641cb
...@@ -45,15 +45,18 @@ class FillAnyLikeKernel : public framework::OpKernel<T> { ...@@ -45,15 +45,18 @@ class FillAnyLikeKernel : public framework::OpKernel<T> {
static_cast<CommonType>(std::numeric_limits<T>::lowest())) && static_cast<CommonType>(std::numeric_limits<T>::lowest())) &&
(common_type_value <= (common_type_value <=
static_cast<CommonType>(std::numeric_limits<T>::max())), static_cast<CommonType>(std::numeric_limits<T>::max())),
true, platform::errors::InvalidArgument( true,
"filled value is out of range for" platform::errors::InvalidArgument(
" targeted type in fill_any_like, your kernel type is %s" "The filled value is out of range for target type, "
", please check value you set.", "current kernel type is %s, the range should between %f "
typeid(T).name())); "and %f, but now value is %f.",
typeid(T).name(),
static_cast<CommonType>(std::numeric_limits<T>::lowest()),
static_cast<CommonType>(std::numeric_limits<T>::max()), value));
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
std::isnan(value), false, std::isnan(value), false,
platform::errors::InvalidArgument("filled value should not be NaN," platform::errors::InvalidArgument("The filled value is NaN."));
" but received NaN"));
math::SetConstant<DeviceContext, T> setter; math::SetConstant<DeviceContext, T> setter;
setter(context.template device_context<DeviceContext>(), out, setter(context.template device_context<DeviceContext>(), out,
......
/* 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/fill_any_like_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
namespace paddle {
namespace operators {
template <typename T>
class FillAnyLikeNPUKernel : public framework::OpKernel<T> {
public:
using CommonType = typename std::common_type<
float,
typename std::conditional<std::is_same<T, platform::float16>::value,
float, T>::type>::type;
void Compute(const framework::ExecutionContext& context) const override {
auto data_type = static_cast<framework::proto::VarType::Type>(
context.Attr<int>("dtype"));
auto* out = context.Output<framework::Tensor>("Out");
out->mutable_data<T>(context.GetPlace());
float value = context.Attr<float>("value");
auto common_type_value = static_cast<CommonType>(value);
PADDLE_ENFORCE_EQ(
(common_type_value >=
static_cast<CommonType>(std::numeric_limits<T>::lowest())) &&
(common_type_value <=
static_cast<CommonType>(std::numeric_limits<T>::max())),
true,
platform::errors::InvalidArgument(
"The filled value is out of range for target type, "
"current kernel type is %s, the range should between %f "
"and %f, but now value is %f.",
typeid(T).name(),
static_cast<CommonType>(std::numeric_limits<T>::lowest()),
static_cast<CommonType>(std::numeric_limits<T>::max()), value));
PADDLE_ENFORCE_EQ(
std::isnan(value), false,
platform::errors::InvalidArgument("The filled value is NaN."));
Tensor tensor_tmp(data_type);
tensor_tmp.mutable_data<T>({1}, context.GetPlace());
FillNpuTensorWithConstant<T>(&tensor_tmp, static_cast<T>(value));
auto stream =
context.template device_context<paddle::platform::NPUDeviceContext>()
.stream();
auto shape = out->dims();
const auto& runner = NpuOpRunner("FillD", {tensor_tmp}, {*out},
{{"dims", framework::vectorize(shape)}});
runner.Run(stream);
}
};
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
REGISTER_OP_NPU_KERNEL(fill_any_like, ops::FillAnyLikeNPUKernel<int>,
ops::FillAnyLikeNPUKernel<float>,
ops::FillAnyLikeNPUKernel<paddle::platform::float16>);
# 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()
class TestFillAnyLikeNPUOp(OpTest):
def setUp(self):
self.set_npu()
self.place = paddle.NPUPlace(0)
self.op_type = "fill_any_like"
self.dtype = np.float32
self.shape = [2, 3, 4, 5]
self.value = 0.0
self.init()
self.inputs = {'X': np.random.random(self.shape).astype(self.dtype)}
self.attrs = {'value': self.value}
self.outputs = {'Out': np.full(self.shape, self.value, self.dtype)}
def init(self):
pass
def set_npu(self):
self.__class__.use_npu = True
def test_check_output(self):
self.check_output_with_place(self.place)
class TestFillAnyLikeNPUOpInt32(TestFillAnyLikeNPUOp):
def init(self):
self.dtype = np.int32
self.value = -1
class TestFillAnyLikeNPUOpFloat32(TestFillAnyLikeNPUOp):
def init(self):
self.dtype = np.float32
self.value = 0.09
class TestFillAnyLikeNPUOpFloat16(TestFillAnyLikeNPUOp):
def init(self):
self.dtype = np.float16
self.value = 0.05
class TestFillAnyLikeNPUOpValue1(TestFillAnyLikeNPUOp):
def init(self):
self.value = 1.0
class TestFillAnyLikeNPUOpValue2(TestFillAnyLikeNPUOp):
def init(self):
self.value = 1e-9
class TestFillAnyLikeNPUOpShape(TestFillAnyLikeNPUOp):
def init(self):
self.shape = [12, 10]
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册