test_trt_pool3d_op.py 11.9 KB
Newer Older
F
feng_shuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15
import itertools
F
feng_shuai 已提交
16 17 18
import os
import shutil
import unittest
19

F
feng_shuai 已提交
20 21
import numpy as np
from inference_pass_test import InferencePassTest
22

F
feng_shuai 已提交
23
import paddle
24 25
from paddle import fluid
from paddle.fluid import core
26
from paddle.fluid.core import AnalysisConfig, PassVersionChecker
F
feng_shuai 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45


class TensorRTPool3dTest(InferencePassTest):
    def setUp(self):
        self.bs = 1
        self.channel = 3
        self.depth = 8
        self.height = 8
        self.width = 8
        self.pool_size = 2
        self.pool_type = 'max'
        self.pool_stride = 1
        self.pool_padding = 0
        self.ceil_mode = False
        self.exclusive = False
        self.enable_trt = True
        self.serialize = False
        self.precision = AnalysisConfig.Precision.Float32
        self.feeds = {
46 47 48
            'data': np.random.random(
                [self.bs, self.channel, self.depth, self.height, self.width]
            ).astype('float32'),
F
feng_shuai 已提交
49 50 51 52 53 54 55 56
        }

    def set_extra_config(self):
        pass

    def build_network(self):
        self.set_extra_config()
        self.trt_parameters = TensorRTPool3dTest.TensorRTParam(
57 58
            1 << 30, self.bs, 0, self.precision, self.serialize, False
        )
F
feng_shuai 已提交
59 60

        with fluid.program_guard(self.main_program, self.startup_program):
61
            data = paddle.static.data(
F
feng_shuai 已提交
62 63
                name='data',
                shape=[-1, self.channel, self.depth, self.height, self.width],
64 65
                dtype='float32',
            )
C
ccrrong 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            if self.pool_type == "max":
                pool_out = paddle.nn.functional.max_pool3d(
                    x=data,
                    kernel_size=self.pool_size,
                    stride=self.pool_stride,
                    padding=self.pool_padding,
                    ceil_mode=self.ceil_mode,
                )
            else:
                pool_out = paddle.nn.functional.avg_pool3d(
                    x=data,
                    kernel_size=self.pool_size,
                    stride=self.pool_stride,
                    padding=self.pool_padding,
                    ceil_mode=self.ceil_mode,
                    exclusive=self.exclusive,
                )
83
            # out = paddle.static.nn.batch_norm(pool_out, is_test=True)
F
feng_shuai 已提交
84 85 86 87 88 89 90
            self.fetch_list = [pool_out]

    def check_output(self):
        if os.path.exists(self.path + "_opt_cache"):
            shutil.rmtree(self.path + "_opt_cache")
        if core.is_compiled_with_cuda():
            use_gpu = True
L
Leo Chen 已提交
91 92 93 94 95
            if self.precision == AnalysisConfig.Precision.Float32:
                atol, rtol = (1e-5, 1e-5)
            elif self.precision == AnalysisConfig.Precision.Half:
                atol, rtol = (1e-3, 1e-3)
            else:
96
                raise ValueError(f"Unsupported precision {self.precision}")
L
Leo Chen 已提交
97
            self.check_output_with_option(use_gpu, atol=atol, rtol=rtol)
