diff --git a/cmake/external/xpu.cmake b/cmake/external/xpu.cmake index 7d1cca4feb6a6c8e51b9d6359f95d3299e01700f..d75af71203bf9dd70c619eec54e101e1a45843e9 100644 --- a/cmake/external/xpu.cmake +++ b/cmake/external/xpu.cmake @@ -10,7 +10,7 @@ set(XPU_RT_LIB_NAME "libxpurt.so") 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}/20220601") + set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220703") else() set(XPU_BASE_URL "${XPU_BASE_URL}") endif() @@ -19,14 +19,14 @@ endif() if(NOT DEFINED XPU_XDNN_BASE_URL) set(XPU_XDNN_BASE_URL_WITHOUT_DATE "https://klx-sdk-release-public.su.bcebos.com/xdnn/dev") - set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220601") + set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220703") else() set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL}") endif() if(WITH_AARCH64) set(XPU_XRE_DIR_NAME "xre-kylin_aarch64") - set(XPU_XDNN_DIR_NAME "XDNN-kylin_aarch64") + set(XPU_XDNN_DIR_NAME "xdnn-kylin_aarch64") set(XPU_XCCL_DIR_NAME "xccl-kylin_aarch64") set(XPU_XDNN_URL "${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" @@ -40,7 +40,7 @@ elseif(WITH_SUNWAY) CACHE STRING "" FORCE) elseif(WITH_BDCENTOS) set(XPU_XRE_DIR_NAME "xre-bdcentos_x86_64") - set(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") + set(XPU_XDNN_DIR_NAME "xdnn-bdcentos_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") # ubuntu and centos: use output by XDNN API team set(XPU_XDNN_URL @@ -48,7 +48,7 @@ elseif(WITH_BDCENTOS) CACHE STRING "" FORCE) elseif(WITH_UBUNTU) set(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64") - set(XPU_XDNN_DIR_NAME "XDNN-ubuntu_x86_64") + set(XPU_XDNN_DIR_NAME "xdnn-ubuntu_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") # ubuntu and centos: use output by XDNN API team set(XPU_XDNN_URL @@ -56,7 +56,7 @@ elseif(WITH_UBUNTU) CACHE STRING "" FORCE) elseif(WITH_CENTOS) set(XPU_XRE_DIR_NAME "xre-centos7_x86_64") - set(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") + set(XPU_XDNN_DIR_NAME "xdnn-bdcentos_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") # ubuntu and centos: use output by XDNN API team set(XPU_XDNN_URL @@ -64,7 +64,7 @@ elseif(WITH_CENTOS) CACHE STRING "" FORCE) else() set(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64") - set(XPU_XDNN_DIR_NAME "XDNN-ubuntu_x86_64") + set(XPU_XDNN_DIR_NAME "xdnn-ubuntu_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") # default: use output by XDNN API team set(XPU_XDNN_URL diff --git a/python/paddle/fluid/tests/unittests/xpu/test_assign_value_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_assign_value_op_xpu.py new file mode 100644 index 0000000000000000000000000000000000000000..6455b157cb2ca28e2b0fc02569a02e03ca3b805a --- /dev/null +++ b/python/paddle/fluid/tests/unittests/xpu/test_assign_value_op_xpu.py @@ -0,0 +1,135 @@ +# Copyright (c) 2018 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 unittest +import numpy +import sys + +sys.path.append("..") +import paddle.fluid as fluid +import paddle.fluid.framework as framework +import paddle.fluid.layers as layers +from op_test_xpu import XPUOpTest +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper +import paddle + +paddle.enable_static() + + +class XPUTestAssignValueOp(XPUOpTestWrapper): + + def __init__(self): + self.op_name = 'assign_value' + self.use_dynamic_create_class = False + + class TestAssignValueOp(XPUOpTest): + + def init(self): + self.dtype = self.in_type + self.place = paddle.XPUPlace(0) + self.op_type = 'assign_value' + + def setUp(self): + self.init() + self.inputs = {} + self.attrs = {} + self.init_data() + self.attrs["shape"] = self.value.shape + self.attrs["dtype"] = framework.convert_np_dtype_to_dtype_( + self.value.dtype) + self.outputs = {"Out": self.value} + + def init_data(self): + self.value = numpy.random.random(size=(2, 5)).astype(self.dtype) + self.attrs["fp32_values"] = [float(v) for v in self.value.flat] + + def test_forward(self): + self.check_output_with_place(self.place) + + class TestAssignValueOp2(TestAssignValueOp): + + def init_data(self): + self.value = numpy.random.random(size=(2, 5)).astype(numpy.int32) + self.attrs["int32_values"] = [int(v) for v in self.value.flat] + + class TestAssignValueOp3(TestAssignValueOp): + + def init_data(self): + self.value = numpy.random.random(size=(2, 5)).astype(numpy.int64) + self.attrs["int64_values"] = [int(v) for v in self.value.flat] + + class TestAssignValueOp4(TestAssignValueOp): + + def init_data(self): + self.value = numpy.random.choice(a=[False, True], + size=(2, 5)).astype(numpy.bool) + self.attrs["bool_values"] = [int(v) for v in self.value.flat] + + +class TestAssignApi(unittest.TestCase): + + def setUp(self): + self.init_dtype() + self.value = (-100 + 200 * numpy.random.random(size=(2, 5))).astype( + self.dtype) + self.place = fluid.XPUPlace(0) + + def init_dtype(self): + self.dtype = "float32" + + def test_assign(self): + main_program = fluid.Program() + with fluid.program_guard(main_program): + x = layers.create_tensor(dtype=self.dtype) + layers.assign(input=self.value, output=x) + + exe = fluid.Executor(self.place) + [fetched_x] = exe.run(main_program, feed={}, fetch_list=[x]) + self.assertTrue(numpy.array_equal(fetched_x, self.value), + "fetch_x=%s val=%s" % (fetched_x, self.value)) + self.assertEqual(fetched_x.dtype, self.value.dtype) + + +class TestAssignApi2(TestAssignApi): + + def init_dtype(self): + self.dtype = "int32" + + +class TestAssignApi3(TestAssignApi): + + def init_dtype(self): + self.dtype = "int64" + + +class TestAssignApi4(TestAssignApi): + + def setUp(self): + self.init_dtype() + self.value = numpy.random.choice(a=[False, True], + size=(2, 5)).astype(numpy.bool) + self.place = fluid.XPUPlace(0) + + def init_dtype(self): + self.dtype = "bool" + + +support_types = get_xpu_op_support_types('assign_value') +for stype in support_types: + create_test_class(globals(), XPUTestAssignValueOp, stype) + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_iou_similarity_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_iou_similarity_op_xpu.py index ceb154f1e352054985f8df2d792850ab210aaa18..56ad05505a3acb30796df7f43d0ead178e0de822 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_iou_similarity_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_iou_similarity_op_xpu.py @@ -22,99 +22,116 @@ import unittest import numpy as np import numpy.random as random import sys -import math -from op_test import OpTest + +sys.path.append("..") from op_test_xpu import XPUOpTest +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper import paddle paddle.enable_static() -class TestXPUIOUSimilarityOp(XPUOpTest): - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def setUp(self): - self.op_type = "iou_similarity" - self.boxes1 = random.rand(2, 4).astype('float32') - self.boxes2 = random.rand(3, 4).astype('float32') - self.output = random.rand(2, 3).astype('float32') - self.box_normalized = False - # run python iou computation - self._compute_iou() - self.inputs = {'X': self.boxes1, 'Y': self.boxes2} - self.attrs = {"box_normalized": self.box_normalized, 'use_xpu': True} - self.outputs = {'Out': self.output} - - def _compute_iou(self, ): - for row in range(self.boxes1.shape[0]): - for col in range(self.boxes2.shape[0]): - xmin1, ymin1, xmax1, ymax1 = self.boxes1[row] - xmin2, ymin2, xmax2, ymax2 = self.boxes2[col] - if not self.box_normalized: - area1 = (ymax1 - ymin1 + 1) * (xmax1 - xmin1 + 1) - area2 = (ymax2 - ymin2 + 1) * (xmax2 - xmin2 + 1) - else: - area1 = (ymax1 - ymin1) * (xmax1 - xmin1) - area2 = (ymax2 - ymin2) * (xmax2 - xmin2) - - inter_xmax = min(xmax1, xmax2) - inter_ymax = min(ymax1, ymax2) - inter_xmin = max(xmin1, xmin2) - inter_ymin = max(ymin1, ymin2) - inter_height = inter_ymax - inter_ymin - inter_width = inter_xmax - inter_xmin - if not self.box_normalized: - inter_height += 1 - inter_width += 1 - inter_height = max(inter_height, 0) - inter_width = max(inter_width, 0) - inter_area = inter_width * inter_height - union_area = area1 + area2 - inter_area - sim_score = inter_area / union_area - self.output[row, col] = sim_score - - -class TestXPUIOUSimilarityOpWithLoD(TestXPUIOUSimilarityOp): - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) - - def setUp(self): - super(TestXPUIOUSimilarityOpWithLoD, self).setUp() - self.boxes1_lod = [[1, 1]] - self.output_lod = [[1, 1]] - self.box_normalized = False - # run python iou computation - self._compute_iou() - self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2} - self.attrs = {"box_normalized": self.box_normalized} - self.outputs = {'Out': (self.output, self.output_lod)} - - -class TestXPUIOUSimilarityOpWithBoxNormalized(TestXPUIOUSimilarityOp): - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) - - def setUp(self): - super(TestXPUIOUSimilarityOpWithBoxNormalized, self).setUp() - self.boxes1_lod = [[1, 1]] - self.output_lod = [[1, 1]] - self.box_normalized = True - # run python iou computation - self._compute_iou() - self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2} - self.attrs = {"box_normalized": self.box_normalized} - self.outputs = {'Out': (self.output, self.output_lod)} - +class XPUTestIOUSimilarityOp(XPUOpTestWrapper): + + def __init__(self): + self.op_name = 'iou_similarity' + self.use_dynamic_create_class = False + + class TestXPUIOUSimilarityOp(XPUOpTest): + + def init(self): + self.dtype = self.in_type + self.place = paddle.XPUPlace(0) + self.op_type = 'iou_similarity' + + def test_check_output(self): + self.check_output_with_place(self.place) + + def setUp(self): + self.init() + self.boxes1 = random.rand(2, 4).astype(self.dtype) + self.boxes2 = random.rand(3, 4).astype(self.dtype) + self.output = random.rand(2, 3).astype(self.dtype) + self.box_normalized = False + # run python iou computation + self._compute_iou() + self.inputs = {'X': self.boxes1, 'Y': self.boxes2} + self.attrs = { + "box_normalized": self.box_normalized, + 'use_xpu': True + } + self.outputs = {'Out': self.output} + + def _compute_iou(self, ): + for row in range(self.boxes1.shape[0]): + for col in range(self.boxes2.shape[0]): + xmin1, ymin1, xmax1, ymax1 = self.boxes1[row] + xmin2, ymin2, xmax2, ymax2 = self.boxes2[col] + if not self.box_normalized: + area1 = (ymax1 - ymin1 + 1) * (xmax1 - xmin1 + 1) + area2 = (ymax2 - ymin2 + 1) * (xmax2 - xmin2 + 1) + else: + area1 = (ymax1 - ymin1) * (xmax1 - xmin1) + area2 = (ymax2 - ymin2) * (xmax2 - xmin2) + + inter_xmax = min(xmax1, xmax2) + inter_ymax = min(ymax1, ymax2) + inter_xmin = max(xmin1, xmin2) + inter_ymin = max(ymin1, ymin2) + inter_height = inter_ymax - inter_ymin + inter_width = inter_xmax - inter_xmin + if not self.box_normalized: + inter_height += 1 + inter_width += 1 + inter_height = max(inter_height, 0) + inter_width = max(inter_width, 0) + inter_area = inter_width * inter_height + union_area = area1 + area2 - inter_area + sim_score = inter_area / union_area + self.output[row, col] = sim_score + + class TestXPUIOUSimilarityOpWithLoD(TestXPUIOUSimilarityOp): + + def test_check_output(self): + self.check_output_with_place(self.place, check_dygraph=False) + + def setUp(self): + super().setUp() + self.boxes1_lod = [[1, 1]] + self.output_lod = [[1, 1]] + self.box_normalized = False + # run python iou computation + self._compute_iou() + self.inputs = { + 'X': (self.boxes1, self.boxes1_lod), + 'Y': self.boxes2 + } + self.attrs = {"box_normalized": self.box_normalized} + self.outputs = {'Out': (self.output, self.output_lod)} + + class TestXPUIOUSimilarityOpWithBoxNormalized(TestXPUIOUSimilarityOp): + + def test_check_output(self): + self.check_output_with_place(self.place, check_dygraph=False) + + def setUp(self): + super().setUp() + self.boxes1_lod = [[1, 1]] + self.output_lod = [[1, 1]] + self.box_normalized = True + # run python iou computation + self._compute_iou() + self.inputs = { + 'X': (self.boxes1, self.boxes1_lod), + 'Y': self.boxes2 + } + self.attrs = {"box_normalized": self.box_normalized} + self.outputs = {'Out': (self.output, self.output_lod)} + + +support_types = get_xpu_op_support_types('iou_similarity') +for stype in support_types: + create_test_class(globals(), XPUTestIOUSimilarityOp, stype) if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_one_hot_v2_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_one_hot_v2_op_xpu.py index afeccd637a265cc7e24f86ecd2e5713c135e0d2d..d45e0ce34d42f226192a2b7cc2b50f91b897f487 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_one_hot_v2_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_one_hot_v2_op_xpu.py @@ -18,136 +18,130 @@ import unittest import numpy as np import paddle import paddle.fluid.core as core +import paddle.fluid as fluid import sys sys.path.append("..") from op_test_xpu import XPUOpTest -import paddle.fluid as fluid -from paddle.fluid import Program, program_guard -import time +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper paddle.enable_static() -class TestOneHotOp(XPUOpTest): +class XPUTestOneHotOp(XPUOpTestWrapper): - def setUp(self): - self.use_xpu = True - self.op_type = 'one_hot_v2' - depth = 10 - depth_np = np.array(10).astype('int32') - # dimension = 12 - x_lod = [[4, 1, 3, 3]] - x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] - x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) + def __init__(self): + self.op_name = 'one_hot_v2' + self.use_dynamic_create_class = False - out = np.zeros(shape=(np.product(x.shape), depth)).astype('float32') + class TestOneHotOp(XPUOpTest): - for i in range(np.product(x.shape)): - out[i, x[i]] = 1.0 + def init(self): + self.dtype = self.in_type + self.place = paddle.XPUPlace(0) + self.op_type = 'one_hot_v2' - self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np} - self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)} - self.outputs = {'Out': (out, x_lod)} + def setUp(self): + self.init() + depth = 10 + depth_np = np.array(10).astype('int32') + # dimension = 12 + x_lod = [[4, 1, 3, 3]] + x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] + x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) - def test_check_output(self): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) + out = np.zeros(shape=(np.product(x.shape), + depth)).astype(self.dtype) + for i in range(np.product(x.shape)): + out[i, x[i]] = 1.0 -class TestOneHotOp_attr(XPUOpTest): + self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np} + self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)} + self.outputs = {'Out': (out, x_lod)} - def setUp(self): - self.op_type = 'one_hot_v2' - depth = 10 - dimension = 12 - x_lod = [[4, 1, 3, 3]] - x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] - x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1]) + def test_check_output(self): + self.check_output_with_place(self.place) - out = np.zeros(shape=(np.product(x.shape[:-1]), 1, - depth)).astype('float32') + class TestOneHotOp_attr(TestOneHotOp): - for i in range(np.product(x.shape)): - out[i, 0, x[i]] = 1.0 + def setUp(self): + self.init() + depth = 10 + dimension = 12 + x_lod = [[4, 1, 3, 3]] + x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] + x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1]) - self.inputs = {'X': (x, x_lod)} - self.attrs = {'dtype': int(core.VarDesc.VarType.FP32), 'depth': depth} - self.outputs = {'Out': (out, x_lod)} + out = np.zeros(shape=(np.product(x.shape[:-1]), 1, + depth)).astype(self.dtype) - def test_check_output(self): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) + for i in range(np.product(x.shape)): + out[i, 0, x[i]] = 1.0 + self.inputs = {'X': (x, x_lod)} + self.attrs = { + 'dtype': int(core.VarDesc.VarType.FP32), + 'depth': depth + } + self.outputs = {'Out': (out, x_lod)} -class TestOneHotOp_default_dtype(XPUOpTest): + class TestOneHotOp_default_dtype(TestOneHotOp): - def setUp(self): - self.op_type = 'one_hot_v2' - depth = 10 - depth_np = np.array(10).astype('int32') - dimension = 12 - x_lod = [[4, 1, 3, 3]] - x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] - x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) + def setUp(self): + self.init() + depth = 10 + depth_np = np.array(10).astype('int32') + dimension = 12 + x_lod = [[4, 1, 3, 3]] + x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] + x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) - out = np.zeros(shape=(np.product(x.shape), depth)).astype('float32') + out = np.zeros(shape=(np.product(x.shape), + depth)).astype(self.dtype) - for i in range(np.product(x.shape)): - out[i, x[i]] = 1.0 + for i in range(np.product(x.shape)): + out[i, x[i]] = 1.0 - self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np} - self.attrs = {} - self.outputs = {'Out': (out, x_lod)} + self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np} + self.attrs = {} + self.outputs = {'Out': (out, x_lod)} - def test_check_output(self): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) + class TestOneHotOp_default_dtype_attr(TestOneHotOp): + def setUp(self): + self.init() + depth = 10 + dimension = 12 + x_lod = [[4, 1, 3, 3]] + x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] + x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1]) -class TestOneHotOp_default_dtype_attr(XPUOpTest): + out = np.zeros(shape=(np.product(x.shape[:-1]), 1, + depth)).astype(self.dtype) - def setUp(self): - self.op_type = 'one_hot_v2' - depth = 10 - dimension = 12 - x_lod = [[4, 1, 3, 3]] - x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] - x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1]) + for i in range(np.product(x.shape)): + out[i, 0, x[i]] = 1.0 - out = np.zeros(shape=(np.product(x.shape[:-1]), 1, - depth)).astype('float32') + self.inputs = {'X': (x, x_lod)} + self.attrs = {'depth': depth} + self.outputs = {'Out': (out, x_lod)} - for i in range(np.product(x.shape)): - out[i, 0, x[i]] = 1.0 + class TestOneHotOp_out_of_range(TestOneHotOp): - self.inputs = {'X': (x, x_lod)} - self.attrs = {'depth': depth} - self.outputs = {'Out': (out, x_lod)} + def setUp(self): + self.init() + depth = 10 + x_lod = [[4, 1, 3, 3]] + x = [np.random.choice([-1, depth]) for i in range(sum(x_lod[0]))] + x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) - def test_check_output(self): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) + out = np.zeros(shape=(np.product(x.shape), + depth)).astype(self.dtype) - -class TestOneHotOp_out_of_range(XPUOpTest): - - def setUp(self): - self.op_type = 'one_hot_v2' - depth = 10 - x_lod = [[4, 1, 3, 3]] - x = [np.random.choice([-1, depth]) for i in range(sum(x_lod[0]))] - x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) - - out = np.zeros(shape=(np.product(x.shape), depth)).astype('float32') - - self.inputs = {'X': (x, x_lod)} - self.attrs = {'depth': depth, 'allow_out_of_range': True} - self.outputs = {'Out': (out, x_lod)} - - def test_check_output(self): - place = paddle.XPUPlace(0) - self.check_output_with_place(place, check_dygraph=False) + self.inputs = {'X': (x, x_lod)} + self.attrs = {'depth': depth, 'allow_out_of_range': True} + self.outputs = {'Out': (out, x_lod)} class TestOneHotOpApi(unittest.TestCase): @@ -200,6 +194,9 @@ class BadInputTestOnehotV2(unittest.TestCase): self.assertRaises(TypeError, test_bad_x) +support_types = get_xpu_op_support_types('one_hot_v2') +for stype in support_types: + create_test_class(globals(), XPUTestOneHotOp, stype) + if __name__ == '__main__': - paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_reduce_mean_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_reduce_mean_op_xpu.py index ef483870c68ee0314901c31df86f9ed0ac405887..90fe474e09cd1d2b077bf1a15e6e8cddd550dbab 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_reduce_mean_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_reduce_mean_op_xpu.py @@ -19,196 +19,180 @@ import numpy as np import sys sys.path.append("..") -from op_test import OpTest, skip_check_grad_ci +from op_test_xpu import XPUOpTest +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper import paddle -import paddle.fluid.core as core -import paddle.fluid as fluid -from paddle.fluid import compiler, Program, program_guard -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +paddle.enable_static() -class TestMeanOp(OpTest): - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")} - self.attrs = {'use_xpu': True} - self.outputs = {'Out': self.inputs['X'].mean(axis=0)} +class XPUTestMeanOp(XPUOpTestWrapper): - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) + def __init__(self): + self.op_name = 'reduce_mean' + self.use_dynamic_create_class = False - def check_grad_(self): - self.check_grad(['X'], 'Out') + class TestMeanOp(XPUOpTest): + def setUp(self): + self.dtype = self.in_type + self.place = paddle.XPUPlace(0) + self.op_type = "reduce_mean" + self.inputs = {'X': np.random.random((5, 6, 10)).astype(self.dtype)} + self.attrs = {'use_xpu': True} + self.outputs = {'Out': self.inputs['X'].mean(axis=0)} -class TestMeanOp5D(OpTest): + def test_check_output(self): + self.check_output_with_place(self.place) - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = { - 'X': np.random.random((1, 2, 5, 6, 10)).astype("float32") - } - self.attrs = {'use_xpu': True} - self.outputs = {'Out': self.inputs['X'].mean(axis=0)} - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - self.check_grad(['X'], 'Out') - - -class TestMeanOp6D(OpTest): - - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = { - 'X': np.random.random((1, 1, 2, 5, 6, 10)).astype("float32") - } - self.attrs = {'use_xpu': True} - self.outputs = {'Out': self.inputs['X'].mean(axis=0)} - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - self.check_grad(['X'], 'Out') - - -class TestMeanOp8D(OpTest): - - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = { - 'X': np.random.random((1, 3, 1, 2, 1, 4, 3, 10)).astype("float32") - } - self.attrs = {'dim': (0, 3), 'use_xpu': True} - self.outputs = {'Out': self.inputs['X'].mean(axis=(0, 3))} - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - self.check_grad(['X'], 'Out') - - -class Test1DReduce(OpTest): - - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = {'X': np.random.random(120).astype("float32")} - self.attrs = {'use_xpu': True} - self.outputs = {'Out': self.inputs['X'].mean(axis=0)} - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - self.check_grad(['X'], 'Out') - - -class Test2DReduce0(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.attrs = {'dim': [0], 'use_xpu': True} - self.inputs = {'X': np.random.random((20, 10)).astype("float32")} - self.outputs = {'Out': self.inputs['X'].mean(axis=0)} - - -class Test2DReduce1(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.attrs = {'dim': [1], 'use_xpu': True} - self.inputs = {'X': np.random.random((20, 10)).astype("float32")} - self.outputs = { - 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) - } - - -class Test3DReduce0(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.attrs = {'dim': [1], 'use_xpu': True} - self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")} - self.outputs = { - 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) - } - - -class Test3DReduce1(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.attrs = {'dim': [2], 'use_xpu': True} - self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")} - self.outputs = { - 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) - } - - -class Test3DReduce2(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.attrs = {'dim': [-2], 'use_xpu': True} - self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")} - self.outputs = { - 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) - } - - -class Test3DReduce3(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.attrs = {'dim': [1, 2], 'use_xpu': True} - self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")} - self.outputs = { - 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) - } - - -class TestKeepDimReduce(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")} - self.attrs = {'dim': [1], 'keep_dim': True, 'use_xpu': True} - self.outputs = { - 'Out': - self.inputs['X'].mean(axis=tuple(self.attrs['dim']), - keepdims=self.attrs['keep_dim']) - } - - -class TestKeepDim8DReduce(Test1DReduce): - - def setUp(self): - self.op_type = "reduce_mean" - self.inputs = { - 'X': np.random.random((2, 5, 3, 2, 2, 3, 4, 2)).astype("float32") - } - self.attrs = {'dim': (3, 4, 5), 'keep_dim': True, 'use_xpu': True} - self.outputs = { - 'Out': - self.inputs['X'].mean(axis=tuple(self.attrs['dim']), - keepdims=self.attrs['keep_dim']) - } + def test_check_grad(self): + self.check_grad_with_place(self.place, ['X'], 'Out') + class TestMeanOp5D(TestMeanOp): + + def setUp(self): + super().setUp() + self.inputs = { + 'X': np.random.random((1, 2, 5, 6, 10)).astype(self.dtype) + } + self.attrs = {'use_xpu': True} + self.outputs = {'Out': self.inputs['X'].mean(axis=0)} + + class TestMeanOp6D(TestMeanOp): + + def setUp(self): + super().setUp() + self.inputs = { + 'X': np.random.random((1, 1, 2, 5, 6, 10)).astype(self.dtype) + } + self.attrs = {'use_xpu': True} + self.outputs = {'Out': self.inputs['X'].mean(axis=0)} + + class TestMeanOp8D(TestMeanOp): + + def setUp(self): + super().setUp() + self.inputs = { + 'X': np.random.random( + (1, 3, 1, 2, 1, 4, 3, 10)).astype(self.dtype) + } + self.attrs = {'dim': (0, 3), 'use_xpu': True} + self.outputs = {'Out': self.inputs['X'].mean(axis=(0, 3))} + + +class XPUTestReduce(XPUOpTestWrapper): + + def __init__(self): + self.op_name = 'reduce_mean' + self.use_dynamic_create_class = False + + class Test1DReduce(XPUOpTest): + + def setUp(self): + self.dtype = self.in_type + self.place = paddle.XPUPlace(0) + self.op_type = "reduce_mean" + self.inputs = {'X': np.random.random(120).astype(self.dtype)} + self.attrs = {'use_xpu': True} + self.outputs = {'Out': self.inputs['X'].mean(axis=0)} + + def test_check_output(self): + self.check_output_with_place(self.place) + + # There is a api bug in checking grad when dim[0] > 0 + # def test_check_grad(self): + # self.check_output_with_place(self.place, ['X'], 'Out') + + class Test2DReduce0(Test1DReduce): + + def setUp(self): + super().setUp() + self.attrs = {'dim': [0], 'use_xpu': True} + self.inputs = {'X': np.random.random((20, 10)).astype(self.dtype)} + self.outputs = {'Out': self.inputs['X'].mean(axis=0)} + + class Test2DReduce1(Test1DReduce): + + def setUp(self): + super().setUp() + self.attrs = {'dim': [1], 'use_xpu': True} + self.inputs = {'X': np.random.random((20, 10)).astype(self.dtype)} + self.outputs = { + 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) + } + + class Test3DReduce0(Test1DReduce): + + def setUp(self): + super().setUp() + self.attrs = {'dim': [1], 'use_xpu': True} + self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)} + self.outputs = { + 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) + } + + class Test3DReduce1(Test1DReduce): + + def setUp(self): + super().setUp() + self.attrs = {'dim': [2], 'use_xpu': True} + self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)} + self.outputs = { + 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) + } + + class Test3DReduce2(Test1DReduce): + + def setUp(self): + super().setUp() + self.attrs = {'dim': [-2], 'use_xpu': True} + self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)} + self.outputs = { + 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) + } + + class Test3DReduce3(Test1DReduce): + + def setUp(self): + super().setUp() + self.attrs = {'dim': [1, 2], 'use_xpu': True} + self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)} + self.outputs = { + 'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim'])) + } + + class TestKeepDimReduce(Test1DReduce): + + def setUp(self): + super().setUp() + self.inputs = {'X': np.random.random((5, 6, 10)).astype(self.dtype)} + self.attrs = {'dim': [1], 'keep_dim': True, 'use_xpu': True} + self.outputs = { + 'Out': + self.inputs['X'].mean(axis=tuple(self.attrs['dim']), + keepdims=self.attrs['keep_dim']) + } + + class TestKeepDim8DReduce(Test1DReduce): + + def setUp(self): + super().setUp() + self.inputs = { + 'X': np.random.random( + (2, 5, 3, 2, 2, 3, 4, 2)).astype(self.dtype) + } + self.attrs = {'dim': (3, 4, 5), 'keep_dim': True, 'use_xpu': True} + self.outputs = { + 'Out': + self.inputs['X'].mean(axis=tuple(self.attrs['dim']), + keepdims=self.attrs['keep_dim']) + } + + +support_types = get_xpu_op_support_types('reduce_mean') +for stype in support_types: + create_test_class(globals(), XPUTestMeanOp, stype) + create_test_class(globals(), XPUTestReduce, stype) if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_roi_align_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_roi_align_op_xpu.py index 4c830b1e8729a6472cd5bf5dd2993cd3c1c3c535..deebd2e02ff8addb91f296207bb15c554eb05ea3 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_roi_align_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_roi_align_op_xpu.py @@ -20,208 +20,220 @@ import unittest import math import numpy as np import paddle.fluid.core as core -from op_test import OpTest, skip_check_grad_ci from op_test_xpu import XPUOpTest import paddle -import paddle.fluid as fluid -from paddle.fluid import Program, program_guard - - -class TestROIAlignOp(XPUOpTest): - - def set_data(self): - self.init_test_case() - self.make_rois() - self.calc_roi_align() - - self.inputs = { - 'X': self.x, - 'ROIs': (self.rois[:, 1:5], self.rois_lod), - } - self.attrs = { - 'spatial_scale': self.spatial_scale, - 'pooled_height': self.pooled_height, - 'pooled_width': self.pooled_width, - 'sampling_ratio': self.sampling_ratio, - 'aligned': self.continuous_coordinate - } - - self.outputs = {'Out': self.out_data} - - def init_test_case(self): - self.batch_size = 3 - self.channels = 3 - self.height = 8 - self.width = 6 - - self.xpu_version = core.get_xpu_device_version(0) - - # n, c, h, w - self.x_dim = (self.batch_size, self.channels, self.height, self.width) - - self.spatial_scale = 1.0 / 2.0 - self.pooled_height = 2 - self.pooled_width = 2 - self.sampling_ratio = -1 - if self.xpu_version == core.XPUVersion.XPU1: - self.continuous_coordinate = False - else: - self.continuous_coordinate = bool(np.random.randint(2)) - self.x = np.random.random(self.x_dim).astype('float32') - - def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h, roi_bin_grid_w, - bin_size_h, bin_size_w): - count = roi_bin_grid_h * roi_bin_grid_w - bilinear_pos = np.zeros( - [self.channels, self.pooled_height, self.pooled_width, count, 4], - np.float32) - bilinear_w = np.zeros([self.pooled_height, self.pooled_width, count, 4], - np.float32) - for ph in range(self.pooled_width): - for pw in range(self.pooled_height): - c = 0 - for iy in range(roi_bin_grid_h): - y = roi_ymin + ph * bin_size_h + (iy + 0.5) * \ - bin_size_h / roi_bin_grid_h - for ix in range(roi_bin_grid_w): - x = roi_xmin + pw * bin_size_w + (ix + 0.5) * \ - bin_size_w / roi_bin_grid_w - if y < -1.0 or y > self.height or \ - x < -1.0 or x > self.width: - continue - if y <= 0: - y = 0 - if x <= 0: - x = 0 - y_low = int(y) - x_low = int(x) - if y_low >= self.height - 1: - y = y_high = y_low = self.height - 1 - else: - y_high = y_low + 1 - if x_low >= self.width - 1: - x = x_high = x_low = self.width - 1 - else: - x_high = x_low + 1 - ly = y - y_low - lx = x - x_low - hy = 1 - ly - hx = 1 - lx - for ch in range(self.channels): - bilinear_pos[ch, ph, pw, c, 0] = x_i[ch, y_low, - x_low] - bilinear_pos[ch, ph, pw, c, 1] = x_i[ch, y_low, - x_high] - bilinear_pos[ch, ph, pw, c, 2] = x_i[ch, y_high, - x_low] - bilinear_pos[ch, ph, pw, c, 3] = x_i[ch, y_high, - x_high] - bilinear_w[ph, pw, c, 0] = hy * hx - bilinear_w[ph, pw, c, 1] = hy * lx - bilinear_w[ph, pw, c, 2] = ly * hx - bilinear_w[ph, pw, c, 3] = ly * lx - c = c + 1 - return bilinear_pos, bilinear_w - - def calc_roi_align(self): - self.out_data = np.zeros( - (self.rois_num, self.channels, self.pooled_height, - self.pooled_width)).astype('float32') - - for i in range(self.rois_num): - roi = self.rois[i] - roi_batch_id = int(roi[0]) - x_i = self.x[roi_batch_id] - roi_offset = 0.5 if self.continuous_coordinate else 0 - roi_xmin = roi[1] * self.spatial_scale - roi_offset - roi_ymin = roi[2] * self.spatial_scale - roi_offset - roi_xmax = roi[3] * self.spatial_scale - roi_offset - roi_ymax = roi[4] * self.spatial_scale - roi_offset - roi_width = roi_xmax - roi_xmin - roi_height = roi_ymax - roi_ymin - if not self.continuous_coordinate: - roi_width = max(roi_width, 1) - roi_height = max(roi_height, 1) - bin_size_h = float(roi_height) / float(self.pooled_height) - bin_size_w = float(roi_width) / float(self.pooled_width) - roi_bin_grid_h = self.sampling_ratio if self.sampling_ratio > 0 else \ - math.ceil(roi_height / self.pooled_height) - roi_bin_grid_w = self.sampling_ratio if self.sampling_ratio > 0 else \ - math.ceil(roi_width / self.pooled_width) - count = int(roi_bin_grid_h * roi_bin_grid_w) - pre_size = count * self.pooled_width * self.pooled_height - bilinear_pos, bilinear_w = self.pre_calc(x_i, roi_xmin, roi_ymin, - int(roi_bin_grid_h), - int(roi_bin_grid_w), - bin_size_h, bin_size_w) - for ch in range(self.channels): - align_per_bin = (bilinear_pos[ch] * bilinear_w).sum(axis=-1) - output_val = align_per_bin.mean(axis=-1) - self.out_data[i, ch, :, :] = output_val - - def make_rois(self): - rois = [] - self.rois_lod = [[]] - for bno in range(self.batch_size): - self.rois_lod[0].append(bno + 1) - for i in range(bno + 1): - x1 = np.random.random_integers( - 0, self.width // self.spatial_scale - self.pooled_width) - y1 = np.random.random_integers( - 0, self.height // self.spatial_scale - self.pooled_height) - - x2 = np.random.random_integers(x1 + self.pooled_width, - self.width // self.spatial_scale) - y2 = np.random.random_integers( - y1 + self.pooled_height, self.height // self.spatial_scale) - - roi = [bno, x1, y1, x2, y2] - rois.append(roi) - self.rois_num = len(rois) - self.rois = np.array(rois).astype("float32") - - def setUp(self): - self.op_type = "roi_align" - self.set_data() - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - if core.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_grad_with_place(place, {'X'}, 'Out') - - -class TestROIAlignInLodOp(TestROIAlignOp): - - def set_data(self): - self.init_test_case() - self.make_rois() - self.calc_roi_align() - - seq_len = self.rois_lod[0] - - self.inputs = { - 'X': self.x, - 'ROIs': (self.rois[:, 1:5], self.rois_lod), - 'RoisNum': np.asarray(seq_len).astype('int32') - } - - self.attrs = { - 'spatial_scale': self.spatial_scale, - 'pooled_height': self.pooled_height, - 'pooled_width': self.pooled_width, - 'sampling_ratio': self.sampling_ratio, - 'aligned': self.continuous_coordinate - } - - self.outputs = {'Out': self.out_data} - +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper + +paddle.enable_static() + + +class XPUTestROIAlignOp(XPUOpTestWrapper): + + def __init__(self): + self.op_name = 'roi_align' + self.use_dynamic_create_class = False + + class TestROIAlignOp(XPUOpTest): + + def set_data(self): + self.init_test_case() + self.make_rois() + self.calc_roi_align() + + self.inputs = { + 'X': self.x, + 'ROIs': (self.rois[:, 1:5], self.rois_lod), + } + self.attrs = { + 'spatial_scale': self.spatial_scale, + 'pooled_height': self.pooled_height, + 'pooled_width': self.pooled_width, + 'sampling_ratio': self.sampling_ratio, + 'aligned': self.continuous_coordinate + } + + self.outputs = {'Out': self.out_data} + + def init_test_case(self): + self.batch_size = 3 + self.channels = 3 + self.height = 8 + self.width = 6 + + self.xpu_version = core.get_xpu_device_version(0) + + # n, c, h, w + self.x_dim = (self.batch_size, self.channels, self.height, + self.width) + + self.spatial_scale = 1.0 / 2.0 + self.pooled_height = 2 + self.pooled_width = 2 + self.sampling_ratio = -1 + if self.xpu_version == core.XPUVersion.XPU1: + self.continuous_coordinate = False + else: + self.continuous_coordinate = bool(np.random.randint(2)) + self.x = np.random.random(self.x_dim).astype(self.dtype) + + def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h, + roi_bin_grid_w, bin_size_h, bin_size_w): + count = roi_bin_grid_h * roi_bin_grid_w + bilinear_pos = np.zeros([ + self.channels, self.pooled_height, self.pooled_width, count, 4 + ], np.float32) + bilinear_w = np.zeros( + [self.pooled_height, self.pooled_width, count, 4], np.float32) + for ph in range(self.pooled_width): + for pw in range(self.pooled_height): + c = 0 + for iy in range(roi_bin_grid_h): + y = roi_ymin + ph * bin_size_h + (iy + 0.5) * \ + bin_size_h / roi_bin_grid_h + for ix in range(roi_bin_grid_w): + x = roi_xmin + pw * bin_size_w + (ix + 0.5) * \ + bin_size_w / roi_bin_grid_w + if y < -1.0 or y > self.height or \ + x < -1.0 or x > self.width: + continue + if y <= 0: + y = 0 + if x <= 0: + x = 0 + y_low = int(y) + x_low = int(x) + if y_low >= self.height - 1: + y = y_high = y_low = self.height - 1 + else: + y_high = y_low + 1 + if x_low >= self.width - 1: + x = x_high = x_low = self.width - 1 + else: + x_high = x_low + 1 + ly = y - y_low + lx = x - x_low + hy = 1 - ly + hx = 1 - lx + for ch in range(self.channels): + bilinear_pos[ch, ph, pw, c, 0] = x_i[ch, y_low, + x_low] + bilinear_pos[ch, ph, pw, c, 1] = x_i[ch, y_low, + x_high] + bilinear_pos[ch, ph, pw, c, 2] = x_i[ch, y_high, + x_low] + bilinear_pos[ch, ph, pw, c, 3] = x_i[ch, y_high, + x_high] + bilinear_w[ph, pw, c, 0] = hy * hx + bilinear_w[ph, pw, c, 1] = hy * lx + bilinear_w[ph, pw, c, 2] = ly * hx + bilinear_w[ph, pw, c, 3] = ly * lx + c = c + 1 + return bilinear_pos, bilinear_w + + def calc_roi_align(self): + self.out_data = np.zeros( + (self.rois_num, self.channels, self.pooled_height, + self.pooled_width)).astype(self.dtype) + + for i in range(self.rois_num): + roi = self.rois[i] + roi_batch_id = int(roi[0]) + x_i = self.x[roi_batch_id] + roi_offset = 0.5 if self.continuous_coordinate else 0 + roi_xmin = roi[1] * self.spatial_scale - roi_offset + roi_ymin = roi[2] * self.spatial_scale - roi_offset + roi_xmax = roi[3] * self.spatial_scale - roi_offset + roi_ymax = roi[4] * self.spatial_scale - roi_offset + roi_width = roi_xmax - roi_xmin + roi_height = roi_ymax - roi_ymin + if not self.continuous_coordinate: + roi_width = max(roi_width, 1) + roi_height = max(roi_height, 1) + bin_size_h = float(roi_height) / float(self.pooled_height) + bin_size_w = float(roi_width) / float(self.pooled_width) + roi_bin_grid_h = self.sampling_ratio if self.sampling_ratio > 0 else \ + math.ceil(roi_height / self.pooled_height) + roi_bin_grid_w = self.sampling_ratio if self.sampling_ratio > 0 else \ + math.ceil(roi_width / self.pooled_width) + count = int(roi_bin_grid_h * roi_bin_grid_w) + pre_size = count * self.pooled_width * self.pooled_height + bilinear_pos, bilinear_w = self.pre_calc( + x_i, roi_xmin, roi_ymin, int(roi_bin_grid_h), + int(roi_bin_grid_w), bin_size_h, bin_size_w) + for ch in range(self.channels): + align_per_bin = (bilinear_pos[ch] * bilinear_w).sum(axis=-1) + output_val = align_per_bin.mean(axis=-1) + self.out_data[i, ch, :, :] = output_val + + def make_rois(self): + rois = [] + self.rois_lod = [[]] + for bno in range(self.batch_size): + self.rois_lod[0].append(bno + 1) + for i in range(bno + 1): + x1 = np.random.random_integers( + 0, self.width // self.spatial_scale - self.pooled_width) + y1 = np.random.random_integers( + 0, + self.height // self.spatial_scale - self.pooled_height) + + x2 = np.random.random_integers( + x1 + self.pooled_width, + self.width // self.spatial_scale) + y2 = np.random.random_integers( + y1 + self.pooled_height, + self.height // self.spatial_scale) + + roi = [bno, x1, y1, x2, y2] + rois.append(roi) + self.rois_num = len(rois) + self.rois = np.array(rois).astype(self.dtype) + + def setUp(self): + self.set_xpu() + self.op_type = "roi_align" + self.place = paddle.XPUPlace(0) + self.dtype = self.in_type + self.set_data() + + def set_xpu(self): + self.__class__.use_xpu = True + + def test_check_output(self): + self.check_output_with_place(self.place) + + def test_check_grad(self): + self.check_grad_with_place(self.place, {'X'}, 'Out') + + class TestROIAlignInLodOp(TestROIAlignOp): + + def set_data(self): + self.init_test_case() + self.make_rois() + self.calc_roi_align() + + seq_len = self.rois_lod[0] + + self.inputs = { + 'X': self.x, + 'ROIs': (self.rois[:, 1:5], self.rois_lod), + 'RoisNum': np.asarray(seq_len).astype('int32') + } + + self.attrs = { + 'spatial_scale': self.spatial_scale, + 'pooled_height': self.pooled_height, + 'pooled_width': self.pooled_width, + 'sampling_ratio': self.sampling_ratio, + 'aligned': self.continuous_coordinate + } + + self.outputs = {'Out': self.out_data} + + +support_types = get_xpu_op_support_types('roi_align') +for stype in support_types: + create_test_class(globals(), XPUTestROIAlignOp, stype) if __name__ == '__main__': unittest.main()