From 7852a5b2b38fc5d47cc8eef8456ecb34a3eac724 Mon Sep 17 00:00:00 2001 From: Pranav Marathe Date: Fri, 19 Apr 2019 09:43:36 -0700 Subject: [PATCH 1/5] Adds asymmetric padding for TRT 5.1.3+ --- .../tf2tensorrt/convert/convert_nodes.cc | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc index 2b5e8f8993b..1938379cc23 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc @@ -1969,6 +1969,10 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, } else { padding = {{0, 0}, {0, 0}}; } + +// TensorRT 5.1 added support for asymmetric padding. Due to a bug in 5.1.2, we can only use asymmetric padding in convolutions with 5.1.3+. +#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) +#else if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { // Handle asymmetric padding. @@ -1981,6 +1985,7 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, padding = {{0, 0}, {0, 0}}; tensor = pad_layer->getOutput(0); } +#endif // Add convolution. nvinfer1::ILayer* conv_layer = nullptr; @@ -1991,7 +1996,21 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, biases.GetTrtWeights()); TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); - layer->setPadding({padding[0].first, padding[1].first}); +// TensorRT 5.1.3 added support for padding modes. +#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) + if (attrs.get("padding") == "SAME") { + VLOG(2) << "Using SAME padding"; + // SAME_UPPER means that post padding is preferred. + layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); + } + // For VALID padding, we need to manually set the padding. + layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); + layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); + VLOG(2) << "Set pre-padding to: " << DebugString(layer->getPrePadding()) << " and post-padding to: " << DebugString(layer->getPostPadding()); +#else + layer->setPadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); + VLOG(2) << "Set padding to: " << DebugString(layer->getPadding()); +#endif layer->setName(node_def.name().c_str()); layer->setNbGroups(num_groups); conv_layer = layer; @@ -2002,7 +2021,21 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, biases.GetTrtWeights()); TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); - layer->setPadding({padding[0].first, padding[1].first}); +// TensorRT 5.1.3 added support for padding modes. +#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) + if (attrs.get("padding") == "SAME") { + VLOG(2) << "Using SAME padding"; + // SAME_UPPER means that post padding is preferred. + layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); + } + // For VALID padding, we need to manually set the padding. + layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); + layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); + VLOG(2) << "Set pre-padding to: " << DebugString(layer->getPrePadding()) << " and post-padding to: " << DebugString(layer->getPostPadding()); +#else + layer->setPadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); + VLOG(2) << "Set padding to: " << DebugString(layer->getPadding()); +#endif layer->setName(node_def.name().c_str()); layer->setNbGroups(num_groups); layer->setDilation(dilation); @@ -2748,6 +2781,9 @@ Status ConvertPool(OpConverterParams* params) { padding = {{0, 0}, {0, 0}}; } +// TensorRT 5.1 added support for asymmetric padding. +#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) +#else if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { VLOG(2) << "Padding!!!: " << padding[0].first << padding[0].second @@ -2761,6 +2797,7 @@ Status ConvertPool(OpConverterParams* params) { padding = {{0, 0}, {0, 0}}; tensor = pad_layer->getOutput(0); } +#endif nvinfer1::IPoolingLayer* layer = params->converter->network()->addPooling(*tensor, type, ksize); @@ -2772,7 +2809,21 @@ Status ConvertPool(OpConverterParams* params) { layer->getOutput(0)); layer->setStride(stride); - layer->setPadding({padding[0].first, padding[1].first}); +// TensorRT 5.1.3 added support for padding modes. +#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) + if (attrs.get("padding") == "SAME") { + // SAME_UPPER means that post padding is preferred. + layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); + } +#endif +// TensorRT 5.1 has support for asymmetric padding. +#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) + // If padding mode is not SAME, then these values will be used instead. + layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); + layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); +#else + layer->setPadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); +#endif layer->setName(node_def.name().c_str()); nvinfer1::ITensor* output_tensor = layer->getOutput(0); From 114c2e7b3dbdc5cb7b3bcdadb23a31e491a5cab8 Mon Sep 17 00:00:00 2001 From: Pranav Marathe Date: Fri, 19 Apr 2019 09:44:54 -0700 Subject: [PATCH 2/5] clang-formats modified files --- .../tf2tensorrt/convert/convert_nodes.cc | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc index 1938379cc23..e0d4a462c0c 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc @@ -1970,8 +1970,12 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, padding = {{0, 0}, {0, 0}}; } -// TensorRT 5.1 added support for asymmetric padding. Due to a bug in 5.1.2, we can only use asymmetric padding in convolutions with 5.1.3+. -#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) +// TensorRT 5.1 added support for asymmetric padding. Due to a bug in 5.1.2, we +// can only use asymmetric padding in convolutions with 5.1.3+. +#if (NV_TENSORRT_MAJOR > 5) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ + NV_TENSORRT_PATCH >= 3) #else if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { @@ -1997,7 +2001,10 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); // TensorRT 5.1.3 added support for padding modes. -#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) +#if (NV_TENSORRT_MAJOR > 5) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ + NV_TENSORRT_PATCH >= 3) if (attrs.get("padding") == "SAME") { VLOG(2) << "Using SAME padding"; // SAME_UPPER means that post padding is preferred. @@ -2005,8 +2012,10 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, } // For VALID padding, we need to manually set the padding. layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); - layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); - VLOG(2) << "Set pre-padding to: " << DebugString(layer->getPrePadding()) << " and post-padding to: " << DebugString(layer->getPostPadding()); + layer->setPostPadding( + nvinfer1::DimsHW{padding[0].second, padding[1].second}); + VLOG(2) << "Set pre-padding to: " << DebugString(layer->getPrePadding()) + << " and post-padding to: " << DebugString(layer->getPostPadding()); #else layer->setPadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); VLOG(2) << "Set padding to: " << DebugString(layer->getPadding()); @@ -2022,7 +2031,10 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); // TensorRT 5.1.3 added support for padding modes. -#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) +#if (NV_TENSORRT_MAJOR > 5) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ + NV_TENSORRT_PATCH >= 3) if (attrs.get("padding") == "SAME") { VLOG(2) << "Using SAME padding"; // SAME_UPPER means that post padding is preferred. @@ -2030,8 +2042,10 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, } // For VALID padding, we need to manually set the padding. layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); - layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); - VLOG(2) << "Set pre-padding to: " << DebugString(layer->getPrePadding()) << " and post-padding to: " << DebugString(layer->getPostPadding()); + layer->setPostPadding( + nvinfer1::DimsHW{padding[0].second, padding[1].second}); + VLOG(2) << "Set pre-padding to: " << DebugString(layer->getPrePadding()) + << " and post-padding to: " << DebugString(layer->getPostPadding()); #else layer->setPadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); VLOG(2) << "Set padding to: " << DebugString(layer->getPadding()); @@ -2782,7 +2796,8 @@ Status ConvertPool(OpConverterParams* params) { } // TensorRT 5.1 added support for asymmetric padding. -#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) +#if (NV_TENSORRT_MAJOR > 5) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) #else if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { @@ -2810,14 +2825,18 @@ Status ConvertPool(OpConverterParams* params) { layer->setStride(stride); // TensorRT 5.1.3 added support for padding modes. -#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && NV_TENSORRT_PATCH >= 3) +#if (NV_TENSORRT_MAJOR > 5) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ + NV_TENSORRT_PATCH >= 3) if (attrs.get("padding") == "SAME") { // SAME_UPPER means that post padding is preferred. layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); } #endif // TensorRT 5.1 has support for asymmetric padding. -#if (NV_TENSORRT_MAJOR > 5) || (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) +#if (NV_TENSORRT_MAJOR > 5) || \ + (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) // If padding mode is not SAME, then these values will be used instead. layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); From 403e524554b69205c18ac84f5c5ed65dd46ef1de Mon Sep 17 00:00:00 2001 From: Pranav Marathe Date: Fri, 19 Apr 2019 11:47:56 -0700 Subject: [PATCH 3/5] Uses helper macro for version checks --- .../tf2tensorrt/convert/convert_nodes.cc | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc index e0d4a462c0c..056190213af 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc @@ -1972,10 +1972,7 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, // TensorRT 5.1 added support for asymmetric padding. Due to a bug in 5.1.2, we // can only use asymmetric padding in convolutions with 5.1.3+. -#if (NV_TENSORRT_MAJOR > 5) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ - NV_TENSORRT_PATCH >= 3) +#if IS_TRT_VERSION_GE(5, 1, 3, 0) #else if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { @@ -2001,10 +1998,7 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); // TensorRT 5.1.3 added support for padding modes. -#if (NV_TENSORRT_MAJOR > 5) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ - NV_TENSORRT_PATCH >= 3) +#if IS_TRT_VERSION_GE(5, 1, 3, 0) if (attrs.get("padding") == "SAME") { VLOG(2) << "Using SAME padding"; // SAME_UPPER means that post padding is preferred. @@ -2031,10 +2025,7 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); // TensorRT 5.1.3 added support for padding modes. -#if (NV_TENSORRT_MAJOR > 5) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ - NV_TENSORRT_PATCH >= 3) +#if IS_TRT_VERSION_GE(5, 1, 3, 0) if (attrs.get("padding") == "SAME") { VLOG(2) << "Using SAME padding"; // SAME_UPPER means that post padding is preferred. @@ -2796,8 +2787,7 @@ Status ConvertPool(OpConverterParams* params) { } // TensorRT 5.1 added support for asymmetric padding. -#if (NV_TENSORRT_MAJOR > 5) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) +#if IS_TRT_VERSION_GE(5, 1, 0, 0) #else if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { @@ -2825,18 +2815,14 @@ Status ConvertPool(OpConverterParams* params) { layer->setStride(stride); // TensorRT 5.1.3 added support for padding modes. -#if (NV_TENSORRT_MAJOR > 5) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR > 1) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR == 1 && \ - NV_TENSORRT_PATCH >= 3) +#if IS_TRT_VERSION_GE(5, 1, 3, 0) if (attrs.get("padding") == "SAME") { // SAME_UPPER means that post padding is preferred. layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); } #endif // TensorRT 5.1 has support for asymmetric padding. -#if (NV_TENSORRT_MAJOR > 5) || \ - (NV_TENSORRT_MAJOR == 5 && NV_TENSORRT_MINOR >= 1) +#if IS_TRT_VERSION_GE(5, 1, 0, 0) // If padding mode is not SAME, then these values will be used instead. layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); layer->setPostPadding(nvinfer1::DimsHW{padding[0].second, padding[1].second}); From 156ee3d1122eca463cc1f94572a0abb6a0d1e72c Mon Sep 17 00:00:00 2001 From: Pranav Marathe Date: Thu, 25 Apr 2019 14:36:50 -0700 Subject: [PATCH 4/5] More readable version checks --- tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc index 056190213af..d3af546938e 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc @@ -1972,8 +1972,7 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, // TensorRT 5.1 added support for asymmetric padding. Due to a bug in 5.1.2, we // can only use asymmetric padding in convolutions with 5.1.3+. -#if IS_TRT_VERSION_GE(5, 1, 3, 0) -#else +#if !IS_TRT_VERSION_GE(5, 1, 3, 0) if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { // Handle asymmetric padding. @@ -2787,8 +2786,7 @@ Status ConvertPool(OpConverterParams* params) { } // TensorRT 5.1 added support for asymmetric padding. -#if IS_TRT_VERSION_GE(5, 1, 0, 0) -#else +#if !IS_TRT_VERSION_GE(5, 1, 0, 0) if (padding[0].first != padding[0].second || padding[1].first != padding[1].second) { VLOG(2) << "Padding!!!: " << padding[0].first << padding[0].second From ec628d3f963f8b8afba78424bd160eae2f591723 Mon Sep 17 00:00:00 2001 From: Pranav Marathe Date: Thu, 25 Apr 2019 14:40:35 -0700 Subject: [PATCH 5/5] Removes some redundant comments --- tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc index d3af546938e..8bddf611453 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc @@ -2023,14 +2023,11 @@ Status ConvertConv2DHelper(OpConverterParams* params, int group, biases.GetTrtWeights()); TFTRT_RETURN_ERROR_IF_NULLPTR(layer, node_def.name()); layer->setStride(stride); -// TensorRT 5.1.3 added support for padding modes. #if IS_TRT_VERSION_GE(5, 1, 3, 0) if (attrs.get("padding") == "SAME") { VLOG(2) << "Using SAME padding"; - // SAME_UPPER means that post padding is preferred. layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); } - // For VALID padding, we need to manually set the padding. layer->setPrePadding(nvinfer1::DimsHW{padding[0].first, padding[1].first}); layer->setPostPadding( nvinfer1::DimsHW{padding[0].second, padding[1].second});