From 2c2137bbc66512cb37b21f8a320787684041315e Mon Sep 17 00:00:00 2001 From: HongyuJia Date: Thu, 18 Aug 2022 10:29:35 +0800 Subject: [PATCH] [phi] Transfer fluid bilinear_interp_v2 to phi bilinear_interp (add yaml) (#45140) * transfer bilinear op to phi, change bname from bilinear_interp_v2 to bilinear_interp * reserve linear_interp param * fix cross device import --- paddle/phi/api/yaml/legacy_api.yaml | 11 +++ paddle/phi/api/yaml/legacy_backward.yaml | 12 +++ paddle/phi/core/compat/op_utils.h | 2 + .../kernels/cpu/interpolate_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/interpolate_kernel.cc | 2 +- .../kernels/gpu/interpolate_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/interpolate_kernel.cu | 2 +- paddle/phi/ops/compat/interpolate_sig.cc | 6 +- .../unittests/test_bicubic_interp_v2_op.py | 9 ++- .../unittests/test_bilinear_interp_v2_op.py | 77 ++++++++++++++----- .../unittests/test_linear_interp_v2_op.py | 9 ++- .../unittests/test_nearest_interp_v2_op.py | 10 +-- python/paddle/nn/functional/common.py | 12 ++- 13 files changed, 115 insertions(+), 41 deletions(-) diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index ef25d3a645..897944894d 100755 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -364,6 +364,17 @@ data_type : x backward : bicubic_interp_grad +- api : bilinear_interp + args : (Tensor x, Tensor out_size, Tensor[] size_tensor, Tensor scale_tensor, str data_layout, int out_d, int out_h, int out_w, float[] scale, str interp_method, bool align_corners, int align_mode) + output : Tensor(output) + infer_meta : + func : InterpolateInferMeta + optional: out_size, size_tensor, scale_tensor + kernel : + func : bilinear_interp + data_type : x + backward : bilinear_interp_grad + - api : bilinear_tensor_product args : (Tensor x, Tensor y, Tensor weight, Tensor bias) output : Tensor diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 69b3040bd5..6244821a0b 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -295,6 +295,18 @@ func : bicubic_interp_grad data_type : output_grad +- backward_api : bilinear_interp_grad + forward : bilinear_interp (Tensor x, Tensor out_size, Tensor[] size_tensor, Tensor scale_tensor, str data_layout, int out_d, int out_h, int out_w, float[] scale, str interp_method, bool align_corners, int align_mode) -> Tensor(output) + args : (Tensor x, Tensor out_size, Tensor[] size_tensor, Tensor scale_tensor, Tensor output_grad, str data_layout, int out_d, int out_h, int out_w, float[] scale, str interp_method, bool align_corners, int align_mode) + output : Tensor(x_grad) + infer_meta : + func : UnchangedInferMeta + param: [x] + optional: out_size, size_tensor, scale_tensor + kernel : + func : bilinear_interp_grad + data_type : output_grad + - backward_api : bilinear_tensor_product_grad forward : bilinear_tensor_product (Tensor x, Tensor y, Tensor weight, Tensor bias) -> Tensor(out) args : (Tensor x, Tensor y, Tensor weight, Tensor out_grad) diff --git a/paddle/phi/core/compat/op_utils.h b/paddle/phi/core/compat/op_utils.h index 05c05d06d6..a08a4593b6 100644 --- a/paddle/phi/core/compat/op_utils.h +++ b/paddle/phi/core/compat/op_utils.h @@ -74,6 +74,8 @@ const std::unordered_set deprecated_op_names( "top_k_grad", "linear_interp", "linear_interp_grad", + "bilinear_interp", + "bilinear_interp_grad", "nearest_interp", "nearest_interp_grad", "bicubic_interp", diff --git a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc index 862fc3ac7f..3004ff6229 100644 --- a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc @@ -1036,7 +1036,7 @@ void BicubicInterpGradKernel( } // namespace phi -PD_REGISTER_KERNEL(bilinear_interp_v2_grad, +PD_REGISTER_KERNEL(bilinear_interp_grad, CPU, ALL_LAYOUT, phi::BilinearInterpGradKernel, diff --git a/paddle/phi/kernels/cpu/interpolate_kernel.cc b/paddle/phi/kernels/cpu/interpolate_kernel.cc index 0751bac925..98f21172d4 100644 --- a/paddle/phi/kernels/cpu/interpolate_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_kernel.cc @@ -1187,7 +1187,7 @@ void BicubicInterpKernel( } // namespace phi -PD_REGISTER_KERNEL(bilinear_interp_v2, +PD_REGISTER_KERNEL(bilinear_interp, CPU, ALL_LAYOUT, phi::BilinearInterpKernel, diff --git a/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu b/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu index dc83022214..906f5d7ec0 100644 --- a/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu @@ -1569,7 +1569,7 @@ void BicubicInterpGradKernel( } // namespace phi -PD_REGISTER_KERNEL(bilinear_interp_v2_grad, +PD_REGISTER_KERNEL(bilinear_interp_grad, GPU, ALL_LAYOUT, phi::BilinearInterpGradKernel, diff --git a/paddle/phi/kernels/gpu/interpolate_kernel.cu b/paddle/phi/kernels/gpu/interpolate_kernel.cu index 9c2a022468..60540ebfb5 100644 --- a/paddle/phi/kernels/gpu/interpolate_kernel.cu +++ b/paddle/phi/kernels/gpu/interpolate_kernel.cu @@ -1440,7 +1440,7 @@ void BicubicInterpKernel( } // namespace phi -PD_REGISTER_KERNEL(bilinear_interp_v2, +PD_REGISTER_KERNEL(bilinear_interp, GPU, ALL_LAYOUT, phi::BilinearInterpKernel, diff --git a/paddle/phi/ops/compat/interpolate_sig.cc b/paddle/phi/ops/compat/interpolate_sig.cc index e2e6987b28..c68a6b782f 100644 --- a/paddle/phi/ops/compat/interpolate_sig.cc +++ b/paddle/phi/ops/compat/interpolate_sig.cc @@ -18,7 +18,7 @@ namespace phi { KernelSignature BilinearInterpOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature("bilinear_interp_v2", + return KernelSignature("bilinear_interp", {"X", "OutSize", "SizeTensor", "Scale"}, {"data_layout", "out_d", @@ -92,7 +92,7 @@ KernelSignature BicubicInterpOpArgumentMapping( KernelSignature BilinearInterpGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature("bilinear_interp_v2_grad", + return KernelSignature("bilinear_interp_grad", {"X", "OutSize", "SizeTensor", "Scale", "Out@GRAD"}, {"data_layout", "out_d", @@ -167,10 +167,12 @@ KernelSignature BicubicInterpGradOpArgumentMapping( } // namespace phi PD_REGISTER_BASE_KERNEL_NAME(linear_interp_v2, linear_interp); +PD_REGISTER_BASE_KERNEL_NAME(bilinear_interp_v2, bilinear_interp); PD_REGISTER_BASE_KERNEL_NAME(nearest_interp_v2, nearest_interp); PD_REGISTER_BASE_KERNEL_NAME(bicubic_interp_v2, bicubic_interp); PD_REGISTER_BASE_KERNEL_NAME(linear_interp_v2_grad, linear_interp_grad); +PD_REGISTER_BASE_KERNEL_NAME(bilinear_interp_v2_grad, bilinear_interp_grad); PD_REGISTER_BASE_KERNEL_NAME(nearest_interp_v2_grad, nearest_interp_grad); PD_REGISTER_BASE_KERNEL_NAME(bicubic_interp_v2_grad, bicubic_interp_grad); 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 6dc9845658..ed581eb724 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 @@ -23,7 +23,6 @@ import paddle from paddle.fluid import Program, program_guard from paddle.fluid.framework import _test_eager_guard from paddle.nn.functional import interpolate -from paddle._C_ops import final_state_bicubic_interp def bicubic_interp_test(x, @@ -49,9 +48,11 @@ def bicubic_interp_test(x, if not isinstance(SizeTensor, list) and not isinstance( SizeTensor, tuple): SizeTensor = [SizeTensor] - return final_state_bicubic_interp(x, OutSize, SizeTensor, Scale, - data_layout, out_d, out_h, out_w, scale, - interp_method, align_corners, align_mode) + return paddle._C_ops.final_state_bicubic_interp(x, OutSize, SizeTensor, + Scale, data_layout, out_d, + out_h, out_w, scale, + interp_method, + align_corners, align_mode) def cubic_1(x, a): 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 82acee3337..06c3db0e79 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 @@ -23,6 +23,36 @@ from paddle.nn.functional import interpolate import paddle +def bilinear_interp_test(x, + OutSize=None, + SizeTensor=None, + Scale=None, + data_layout='NCHW', + out_d=-1, + out_h=-1, + out_w=-1, + scale=[], + interp_method='linear', + align_corners=False, + align_mode=1): + if isinstance(scale, float) or isinstance(scale, int): + scale_list = [] + for _ in range(len(x.shape) - 2): + scale_list.append(scale) + scale = list(map(float, scale_list)) + elif isinstance(scale, list) or isinstance(scale, tuple): + scale = list(map(float, scale)) + if SizeTensor is not None: + if not isinstance(SizeTensor, list) and not isinstance( + SizeTensor, tuple): + SizeTensor = [SizeTensor] + return paddle._C_ops.final_state_bilinear_interp(x, OutSize, SizeTensor, + Scale, data_layout, out_d, + out_h, out_w, scale, + interp_method, + align_corners, align_mode) + + def bilinear_interp_np(input, out_h, out_w, @@ -106,6 +136,7 @@ def bilinear_interp_np(input, class TestBilinearInterpOp(OpTest): def setUp(self): + self.python_api = bilinear_interp_test self.out_size = None self.actual_shape = None self.data_layout = 'NCHW' @@ -164,17 +195,17 @@ class TestBilinearInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad(['X'], 'Out', in_place=True, check_eager=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 = 0. + self.scale = [] self.out_size = np.array([3, 3]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -187,7 +218,7 @@ class TestBilinearInterpCase1(TestBilinearInterpOp): self.input_shape = [4, 1, 7, 8] self.out_h = 1 self.out_w = 1 - self.scale = 0. + self.scale = [] self.align_corners = True self.align_mode = 1 @@ -199,7 +230,7 @@ class TestBilinearInterpCase2(TestBilinearInterpOp): self.input_shape = [3, 3, 9, 6] self.out_h = 12 self.out_w = 12 - self.scale = 0. + self.scale = [] self.align_corners = True self.align_mode = 1 @@ -211,7 +242,7 @@ class TestBilinearInterpCase3(TestBilinearInterpOp): self.input_shape = [1, 1, 32, 64] self.out_h = 64 self.out_w = 32 - self.scale = 0. + self.scale = [] self.align_corners = True self.align_mode = 1 @@ -223,7 +254,7 @@ class TestBilinearInterpCase4(TestBilinearInterpOp): self.input_shape = [4, 1, 7, 8] self.out_h = 1 self.out_w = 1 - self.scale = 0. + self.scale = [] self.out_size = np.array([2, 2]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -236,7 +267,7 @@ class TestBilinearInterpCase5(TestBilinearInterpOp): self.input_shape = [3, 3, 9, 6] self.out_h = 12 self.out_w = 12 - self.scale = 0. + self.scale = [] self.out_size = np.array([11, 11]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -249,7 +280,7 @@ class TestBilinearInterpCase6(TestBilinearInterpOp): self.input_shape = [1, 1, 32, 64] self.out_h = 64 self.out_w = 32 - self.scale = 0. + self.scale = [] self.out_size = np.array([65, 33]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -274,7 +305,7 @@ class TestBilinearInterpSame(TestBilinearInterpOp): self.input_shape = [2, 3, 32, 64] self.out_h = 32 self.out_w = 64 - self.scale = 0. + self.scale = [] self.align_corners = True self.align_mode = 1 @@ -286,7 +317,7 @@ class TestBilinearInterpActualShape(TestBilinearInterpOp): self.input_shape = [3, 2, 32, 16] self.out_h = 64 self.out_w = 32 - self.scale = 0. + self.scale = [] self.out_size = np.array([66, 40]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -299,7 +330,7 @@ class TestBilinearInterpDataLayout(TestBilinearInterpOp): self.input_shape = [2, 5, 5, 3] self.out_h = 2 self.out_w = 2 - self.scale = 0. + self.scale = [] self.out_size = np.array([3, 3]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -309,6 +340,7 @@ class TestBilinearInterpDataLayout(TestBilinearInterpOp): class TestBilinearInterpOpUint8(OpTest): def setUp(self): + self.python_api = bilinear_interp_test self.out_size = None self.actual_shape = None self.init_test_case() @@ -355,14 +387,16 @@ class TestBilinearInterpOpUint8(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output_with_place(place=core.CPUPlace(), atol=1) + self.check_output_with_place(place=core.CPUPlace(), + atol=1, + check_eager=True) def init_test_case(self): self.interp_method = 'bilinear' self.input_shape = [1, 3, 9, 6] self.out_h = 10 self.out_w = 9 - self.scale = 0. + self.scale = [] self.align_corners = True self.align_mode = 1 @@ -374,7 +408,7 @@ class TestBilinearInterpCase1Uint8(TestBilinearInterpOpUint8): self.input_shape = [2, 3, 32, 64] self.out_h = 64 self.out_w = 32 - self.scale = 0. + self.scale = [] self.align_corners = True self.align_mode = 1 @@ -386,7 +420,7 @@ class TestBilinearInterpCase2Uint8(TestBilinearInterpOpUint8): self.input_shape = [4, 1, 7, 8] self.out_h = 5 self.out_w = 13 - self.scale = 0. + self.scale = [] self.out_size = np.array([6, 15]).astype("int32") self.align_corners = True self.align_mode = 1 @@ -476,6 +510,7 @@ class TestBilinearInterpZero(TestBilinearInterpOp): class TestBilinearInterpOp_attr_tensor(OpTest): def setUp(self): + self.python_api = bilinear_interp_test self.out_size = None self.actual_shape = None self.init_test_case() @@ -531,17 +566,17 @@ class TestBilinearInterpOp_attr_tensor(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad(['X'], 'Out', in_place=True, check_eager=True) def init_test_case(self): self.interp_method = 'bilinear' self.input_shape = [2, 3, 5, 5] self.out_h = 3 self.out_w = 3 - self.scale = 0. + self.scale = [] self.out_size = [3, 3] self.align_corners = True @@ -554,7 +589,7 @@ class TestBilinearInterp_attr_tensor_Case1(TestBilinearInterpOp_attr_tensor): self.input_shape = [3, 3, 9, 6] self.out_h = 12 self.out_w = 12 - self.scale = 0. + self.scale = [] self.out_size = [8, 12] self.align_corners = True @@ -567,7 +602,7 @@ class TestBilinearInterp_attr_tensor_Case2(TestBilinearInterpOp_attr_tensor): self.input_shape = [3, 2, 32, 16] self.out_h = 64 self.out_w = 32 - self.scale = 0. + self.scale = [] self.out_size = np.array([66, 40]).astype("int32") self.align_corners = True self.shape_by_1Dtensor = True 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 82ed2b83a0..6782820070 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 @@ -22,7 +22,6 @@ import paddle.fluid.core as core import paddle.fluid as fluid from paddle.fluid import Program, program_guard from paddle.nn.functional import interpolate -from paddle._C_ops import final_state_linear_interp def linear_interp_test(x, @@ -48,9 +47,11 @@ def linear_interp_test(x, if not isinstance(SizeTensor, list) and not isinstance( SizeTensor, tuple): SizeTensor = [SizeTensor] - return final_state_linear_interp(x, OutSize, SizeTensor, Scale, data_layout, - out_d, out_h, out_w, scale, interp_method, - align_corners, align_mode) + return paddle._C_ops.final_state_linear_interp(x, OutSize, SizeTensor, + Scale, data_layout, out_d, + out_h, out_w, scale, + interp_method, align_corners, + align_mode) def linear_interp_np(input, 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 07e913a0a2..d2735eaf4b 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 @@ -22,7 +22,6 @@ import paddle.fluid as fluid import paddle.nn as nn import paddle from paddle.nn.functional import interpolate -from paddle import _C_ops paddle.enable_static() @@ -50,10 +49,11 @@ def nearest_interp_test(x, if not isinstance(SizeTensor, list) and not isinstance( SizeTensor, tuple): SizeTensor = [SizeTensor] - return _C_ops.final_state_nearest_interp(x, OutSize, SizeTensor, Scale, - data_layout, out_d, out_h, out_w, - scale, interp_method, - align_corners, align_mode) + return paddle._C_ops.final_state_nearest_interp(x, OutSize, SizeTensor, + Scale, data_layout, out_d, + out_h, out_w, scale, + interp_method, + align_corners, align_mode) def nearest_neighbor_interp_np(X, diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index 9ed41ed7c8..c6b4f835ba 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -603,7 +603,17 @@ def interpolate(x, else: out = _C_ops.linear_interp_v2(x, *dy_attr) elif resample_type == "bilinear": - out = _C_ops.bilinear_interp_v2(x, *dy_attr) + if in_dygraph_mode(): + out = _C_ops.final_state_bilinear_interp( + x, inputs['OutSize'] if 'OutSize' in inputs else None, + inputs['SizeTensor'] if 'SizeTensor' in inputs else None, + inputs['Scale'] if 'Scale' in inputs else None, + attrs['data_layout'], attrs['out_d'], attrs['out_h'], + attrs['out_w'], attrs['scale'] if 'scale' in attrs else [], + attrs['interp_method'], attrs['align_corners'], + attrs['align_mode']) + else: + out = _C_ops.bilinear_interp_v2(x, *dy_attr) elif resample_type == "trilinear": out = _C_ops.trilinear_interp_v2(x, *dy_attr) elif resample_type == "nearest": -- GitLab