Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yaml for deformable_conv and deformable_conv_v1 OPs #41644

Merged
merged 4 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions paddle/phi/infermeta/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label,
logits_grad->set_dtype(softmax.dtype());
}

void DeformableConvGradInferMeta(const MetaTensor& x,
const MetaTensor& offset,
const MetaTensor& filter,
paddle::optional<const MetaTensor&> mask,
const MetaTensor& out_grad,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& dilations,
int deformable_groups,
int groups,
int im2col_step,
MetaTensor* dx,
MetaTensor* offset_grad,
MetaTensor* filter_grad,
MetaTensor* mask_grad) {
GeneralTernaryGradInferMeta(x, offset, filter, dx, offset_grad, filter_grad);
if (mask) {
UnchangedInferMeta(mask.get(), mask_grad);
}
}

void GatherNdGradInferMeta(const MetaTensor& x,
const MetaTensor& index,
const MetaTensor& out_grad,
Expand Down
16 changes: 16 additions & 0 deletions paddle/phi/infermeta/backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label,
MetaTensor* logits_grad,
MetaConfig config = MetaConfig());

void DeformableConvGradInferMeta(const MetaTensor& x,
const MetaTensor& offset,
const MetaTensor& filter,
paddle::optional<const MetaTensor&> mask,
const MetaTensor& out_grad,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& dilations,
int deformable_groups,
int groups,
int im2col_step,
MetaTensor* dx,
MetaTensor* offset_grad,
MetaTensor* filter_grad,
MetaTensor* mask_grad);

void GatherNdGradInferMeta(const MetaTensor& x,
const MetaTensor& index,
const MetaTensor& out_grad,
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ set_tests_properties(test_lstm_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_imperative_star_gan_with_gradient_penalty PROPERTIES TIMEOUT 120)

set_tests_properties(test_bicubic_interp_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_deformable_conv_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_deformable_conv_op PROPERTIES TIMEOUT 200)
set_tests_properties(test_nearest_interp_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_profiler PROPERTIES TIMEOUT 120)
set_tests_properties(test_inplace_softmax_with_cross_entropy PROPERTIES TIMEOUT 120)
Expand Down Expand Up @@ -1045,7 +1045,7 @@ set_tests_properties(test_distributed_fused_lamb_op_with_clip PROPERTIES TIMEOUT
set_tests_properties(test_distributed_fused_lamb_op_without_clip PROPERTIES TIMEOUT 120)
set_tests_properties(test_elementwise_min_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_nan_inf PROPERTIES TIMEOUT 120)
set_tests_properties(test_deformable_conv_v1_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_deformable_conv_v1_op PROPERTIES TIMEOUT 300)
set_tests_properties(test_parallel_executor_transformer_auto_growth PROPERTIES TIMEOUT 120)
set_tests_properties(test_py_reader_using_executor PROPERTIES TIMEOUT 120)
set_tests_properties(test_elementwise_add_op PROPERTIES TIMEOUT 120)
Expand Down
9 changes: 9 additions & 0 deletions python/paddle/fluid/tests/unittests/test_deform_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import paddle.nn.initializer as I
import numpy as np
import unittest
from paddle.fluid.framework import _test_eager_guard
from unittest import TestCase


Expand Down Expand Up @@ -183,6 +184,10 @@ def test_identity(self):
self.place = paddle.CUDAPlace(0)
self._test_identity()

def test_identity_with_eager_guard(self):
with _test_eager_guard():
self.test_identity()


class TestDeformConv2DFunctional(TestCase):
batch_size = 4
Expand Down Expand Up @@ -418,6 +423,10 @@ def test_identity(self):
self.place = paddle.CUDAPlace(0)
self._test_identity()

def test_identity_with_eager_guard(self):
with _test_eager_guard():
self.test_identity()


# testcases for DeformConv2D
class TestDeformConv2DWithPadding(TestDeformConv2D):
Expand Down
35 changes: 31 additions & 4 deletions python/paddle/fluid/tests/unittests/test_deformable_conv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

from __future__ import print_function

import paddle
import unittest
import numpy as np

import paddle
import paddle.fluid.core as core
import paddle.fluid as fluid
from op_test import OpTest
from paddle.fluid.framework import _test_eager_guard

paddle.enable_static()


def dmc_bilinear(data_im, height, width, h, w):
Expand Down Expand Up @@ -108,8 +110,24 @@ def dconv_im2col_gemm(input, offset, mask, filter, group, conv_param):
return out


def deform_conv2d_wrapper(x,
offset,
weight,
mask=None,
stride=1,
padding=0,
dilation=1,
deformable_groups=1,
groups=1,
im2col_step=1):
return paddle.vision.ops.deform_conv2d(x, offset, weight, None, stride,
padding, dilation, deformable_groups,
groups, mask)


class TestModulatedDeformableConvOp(OpTest):
def setUp(self):
self.python_api = deform_conv2d_wrapper
self.op_type = "deformable_conv"
self.init_type()
self.init_group()
Expand Down Expand Up @@ -148,13 +166,14 @@ def setUp(self):
self.outputs = {'Output': output}

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(
{'Input', 'Offset', 'Mask', 'Filter'},
'Output',
max_relative_error=0.05)
max_relative_error=0.05,
check_eager=True)

def init_test_case(self):
self.pad = [1, 1]
Expand Down Expand Up @@ -327,6 +346,10 @@ def test_invalid_filter():

self.assertRaises(ValueError, test_invalid_filter)

def test_error_with_eager_guard(self):
with _test_eager_guard():
self.test_error()


class TestDeformConv2DAPI(unittest.TestCase):
def test_api(self):
Expand Down Expand Up @@ -358,6 +381,10 @@ def test_deform_conv2d_v2():

test_deform_conv2d_v2()

def test_api_with_eager_guard(self):
with _test_eager_guard():
self.test_api()


if __name__ == '__main__':
unittest.main()
35 changes: 30 additions & 5 deletions python/paddle/fluid/tests/unittests/test_deformable_conv_v1_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

from __future__ import print_function

import paddle
import unittest
import numpy as np

import paddle.fluid.core as core
import paddle.fluid as fluid
import paddle.fluid.core as core
from op_test import OpTest
from paddle.fluid.framework import _test_eager_guard


def dmc_bilinear(data_im, height, width, h, w):
Expand Down Expand Up @@ -105,8 +106,24 @@ def dconv_im2col_gemm(input, offset, filter, group, conv_param):
return out


def deform_conv2d_wrapper(x,
offset,
weight,
mask=None,
stride=1,
padding=0,
dilation=1,
deformable_groups=1,
groups=1,
im2col_step=1):
return paddle.vision.ops.deform_conv2d(x, offset, weight, None, stride,
padding, dilation, deformable_groups,
groups, mask)


class TestModulatedDeformableConvOp(OpTest):
def setUp(self):
self.python_api = deform_conv2d_wrapper
self.op_type = "deformable_conv_v1"
self.init_type()
self.init_group()
Expand Down Expand Up @@ -142,18 +159,22 @@ def setUp(self):
self.outputs = {'Output': output}

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(
['Input', 'Offset', 'Filter'], 'Output', max_relative_error=0.05)
['Input', 'Offset', 'Filter'],
'Output',
max_relative_error=0.05,
check_eager=True)

