From b470bd18d593f67a766b5b3ba29e01e91c1d7884 Mon Sep 17 00:00:00 2001 From: Wang Bojun <105858416+wwbitejotunn@users.noreply.github.com> Date: Mon, 25 Jul 2022 12:52:49 +0800 Subject: [PATCH] Phi gird sampler migration (#44562) * add_ymal_utest for phi grid_sampler op --- paddle/phi/api/yaml/legacy_api.yaml | 12 ++++++++++++ paddle/phi/api/yaml/legacy_backward.yaml | 12 ++++++++++++ .../fluid/tests/unittests/test_grid_sampler_op.py | 6 ++++-- python/paddle/nn/functional/vision.py | 6 +++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index 464ea1f00ba..6408b908130 100644 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -922,6 +922,18 @@ kernel : func : greater_than +# grid sample +- api : grid_sample + args : (Tensor x, Tensor grid, str mode, str padding_mode, bool align_corners) + output : Tensor(out) + infer_meta : + func : GridSampleBaseInferMeta + param : [x, grid] + kernel: + func : grid_sample + data_type : x + backward : grid_sample_grad + - api : group_norm args : (Tensor x, Tensor scale, Tensor bias, float epsilon, int groups, str data_layout) output : Tensor(y), Tensor(mean), Tensor(variance) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 423958399a5..ba8c78edd97 100644 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -848,6 +848,18 @@ data_type : out_grad optional: out, dst_count +# grid sample +- backward_api : grid_sample_grad + forward : grid_sample (Tensor x, Tensor grid, str mode, str padding_mode, bool align_corners) -> Tensor(out) + args : (Tensor x, Tensor grid, Tensor out_grad, str mode, str padding_mode, bool align_corners) + output : Tensor(x_grad), Tensor(grid_grad) + infer_meta : + func : GeneralBinaryGradInferMeta + param : [x, grid] + kernel : + func : grid_sample_grad + data_type : x + - backward_api : group_norm_grad forward : group_norm (Tensor x, Tensor scale, Tensor bias, float epsilon, int groups, str data_layout) -> Tensor(y), Tensor(mean), Tensor(variance) args : (Tensor x, Tensor scale, Tensor bias, Tensor y, Tensor mean, Tensor variance, Tensor y_grad, float epsilon, int groups, str data_layout) diff --git a/python/paddle/fluid/tests/unittests/test_grid_sampler_op.py b/python/paddle/fluid/tests/unittests/test_grid_sampler_op.py index f32387d07a9..4b813330b40 100644 --- a/python/paddle/fluid/tests/unittests/test_grid_sampler_op.py +++ b/python/paddle/fluid/tests/unittests/test_grid_sampler_op.py @@ -144,6 +144,7 @@ class TestGridSamplerOp(OpTest): self.use_cudnn = False self.numeric_grad_delta = 0.0001 self.op_type = 'grid_sampler' + self.python_api = paddle.nn.functional.grid_sample self.align_corners = True self.padding_mode = "zeros" self.mode = "bilinear" @@ -171,13 +172,14 @@ class TestGridSamplerOp(OpTest): } def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): self.check_grad(['X', 'Grid'], 'Output', max_relative_error=0.01, - numeric_grad_delta=self.numeric_grad_delta) + numeric_grad_delta=self.numeric_grad_delta, + check_eager=True) def initTestCase(self): self.x_shape = (2, 3, 8, 8) diff --git a/python/paddle/nn/functional/vision.py b/python/paddle/nn/functional/vision.py index 521a44f758b..b1a962845c4 100644 --- a/python/paddle/nn/functional/vision.py +++ b/python/paddle/nn/functional/vision.py @@ -21,6 +21,7 @@ import numpy as np from paddle import _C_ops from ...device import is_compiled_with_rocm from paddle import in_dynamic_mode +from paddle.fluid.framework import in_dygraph_mode from paddle.framework import _non_static_mode __all__ = [] @@ -272,7 +273,10 @@ def grid_sample(x, x.stop_gradient = False grid.stop_gradient = False - if in_dynamic_mode(): + if in_dygraph_mode(): + return _C_ops.final_state_grid_sample(x, grid, mode, padding_mode, + align_corners) + elif in_dynamic_mode(): attrs = ('mode', mode, 'padding_mode', padding_mode, 'align_corners', align_corners, 'use_cudnn', use_cudnn) out = getattr(_C_ops, 'grid_sampler')(x, grid, *attrs) -- GitLab