F
feng_shuai 已提交
98
            self.assertTrue(
99 100
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
F
feng_shuai 已提交
101 102 103 104 105 106 107

    def run_test(self):
        self.build_network()
        self.check_output()

    def test(self):
        precision_options = [
108 109
            AnalysisConfig.Precision.Float32,
            AnalysisConfig.Precision.Half,
F
feng_shuai 已提交
110 111
        ]
        serialize_options = [False, True]
112 113 114
        dynamic_shape_profile = InferencePassTest.DynamicShapeParam(
            {
                'data': [
115 116 117 118 119
                    self.bs,
                    self.channel,
                    self.depth // 2,
                    self.height // 2,
                    self.width // 2,
120
                ]
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            },
            {
                'data': [
                    self.bs,
                    self.channel,
                    self.depth,
                    self.height,
                    self.width,
                ]
            },
            {
                'data': [
                    self.bs,
                    self.channel,
                    self.depth,
                    self.height,
                    self.width,
                ]
            },
            False,
        )
F
feng_shuai 已提交
142 143 144
        dynamic_shape_options = [None, dynamic_shape_profile]

        for precision, serialize, dynamic_shape in itertools.product(
145 146
            precision_options, serialize_options, dynamic_shape_options
        ):
F
feng_shuai 已提交
147
            is_dynamic = True if dynamic_shape_options is not None else False
148
            with self.subTest(
149 150 151 152
                'Precision: {}, Serialize: {}, Dynamic: {}'.format(
                    precision, serialize, is_dynamic
                )
            ):
F
feng_shuai 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
                self.precision = precision
                self.serialize = serialize
                self.dynamic_shape_params = dynamic_shape
                self.run_test()


class TensorRTAvgPool3dTest(TensorRTPool3dTest):
    def set_extra_config(self):
        self.pool_size = 2
        self.pool_type = 'avg'
        self.pool_stride = 1
        self.pool_padding = 0
        self.ceil_mode = False
        self.exclusive = False


class TensorRTAdaptiveAvgPool3DTest(InferencePassTest):
    def setUp(self):
        self.bs = 1
        self.channel = 3
        self.depth = 8
        self.height = 8
        self.width = 8
        self.enable_trt = True
        self.serialize = False
        self.precision = AnalysisConfig.Precision.Float32
        self.feeds = {
180 181 182
            'data': np.random.random(
                [self.bs, self.channel, self.depth, self.height, self.width]
            ).astype('float32'),
F
feng_shuai 已提交
183 184 185 186
        }

    def build_network(self):
        self.trt_parameters = TensorRTPool3dTest.TensorRTParam(
187 188
            1 << 30, self.bs, 0, self.precision, self.serialize, False
        )
F
feng_shuai 已提交
189 190

        with fluid.program_guard(self.main_program, self.startup_program):
191
            data = paddle.static.data(
F
feng_shuai 已提交
192 193
                name='data',
                shape=[-1, self.channel, self.depth, self.height, self.width],
194 195
                dtype='float32',
            )
F
feng_shuai 已提交
196
            pool_out = paddle.nn.functional.adaptive_avg_pool3d(
197 198
                x=data, output_size=[3, 3, 3]
            )
199
            # out = paddle.static.nn.batch_norm(pool_out, is_test=True)
F
feng_shuai 已提交
200 201 202 203 204 205 206 207 208
            self.fetch_list = [pool_out]

    def check_output(self):
        if os.path.exists(self.path + "_opt_cache"):
            shutil.rmtree(self.path + "_opt_cache")
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu)
            self.assertTrue(
209 210
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
F
feng_shuai 已提交
211 212 213 214 215 216 217

    def run_test(self):
        self.build_network()
        self.check_output()

    def test(self):
        precision_options = [
218 219
            AnalysisConfig.Precision.Float32,
            AnalysisConfig.Precision.Half,
F
feng_shuai 已提交
220 221
        ]
        serialize_options = [False, True]
222 223 224
        dynamic_shape_profile = InferencePassTest.DynamicShapeParam(
            {
                'data': [
225 226 227 228 229
                    self.bs,
                    self.channel,
                    self.depth // 2,
                    self.height // 2,
                    self.width // 2,
230
                ]
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
            },
            {
                'data': [
                    self.bs,
                    self.channel,
                    self.depth,
                    self.height,
                    self.width,
                ]
            },
            {
                'data': [
                    self.bs,
                    self.channel,
                    self.depth,
                    self.height,
                    self.width,
                ]
            },
            False,
        )
F
feng_shuai 已提交
252 253 254
        dynamic_shape_options = [None, dynamic_shape_profile]

        for precision, serialize, dynamic_shape in itertools.product(
255 256
            precision_options, serialize_options, dynamic_shape_options
        ):
F
feng_shuai 已提交
257
            is_dynamic = True if dynamic_shape_options is not None else False
258
            with self.subTest(
259 260 261 262
                'Precision: {}, Serialize: {}, Dynamic: {}'.format(
                    precision, serialize, is_dynamic
                )
            ):
F
feng_shuai 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
                self.precision = precision
                self.serialize = serialize
                self.dynamic_shape_params = dynamic_shape
                self.run_test()


class TensorRTAdaptiveMaxPool3DTest(InferencePassTest):
    def setUp(self):
        self.bs = 1
        self.channel = 3
        self.depth = 8
        self.height = 8
        self.width = 8
        self.enable_trt = True
        self.serialize = False
        self.precision = AnalysisConfig.Precision.Float32
        self.feeds = {
280 281 282
            'data': np.random.random(
                [self.bs, self.channel, self.depth, self.height, self.width]
            ).astype('float32'),
F
feng_shuai 已提交
283 284 285 286
        }

    def build_network(self):
        self.trt_parameters = TensorRTPool3dTest.TensorRTParam(
287 288
            1 << 30, self.bs, 0, self.precision, self.serialize, False
        )
F
feng_shuai 已提交
289 290

        with fluid.program_guard(self.main_program, self.startup_program):
291
            data = paddle.static.data(
F
feng_shuai 已提交
292 293
                name='data',
                shape=[-1, self.channel, self.depth, self.height, self.width],
294 295
                dtype='float32',
            )
F
feng_shuai 已提交
296
            pool_out = paddle.nn.functional.adaptive_max_pool3d(
297 298
                x=data, output_size=[3, 3, 3]
            )
299
            # out = paddle.static.nn.batch_norm(pool_out, is_test=True)
F
feng_shuai 已提交
300 301 302 303 304 305 306 307 308
            self.fetch_list = [pool_out]

    def check_output(self):
        if os.path.exists(self.path + "_opt_cache"):
            shutil.rmtree(self.path + "_opt_cache")
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu)
            self.assertTrue(
309 310
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
F
feng_shuai 已提交
311 312 313 314 315 316 317

    def run_test(self):
        self.build_network()
        self.check_output()

    def test(self):
        precision_options = [
318 319
            AnalysisConfig.Precision.Float32,
            AnalysisConfig.Precision.Half,
F
feng_shuai 已提交
320 321
        ]
        serialize_options = [False, True]
322 323 324
        dynamic_shape_profile = InferencePassTest.DynamicShapeParam(
            {
                'data': [
325 326 327 328 329
                    self.bs,
                    self.channel,
                    self.depth // 2,
                    self.height // 2,
                    self.width // 2,
330
                ]
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
            },
            {
                'data': [
                    self.bs,
                    self.channel,
                    self.depth,
                    self.height,
                    self.width,
                ]
            },
            {
                'data': [
                    self.bs,
                    self.channel,
                    self.depth,
                    self.height,
                    self.width,
                ]
            },
            False,
        )
F
feng_shuai 已提交
352 353 354
        dynamic_shape_options = [None, dynamic_shape_profile]

        for precision, serialize, dynamic_shape in itertools.product(
355 356
            precision_options, serialize_options, dynamic_shape_options
        ):
F
feng_shuai 已提交
357
            is_dynamic = True if dynamic_shape_options is not None else False
358
            with self.subTest(
359 360 361 362
                'Precision: {}, Serialize: {}, Dynamic: {}'.format(
                    precision, serialize, is_dynamic
                )
            ):
F
feng_shuai 已提交
363 364 365 366 367 368 369 370
                self.precision = precision
                self.serialize = serialize
                self.dynamic_shape_params = dynamic_shape
                self.run_test()


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