def test_check_grad_no_filter(self):
self.check_grad(
['Input', 'Offset'],
'Output',
max_relative_error=0.1,
no_grad_set=set(['Filter']))
no_grad_set=set(['Filter']),
check_eager=True)

def init_test_case(self):
self.pad = [1, 1]
Expand Down Expand Up @@ -292,6 +313,10 @@ def test_invalid_offset():

self.assertRaises(TypeError, test_invalid_offset)

def test_error_with_eager_guard(self):
with _test_eager_guard():
self.test_error()


if __name__ == '__main__':
unittest.main()
10 changes: 10 additions & 0 deletions python/paddle/utils/code_gen/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@
func : cumsum
backward : cumsum_grad

- api : deformable_conv
args : (Tensor x, Tensor offset, Tensor filter, Tensor mask, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step)
output : Tensor(out)
infer_meta :
func : DeformableConvInferMeta
kernel :
func : deformable_conv
optional : mask
backward : deformable_conv_grad

- api : depthwise_conv2d_transpose
args : (Tensor x, Tensor filter, int[] strides, int[] paddings, int[] output_padding, int[] output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
output : Tensor(out)
Expand Down
10 changes: 10 additions & 0 deletions python/paddle/utils/code_gen/backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@
output : Tensor(x_grad)
invoke : cumsum(out_grad, axis, flatten, exclusive, !reverse)

- backward_api : deformable_conv_grad
forward : deformable_conv(Tensor x, Tensor offset, Tensor filter, Tensor mask, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step) -> Tensor(out)
args : (Tensor x, Tensor offset, Tensor filter, Tensor mask, Tensor out_grad, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step)
output : Tensor(x_grad), Tensor(offset_grad), Tensor(filter_grad), Tensor(mask_grad)
infer_meta :
func : DeformableConvGradInferMeta
kernel :
func : deformable_conv_grad
optional : mask

- backward_api : depthwise_conv2d_transpose_grad
forward : depthwise_conv2d_transpose(Tensor x, Tensor filter, int[] strides, int[] paddings, int[] output_padding, int[] output_size, str padding_algorithm, int groups, int[] dilations, str data_format) -> Tensor(out)
args : (Tensor x, Tensor filter, Tensor out_grad, int[] strides, int[] paddings, int[] output_padding, int[] output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
Expand Down
10 changes: 9 additions & 1 deletion python/paddle/vision/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,15 @@ def deform_conv2d(x,

use_deform_conv2d_v1 = True if mask is None else False

if _non_static_mode():
if in_dygraph_mode():
pre_bias = _C_ops.final_state_deformable_conv(
x, offset, weight, mask, stride, padding, dilation,
deformable_groups, groups, 1)
if bias is not None:
out = nn.elementwise_add(pre_bias, bias, axis=1)
else:
out = pre_bias
elif _in_legacy_dygraph():
attrs = ('strides', stride, 'paddings', padding, 'dilations', dilation,
'deformable_groups', deformable_groups, 'groups', groups,
'im2col_step', 1)
Expand Down
2 changes: 1 addition & 1 deletion tools/infrt/skipped_phi_api.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"phi_apis":["conj", "dropout", "expand_as", "nll_loss", "psroi_pool", "roi_align", "roi_pool", "label_smooth", "layer_norm"],
"phi_apis":["conj", "deformable_conv", "dropout", "expand_as", "nll_loss", "psroi_pool", "roi_align", "roi_pool", "label_smooth", "layer_norm"],
"phi_kernels":["equal_all"]
}