test_trt_conv_pass.py 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Copyright (c) 2020 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 os
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

C
carryyu 已提交
24 25
os.environ['NVIDIA_TF32_OVERRIDE'] = '0'

26 27

class TensorRTSubgraphPassConvTest(InferencePassTest):
28

29 30 31
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
32 33 34 35 36 37 38 39 40 41 42
            data = fluid.data(name="data",
                              shape=[-1, 6, 64, 64],
                              dtype="float32")
            conv_out = fluid.layers.conv2d(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,
                                           act=None)
43 44 45 46 47 48 49 50 51 52 53 54 55
        self.feeds = {
            "data": np.random.random([1, 6, 64, 64]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TensorRTSubgraphPassConvTest.TensorRTParam(
            1 << 30, 32, 0, 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 = 3
        self.conv_padding = [1, 1]
56
        self.use_cudnn = True
57 58 59 60 61 62 63 64 65 66

    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 TensorRTSubgraphPassConvValidPaddingTest(TensorRTSubgraphPassConvTest):
67

68 69 70 71 72
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 3
        self.conv_padding = 'VALID'
73
        self.use_cudnn = True
74 75 76


class TensorRTSubgraphPassConvSamePaddingTest(InferencePassTest):
77

78 79 80 81 82
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 3
        self.conv_padding = 'SAME'
83
        self.use_cudnn = True
84 85 86


class TensorRTSubgraphPassDepthwiseConvTest(TensorRTSubgraphPassConvTest):
87

88 89 90 91 92
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 6
        self.conv_padding = [1, 1]
93 94 95 96
        self.use_cudnn = False


class TensorRTSubgraphPassDepthwiseConv2Test(TensorRTSubgraphPassConvTest):
97

98 99 100 101 102 103
    def set_params(self):
        self.conv_num_filters = 12
        self.conv_filter_size = 6
        self.conv_groups = 6
        self.conv_padding = [1, 1]
        self.use_cudnn = False
104 105 106


class TensorRTSubgraphPassConvTransposeTest(InferencePassTest):
107

108 109 110
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
111 112 113
            data = fluid.data(name="data",
                              shape=[-1, 6, 64, 64],
                              dtype="float32")
114 115 116 117 118 119 120
            conv_out = fluid.layers.conv2d_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,
121
                use_cudnn=self.use_cudnn,
122 123 124 125 126 127 128 129 130 131 132 133 134 135
                act=None)
        self.feeds = {
            "data": np.random.random([1, 6, 64, 64]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TensorRTSubgraphPassConvTransposeTest.TensorRTParam(
            1 << 30, 32, 0, 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]
136
        self.use_cudnn = True
137 138 139 140 141 142 143 144 145 146 147

    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 TensorRTSubgraphPassConvTransposeValidPaddingTest(
        TensorRTSubgraphPassConvTransposeTest):
148

149 150 151 152 153
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 1
        self.conv_padding = 'VALID'
154
        self.use_cudnn = True
155 156


157 158
class TensorRTSubgraphPassConvTransposeSamePaddingTest(
        TensorRTSubgraphPassConvTransposeTest):
159

160 161 162 163 164
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
        self.conv_groups = 1
        self.conv_padding = 'SAME'
165
        self.use_cudnn = True
166 167


168
class TensorRTSubgraphPassConvTransposeMultiGroupTest(
169
        TensorRTSubgraphPassConvTransposeTest):
170

171 172 173
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 6
174 175 176 177 178
        self.conv_groups = 2
        self.conv_padding = [1, 1]
        self.use_cudnn = True


179 180
class TensorRTSubgraphPassConvTranspose2Test(
        TensorRTSubgraphPassConvTransposeTest):
181

182 183 184 185 186 187 188 189
    def set_params(self):
        self.conv_num_filters = 12
        self.conv_filter_size = 4
        self.conv_groups = 6
        self.conv_padding = [1, 1]
        self.use_cudnn = False


190 191
class TensorRTSubgraphPassDepthwiseConvTransposeTest(
        TensorRTSubgraphPassConvTransposeTest):
192

193 194 195 196
    def set_params(self):
        self.conv_num_filters = 6
        self.conv_filter_size = 4
        self.conv_groups = 6
197
        self.conv_padding = [1, 1]
198
        self.use_cudnn = False
199 200


W
wenbin 已提交
201
class DynamicShapeTensorRTSubgraphPassConvTest(InferencePassTest):
202

W
wenbin 已提交
203 204 205
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
206 207 208 209 210 211 212 213 214 215 216 217
            data = fluid.data(name="data",
                              shape=[-1, 6, -1, -1],
                              dtype="float32")
            conv_out = fluid.layers.conv2d(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)
W
wenbin 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        self.feeds = {
            "data": np.random.random([32, 6, 64, 64]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = DynamicShapeTensorRTSubgraphPassConvTest.TensorRTParam(
            1 << 30, 32, 0, AnalysisConfig.Precision.Float32, False, False)
        self.dynamic_shape_params = DynamicShapeTensorRTSubgraphPassConvTest.DynamicShapeParam(
            {
                "conv2d_0.tmp_0": [1, 6, 8, 8],
                "data": [1, 6, 8, 8],
                "depthwise_conv2d_0.tmp_0": [1, 6, 8, 8]
            }, {
                "conv2d_0.tmp_0": [32, 6, 64, 64],
                "data": [32, 6, 64, 64],
                "depthwise_conv2d_0.tmp_0": [32, 6, 64, 64]
            }, {
                "conv2d_0.tmp_0": [16, 6, 16, 16],
                "data": [16, 6, 16, 16],
W
wenbin 已提交
236
                "depthwise_conv2d_0.tmp_0": [16, 6, 16, 16]
W
wenbin 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            }, 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]

    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 DynamicShapeTensorRTSubgraphPassDepthwiseConvTransposeTest(
        DynamicShapeTensorRTSubgraphPassConvTest):
258

W
wenbin 已提交
259 260 261 262 263 264 265 266 267
    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 = False
        self.stride = [2, 2]


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