From 1128db30cfc03dc9a17da4221c76c3e60b98b7f5 Mon Sep 17 00:00:00 2001 From: helen88 Date: Thu, 10 Mar 2022 09:59:38 +0800 Subject: [PATCH] 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 --- cmake/external/xpu.cmake | 2 +- paddle/fluid/operators/tril_triu_op_xpu.cc | 53 +++++++ .../fluid/platform/device/xpu/xpu2_op_list.h | 4 + .../unittests/xpu/test_tril_triu_op_xpu.py | 143 ++++++++++++++++++ 4 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 paddle/fluid/operators/tril_triu_op_xpu.cc create mode 100644 python/paddle/fluid/tests/unittests/xpu/test_tril_triu_op_xpu.py diff --git a/cmake/external/xpu.cmake b/cmake/external/xpu.cmake index 45a76fdc1f1..cfbe68eecba 100644 --- a/cmake/external/xpu.cmake +++ b/cmake/external/xpu.cmake @@ -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() diff --git a/paddle/fluid/operators/tril_triu_op_xpu.cc b/paddle/fluid/operators/tril_triu_op_xpu.cc new file mode 100644 index 00000000000..e36cbcf228c --- /dev/null +++ b/paddle/fluid/operators/tril_triu_op_xpu.cc @@ -0,0 +1,53 @@ +/* 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 +class TrilTriuXPUKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& context) const override { + const auto* x = context.Input("X"); + const auto* x_data = x->data(); + auto* out = context.Output("Out"); + auto* out_data = out->mutable_data(context.GetPlace()); + + const int diagonal = context.Attr("diagonal"); + const bool lower = context.Attr("lower"); + auto xshape = phi::vectorize(x->dims()); + auto& dev_ctx = context.template device_context(); + 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, + ops::TrilTriuXPUKernel); +#endif diff --git a/paddle/fluid/platform/device/xpu/xpu2_op_list.h b/paddle/fluid/platform/device/xpu/xpu2_op_list.h index e6b08ed7bc3..3789ec322ac 100644 --- a/paddle/fluid/platform/device/xpu/xpu2_op_list.h +++ b/paddle/fluid/platform/device/xpu/xpu2_op_list.h @@ -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()), diff --git a/python/paddle/fluid/tests/unittests/xpu/test_tril_triu_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_tril_triu_op_xpu.py new file mode 100644 index 00000000000..785549abba8 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/xpu/test_tril_triu_op_xpu.py @@ -0,0 +1,143 @@ +# 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() -- GitLab