From cf8a55730b089072e33fbfccaa405ca330cce721 Mon Sep 17 00:00:00 2001 From: feng_shuai Date: Tue, 15 Feb 2022 13:37:56 +0800 Subject: [PATCH] pool2d_coonvert_ut (#39545) --- .../inference/tensorrt/convert/pool2d_op.cc | 146 +++++++++++------- .../tensorrt/plugin/pool_op_plugin.cu | 61 +++++++- .../tensorrt/plugin/pool_op_plugin.h | 84 +++++----- .../ir/inference/test_trt_convert_pool2d.py | 44 ++---- .../ir/inference/test_trt_pool_op.py | 11 ++ 5 files changed, 203 insertions(+), 143 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/pool2d_op.cc b/paddle/fluid/inference/tensorrt/convert/pool2d_op.cc index 26d87e4832..fe04d552e4 100644 --- a/paddle/fluid/inference/tensorrt/convert/pool2d_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/pool2d_op.cc @@ -106,6 +106,9 @@ class Pool2dOpConverter : public OpConverter { reduce_operation = nvinfer1::ReduceOperation::kAVG; plugin_pool_type = plugin::PoolPlugin::PoolType::avg; } + if (global_pooling || adaptive) { + std::fill(paddings.begin(), paddings.end(), 0); + } if (padding_algorithm == "VALID") { std::fill(paddings.begin(), paddings.end(), 0); @@ -136,6 +139,46 @@ class Pool2dOpConverter : public OpConverter { #endif } + std::vector real_paddings = paddings; + for (int i = 0; i < 2; ++i) { + int copy_pad = *(paddings.begin() + i); + real_paddings.insert(real_paddings.begin() + 2 * i + 1, copy_pad); + } + // SAME + if (padding_algorithm == "SAME") { + // expand + for (int i = 0; i < 2; ++i) { + int copy_pad = *(paddings.begin() + 2 * i); + paddings.insert(paddings.begin() + 2 * i + 1, copy_pad); + } + // compute + for (int i = 0; i < 2; ++i) { + int out_size = (input_shape.d[2 + i] + strides[i] - 1) / strides[i]; + int pad_sum = std::max( + (out_size - 1) * strides[i] + ksize[i] - input_shape.d[2 + i], 0); + int pad_0 = pad_sum / 2; + int pad_1 = pad_sum - pad_0; + paddings[i * 2] = pad_0; + paddings[i * 2 + 1] = pad_1; + } + real_paddings = paddings; + // slice + for (int i = 0; i < 2; ++i) { + paddings.erase(paddings.begin() + i + 1); + } + } + // VALID + if (padding_algorithm == "VALID") { + std::fill(real_paddings.begin(), real_paddings.end(), 0); + } + + if (global_pooling == true && !engine_->with_dynamic_shape()) { + nv_ksize.d[0] = input_shape.d[input_dims - 2]; + nv_ksize.d[1] = input_shape.d[input_dims - 1]; + ksize[0] = input_shape.d[input_dims - 2]; + ksize[1] = input_shape.d[input_dims - 1]; + } + if (engine_->with_dynamic_shape()) { if (!adaptive && !global_pooling && !ceil_mode) { // input_shape.d < 0 means we can't get shape info here. @@ -173,15 +216,15 @@ class Pool2dOpConverter : public OpConverter { pool_layer->setPaddingMode(nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP); } layer = pool_layer; - } else if (global_pooling) { + } else if (global_pooling && !adaptive) { auto *reduce_layer = TRT_ENGINE_ADD_LAYER(engine_, Reduce, *input1, reduce_operation, 12, true); layer = reduce_layer; } else { #if IS_TRT_VERSION_GE(6000) - plugin::PoolPluginDynamic *plugin = - new plugin::PoolPluginDynamic(ceil_mode, pool_type, adaptive, ksize, - strides, paddings, global_pooling); + plugin::PoolPluginDynamic *plugin = new plugin::PoolPluginDynamic( + ceil_mode, pool_type, adaptive, exclusive, ksize, strides, paddings, + global_pooling); layer = engine_->AddDynamicPlugin(&input1, 1, plugin); #endif } @@ -195,21 +238,13 @@ class Pool2dOpConverter : public OpConverter { return; } - if (global_pooling == true) { - nv_ksize.d[0] = input_shape.d[input_dims - 2]; - nv_ksize.d[1] = input_shape.d[input_dims - 1]; + if (global_pooling == true && adaptive == false) { auto *pool_layer = TRT_ENGINE_ADD_LAYER(engine_, Pooling, *input1, nv_pool_type, nv_ksize); PADDLE_ENFORCE_NOT_NULL( pool_layer, platform::errors::Fatal( "trt pool layer in converter could not be created.")); auto output_name = op_desc.Output("Out")[0]; - pool_layer->setStride(nv_strides); - pool_layer->setPadding(nv_paddings); - if (padding_algorithm == "SAME") { - pool_layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); - } - pool_layer->setAverageCountExcludesPadding(exclusive); pool_layer->setName(("pool2d (Output: " + output_name + ")").c_str()); pool_layer->getOutput(0)->setName(output_name.c_str()); engine_->SetITensor(output_name, pool_layer->getOutput(0)); @@ -222,58 +257,61 @@ class Pool2dOpConverter : public OpConverter { if (!adaptive) { if (ceil_mode) { - nvinfer1::DimsHW pre_pad(0, 0); - nvinfer1::DimsHW post_pad(0, 0); - // If ceil mode is true, we will pad the appropriate size to the input. - DealCeilMode(input_shape, ksize, strides, paddings, &pre_pad, &post_pad, - input_dims); - auto *pad_layer = - TRT_ENGINE_ADD_LAYER(engine_, Padding, *input1, pre_pad, post_pad); - + std::vector input_shape_v; + for (int i = 0; i < input_dims; i++) { + input_shape_v.push_back(input_shape.d[i]); + } + plugin::PoolPlugin *plugin = new plugin::PoolPlugin( + ceil_mode, plugin_pool_type, adaptive, exclusive, ksize, strides, + paddings, input_shape_v, real_paddings); + auto *pool_layer = engine_->AddPlugin(&input1, 1, plugin); PADDLE_ENFORCE_NOT_NULL( - pad_layer, platform::errors::Fatal( - "Pad layer in poolOp converter could not be " - "created. The pointer to pad layer is `NULL`.")); - input1 = pad_layer->getOutput(0); - } + pool_layer, + platform::errors::Fatal( + "trt pool plugin layer in converter could not be created.")); + layer = pool_layer; + } else { #if IS_TRT_VERSION_GE(8000) - // Exclude padding pixels from the average mean is not supported well by - // TRT - // so enable padding for trt8.0 above. - if ((g_post_pad.w() > 0 || g_post_pad.h() > 0) && - (padding_algorithm != "SAME") && !ceil_mode) { - auto *pad_layer = TRT_ENGINE_ADD_LAYER(engine_, Padding, *input1, - g_pre_pad, g_post_pad); - PADDLE_ENFORCE_NOT_NULL( - pad_layer, platform::errors::Fatal( - "Pad layer in poolOp converter could not be " - "created. The pointer to pad layer is `NULL`.")); - input1 = pad_layer->getOutput(0); - } + // Exclude padding pixels from the average mean is not supported well by + // TRT + // so enable padding for trt8.0 above. + if ((g_post_pad.w() > 0 || g_post_pad.h() > 0) && + (padding_algorithm != "SAME") && !ceil_mode) { + auto *pad_layer = TRT_ENGINE_ADD_LAYER(engine_, Padding, *input1, + g_pre_pad, g_post_pad); + PADDLE_ENFORCE_NOT_NULL( + pad_layer, platform::errors::Fatal( + "Pad layer in poolOp converter could not be " + "created. The pointer to pad layer is `NULL`.")); + input1 = pad_layer->getOutput(0); + } #endif - auto *pool_layer = TRT_ENGINE_ADD_LAYER(engine_, Pooling, *input1, - nv_pool_type, nv_ksize); - PADDLE_ENFORCE_NOT_NULL( - pool_layer, platform::errors::Fatal( - "trt pool layer in converter could not be created.")); - pool_layer->setStride(nv_strides); - pool_layer->setPadding(nv_paddings); - if (padding_algorithm == "SAME") { - pool_layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); + auto *pool_layer = TRT_ENGINE_ADD_LAYER(engine_, Pooling, *input1, + nv_pool_type, nv_ksize); + PADDLE_ENFORCE_NOT_NULL( + pool_layer, + platform::errors::Fatal( + "trt pool layer in converter could not be created.")); + pool_layer->setStride(nv_strides); + pool_layer->setPadding(nv_paddings); + if (padding_algorithm == "SAME") { + pool_layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER); + } + pool_layer->setAverageCountExcludesPadding(exclusive); + layer = pool_layer; } - pool_layer->setAverageCountExcludesPadding(exclusive); - layer = pool_layer; + } else { // Average pooling needs to exclude the padding pixels from the average // mean. - // It is not supported well by TRT, we use a plugin here. + // It is not supported well by TRT, we use a plugin here std::vector input_shape_v; for (int i = 0; i < input_dims; i++) { input_shape_v.push_back(input_shape.d[i]); } - plugin::PoolPlugin *plugin = - new plugin::PoolPlugin(ceil_mode, plugin_pool_type, adaptive, ksize, - strides, paddings, input_shape_v); + plugin::PoolPlugin *plugin = new plugin::PoolPlugin( + ceil_mode, plugin_pool_type, adaptive, exclusive, ksize, strides, + paddings, input_shape_v, real_paddings); auto *pool_layer = engine_->AddPlugin(&input1, 1, plugin); PADDLE_ENFORCE_NOT_NULL( pool_layer, diff --git a/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.cu b/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.cu index 21c8812f37..6d711c26ad 100644 --- a/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.cu +++ b/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.cu @@ -35,6 +35,36 @@ nvinfer1::Dims PoolPlugin::getOutputDimensions(int index, return output_dims; } +size_t PoolPlugin::getSerializationSize() const TRT_NOEXCEPT { + return getBaseSerializationSize() + SerializedSize(ceil_mode_) + + SerializedSize(pool_type_) + SerializedSize(adaptive_) + + SerializedSize(exclusive_) + SerializedSize(ksize_) + + SerializedSize(strides_) + SerializedSize(paddings_) + + SerializedSize(real_paddings_) + SerializedSize(input_shape_) + + SerializedSize(output_shape_); +} + +// TRT will call this func when we need to serialize the configuration of +// tensorrt. +void PoolPlugin::serialize(void *buffer) const TRT_NOEXCEPT { + serializeBase(buffer); + SerializeValue(&buffer, ceil_mode_); + SerializeValue(&buffer, pool_type_); + SerializeValue(&buffer, adaptive_); + SerializeValue(&buffer, exclusive_); + SerializeValue(&buffer, ksize_); + SerializeValue(&buffer, strides_); + SerializeValue(&buffer, paddings_); + SerializeValue(&buffer, real_paddings_); + SerializeValue(&buffer, input_shape_); + SerializeValue(&buffer, output_shape_); +} + +PoolPlugin *PoolPlugin::clone() const TRT_NOEXCEPT { + return new PoolPlugin(ceil_mode_, pool_type_, adaptive_, exclusive_, ksize_, + strides_, paddings_, input_shape_, real_paddings_); +} + int PoolPlugin::enqueue(int batchSize, const void *const *inputs, #if IS_TRT_VERSION_LT(8000) void **outputs, void *workspace, @@ -59,14 +89,15 @@ int PoolPlugin::enqueue(int batchSize, const void *const *inputs, paddle::operators::math::MaxPool, float> pool2d_forward; pool2d_forward(idata, input_shape, output_shape, ksize_, strides_, - paddings_, true, adaptive_, odatas[0], stream, pool_process); + paddings_, true, false, odatas[0], stream, pool_process); } else if (pool_type_ == PoolType::avg) { paddle::operators::math::AvgPool pool_process; paddle::operators::math::Pool2dDirectCUDAFunctor< paddle::operators::math::AvgPool, float> pool2d_forward; pool2d_forward(idata, input_shape, output_shape, ksize_, strides_, - paddings_, true, adaptive_, odatas[0], stream, pool_process); + paddings_, exclusive_, adaptive_, odatas[0], stream, + pool_process); } return cudaGetLastError() != cudaSuccess; @@ -82,6 +113,7 @@ PoolPluginDynamic::PoolPluginDynamic(void const *serialData, DeserializeValue(&serialData, &serialLength, &pool_type); pool_type_ = std::string(pool_type); DeserializeValue(&serialData, &serialLength, &adaptive_); + DeserializeValue(&serialData, &serialLength, &exclusive_); DeserializeValue(&serialData, &serialLength, &ksize_); DeserializeValue(&serialData, &serialLength, &strides_); DeserializeValue(&serialData, &serialLength, &paddings_); @@ -90,21 +122,27 @@ PoolPluginDynamic::PoolPluginDynamic(void const *serialData, size_t PoolPluginDynamic::getSerializationSize() const TRT_NOEXCEPT { return SerializedSize(ceil_mode_) + SerializedSize(pool_type_.c_str()) + - SerializedSize(adaptive_) + SerializedSize(ksize_) + - SerializedSize(strides_) + SerializedSize(paddings_) + - SerializedSize(is_global_); + SerializedSize(adaptive_) + SerializedSize(exclusive_) + + SerializedSize(ksize_) + SerializedSize(strides_) + + SerializedSize(paddings_) + SerializedSize(is_global_); } void PoolPluginDynamic::serialize(void *buffer) const TRT_NOEXCEPT { SerializeValue(&buffer, ceil_mode_); SerializeValue(&buffer, pool_type_.c_str()); SerializeValue(&buffer, adaptive_); + SerializeValue(&buffer, exclusive_); SerializeValue(&buffer, ksize_); SerializeValue(&buffer, strides_); SerializeValue(&buffer, paddings_); SerializeValue(&buffer, is_global_); } +nvinfer1::IPluginV2DynamicExt *PoolPluginDynamic::clone() const TRT_NOEXCEPT { + return new PoolPluginDynamic(ceil_mode_, pool_type_, adaptive_, exclusive_, + ksize_, strides_, paddings_, is_global_); +} + nvinfer1::DimsExprs PoolPluginDynamic::getOutputDimensions( int output_index, const nvinfer1::DimsExprs *inputs, int nb_inputs, nvinfer1::IExprBuilder &expr_builder) TRT_NOEXCEPT { @@ -117,11 +155,14 @@ nvinfer1::DimsExprs PoolPluginDynamic::getOutputDimensions( platform::errors::InvalidArgument("The channel dimension should be " "static, but we found it's dynamic.")); nvinfer1::DimsExprs output(inputs[0]); - if (is_global_) { + if (is_global_ && !adaptive_) { output.d[2] = expr_builder.constant(1); output.d[3] = expr_builder.constant(1); return output; } + if (is_global_ && adaptive_) { + return inputs[0]; + } if (adaptive_) { output.d[2] = expr_builder.constant(ksize_[0]); output.d[3] = expr_builder.constant(ksize_[1]); @@ -245,6 +286,10 @@ int PoolPluginDynamic::enqueue(const nvinfer1::PluginTensorDesc *input_desc, output_shape[2] = data_dim[0]; output_shape[3] = data_dim[1]; } + if (adaptive_) { + output_shape[2] = h; + output_shape[3] = w; + } if (pool_type_ == "max") { paddle::operators::math::MaxPool pool_process; @@ -252,14 +297,14 @@ int PoolPluginDynamic::enqueue(const nvinfer1::PluginTensorDesc *input_desc, paddle::operators::math::MaxPool, float> pool2d_forward; pool2d_forward(input, input_shape, output_shape, ksize, strides_, paddings, - true, adaptive_, output, stream, pool_process); + true, false, output, stream, pool_process); } else if (pool_type_ == "avg") { paddle::operators::math::AvgPool pool_process; paddle::operators::math::Pool2dDirectCUDAFunctor< paddle::operators::math::AvgPool, float> pool2d_forward; pool2d_forward(input, input_shape, output_shape, ksize, strides_, paddings, - true, adaptive_, output, stream, pool_process); + exclusive_, adaptive_, output, stream, pool_process); } return cudaGetLastError() != cudaSuccess; diff --git a/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.h b/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.h index 6ced066a35..d1bf2cd02e 100644 --- a/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.h +++ b/paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.h @@ -29,26 +29,32 @@ static std::vector CalcOutputSize(const std::vector& input_shape, const bool& adaptive, const std::vector& ksize, const std::vector& strides, - const std::vector& paddings) { + const std::vector& real_paddings) { std::vector output_shape = input_shape; if (adaptive) { output_shape[0] = ksize[0]; output_shape[1] = ksize[1]; } else { - int output_h, output_w; - if (!ceil_mode) { - output_h = (input_shape[0] - ksize[0] + 2 * paddings[0]) / strides[0] + 1; - output_w = (input_shape[1] - ksize[1] + 2 * paddings[1]) / strides[1] + 1; - } else { - output_h = - (input_shape[0] - ksize[0] + 2 * paddings[0] + strides[0] - 1) / - strides[0] + - 1; - output_w = - (input_shape[1] - ksize[1] + 2 * paddings[1] + strides[1] - 1) / - strides[1] + - 1; + int output_h = 0, output_w = 0; + if (ceil_mode) { + output_h = (input_shape[0] - ksize[0] + real_paddings[0] + + real_paddings[1] + strides[0] - 1) / + strides[0] + + 1; + output_w = (input_shape[1] - ksize[1] + real_paddings[2] + + real_paddings[3] + strides[1] - 1) / + strides[1] + + 1; } + // TRT will use native layer when ceil_model=false + /* + else{ + output_h = (input_shape[0] - ksize[0] + real_paddings[0] + + real_paddings[1]) / strides[0] + 1; + output_w = (input_shape[1] - ksize[1] + real_paddings[2] + + real_paddings[3]) / strides[1] + 1; + } + */ output_shape[0] = output_h; output_shape[1] = output_w; } @@ -57,47 +63,32 @@ static std::vector CalcOutputSize(const std::vector& input_shape, class PoolPlugin : public PluginTensorRT { public: - size_t getSerializationSize() const TRT_NOEXCEPT override { - return getBaseSerializationSize() + SerializedSize(ceil_mode_) + - SerializedSize(pool_type_) + SerializedSize(adaptive_) + - SerializedSize(ksize_) + SerializedSize(strides_) + - SerializedSize(paddings_) + SerializedSize(input_shape_) + - SerializedSize(output_shape_); - } + size_t getSerializationSize() const TRT_NOEXCEPT override; - // TRT will call this func when we need to serialize the configuration of - // tensorrt. - void serialize(void* buffer) const TRT_NOEXCEPT override { - serializeBase(buffer); - SerializeValue(&buffer, ceil_mode_); - SerializeValue(&buffer, pool_type_); - SerializeValue(&buffer, adaptive_); - SerializeValue(&buffer, ksize_); - SerializeValue(&buffer, strides_); - SerializeValue(&buffer, paddings_); - SerializeValue(&buffer, input_shape_); - SerializeValue(&buffer, output_shape_); - } + void serialize(void* buffer) const TRT_NOEXCEPT override; enum class PoolType { max = 0, avg, }; PoolPlugin() {} - PoolPlugin(bool ceil_mode, PoolType pool_type, bool adaptive, + PoolPlugin(bool ceil_mode, PoolType pool_type, bool adaptive, bool exclusive, std::vector ksize, std::vector strides, - std::vector paddings, std::vector input_shape) + std::vector paddings, std::vector input_shape, + std::vector real_paddings) : ceil_mode_(ceil_mode), pool_type_(pool_type), adaptive_(adaptive), + exclusive_(exclusive), ksize_(ksize), strides_(strides), paddings_(paddings), + real_paddings_(real_paddings), input_shape_(input_shape) { output_shape_ = input_shape_; std::vector output_shape = CalcOutputSize({input_shape_[1], input_shape_[2]}, ceil_mode_, - adaptive_, ksize_, strides_, paddings_); + adaptive_, ksize_, strides_, real_paddings_); output_shape_[1] = output_shape[0]; output_shape_[2] = output_shape[1]; } @@ -109,17 +100,16 @@ class PoolPlugin : public PluginTensorRT { DeserializeValue(&serialData, &serialLength, &ceil_mode_); DeserializeValue(&serialData, &serialLength, &pool_type_); DeserializeValue(&serialData, &serialLength, &adaptive_); + DeserializeValue(&serialData, &serialLength, &exclusive_); DeserializeValue(&serialData, &serialLength, &ksize_); DeserializeValue(&serialData, &serialLength, &strides_); DeserializeValue(&serialData, &serialLength, &paddings_); + DeserializeValue(&serialData, &serialLength, &real_paddings_); DeserializeValue(&serialData, &serialLength, &input_shape_); DeserializeValue(&serialData, &serialLength, &output_shape_); } - PoolPlugin* clone() const TRT_NOEXCEPT override { - return new PoolPlugin(ceil_mode_, pool_type_, adaptive_, ksize_, strides_, - paddings_, input_shape_); - } + PoolPlugin* clone() const TRT_NOEXCEPT override; const char* getPluginType() const TRT_NOEXCEPT override { return "pool_plugin"; @@ -139,9 +129,11 @@ class PoolPlugin : public PluginTensorRT { bool ceil_mode_; PoolType pool_type_; bool adaptive_; + bool exclusive_; std::vector ksize_; std::vector strides_; std::vector paddings_; + std::vector real_paddings_; std::vector input_shape_; std::vector output_shape_; }; @@ -167,12 +159,14 @@ class PoolPluginDynamic : public DynamicPluginTensorRT { public: PoolPluginDynamic() {} PoolPluginDynamic(const bool& ceil_mode, const std::string& pool_type, - const bool& adaptive, const std::vector& ksize, + const bool& adaptive, bool exclusive, + const std::vector& ksize, const std::vector& strides, const std::vector& paddings, const bool& is_global) : ceil_mode_(ceil_mode), pool_type_(pool_type), adaptive_(adaptive), + exclusive_(exclusive), ksize_(ksize), strides_(strides), paddings_(paddings), @@ -180,10 +174,7 @@ class PoolPluginDynamic : public DynamicPluginTensorRT { PoolPluginDynamic(void const* serialData, size_t serialLength); ~PoolPluginDynamic() {} - nvinfer1::IPluginV2DynamicExt* clone() const TRT_NOEXCEPT override { - return new PoolPluginDynamic(ceil_mode_, pool_type_, adaptive_, ksize_, - strides_, paddings_, is_global_); - } + nvinfer1::IPluginV2DynamicExt* clone() const TRT_NOEXCEPT override; const char* getPluginType() const TRT_NOEXCEPT override { return "pool_plugin_dynamic"; @@ -229,6 +220,7 @@ class PoolPluginDynamic : public DynamicPluginTensorRT { bool ceil_mode_; std::string pool_type_; bool adaptive_; + bool exclusive_; std::vector ksize_; std::vector strides_; std::vector paddings_; diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_pool2d.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_pool2d.py index ddb96c37db..89ce1145d7 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_pool2d.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_pool2d.py @@ -52,7 +52,7 @@ class TrtConvertPool2dTest(TrtLayerAutoScanTest): return np.random.random([24, 3, 3, 3]).astype(np.float32) for strides in [[1, 1], [1, 2], [2, 2]]: - for paddings in [[0, 2], [0, 3], [0, 1, 2, 3]]: + for paddings in [[0, 2], [0, 3]]: for pooling_type in ['max', 'avg']: for padding_algotithm in ['EXPLICIT', 'SAME', 'VAILD']: for ksize in [[2, 3], [3, 3]]: @@ -145,44 +145,18 @@ class TrtConvertPool2dTest(TrtLayerAutoScanTest): True), 1e-5 def add_skip_trt_case(self): - def teller1(program_config, predictor_config): - if len(program_config.ops[0].attrs['paddings']) == 4: - return True - return False - - self.add_skip_case(teller1, SkipReasons.TRT_NOT_IMPLEMENTED, - "4-dims paddings are not support for trt now.") - - def teller2(program_config, predictor_config): - if program_config.ops[0].attrs['global_pooling'] == True: - return True - return False - - self.add_skip_case( - teller2, SkipReasons.TRT_NOT_IMPLEMENTED, - "It is not support that global_pooling is true for trt now.") - - def teller3(program_config, predictor_config): - if self.dynamic_shape.min_input_shape == {} and program_config.ops[ - 0].attrs['ceil_mode'] == True: - return True - return False - - self.add_skip_case( - teller3, SkipReasons.TRT_NOT_IMPLEMENTED, - "It is not support that ceil_mode is true in static mode for trt now." - ) - - def teller4(program_config, predictor_config): - if self.dynamic_shape.min_input_shape != {} and ( - program_config.ops[0].attrs['strides'] == [1, 2] or - program_config.ops[0].attrs['strides'] == [2, 2]): + def teller(program_config, predictor_config): + if program_config.ops[0].attrs['pooling_type'] == 'avg' and \ + program_config.ops[0].attrs['global_pooling'] == False and \ + program_config.ops[0].attrs['exclusive'] == True and \ + program_config.ops[0].attrs['adaptive'] == False and \ + program_config.ops[0].attrs['ceil_mode'] == True: return True return False self.add_skip_case( - teller4, SkipReasons.TRT_NOT_IMPLEMENTED, - "It is not support that strides is not equal [1, 1] in dynamic mode for trt now." + teller, SkipReasons.TRT_NOT_IMPLEMENTED, + "The results of some cases are Nan, but the results of TensorRT and GPU are the same." ) def test(self): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_pool_op.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_pool_op.py index 26ad45db7a..d71937f986 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_pool_op.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_pool_op.py @@ -119,6 +119,17 @@ class TensorRTAvgPoolTest(TensorRTPoolTest): self.exclusive = False +class TensorRTAvgCeilPoolTest(TensorRTPoolTest): + def set_extra_config(self): + self.pool_size = 2 + self.pool_type = 'avg' + self.pool_stride = 1 + self.pool_padding = 0 + self.global_pooling = False + self.ceil_mode = True + self.exclusive = False + + class TensorRTGlobalPoolTest(TensorRTPoolTest): def set_extra_config(self): self.pool_size = 2 -- GitLab