test_trt_convert_tile.py 9.8 KB
Newer Older
已提交
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
已提交
3 4 5
# 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
6
#
已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
已提交
9 10 11 12 13 14
# 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 unittest
已提交
16
from functools import partial
17
from typing import Any, Dict, List
已提交
18

W
Wilber 已提交
19
import hypothesis.strategies as st
20 21 22 23 24 25
import numpy as np
from hypothesis import given
from program_config import ProgramConfig, TensorConfig
from trt_layer_auto_scan_test import TrtLayerAutoScanTest

import paddle.inference as paddle_infer
W
Wilber 已提交
26

已提交
27 28 29 30 31

class TrtConvertTileTest(TrtLayerAutoScanTest):
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
        inputs = program_config.inputs
        attrs = [
32
            program_config.ops[i].attrs for i in range(len(program_config.ops))
已提交
33 34 35 36 37 38 39
        ]
        for x in attrs[0]['repeat_times']:
            if x <= 0:
                return False

        return True

W
Wilber 已提交
40
    def sample_program_configs(self, *args, **kwargs):
已提交
41 42 43
        def generate_input1(attrs: List[Dict[str, Any]]):
            return np.ones([1, 2, 3, 4]).astype(np.float32)

W
Wilber 已提交
44 45
        dics = [{"repeat_times": kwargs['repeat_times']}]

46 47 48 49 50 51 52 53
        ops_config = [
            {
                "op_type": "tile",
                "op_inputs": {"X": ["input_data"]},
                "op_outputs": {"Out": ["tile_output_data"]},
                "op_attrs": dics[0],
            }
        ]
W
Wilber 已提交
54 55 56 57 58 59
        ops = self.generate_op_config(ops_config)

        program_config = ProgramConfig(
            ops=ops,
            weights={},
            inputs={
60 61 62
                "input_data": TensorConfig(
                    data_gen=partial(generate_input1, dics)
                )
W
Wilber 已提交
63
            },
64 65
            outputs=["tile_output_data"],
        )
W
Wilber 已提交
66 67

        yield program_config
已提交
68 69

    def sample_predictor_configs(
70 71
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
已提交
72
        def generate_dynamic_shape(attrs):
73
            self.dynamic_shape.min_input_shape = {"input_data": [1, 2, 3, 4]}
已提交
74 75 76 77 78 79 80 81 82
            self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]}
            self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]}

        def clear_dynamic_shape():
            self.dynamic_shape.min_input_shape = {}
            self.dynamic_shape.max_input_shape = {}
            self.dynamic_shape.opt_input_shape = {}

        def generate_trt_nodes_num(attrs, dynamic_shape):
W
Wilber 已提交
83 84
            ver = paddle_infer.get_trt_compile_version()
            if ver[0] * 1000 + ver[1] * 100 + ver[0] * 10 >= 7000:
85
                return 1, 2
已提交
86
            else:
W
Wilber 已提交
87
                return 0, 3
已提交
88 89

        attrs = [
90
            program_config.ops[i].attrs for i in range(len(program_config.ops))
已提交
91 92 93 94 95 96
        ]

        # for static_shape
        clear_dynamic_shape()
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
97 98
            attrs, False
        ), 1e-5
已提交
99 100
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
101 102
            attrs, False
        ), 1e-3
已提交
103 104 105 106

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
107
        yield self.create_inference_config(), generate_trt_nodes_num(
108 109
            attrs, True
        ), 1e-5
已提交
110
        self.trt_param.precision = paddle_infer.PrecisionType.Half
111
        yield self.create_inference_config(), generate_trt_nodes_num(
112 113
            attrs, True
        ), 1e-3
已提交
114

W
Wilber 已提交
115 116 117
    @given(repeat_times=st.sampled_from([[100], [1, 2], [0, 3], [1, 2, 100]]))
    def test(self, *args, **kwargs):
        self.run_test(*args, **kwargs)
已提交
118 119


