test_trt_conv3d_transpose_op.py 5.0 KB
Newer Older
W
wenbin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import numpy as np
from inference_pass_test import InferencePassTest
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.core import PassVersionChecker
from paddle.fluid.core import AnalysisConfig


class TensorRTSubgraphPassConv3dTransposeTest(InferencePassTest):
25

W
wenbin 已提交
26 27 28
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
29 30 31
            data = fluid.data(name="data",
                              shape=[-1, 4, 4, 32, 32],
                              dtype="float32")
W
wenbin 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
            conv_out = fluid.layers.conv3d_transpose(
                input=data,
                num_filters=self.conv_num_filters,
                filter_size=self.conv_filter_size,
                groups=self.conv_groups,
                padding=self.conv_padding,
                bias_attr=False,
                use_cudnn=self.use_cudnn,
                stride=1,
                act=None)
        self.feeds = {
            "data": np.random.random([1, 4, 4, 32, 32]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TensorRTSubgraphPassConv3dTransposeTest.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False)
        self.fetch_list = [conv_out]

    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 1
        self.conv_padding = [1, 1, 1]
        self.use_cudnn = True

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TensorRTSubgraphPassConv3dTransposeSamePaddingTest(
        TensorRTSubgraphPassConv3dTransposeTest):
67

W
wenbin 已提交
68 69 70 71 72 73 74 75 76 77
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 1
        self.conv_padding = 'VALID'
        self.use_cudnn = True


class TensorRTSubgraphPassConv3dTransposeMultigroupTest(
        TensorRTSubgraphPassConv3dTransposeTest):
78

W
wenbin 已提交
79 80 81 82 83 84 85 86 87
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 2
        self.conv_padding = 'VALID'
        self.use_cudnn = True


class DynamicShapeTensorRTSubgraphPassConv3dTransposeTest(InferencePassTest):
88

W
wenbin 已提交
89 90 91
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
92 93 94
            data = fluid.data(name="data",
                              shape=[-1, 6, -1, -1, -1],
                              dtype="float32")
W
wenbin 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            conv_out = fluid.layers.conv3d_transpose(
                input=data,
                num_filters=self.conv_num_filters,
                filter_size=self.conv_filter_size,
                groups=self.conv_groups,
                padding=self.conv_padding,
                bias_attr=False,
                use_cudnn=self.use_cudnn,
                stride=self.stride,
                act=None)
        self.feeds = {
            "data": np.random.random([1, 6, 32, 32, 8]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = DynamicShapeTensorRTSubgraphPassConv3dTransposeTest.TensorRTParam(
            1 << 30, 32, 0, AnalysisConfig.Precision.Float32, False, False)
        self.dynamic_shape_params = DynamicShapeTensorRTSubgraphPassConv3dTransposeTest.DynamicShapeParam(
            {
                "data": [1, 6, 8, 8, 8],
                "conv3d_transpose_0.tmp_0": [1, 6, 8, 8, 1],
            }, {
                "data": [32, 6, 32, 32, 8],
                "conv3d_transpose_0.tmp_0": [32, 6, 64, 64, 16],
            }, {
                "data": [16, 6, 16, 16, 8],
                "conv3d_transpose_0.tmp_0": [16, 6, 16, 16, 8],
            }, False)
        self.fetch_list = [conv_out]

    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 6
        self.conv_padding = 'SAME'
        self.use_cudnn = True
        self.stride = [2, 2, 2]

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


if __name__ == "__main__":
    unittest.main()