From df42355709e5f1076ff2c6d50164000cafc5b8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=89=91=E5=AF=92?= Date: Wed, 29 Mar 2023 10:25:08 +0800 Subject: [PATCH] [AMP OP&Test] add bf16 fp16 test case for interpolate (#51160) --- .../kernels/cpu/interpolate_grad_kernel.cc | 1 - .../unittests/test_bicubic_interp_v2_op.py | 339 +++++++++++++--- .../unittests/test_bilinear_interp_v2_op.py | 365 ++++++++++++++--- .../unittests/test_linear_interp_v2_op.py | 122 +++++- .../unittests/test_nearest_interp_v2_op.py | 369 ++++++++++++++--- .../unittests/test_trilinear_interp_v2_op.py | 378 +++++++++++++++--- 6 files changed, 1338 insertions(+), 236 deletions(-) diff --git a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc index c1e7b0129e5..83f1a452fe4 100644 --- a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc @@ -110,7 +110,6 @@ static void BilinearInterpolationGrad(const DenseTensor& output_grad, for (int j = 0; j < c; j++) { // loop for channels // bilinear interpolation grad if (data_layout == DataLayout::kNCHW) { - // const T grad = output_grad_t(i, j, k, l); const MT grad = static_cast(output_grad_t(i, j, k, l)); input_grad_t(i, j, y_n, x_w) += static_cast(grad * d_s * d_e); input_grad_t(i, j, y_s, x_w) += static_cast(grad * d_n * d_e); diff --git a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py index b0ea2d50b1a..bebd91d4557 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py @@ -15,14 +15,81 @@ import unittest import numpy as np -from eager_op_test import OpTest +from eager_op_test import OpTest, convert_float_to_uint16 import paddle from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle.fluid import Program, core, program_guard from paddle.nn.functional import interpolate +def create_test_case0(self): + self.interp_method = 'bicubic' + self.input_shape = [2, 3, 5, 5] + self.out_h = 2 + self.out_w = 2 + self.scale = [] + self.out_size = np.array([3, 3]).astype("int32") + self.align_corners = True + + +def create_test_case1(self): + self.interp_method = 'bicubic' + self.input_shape = [4, 1, 7, 8] + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.align_corners = True + + +def create_test_case2(self): + self.interp_method = 'bicubic' + self.input_shape = [3, 3, 9, 6] + self.out_h = 10 + self.out_w = 8 + self.scale = [] + self.align_corners = True + + +def create_test_case3(self): + self.interp_method = 'bicubic' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = [] + self.align_corners = False + + +def create_test_case4(self): + self.interp_method = 'bicubic' + self.input_shape = [4, 1, 7, 8] + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.out_size = np.array([2, 2]).astype("int32") + self.align_corners = True + + +def create_test_case5(self): + self.interp_method = 'bicubic' + self.input_shape = [3, 3, 9, 6] + self.out_h = 11 + self.out_w = 11 + self.scale = [] + self.out_size = np.array([6, 4]).astype("int32") + self.align_corners = False + + +def create_test_case6(self): + self.interp_method = 'bicubic' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = 0 + self.out_size = np.array([64, 32]).astype("int32") + self.align_corners = False + + def bicubic_interp_test( x, OutSize=None, @@ -181,10 +248,12 @@ class TestBicubicInterpOp(OpTest): self.out_size = None self.actual_shape = None self.data_layout = 'NCHW' + self.dtype = np.float64 self.init_test_case() self.op_type = "bicubic_interp_v2" # NOTE(dev): some AsDispensible input is not used under imperative mode. - input_np = np.random.random(self.input_shape).astype("float64") + # Skip check_dygraph while found them in Inputs. + input_np = np.random.random(self.input_shape).astype(self.dtype) scale_h = 0 scale_w = 0 if self.data_layout == "NCHW": @@ -249,76 +318,248 @@ class TestBicubicInterpOp(OpTest): self.check_grad(['X'], 'Out', in_place=True) def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [2, 3, 5, 5] - self.out_h = 2 - self.out_w = 2 - self.scale = [] - self.out_size = np.array([3, 3]).astype("int32") - self.align_corners = True + create_test_case0(self) class TestBicubicInterpCase1(TestBicubicInterpOp): def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [4, 1, 7, 8] - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.align_corners = True + create_test_case1(self) class TestBicubicInterpCase2(TestBicubicInterpOp): def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [3, 3, 9, 6] - self.out_h = 10 - self.out_w = 8 - self.scale = [] - self.align_corners = True + create_test_case2(self) class TestBicubicInterpCase3(TestBicubicInterpOp): def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = [] - self.align_corners = False + create_test_case3(self) class TestBicubicInterpCase4(TestBicubicInterpOp): def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [4, 1, 7, 8] - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.out_size = np.array([2, 2]).astype("int32") - self.align_corners = True + create_test_case4(self) class TestBicubicInterpCase5(TestBicubicInterpOp): def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [3, 3, 9, 6] - self.out_h = 11 - self.out_w = 11 - self.scale = [] - self.out_size = np.array([6, 4]).astype("int32") - self.align_corners = False + create_test_case5(self) class TestBicubicInterpCase6(TestBicubicInterpOp): def init_test_case(self): - self.interp_method = 'bicubic' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = 0 - self.out_size = np.array([64, 32]).astype("int32") - self.align_corners = False + create_test_case6(self) + + +class TestBicubicInterpOpFP16(TestBicubicInterpOp): + def test_check_output(self): + self.check_output(atol=1e-3) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + self.dtype = np.float16 + + +class TestBicubicInterpCase1FP16(TestBicubicInterpOpFP16): + def init_test_case(self): + create_test_case1(self) + self.dtype = np.float16 + + +# this case will cause accuracy loss (backward:0.008773442) +# class TestBicubicInterpCase2FP16(TestBicubicInterpOpFP16): +# def init_test_case(self): +# self.interp_method = 'bicubic' +# self.input_shape = [3, 3, 9, 6] +# self.out_h = 10 +# self.out_w = 8 +# self.scale = [] +# self.align_corners = True + + +class TestBicubicInterpCase3FP16(TestBicubicInterpOpFP16): + def init_test_case(self): + create_test_case3(self) + self.dtype = np.float16 + + +class TestBicubicInterpCase4FP16(TestBicubicInterpOpFP16): + def init_test_case(self): + create_test_case4(self) + self.dtype = np.float16 + + +class TestBicubicInterpCase5FP16(TestBicubicInterpOpFP16): + def init_test_case(self): + create_test_case5(self) + self.dtype = np.float16 + + +class TestBicubicInterpCase6FP16(TestBicubicInterpOpFP16): + def init_test_case(self): + create_test_case6(self) + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpOpBF16(OpTest): + def setUp(self): + self.python_api = bicubic_interp_test + self.out_size = None + self.actual_shape = None + self.data_layout = 'NCHW' + self.init_test_case() + self.op_type = "bicubic_interp_v2" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_dygraph while found them in Inputs. + self.check_dygraph = True + self.dtype = np.uint16 + input_np = np.random.random(self.input_shape).astype("float32") + scale_h = 0 + scale_w = 0 + if self.data_layout == "NCHW": + in_h = self.input_shape[2] + in_w = self.input_shape[3] + else: + in_h = self.input_shape[1] + in_w = self.input_shape[2] + + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0.0: + scale_h = scale_w = float(self.scale) + if isinstance(self.scale, list) and len(self.scale) == 1: + scale_w = scale_h = self.scale[0] + elif isinstance(self.scale, list) and len(self.scale) > 1: + scale_w = self.scale[1] + scale_h = self.scale[0] + out_h = int(in_h * scale_h) + out_w = int(in_w * scale_w) + else: + out_h = self.out_h + out_w = self.out_w + + output_np = bicubic_interp_np( + input_np, + out_h, + out_w, + scale_h, + scale_w, + self.out_size, + self.actual_shape, + self.align_corners, + self.data_layout, + ) + self.inputs = {'X': convert_float_to_uint16(input_np)} + if self.out_size is not None: + self.inputs['OutSize'] = self.out_size + self.check_dygraph = False + if self.actual_shape is not None: + self.inputs['OutSize'] = self.actual_shape + self.check_dygraph = False + + self.attrs = { + 'out_h': self.out_h, + 'out_w': self.out_w, + 'interp_method': self.interp_method, + 'align_corners': self.align_corners, + 'data_layout': self.data_layout, + } + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0.0: + self.scale = [self.scale] + if isinstance(self.scale, list) and len(self.scale) == 1: + self.scale = [self.scale[0], self.scale[0]] + self.attrs['scale'] = self.scale + self.outputs = {'Out': convert_float_to_uint16(output_np)} + + def test_check_output(self): + self.check_output(check_dygraph=self.check_dygraph, atol=1e-2) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + check_dygraph=self.check_dygraph, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpCase1BF16(TestBicubicInterpOpBF16): + def init_test_case(self): + create_test_case1(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpCase2BF16(TestBicubicInterpOpBF16): + def init_test_case(self): + create_test_case2(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpCase3BF16(TestBicubicInterpOpBF16): + def init_test_case(self): + create_test_case3(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpCase4BF16(TestBicubicInterpOpBF16): + def init_test_case(self): + create_test_case4(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpCase5BF16(TestBicubicInterpOpBF16): + def init_test_case(self): + create_test_case5(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBicubicInterpCase6BF16(TestBicubicInterpOpBF16): + def init_test_case(self): + create_test_case6(self) class TestBicubicInterpSame(TestBicubicInterpOp): diff --git a/python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py index e682d1bef29..1b3f615fde3 100755 --- a/python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from eager_op_test import OpTest +from eager_op_test import OpTest, convert_float_to_uint16 import paddle from paddle import fluid @@ -23,6 +23,90 @@ from paddle.fluid import core from paddle.nn.functional import interpolate +def create_test_case0(self): + self.interp_method = 'bilinear' + self.input_shape = [2, 3, 5, 5] + self.out_h = 2 + self.out_w = 2 + self.scale = [] + self.out_size = np.array([3, 3]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case1(self): + self.interp_method = 'bilinear' + self.input_shape = [4, 1, 7, 8] + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.align_corners = True + self.align_mode = 1 + + +def create_test_case2(self): + self.interp_method = 'bilinear' + self.input_shape = [3, 3, 9, 6] + self.out_h = 12 + self.out_w = 12 + self.scale = [] + self.align_corners = True + self.align_mode = 1 + + +def create_test_case3(self): + self.interp_method = 'bilinear' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = [] + self.align_corners = True + self.align_mode = 1 + + +def create_test_case4(self): + self.interp_method = 'bilinear' + self.input_shape = [4, 1, 7, 8] + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.out_size = np.array([2, 2]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case5(self): + self.interp_method = 'bilinear' + self.input_shape = [3, 3, 9, 6] + self.out_h = 12 + self.out_w = 12 + self.scale = [] + self.out_size = np.array([11, 11]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case6(self): + self.interp_method = 'bilinear' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = [] + self.out_size = np.array([65, 33]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case7(self): + self.interp_method = 'bilinear' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = [2.0, 0.5] + self.align_corners = False + self.align_mode = 1 + + def bilinear_interp_test( x, OutSize=None, @@ -156,9 +240,10 @@ class TestBilinearInterpOp(OpTest): self.out_size = None self.actual_shape = None self.data_layout = 'NCHW' + self.dtype = np.float64 self.init_test_case() self.op_type = "bilinear_interp_v2" - input_np = np.random.random(self.input_shape).astype("float64") + input_np = np.random.random(self.input_shape).astype(self.dtype) if self.data_layout == "NCHW": in_h = self.input_shape[2] @@ -225,94 +310,42 @@ class TestBilinearInterpOp(OpTest): self.check_grad(['X'], 'Out', in_place=True) def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [2, 3, 5, 5] - self.out_h = 2 - self.out_w = 2 - self.scale = [] - self.out_size = np.array([3, 3]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case0(self) class TestBilinearInterpCase1(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [4, 1, 7, 8] - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.align_corners = True - self.align_mode = 1 + create_test_case1(self) class TestBilinearInterpCase2(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [3, 3, 9, 6] - self.out_h = 12 - self.out_w = 12 - self.scale = [] - self.align_corners = True - self.align_mode = 1 + create_test_case2(self) class TestBilinearInterpCase3(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = [] - self.align_corners = True - self.align_mode = 1 + create_test_case3(self) class TestBilinearInterpCase4(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [4, 1, 7, 8] - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.out_size = np.array([2, 2]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case4(self) class TestBilinearInterpCase5(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [3, 3, 9, 6] - self.out_h = 12 - self.out_w = 12 - self.scale = [] - self.out_size = np.array([11, 11]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case5(self) class TestBilinearInterpCase6(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = [] - self.out_size = np.array([65, 33]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case6(self) class TestBilinearInterpCase7(TestBilinearInterpOp): def init_test_case(self): - self.interp_method = 'bilinear' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = [2.0, 0.5] - self.align_corners = False - self.align_mode = 1 + create_test_case7(self) class TestBilinearInterpSame(TestBilinearInterpOp): @@ -351,6 +384,214 @@ class TestBilinearInterpDataLayout(TestBilinearInterpOp): self.data_layout = "NHWC" +class TestBilinearInterpOpFP16(TestBilinearInterpOp): + def test_check_output(self): + self.check_output(atol=1e-3) + + def test_check_grad(self): + self.check_grad(['X'], 'Out', in_place=True, max_relative_error=1e-2) + + def init_test_case(self): + create_test_case0(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase1FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case1(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase2FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case2(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase3FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case3(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase4FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case4(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase5FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case5(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase6FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case6(self) + self.dtype = np.float16 + + +class TestBilinearInterpCase7FP16(TestBilinearInterpOpFP16): + def init_test_case(self): + create_test_case7(self) + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpOpBF16(OpTest): + def setUp(self): + self.python_api = bilinear_interp_test + self.out_size = None + self.actual_shape = None + self.data_layout = 'NCHW' + self.init_test_case() + self.op_type = "bilinear_interp_v2" + self.dtype = np.uint16 + input_np = np.random.random(self.input_shape).astype("float32") + + if self.data_layout == "NCHW": + in_h = self.input_shape[2] + in_w = self.input_shape[3] + else: + in_h = self.input_shape[1] + in_w = self.input_shape[2] + scale_h = 0 + scale_w = 0 + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0.0: + scale_h = scale_w = float(self.scale) + if isinstance(self.scale, list) and len(self.scale) == 1: + scale_w = scale_h = self.scale[0] + elif isinstance(self.scale, list) and len(self.scale) > 1: + scale_w = self.scale[1] + scale_h = self.scale[0] + out_h = int(in_h * scale_h) + out_w = int(in_w * scale_w) + else: + out_h = self.out_h + out_w = self.out_w + + output_np = bilinear_interp_np( + input_np, + out_h, + out_w, + 0, + 0, + self.out_size, + self.actual_shape, + self.align_corners, + self.align_mode, + self.data_layout, + ) + self.inputs = {'X': convert_float_to_uint16(input_np)} + if self.out_size is not None: + self.inputs['OutSize'] = self.out_size + if self.actual_shape is not None: + self.inputs['OutSize'] = self.actual_shape + + self.attrs = { + 'out_h': self.out_h, + 'out_w': self.out_w, + 'interp_method': self.interp_method, + 'align_corners': self.align_corners, + 'align_mode': self.align_mode, + 'data_layout': self.data_layout, + } + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0.0: + self.scale = [self.scale] + if isinstance(self.scale, list) and len(self.scale) == 1: + self.scale = [self.scale[0], self.scale[0]] + self.attrs['scale'] = self.scale + self.outputs = {'Out': convert_float_to_uint16(output_np)} + + def test_check_output(self): + self.check_output(atol=1e-2) + + def test_check_grad(self): + self.check_grad(['X'], 'Out', in_place=True, max_relative_error=1e-2) + + def init_test_case(self): + create_test_case0(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase1BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case1(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase2BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case2(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase3BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case3(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase4BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case4(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase5BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case5(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase6BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case6(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestBilinearInterpCase7BF16(TestBilinearInterpOpBF16): + def init_test_case(self): + create_test_case7(self) + + class TestBilinearInterpOpUint8(OpTest): def setUp(self): self.python_api = bilinear_interp_test diff --git a/python/paddle/fluid/tests/unittests/test_linear_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_linear_interp_v2_op.py index 9719c02b85e..323e1d8ca5b 100755 --- a/python/paddle/fluid/tests/unittests/test_linear_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_linear_interp_v2_op.py @@ -16,7 +16,7 @@ import platform import unittest import numpy as np -from eager_op_test import OpTest, paddle_static_guard +from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle from paddle import fluid @@ -24,6 +24,20 @@ from paddle.fluid import Program, core, program_guard from paddle.nn.functional import interpolate +def create_test_case0(self): + self.interp_method = 'linear' + self.input_shape = [1, 3, 100] + self.out_w = 50 + self.scale = 0.5 + self.out_size = np.array( + [ + 50, + ] + ).astype("int32") + self.align_corners = False + self.align_mode = 1 + + def linear_interp_test( x, OutSize=None, @@ -127,9 +141,10 @@ class TestLinearInterpOp(OpTest): self.out_size = None self.actual_shape = None self.data_layout = 'NCHW' + self.dtype = np.float64 self.init_test_case() self.op_type = "linear_interp_v2" - input_np = np.random.random(self.input_shape).astype("float64") + input_np = np.random.random(self.input_shape).astype(self.dtype) scale_w = 0 if self.data_layout == "NCHW": @@ -185,17 +200,7 @@ class TestLinearInterpOp(OpTest): self.check_grad(['X'], 'Out', in_place=True) def init_test_case(self): - self.interp_method = 'linear' - self.input_shape = [1, 3, 100] - self.out_w = 50 - self.scale = 0.5 - self.out_size = np.array( - [ - 50, - ] - ).astype("int32") - self.align_corners = False - self.align_mode = 1 + create_test_case0(self) class TestLinearInterpOpDataLayout(TestLinearInterpOp): @@ -333,6 +338,97 @@ class TestLinearInterpOpAPI2_0(unittest.TestCase): np.testing.assert_allclose(interp.numpy(), expect, rtol=1e-05) +class TestLinearInterpOpFP16(TestLinearInterpOp): + def test_check_output(self): + self.check_output(atol=1e-3) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestLinearInterpOpBF16(OpTest): + def setUp(self): + self.python_api = linear_interp_test + self.out_size = None + self.actual_shape = None + self.data_layout = 'NCHW' + self.init_test_case() + self.op_type = "linear_interp_v2" + self.dtype = np.uint16 + input_np = np.random.random(self.input_shape).astype("float32") + + scale_w = 0 + if self.data_layout == "NCHW": + in_w = self.input_shape[2] + else: + in_w = self.input_shape[1] + + if self.scale > 0: + if isinstance(self.scale, float) or isinstance(self.scale, int): + self.scale = float(self.scale) + if isinstance(self.scale, list): + self.scale = float(self.scale[0]) + out_w = int(in_w * self.scale) + else: + out_w = self.out_w + + output_np = linear_interp_np( + input_np, + out_w, + self.scale, + self.out_size, + self.actual_shape, + self.align_corners, + self.align_mode, + self.data_layout, + ) + self.inputs = {'X': convert_float_to_uint16(input_np)} + if self.out_size is not None: + self.inputs['OutSize'] = self.out_size + if self.actual_shape is not None: + self.inputs['OutSize'] = self.actual_shape + + self.attrs = { + 'out_w': self.out_w, + 'interp_method': self.interp_method, + 'align_corners': self.align_corners, + 'align_mode': self.align_mode, + 'data_layout': self.data_layout, + } + if self.scale > 0: + if isinstance(self.scale, float) or isinstance(self.scale, int): + self.scale = [float(self.scale)] + self.attrs['scale'] = self.scale + self.outputs = {'Out': convert_float_to_uint16(output_np)} + + def test_check_output(self): + place = core.CUDAPlace(0) + self.check_output_with_place(place, atol=1e-2) + + def test_check_grad(self): + place = core.CUDAPlace(0) + self.check_grad_with_place( + place, ['X'], 'Out', in_place=True, max_relative_error=1e-2 + ) + + def init_test_case(self): + create_test_case0(self) + + class TestResizeLinearOpUint8(OpTest): def setUp(self): self.out_size = None diff --git a/python/paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py index 7535e9d0889..2447818c896 100755 --- a/python/paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from eager_op_test import OpTest +from eager_op_test import OpTest, convert_float_to_uint16 import paddle from paddle import fluid @@ -25,6 +25,74 @@ from paddle.nn.functional import interpolate paddle.enable_static() +def create_test_case0(self): + self.interp_method = 'nearest' + self.input_shape = [2, 3, 4, 5] + self.out_h = 2 + self.out_w = 2 + self.scale = [] + self.out_size = np.array([3, 3]).astype("int32") + self.align_corners = True + + +def create_test_case1(self): + self.interp_method = 'nearest' + self.input_shape = [4, 1, 1, 7, 8] + self.out_d = 1 + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.align_corners = True + + +def create_test_case2(self): + self.interp_method = 'nearest' + self.input_shape = [3, 3, 9, 6] + self.out_h = 12 + self.out_w = 12 + self.scale = [] + self.align_corners = True + + +def create_test_case3(self): + self.interp_method = 'nearest' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = [] + self.align_corners = True + + +def create_test_case4(self): + self.interp_method = 'nearest' + self.input_shape = [4, 1, 7, 8] + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.out_size = np.array([2, 2]).astype("int32") + self.align_corners = True + + +def create_test_case5(self): + self.interp_method = 'nearest' + self.input_shape = [3, 3, 9, 6] + self.out_h = 12 + self.out_w = 12 + self.scale = [] + self.out_size = np.array([11, 11]).astype("int32") + self.align_corners = True + + +def create_test_case6(self): + self.interp_method = 'nearest' + self.input_shape = [1, 1, 32, 64] + self.out_h = 64 + self.out_w = 32 + self.scale = [] + self.out_size = np.array([65, 129]).astype("int32") + self.align_corners = True + + def nearest_interp_test( x, OutSize=None, @@ -208,9 +276,10 @@ class TestNearestInterpOp(OpTest): self.out_size = None self.actual_shape = None self.data_layout = 'NCHW' + self.dtype = np.float64 self.init_test_case() self.op_type = "nearest_interp_v2" - input_np = np.random.random(self.input_shape).astype("float64") + input_np = np.random.random(self.input_shape).astype(self.dtype) if self.data_layout == "NCHW" and len(self.input_shape) == 4: in_d = 1 @@ -320,77 +389,37 @@ class TestNearestInterpOp(OpTest): self.check_grad(['X'], 'Out', in_place=True) def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [2, 3, 4, 5] - self.out_h = 2 - self.out_w = 2 - self.scale = [] - self.out_size = np.array([3, 3]).astype("int32") - self.align_corners = True + create_test_case0(self) class TestNearestNeighborInterpCase1(TestNearestInterpOp): def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [4, 1, 1, 7, 8] - self.out_d = 1 - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.align_corners = True + create_test_case1(self) class TestNearestNeighborInterpCase2(TestNearestInterpOp): def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [3, 3, 9, 6] - self.out_h = 12 - self.out_w = 12 - self.scale = [] - self.align_corners = True + create_test_case2(self) class TestNearestNeighborInterpCase3(TestNearestInterpOp): def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = [] - self.align_corners = True + create_test_case3(self) class TestNearestNeighborInterpCase4(TestNearestInterpOp): def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [4, 1, 7, 8] - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.out_size = np.array([2, 2]).astype("int32") - self.align_corners = True + create_test_case4(self) class TestNearestNeighborInterpCase5(TestNearestInterpOp): def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [3, 3, 9, 6] - self.out_h = 12 - self.out_w = 12 - self.scale = [] - self.out_size = np.array([11, 11]).astype("int32") - self.align_corners = True + create_test_case5(self) class TestNearestNeighborInterpCase6(TestNearestInterpOp): def init_test_case(self): - self.interp_method = 'nearest' - self.input_shape = [1, 1, 32, 64] - self.out_h = 64 - self.out_w = 32 - self.scale = [] - self.out_size = np.array([65, 129]).astype("int32") - self.align_corners = True + create_test_case6(self) class TestNearestNeighborInterpSame(TestNearestInterpOp): @@ -414,6 +443,248 @@ class TestNearestNeighborInterpActualShape(TestNearestInterpOp): self.align_corners = True +class TestNearestInterpOpFP16(TestNearestInterpOp): + def test_check_output(self): + self.check_output(check_dygraph=True, atol=1e-3) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + check_dygraph=True, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + self.dtype = np.float16 + + +class TestNearestNeighborInterpCase1FP16(TestNearestInterpOpFP16): + def init_test_case(self): + create_test_case1(self) + self.dtype = np.float16 + + +class TestNearestNeighborInterpCase2FP16(TestNearestInterpOpFP16): + def init_test_case(self): + create_test_case2(self) + self.dtype = np.float16 + + +class TestNearestNeighborInterpCase3FP16(TestNearestInterpOpFP16): + def init_test_case(self): + create_test_case3(self) + self.dtype = np.float16 + + +class TestNearestNeighborInterpCase4FP16(TestNearestInterpOpFP16): + def init_test_case(self): + create_test_case4(self) + self.dtype = np.float16 + + +class TestNearestNeighborInterpCase5FP16(TestNearestInterpOpFP16): + def init_test_case(self): + create_test_case5(self) + self.dtype = np.float16 + + +class TestNearestNeighborInterpCase6FP16(TestNearestInterpOpFP16): + def init_test_case(self): + create_test_case6(self) + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestInterpOpBF16(OpTest): + def setUp(self): + self.python_api = nearest_interp_test + self.out_size = None + self.actual_shape = None + self.data_layout = 'NCHW' + self.init_test_case() + self.op_type = "nearest_interp_v2" + self.dtype = np.uint16 + input_np = np.random.random(self.input_shape).astype("float32") + + if self.data_layout == "NCHW" and len(self.input_shape) == 4: + in_d = 1 + in_h = self.input_shape[2] + in_w = self.input_shape[3] + else: + in_d = 1 + in_h = self.input_shape[1] + in_w = self.input_shape[2] + + if self.data_layout == "NCDHW" and len(self.input_shape) == 5: + in_d = self.input_shape[2] + in_h = self.input_shape[3] + in_w = self.input_shape[4] + else: + in_d = self.input_shape[1] + in_h = self.input_shape[2] + in_w = self.input_shape[3] + scale_d = 0 + scale_h = 0 + scale_w = 0 + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0: + scale_d = scale_h = scale_w = float(self.scale) + if isinstance(self.scale, list) and len(self.scale) == 1: + scale_d = scale_w = scale_h = self.scale[0] + elif isinstance(self.scale, list) and len(self.scale) > 1: + if len(self.scale) == 5: + scale_w = self.scale[2] + scale_h = self.scale[1] + scale_d = self.scale[0] + else: + scale_w = self.scale[1] + scale_h = self.scale[0] + + out_h = int(in_h * scale_h) + out_w = int(in_w * scale_w) + out_d = int(in_d * scale_d) + else: + if len(self.input_shape) == 5: + out_d = self.out_d + out_h = self.out_h + out_w = self.out_w + + if len(self.input_shape) == 4: + output_np = nearest_neighbor_interp_np( + input_np, + out_h, + out_w, + scale_h, + scale_w, + self.out_size, + self.actual_shape, + self.align_corners, + self.data_layout, + ) + elif len(self.input_shape) == 5: + output_np = nearest_neighbor_interp3d_np( + input_np, + out_d, + out_h, + out_w, + scale_d, + scale_h, + scale_w, + self.out_size, + self.actual_shape, + self.align_corners, + self.data_layout, + ) + self.inputs = {'X': convert_float_to_uint16(input_np)} + if self.out_size is not None: + self.inputs['OutSize'] = self.out_size + if self.actual_shape is not None: + self.inputs['OutSize'] = self.actual_shape + if len(self.input_shape) == 5: + self.attrs = { + 'out_d': self.out_d, + 'out_h': self.out_h, + 'out_w': self.out_w, + 'interp_method': self.interp_method, + 'align_corners': self.align_corners, + 'data_layout': self.data_layout, + } + else: + self.attrs = { + 'out_h': self.out_h, + 'out_w': self.out_w, + 'interp_method': self.interp_method, + 'align_corners': self.align_corners, + 'data_layout': self.data_layout, + } + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0: + self.scale = [self.scale] + if isinstance(self.scale, list) and len(self.scale) == 1: + self.scale = [self.scale[0], self.scale[0]] + self.attrs['scale'] = self.scale + self.outputs = {'Out': convert_float_to_uint16(output_np)} + + def test_check_output(self): + self.check_output(check_dygraph=True, atol=1e-2) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + check_dygraph=True, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + + +class TestNearestNeighborInterpCase1BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case1(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestNeighborInterpCase2BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case2(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestNeighborInterpCase3BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case3(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestNeighborInterpCase4BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case4(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestNeighborInterpCase5BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case5(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestNeighborInterpCase6BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case6(self) + + class TestNearestNeighborInterpDataLayout(TestNearestInterpOp): def init_test_case(self): self.interp_method = 'nearest' diff --git a/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py index 24b5355fc1e..be13d5ebda1 100755 --- a/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from eager_op_test import OpTest +from eager_op_test import OpTest, convert_float_to_uint16 import paddle from paddle import fluid @@ -25,6 +25,87 @@ from paddle.nn.functional import interpolate np.random.seed(123) +def create_test_case0(self): + self.interp_method = 'trilinear' + self.input_shape = [2, 3, 4, 4, 4] + self.out_d = 2 + self.out_h = 2 + self.out_w = 2 + self.scale = [] + self.out_size = np.array([3, 3, 3]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case1(self): + self.interp_method = 'trilinear' + self.input_shape = [2, 1, 7, 8, 9] + self.out_d = 1 + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.align_corners = True + self.align_mode = 1 + + +def create_test_case2(self): + self.interp_method = 'trilinear' + self.input_shape = [2, 3, 9, 6, 8] + self.out_d = 12 + self.out_h = 12 + self.out_w = 12 + self.scale = [] + self.align_corners = True + self.align_mode = 1 + + +def create_test_case3(self): + self.interp_method = 'trilinear' + self.input_shape = [3, 2, 16, 8, 4] + self.out_d = 32 + self.out_h = 16 + self.out_w = 8 + self.scale = [] + self.align_corners = True + self.align_mode = 1 + + +def create_test_case4(self): + self.interp_method = 'trilinear' + self.input_shape = [4, 1, 7, 8, 9] + self.out_d = 1 + self.out_h = 1 + self.out_w = 1 + self.scale = [] + self.out_size = np.array([2, 2, 2]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case5(self): + self.interp_method = 'trilinear' + self.input_shape = [3, 3, 9, 6, 8] + self.out_d = 12 + self.out_h = 12 + self.out_w = 12 + self.scale = [] + self.out_size = np.array([11, 11, 11]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + +def create_test_case6(self): + self.interp_method = 'trilinear' + self.input_shape = [1, 1, 16, 8, 4] + self.out_d = 8 + self.out_h = 32 + self.out_w = 16 + self.scale = [] + self.out_size = np.array([17, 9, 5]).astype("int32") + self.align_corners = True + self.align_mode = 1 + + def trilinear_interp_test( x, OutSize=None, @@ -201,10 +282,13 @@ class TestTrilinearInterpOp(OpTest): self.out_size = None self.actual_shape = None self.data_layout = 'NCDHW' + self.dtype = np.float32 self.init_test_case() self.op_type = "trilinear_interp_v2" # NOTE(dev): some AsDispensible input is not used under imperative mode. - input_np = np.random.random(self.input_shape).astype("float32") + # Skip check_dygraph while found them in Inputs. + self.check_dygraph = True + input_np = np.random.random(self.input_shape).astype(self.dtype) scale_w = 0 scale_h = 0 @@ -285,90 +369,37 @@ class TestTrilinearInterpOp(OpTest): self.check_grad(['X'], 'Out', in_place=True) def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [2, 3, 4, 4, 4] - self.out_d = 2 - self.out_h = 2 - self.out_w = 2 - self.scale = [] - self.out_size = np.array([3, 3, 3]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case0(self) class TestTrilinearInterpCase1(TestTrilinearInterpOp): def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [2, 1, 7, 8, 9] - self.out_d = 1 - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.align_corners = True - self.align_mode = 1 + create_test_case1(self) class TestTrilinearInterpCase2(TestTrilinearInterpOp): def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [2, 3, 9, 6, 8] - self.out_d = 12 - self.out_h = 12 - self.out_w = 12 - self.scale = [] - self.align_corners = True - self.align_mode = 1 + create_test_case2(self) class TestTrilinearInterpCase3(TestTrilinearInterpOp): def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [3, 2, 16, 8, 4] - self.out_d = 32 - self.out_h = 16 - self.out_w = 8 - self.scale = [] - self.align_corners = True - self.align_mode = 1 + create_test_case3(self) class TestTrilinearInterpCase4(TestTrilinearInterpOp): def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [4, 1, 7, 8, 9] - self.out_d = 1 - self.out_h = 1 - self.out_w = 1 - self.scale = [] - self.out_size = np.array([2, 2, 2]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case4(self) class TestTrilinearInterpCase5(TestTrilinearInterpOp): def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [3, 3, 9, 6, 8] - self.out_d = 12 - self.out_h = 12 - self.out_w = 12 - self.scale = [] - self.out_size = np.array([11, 11, 11]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case5(self) class TestTrilinearInterpCase6(TestTrilinearInterpOp): def init_test_case(self): - self.interp_method = 'trilinear' - self.input_shape = [1, 1, 16, 8, 4] - self.out_d = 8 - self.out_h = 32 - self.out_w = 16 - self.scale = [] - self.out_size = np.array([17, 9, 5]).astype("int32") - self.align_corners = True - self.align_mode = 1 + create_test_case6(self) class TestTrilinearInterpSame(TestTrilinearInterpOp): @@ -422,6 +453,229 @@ class TestTrilinearInterpDatalayout(TestTrilinearInterpOp): self.data_layout = "NDHWC" +class TestTrilinearInterpOpFP16(TestTrilinearInterpOp): + def test_check_output(self): + self.check_output(check_dygraph=self.check_dygraph, atol=1e-3) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + check_dygraph=self.check_dygraph, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + self.dtype = np.float16 + + +class TestTrilinearInterpCase1FP16(TestTrilinearInterpOpFP16): + def init_test_case(self): + create_test_case1(self) + self.dtype = np.float16 + + +class TestTrilinearInterpCase2FP16(TestTrilinearInterpOpFP16): + def init_test_case(self): + create_test_case2(self) + self.dtype = np.float16 + + +class TestTrilinearInterpCase3FP16(TestTrilinearInterpOpFP16): + def init_test_case(self): + create_test_case3(self) + self.dtype = np.float16 + + +class TestTrilinearInterpCase4FP16(TestTrilinearInterpOpFP16): + def init_test_case(self): + create_test_case4(self) + self.dtype = np.float16 + + +class TestTrilinearInterpCase5FP16(TestTrilinearInterpOpFP16): + def init_test_case(self): + create_test_case5(self) + self.dtype = np.float16 + + +class TestTrilinearInterpCase6FP16(TestTrilinearInterpOpFP16): + def init_test_case(self): + create_test_case6(self) + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestNearestInterpOpBF16(OpTest): + def setUp(self): + self.python_api = trilinear_interp_test + self.out_size = None + self.actual_shape = None + self.data_layout = 'NCDHW' + self.init_test_case() + self.op_type = "trilinear_interp_v2" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_dygraph while found them in Inputs. + self.check_dygraph = True + self.dtype = np.uint16 + input_np = np.random.random(self.input_shape).astype("float32") + + scale_w = 0 + scale_h = 0 + scale_d = 0 + if self.data_layout == "NCDHW": + in_d = self.input_shape[2] + in_h = self.input_shape[3] + in_w = self.input_shape[4] + else: + in_d = self.input_shape[1] + in_h = self.input_shape[2] + in_w = self.input_shape[3] + + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0: + scale_d = scale_h = scale_w = float(self.scale) + if isinstance(self.scale, list) and len(self.scale) == 1: + scale_d = scale_w = scale_h = self.scale[0] + elif isinstance(self.scale, list) and len(self.scale) > 1: + scale_w = self.scale[2] + scale_h = self.scale[1] + scale_d = self.scale[0] + out_d = int(in_d * scale_d) + out_h = int(in_h * scale_h) + out_w = int(in_w * scale_w) + else: + out_d = self.out_d + out_h = self.out_h + out_w = self.out_w + + output_np = trilinear_interp_np( + input_np, + out_d, + out_h, + out_w, + scale_d, + scale_h, + scale_w, + self.out_size, + self.actual_shape, + self.align_corners, + self.align_mode, + self.data_layout, + ) + self.inputs = {'X': convert_float_to_uint16(input_np)} + if self.out_size is not None: + self.inputs['OutSize'] = self.out_size + self.check_dygraph = False + if self.actual_shape is not None: + self.inputs['OutSize'] = self.actual_shape + self.check_dygraph = False + # c++ end treat NCDHW the same way as NCHW + if self.data_layout == 'NCDHW': + data_layout = 'NCHW' + else: + data_layout = 'NHWC' + self.attrs = { + 'out_d': self.out_d, + 'out_h': self.out_h, + 'out_w': self.out_w, + 'interp_method': self.interp_method, + 'align_corners': self.align_corners, + 'align_mode': self.align_mode, + 'data_layout': data_layout, + } + if self.scale: + if isinstance(self.scale, float) or isinstance(self.scale, int): + if self.scale > 0: + self.scale = [self.scale] + if isinstance(self.scale, list) and len(self.scale) == 1: + self.scale = [self.scale[0], self.scale[0], self.scale[0]] + self.attrs['scale'] = self.scale + self.outputs = {'Out': convert_float_to_uint16(output_np)} + + def test_check_output(self): + self.check_output(check_dygraph=self.check_dygraph, atol=1e-2) + + def test_check_grad(self): + self.check_grad( + ['X'], + 'Out', + in_place=True, + check_dygraph=self.check_dygraph, + max_relative_error=1e-2, + ) + + def init_test_case(self): + create_test_case0(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestTrilinearInterpCase1BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case1(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestTrilinearInterpCase2BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case2(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestTrilinearInterpCase3BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case3(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestTrilinearInterpCase4BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case4(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestTrilinearInterpCase5BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case5(self) + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not compiled with CUDA or not support the bfloat16", +) +class TestTrilinearInterpCase6BF16(TestNearestInterpOpBF16): + def init_test_case(self): + create_test_case6(self) + + class TestTrilinearInterpOpUint8(OpTest): def setUp(self): self.python_api = trilinear_interp_test -- GitLab