120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
class TrtConvertTileTest2(TrtLayerAutoScanTest):
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
        return True

    def sample_program_configs(self):
        def generate_input1(attrs: List[Dict[str, Any]]):
            return np.ones([1, 2, 3, 4]).astype(np.float32)

        dics = [{}]
        dics_intput = [
            {"X": ["tile_input"], "RepeatTimes": ["repeat_times"]},
        ]
        ops_config = [
            {
                "op_type": "fill_constant",
                "op_inputs": {},
                "op_outputs": {"Out": ["repeat_times"]},
                "op_attrs": {
                    "dtype": 2,
                    "str_value": "10",
                    "shape": [1],
                },
            },
            {
                "op_type": "tile",
                "op_inputs": dics_intput[0],
                "op_outputs": {"Out": ["tile_out"]},
                "op_attrs": dics[0],
            },
        ]
        ops = self.generate_op_config(ops_config)
        program_config = ProgramConfig(
            ops=ops,
            weights={},
            inputs={
                "tile_input": TensorConfig(
                    data_gen=partial(generate_input1, dics)
                )
            },
            outputs=["tile_out"],
        )

        yield program_config

    def sample_predictor_configs(
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
        def generate_dynamic_shape(attrs):
            self.dynamic_shape.min_input_shape = {"tile_input": [1, 2, 3, 4]}
            self.dynamic_shape.max_input_shape = {"tile_input": [4, 3, 64, 64]}
            self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2, 3, 4]}

        def clear_dynamic_shape():
            self.dynamic_shape.min_input_shape = {}
            self.dynamic_shape.max_input_shape = {}
            self.dynamic_shape.opt_input_shape = {}

        def generate_trt_nodes_num(attrs, dynamic_shape):
            return 1, 2

        attrs = [
            program_config.ops[i].attrs for i in range(len(program_config.ops))
        ]

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
            attrs, True
        ), 1e-5
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
            attrs, True
        ), 1e-3

    def add_skip_trt_case(self):
        pass

    def test(self):
        self.add_skip_trt_case()
        self.run_test()


class TrtConvertTileTest3(TrtLayerAutoScanTest):
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
        attrs = [
            program_config.ops[i].attrs for i in range(len(program_config.ops))
        ]
        return True

    def sample_program_configs(self):
        def generate_input1(attrs: List[Dict[str, Any]]):
            return np.ones([1, 2, 3, 4]).astype(np.float32)

        dics = [{}]
        dics_intput = [
            {
                "X": ["tile_input"],
                "repeat_times_tensor": ["repeat_times1", "repeat_times2"],
            },
        ]
        ops_config = [
            {
                "op_type": "fill_constant",
                "op_inputs": {},
                "op_outputs": {"Out": ["repeat_times1"]},
                "op_attrs": {
                    "dtype": 2,
                    "str_value": "10",
                    "shape": [1],
                },
            },
            {
                "op_type": "fill_constant",
                "op_inputs": {},
                "op_outputs": {"Out": ["repeat_times2"]},
                "op_attrs": {
                    "dtype": 2,
                    "str_value": "12",
                    "shape": [1],
                },
            },
            {
                "op_type": "tile",
                "op_inputs": dics_intput[0],
                "op_outputs": {"Out": ["tile_out"]},
                "op_attrs": dics[0],
            },
        ]
        ops = self.generate_op_config(ops_config)
        program_config = ProgramConfig(
            ops=ops,
            weights={},
            inputs={
                "tile_input": TensorConfig(
                    data_gen=partial(generate_input1, dics)
                )
            },
            outputs=["tile_out"],
        )

        yield program_config

    def sample_predictor_configs(
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
        def generate_dynamic_shape(attrs):
            self.dynamic_shape.min_input_shape = {"tile_input": [1, 2, 3, 4]}
            self.dynamic_shape.max_input_shape = {"tile_input": [4, 3, 64, 64]}
            self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2, 3, 4]}

        def clear_dynamic_shape():
            self.dynamic_shape.min_input_shape = {}
            self.dynamic_shape.max_input_shape = {}
            self.dynamic_shape.opt_input_shape = {}

        def generate_trt_nodes_num(attrs, dynamic_shape):
            return 1, 2

        attrs = [
            program_config.ops[i].attrs for i in range(len(program_config.ops))
        ]

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
            attrs, True
        ), 1e-5
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
            attrs, True
        ), 1e-3

    def add_skip_trt_case(self):
        pass

    def test(self):
        self.add_skip_trt_case()
        self.run_test()


已提交
302 303
if __name__ == "__main__":
    unittest.main()