提交 121e947f 编写于 作者: T TensorFlower Gardener

Merge pull request #35207 from pooyadavoodi:fix_deconv_converter

PiperOrigin-RevId: 286331612
Change-Id: Ifbf078bffb74b8964699deec9d566e01538f2262
......@@ -2230,7 +2230,38 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group,
conv_layer = layer;
}
nvinfer1::ITensor* output_tensor = conv_layer->getOutput(0);
// Add an extra padding for Deconv because TRT doesn't accept the
// argument output_shape and thus the TRT output shape could be wrong
// in case of strides>1.
if (is_conv2d_backprop_input) {
auto tf_output_shape = backprop_output_size.GetTrtDims();
nvinfer1::Dims trt_output_shape = output_tensor->getDimensions();
// What determines the padding size is the difference between the given
// input_sizes (tf_output_shape) and TRT computed size.
const int height_diff =
tf_output_shape.d[h_index - 1] - trt_output_shape.d[1];
const int width_diff =
tf_output_shape.d[w_index - 1] - trt_output_shape.d[2];
if ((height_diff < 0) || (width_diff < 0)) {
return errors::InvalidArgument(
"input_sizes argument of Conv2DBackprop (i.e. output_shape argument "
"of conv2d_transpose)",
"is too small for the given out_backprop argument of Conv2DBackprop "
"(i.e. input argument of conv2d_transpose).",
"(", tf_output_shape.d[h_index - 1], ", ",
tf_output_shape.d[w_index - 1], ") >= ", "(", trt_output_shape.d[1],
", ", trt_output_shape.d[2], ")", node_def.name());
}
// Only add a padding layer if padding sizes are larger than 0
if ((height_diff > 0) || (width_diff > 0)) {
nvinfer1::DimsHW pre_padding(0, 0);
nvinfer1::DimsHW post_padding(height_diff, width_diff);
nvinfer1::IPaddingLayer* padding_layer =
params->converter->network()->addPadding(*output_tensor, pre_padding,
post_padding);
output_tensor = padding_layer->getOutput(0);
}
}
// Restore transpose.
if (need_transpose) {
TF_RETURN_IF_ERROR(params->converter->TransposeTensor(
......
......@@ -3942,6 +3942,31 @@ TEST_F(OpConverterTest, ConvertConv2D) {
/*is_conv2d_backprop_input=*/true,
/*expected_output_dims=*/{1, 2, 4},
/*expected_output=*/{0, 0, -1, 1, -2, 2, -3, 3}},
// Transpose Strided NHWC
TestParams{/*input_dims=*/{2, 2, 1},
/*input=*/{0, 1, 2, 3},
/*filter_dims=*/{1, 2, 1, 1},
/*filter=*/{-1, 1},
/*strides=*/{1, 1, 2, 1},
/*padding=*/"SAME",
/*data_format=*/"NHWC",
/*dilations=*/{1, 1, 1, 1},
/*is_conv2d_backprop_input=*/true,
/*expected_output_dims=*/{2, 4, 1},
/*expected_output=*/{0, 0, -1, 1, -2, 2, -3, 3}},
// Transpose Strided NHWC with VALID padding
TestParams{/*input_dims=*/{3, 1, 1},
/*input=*/{0, 1, 2},
/*filter_dims=*/{2, 1, 1, 1},
/*filter=*/{-1, 1},
/*strides=*/{1, 2, 1, 1},
/*padding=*/"VALID",
/*data_format=*/"NHWC",
/*dilations=*/{1, 1, 1, 1},
/*is_conv2d_backprop_input=*/true,
/*expected_output_dims=*/{7, 1, 1},
/*expected_output=*/{0, 0, -1, 1, -2, 2, 0}},
};
for (int i = 0; i < kConv2DOKCases; i++) {
......@@ -3953,10 +3978,8 @@ TEST_F(OpConverterTest, ConvertConv2D) {
AddTestWeights<float>("weights", ok_params[i].filter_dims,
ok_params[i].filter);
if (ok_params[i].is_conv2d_backprop_input) {
AddTestWeights<float>(
"input_sizes",
{static_cast<int>(ok_params[i].expected_output.size())},
ok_params[i].expected_output);
AddTestWeights<float>("input_sizes", ok_params[i].expected_output_dims,
ok_params[i].expected_output);
}
RunValidationAndConversion(node_def);
TRT_TensorOrWeights output;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册