未验证 提交 1128db30 编写于 作者: z8hanghuan's avatar z8hanghuan 提交者: GitHub

add tril_triu for xpu, *test=kunlun (#40246)

* add tril_triu for xpu, *test=kunlun

* add tril_triu for xpu, *test=kunlun

* add tril_triu for xpu, *test=kunlun

* add tril_triu for xpu, *test=kunlun

* add tril_triu for xpu, *test=kunlun
上级 e72ef603
......@@ -36,7 +36,7 @@ ENDIF()
if(NOT DEFINED XPU_BASE_URL)
SET(XPU_BASE_URL_WITHOUT_DATE "https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev")
SET(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220228")
SET(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220307")
else()
SET(XPU_BASE_URL "${XPU_BASE_URL}")
endif()
......
/* Copyright (c) 2022 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. */
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/operators/tril_triu_op.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
namespace paddle {
namespace operators {
using Tensor = framework::Tensor;
template <typename DeviceContext, typename T>
class TrilTriuXPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
const auto* x = context.Input<framework::Tensor>("X");
const auto* x_data = x->data<T>();
auto* out = context.Output<framework::Tensor>("Out");
auto* out_data = out->mutable_data<T>(context.GetPlace());
const int diagonal = context.Attr<int>("diagonal");
const bool lower = context.Attr<bool>("lower");
auto xshape = phi::vectorize<int>(x->dims());
auto& dev_ctx = context.template device_context<DeviceContext>();
int r = 0;
if (lower) {
r = xpu::tril(dev_ctx.x_context(), x_data, out_data, xshape, diagonal);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "tril_op");
} else {
r = xpu::triu(dev_ctx.x_context(), x_data, out_data, xshape, diagonal);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "triu_op");
}
}
};
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
tril_triu, ops::TrilTriuXPUKernel<paddle::platform::XPUDeviceContext, int>,
ops::TrilTriuXPUKernel<paddle::platform::XPUDeviceContext, float>);
#endif
......@@ -323,6 +323,8 @@ XPUOpMap& get_kl2_ops() {
XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})},
{"split", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace()),
pOpKernelType(vartype::INT32, XPUPlace())})},
{"square_grad", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})},
{"square", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})},
{"squeeze2_grad",
XPUKernelSet({pOpKernelType(vartype::FP64, XPUPlace()),
pOpKernelType(vartype::INT64, XPUPlace()),
......@@ -349,6 +351,8 @@ XPUOpMap& get_kl2_ops() {
pOpKernelType(vartype::FP16, XPUPlace())})},
{"tanh", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace()),
pOpKernelType(vartype::FP16, XPUPlace())})},
{"tril_triu", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace()),
pOpKernelType(vartype::INT32, XPUPlace())})},
{"tile", XPUKernelSet({pOpKernelType(vartype::INT32, XPUPlace()),
pOpKernelType(vartype::INT64, XPUPlace()),
pOpKernelType(vartype::BOOL, XPUPlace()),
......
# Copyright (c) 2022 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 sys
sys.path.append("..")
import paddle
import paddle.fluid.core as core
import paddle.fluid as fluid
import paddle.tensor as tensor
import unittest
import numpy as np
from op_test import OpTest
from op_test_xpu import XPUOpTest
from paddle.fluid.framework import Program, program_guard
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
paddle.enable_static()
class XPUTestTrilTriuOp(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'tril_triu'
self.use_dynamic_create_class = False
class TestTrilTriuOp(XPUOpTest):
def setUp(self):
self.init_dtype()
self.initTestCase()
self.real_op_type = np.random.choice(['triu', 'tril'])
self.real_np_op = getattr(np, self.real_op_type)
self.set_xpu()
self.op_type = "tril_triu"
if self.dtype == np.int32:
self.X = np.arange(
1, self.get_Xshape_prod() + 1,
dtype=self.dtype).reshape(self.Xshape)
else:
self.X = np.random.random(self.Xshape).astype(dtype=self.dtype)
self.inputs = {'X': self.X}
self.attrs = {
'diagonal': self.diagonal,
'lower': True if self.real_op_type == 'tril' else False,
}
self.outputs = {
'Out': self.real_np_op(self.X, self.diagonal)
if self.diagonal else self.real_np_op(self.X)
}
def init_dtype(self):
self.dtype = self.in_type
def get_Xshape_prod(self):
ret = 1
for v in self.Xshape:
ret *= v
return ret
def set_xpu(self):
self.__class__.use_xpu = True
self.__class__.no_need_check_grad = True
self.__class__.op_type = self.real_op_type
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def initTestCase(self):
self.diagonal = None
self.Xshape = (10, 10)
class TestTrilTriuOp1(TestTrilTriuOp):
def initTestCase(self):
self.diagonal = -3
self.Xshape = (5, 5)
class TestTrilTriuOp2(TestTrilTriuOp):
def initTestCase(self):
self.diagonal = 4
self.Xshape = (11, 17)
class TestTrilTriuOp3(TestTrilTriuOp):
def initTestCase(self):
self.diagonal = 10
self.Xshape = (25, 25)
class TestTrilTriuOp4(TestTrilTriuOp):
def initTestCase(self):
self.diagonal = -10
self.Xshape = (33, 11)
class TestTrilTriuOp5(TestTrilTriuOp):
def initTestCase(self):
self.diagonal = 11
self.Xshape = (1, 99)
class TestTrilTriuOpError(unittest.TestCase):
def test_errors1(self):
paddle.enable_static()
data = fluid.data(shape=(20, 22), dtype='float32', name="data1")
op_type = np.random.choice(['triu', 'tril'])
errmsg = {
"diagonal: TypeError":
"diagonal in {} must be a python Int".format(op_type),
}
expected = list(errmsg.keys())[0]
with self.assertRaisesRegex(
eval(expected.split(':')[-1]), errmsg[expected]):
getattr(tensor, op_type)(x=data, diagonal='2022')
def test_errors2(self):
paddle.enable_static()
data = fluid.data(shape=(200, ), dtype='float32', name="data2")
op_type = np.random.choice(['triu', 'tril'])
errmsg = {
"input: ValueError":
"x shape in {} must be at least 2-D".format(op_type),
}
expected = list(errmsg.keys())[0]
with self.assertRaisesRegex(
eval(expected.split(':')[-1]), errmsg[expected]):
getattr(tensor, op_type)(x=data, diagonal=[None])
support_types = get_xpu_op_support_types('tril_triu')
for stype in support_types:
create_test_class(globals(), XPUTestTrilTriuOp, stype)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册