未验证 提交 c09adab8 编写于 作者: H houj04 提交者: GitHub

refactor unittest for nearest_interp_v2_op_xpu. test=kunlun (#39804)

* refactor unittest for nearest_interp_v2_op_xpu. test=kunlun

* fix code style. test=kunlun

* fix code style. test=kunlun
上级 1c29196e
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -16,13 +16,14 @@ from __future__ import print_function
import unittest
import numpy as np
import paddle
import paddle.fluid.core as core
import sys
sys.path.append("..")
import paddle
from op_test import OpTest
from op_test_xpu import XPUOpTest
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
paddle.enable_static()
......@@ -158,16 +159,29 @@ def nearest_neighbor_interp3d_np(X,
return out.astype(X.dtype)
class TestNearestInterpOp(XPUOpTest):
class XPUNearestInterpOpWrapper(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'nearest_interp_v2'
self.use_dynamic_create_class = False
class TestNearestInterpOp(XPUOpTest):
def setUp(self):
self.use_xpu = True
self.place = paddle.XPUPlace(0)
self.init_dtype()
self.out_size = None
self.actual_shape = None
self.data_layout = 'NCHW'
self.interp_method = 'nearest'
self.scale = 0.
self.align_corners = True
self.init_test_case()
self.op_type = "nearest_interp_v2"
input_np = np.random.random(self.input_shape).astype("float32")
input_np = np.random.random(self.input_shape).astype(self.dtype)
# in
if self.data_layout == "NCHW" and len(self.input_shape) == 4:
in_d = 1
in_h = self.input_shape[2]
......@@ -185,6 +199,8 @@ class TestNearestInterpOp(XPUOpTest):
in_d = self.input_shape[1]
in_h = self.input_shape[2]
in_w = self.input_shape[3]
# scale
scale_d = 0
scale_h = 0
scale_w = 0
......@@ -192,8 +208,10 @@ class TestNearestInterpOp(XPUOpTest):
if isinstance(self.scale, float) or isinstance(self.scale, int):
if self.scale > 0:
scale_d = scale_h = scale_w = float(self.scale)
self.scale = [self.scale]
if isinstance(self.scale, list) and len(self.scale) == 1:
scale_d = scale_w = scale_h = self.scale[0]
self.scale = [self.scale[0], self.scale[0]]
elif isinstance(self.scale, list) and len(self.scale) > 1:
if len(self.scale) == 5:
scale_w = self.scale[2]
......@@ -212,6 +230,7 @@ class TestNearestInterpOp(XPUOpTest):
out_h = self.out_h
out_w = self.out_w
# output_np
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,
......@@ -221,11 +240,14 @@ class TestNearestInterpOp(XPUOpTest):
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.outputs = {'Out': output_np}
self.inputs = {'X': 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,
......@@ -243,38 +265,28 @@ class TestNearestInterpOp(XPUOpTest):
'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': output_np}
def init_dtype(self):
self.dtype = self.in_type
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
self.check_output_with_place(self.place)
def test_check_grad(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out', in_place=True)
self.check_grad_with_place(self.place, ['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 = 0.
self.out_size = np.array([3, 3]).astype("int32")
self.align_corners = True
"""
# case copied form gpu but disabled in xpu: not support 5-dim input_shape
class TestNearestNeighborInterpCase1(TestNearestInterpOp):
"""
# case copied form gpu but disabled in xpu: not support 5-dim input_shape
class TestNearestNeighborInterpCase1(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [4, 1, 1, 7, 8]
......@@ -283,86 +295,57 @@ class TestNearestNeighborInterpCase1(TestNearestInterpOp):
self.out_w = 1
self.scale = 0.
self.align_corners = True
"""
"""
class TestNearestNeighborInterpCase2(TestNearestInterpOp):
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 = 0.
self.align_corners = True
class TestNearestNeighborInterpCase3(TestNearestInterpOp):
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 = 0.
self.align_corners = True
class TestNearestNeighborInterpCase4(TestNearestInterpOp):
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 = 0.
self.out_size = np.array([2, 2]).astype("int32")
self.align_corners = True
class TestNearestNeighborInterpCase5(TestNearestInterpOp):
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 = 0.
self.out_size = np.array([11, 11]).astype("int32")
self.align_corners = True
class TestNearestNeighborInterpCase6(TestNearestInterpOp):
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 = 0.
self.out_size = np.array([65, 129]).astype("int32")
self.align_corners = True
class TestNearestNeighborInterpSame(TestNearestInterpOp):
class TestNearestNeighborInterpSame(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [2, 3, 32, 64]
self.out_h = 32
self.out_w = 64
self.scale = 0.
self.align_corners = True
class TestNearestNeighborInterpActualShape(TestNearestInterpOp):
class TestNearestNeighborInterpActualShape(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 32, 16]
self.out_h = 64
self.out_w = 32
self.scale = 0.
self.out_size = np.array([66, 40]).astype("int32")
self.align_corners = True
"""
# case copied form gpu but disabled in xpu: not support NHWC data_layout
class TestNearestNeighborInterpDataLayout(TestNearestInterpOp):
"""
# case copied form gpu but disabled in xpu: not support NHWC data_layout
class TestNearestNeighborInterpDataLayout(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [2, 4, 4, 5]
......@@ -372,50 +355,39 @@ class TestNearestNeighborInterpDataLayout(TestNearestInterpOp):
self.out_size = np.array([3, 8]).astype("int32")
self.align_corners = True
self.data_layout = "NHWC"
"""
"""
class TestNearestInterpWithoutCorners(TestNearestInterpOp):
class TestNearestInterpWithoutCorners(TestNearestInterpOp):
def set_align_corners(self):
self.align_corners = False
class TestNearestNeighborInterpScale1(TestNearestInterpOp):
class TestNearestNeighborInterpScale1(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 7, 5]
self.out_h = 64
self.out_w = 32
self.scale = 2.
self.out_size = np.array([66, 40]).astype("int32")
self.align_corners = True
class TestNearestNeighborInterpScale2(TestNearestInterpOp):
class TestNearestNeighborInterpScale2(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 5, 7]
self.out_h = 64
self.out_w = 32
self.scale = 1.5
self.out_size = np.array([66, 40]).astype("int32")
self.align_corners = True
class TestNearestNeighborInterpScale3(TestNearestInterpOp):
class TestNearestNeighborInterpScale3(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 7, 5]
self.out_h = 64
self.out_w = 32
self.scale = [2.0, 3.0]
self.out_size = np.array([66, 40]).astype("int32")
self.align_corners = True
"""
# case copied form gpu but disabled in xpu: not support 5-dim input_shape
class TestNearestNeighbor3DInterp(TestNearestInterpOp):
"""
# case copied form gpu but disabled in xpu: not support 5-dim input_shape
class TestNearestNeighbor3DInterp(TestNearestInterpOp):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 4, 7, 5]
......@@ -425,14 +397,20 @@ class TestNearestNeighbor3DInterp(TestNearestInterpOp):
self.scale = [4.0, 2.0, 3.0]
self.out_size = np.array([8, 66, 40]).astype("int32")
self.align_corners = True
"""
"""
class TestNearestInterpOp_attr_tensor(XPUOpTest):
class TestNearestInterpOp_attr_tensor(XPUOpTest):
def setUp(self):
self.use_xpu = True
self.place = paddle.XPUPlace(0)
self.init_dtype()
self.out_size = None
self.actual_shape = None
self.interp_method = 'nearest'
self.scale = 0.
self.align_corners = True
self.init_test_case()
self.op_type = "nearest_interp_v2"
self.shape_by_1Dtensor = False
......@@ -442,7 +420,7 @@ class TestNearestInterpOp_attr_tensor(XPUOpTest):
'align_corners': self.align_corners,
}
input_np = np.random.random(self.input_shape).astype("float32")
input_np = np.random.random(self.input_shape).astype(self.dtype)
self.inputs = {'X': input_np}
if self.scale_by_1Dtensor:
......@@ -480,68 +458,57 @@ class TestNearestInterpOp_attr_tensor(XPUOpTest):
if isinstance(self.scale, list) and len(self.scale) == 1:
self.scale = [self.scale[0], self.scale[0]]
self.attrs['scale'] = self.scale
output_np = nearest_neighbor_interp_np(input_np, out_h, out_w, 0, 0,
self.out_size, self.actual_shape,
output_np = nearest_neighbor_interp_np(
input_np, out_h, out_w, 0, 0, self.out_size, self.actual_shape,
self.align_corners)
self.outputs = {'Out': output_np}
def init_dtype(self):
self.dtype = self.in_type
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
self.check_output_with_place(self.place)
def test_check_grad(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out', in_place=True)
self.check_grad_with_place(self.place, ['X'], 'Out', in_place=True)
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [2, 5, 4, 4]
self.out_h = 3
self.out_w = 3
self.scale = 0.
self.out_size = [3, 3]
self.align_corners = True
# out_size is a tensor list
class TestNearestInterp_attr_tensor_Case1(TestNearestInterpOp_attr_tensor):
# out_size is a tensor list
class TestNearestInterp_attr_tensor_Case1(TestNearestInterpOp_attr_tensor):
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 = 0.
self.out_size = [8, 12]
self.align_corners = True
# out_size is a 1-D tensor
class TestNearestInterp_attr_tensor_Case2(TestNearestInterpOp_attr_tensor):
# out_size is a 1-D tensor
class TestNearestInterp_attr_tensor_Case2(TestNearestInterpOp_attr_tensor):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 32, 16]
self.out_h = 64
self.out_w = 32
self.scale = 0.
self.out_size = np.array([66, 40]).astype("int32")
self.align_corners = True
self.shape_by_1Dtensor = True
# scale is a 1-D tensor
class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
# scale is a 1-D tensor
class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
def init_test_case(self):
self.interp_method = 'nearest'
self.input_shape = [3, 2, 32, 16]
self.out_h = 64
self.out_w = 32
self.scale = 2.0
self.out_size = None
self.align_corners = True
self.scale_by_1Dtensor = True
support_types = get_xpu_op_support_types('nearest_interp_v2')
for stype in support_types:
create_test_class(globals(), XPUNearestInterpOpWrapper, stype)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册