Skip to content

Commit

Permalink
[OpAttr]Adapt tensor output_size for conv2d_transpose and depthwise_c…
Browse files Browse the repository at this point in the history
…onv2d_transpose (#45620)

Adapt tensor output_size for conv2d_transpose and depthwise_conv2d_transpose
  • Loading branch information
0x45f authored Sep 7, 2022
1 parent d9a9e63 commit fe169bf
Show file tree
Hide file tree
Showing 23 changed files with 359 additions and 54 deletions.
11 changes: 6 additions & 5 deletions paddle/fluid/operators/conv_transpose_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ void Conv2DTransposeOpMaker::Make() {
AddAttr<std::vector<int>>("output_size",
"(vector<int> default: []), the "
"size of the output tensor")
.SetDefault({});
.SetDefault({})
.SupportTensor();
AddAttr<int>("groups",
"(int default:1), the groups number of the convolution "
"transpose operator. ")
Expand Down Expand Up @@ -398,10 +399,10 @@ namespace ops = paddle::operators;
// conv2d_transpose
DECLARE_INFER_SHAPE_FUNCTOR(conv2d_transpose,
Conv2dTranposeInferShapeFunctor,
PD_INFER_META(phi::ConvTransposeInferMeta));
PD_INFER_META(phi::Conv2dTransposeInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(conv2d_transpose_grad,
Conv2dTranposeGradInferShapeFunctor,
PD_INFER_META(phi::ConvTransposeGradInferMeta));
PD_INFER_META(phi::Conv2dTransposeGradInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(
conv2d_transpose_grad_grad,
Conv2dTranposeDoubleGradInferShapeFunctor,
Expand Down Expand Up @@ -443,10 +444,10 @@ REGISTER_OPERATOR(conv3d_transpose_grad,
// depthwise conv2d_transpose
DECLARE_INFER_SHAPE_FUNCTOR(depthwise_conv2d_transpose,
DepthWiseConv2dTranposeInferShapeFunctor,
PD_INFER_META(phi::ConvTransposeInferMeta));
PD_INFER_META(phi::Conv2dTransposeInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(depthwise_conv2d_transpose_grad,
DepthWiseConv2dTranposeGradInferShapeFunctor,
PD_INFER_META(phi::ConvTransposeGradInferMeta));
PD_INFER_META(phi::Conv2dTransposeGradInferMeta));

REGISTER_OPERATOR(depthwise_conv2d_transpose,
ops::ConvTransposeOp,
Expand Down
8 changes: 4 additions & 4 deletions paddle/phi/api/yaml/legacy_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,10 @@
backward : conv2d_grad

- api : 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)
args : (Tensor x, Tensor filter, int[] strides, int[] paddings, int[] output_padding, IntArray output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
output : Tensor(out)
infer_meta :
func : ConvTransposeInferMeta
func : Conv2dTransposeInferMeta
kernel :
func : conv2d_transpose
use_gpudnn : true
Expand Down Expand Up @@ -665,10 +665,10 @@
backward : depthwise_conv2d_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)
args : (Tensor x, Tensor filter, int[] strides, int[] paddings, int[] output_padding, IntArray output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
output : Tensor(out)
infer_meta :
func : ConvTransposeInferMeta
func : Conv2dTransposeInferMeta
kernel :
func : depthwise_conv2d_transpose
backward : depthwise_conv2d_transpose_grad
Expand Down
16 changes: 8 additions & 8 deletions paddle/phi/api/yaml/legacy_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@
optional : grad_input_grad, grad_filter_grad

- backward_api : conv2d_transpose_double_grad
forward : conv2d_transpose_grad(Tensor x, Tensor filter, Tensor grad_out, int[] strides, int[] paddings, int[] output_padding, int[] output_size, str padding_algorithm, int groups, int[] dilations, str data_format) -> Tensor(grad_x), Tensor(grad_filter)
args : (Tensor x, Tensor filter, Tensor grad_out, Tensor grad_x_grad, Tensor grad_filter_grad, int[] strides, int[] paddings, int[] output_padding, int[] output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
forward : conv2d_transpose_grad(Tensor x, Tensor filter, Tensor grad_out, int[] strides, int[] paddings, int[] output_padding, IntArray output_size, str padding_algorithm, int groups, int[] dilations, str data_format) -> Tensor(grad_x), Tensor(grad_filter)
args : (Tensor x, Tensor filter, Tensor grad_out, Tensor grad_x_grad, Tensor grad_filter_grad, int[] strides, int[] paddings, int[] output_padding, IntArray output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
output : Tensor(x_grad), Tensor(filter_grad), Tensor(grad_out_grad)
infer_meta :
func : Conv2dTransposeDoubleGradInferMeta
Expand All @@ -493,11 +493,11 @@
use_gpudnn : true

- backward_api : conv2d_transpose_grad
forward : 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)
forward : conv2d_transpose(Tensor x, Tensor filter, int[] strides, int[] paddings, int[] output_padding, IntArray 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, IntArray output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
output : Tensor(x_grad), Tensor(filter_grad)
infer_meta :
func : ConvTransposeGradInferMeta
func : Conv2dTransposeGradInferMeta
kernel :
func : conv2d_transpose_grad
use_gpudnn : true
Expand Down Expand Up @@ -635,11 +635,11 @@
optional : grad_input_grad, grad_filter_grad

- 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)
forward : depthwise_conv2d_transpose(Tensor x, Tensor filter, int[] strides, int[] paddings, int[] output_padding, IntArray 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, IntArray output_size, str padding_algorithm, int groups, int[] dilations, str data_format)
output : Tensor(x_grad), Tensor(filter_grad)
infer_meta :
func : ConvTransposeGradInferMeta
func : Conv2dTransposeGradInferMeta
kernel :
func : depthwise_conv2d_transpose_grad

Expand Down
18 changes: 17 additions & 1 deletion paddle/phi/infermeta/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ void ConvTransposeGradInferMeta(const MetaTensor& x,
GeneralBinaryGradInferMeta(x, filter, dx, dfilter);
}

void Conv2dTransposeGradInferMeta(const MetaTensor& x,
const MetaTensor& filter,
const MetaTensor& dout,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
const std::string& data_format,
MetaTensor* dx,
MetaTensor* dfilter) {
GeneralBinaryGradInferMeta(x, filter, dx, dfilter);
}

void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
const MetaTensor& filter,
const MetaTensor& dout,
Expand All @@ -151,7 +167,7 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
16 changes: 15 additions & 1 deletion paddle/phi/infermeta/backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ void ConvTransposeGradInferMeta(const MetaTensor& x,
MetaTensor* dx,
MetaTensor* dfilter);

void Conv2dTransposeGradInferMeta(const MetaTensor& x,
const MetaTensor& filter,
const MetaTensor& dout,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
const std::string& data_format,
MetaTensor* dx,
MetaTensor* dfilter);

void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
const MetaTensor& filter,
const MetaTensor& dout,
Expand All @@ -84,7 +98,7 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
28 changes: 28 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,34 @@ void ConvTransposeInferMeta(const MetaTensor& x,
out->set_dtype(x.dtype());
}

void Conv2dTransposeInferMeta(const MetaTensor& x,
const MetaTensor& filter,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
const std::string& data_format,
MetaTensor* out,
MetaConfig config) {
std::vector<int32_t> vec_output_size(output_size.GetData().begin(),
output_size.GetData().end());
ConvTransposeInferMeta(x,
filter,
strides,
paddings,
output_padding,
vec_output_size,
padding_algorithm,
groups,
dilations,
data_format,
out,
config);
}

void CrossInferMeta(const MetaTensor& x,
const MetaTensor& y,
int axis,
Expand Down
13 changes: 13 additions & 0 deletions paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ void ConvTransposeInferMeta(const MetaTensor& x,
MetaTensor* out,
MetaConfig config = MetaConfig());

void Conv2dTransposeInferMeta(const MetaTensor& x,
const MetaTensor& filter,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
const std::string& data_format,
MetaTensor* out,
MetaConfig config = MetaConfig());

void CrossInferMeta(const MetaTensor& x,
const MetaTensor& y,
int axis,
Expand Down
7 changes: 4 additions & 3 deletions paddle/phi/kernels/conv_transpose_grad_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>
#include <vector>

#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/dense_tensor.h"

namespace phi {
Expand All @@ -29,7 +30,7 @@ void Conv2dTransposeGradKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand All @@ -47,7 +48,7 @@ void Conv2dTransposeDoubleGradKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down Expand Up @@ -80,7 +81,7 @@ void DepthwiseConv2dTransposeGradKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
5 changes: 3 additions & 2 deletions paddle/phi/kernels/conv_transpose_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>
#include <vector>

#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/dense_tensor.h"

namespace phi {
Expand All @@ -28,7 +29,7 @@ void Conv2dTransposeKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down Expand Up @@ -56,7 +57,7 @@ void DepthwiseConv2dTransposeKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/cpu/conv_transpose_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void DepthwiseConv2dTransposeGradKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/cpu/conv_transpose_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void DepthwiseConv2dTransposeKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
4 changes: 2 additions & 2 deletions paddle/phi/kernels/gpu/conv_transpose_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Conv2dTransposeDoubleGradKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down Expand Up @@ -64,7 +64,7 @@ void DepthwiseConv2dTransposeGradKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/gpu/conv_transpose_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void DepthwiseConv2dTransposeKernel(const Context& ctx,
const std::vector<int>& strides,
const std::vector<int>& paddings,
const std::vector<int>& output_padding,
const std::vector<int>& output_size,
const IntArray& output_size,
const std::string& padding_algorithm,
int groups,
const std::vector<int>& dilations,
Expand Down
Loading

0 comments on commit fe169bf

Please sign in to comment.