test_trt_convert_scale.py 9.0 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 19 20 21 22 23

import numpy as np
from program_config import ProgramConfig, TensorConfig
from trt_layer_auto_scan_test import SkipReasons, TrtLayerAutoScanTest

import paddle.inference as paddle_infer
24 25 26 27 28 29 30


class TrtConvertScaleTest(TrtLayerAutoScanTest):
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
        return True

    def sample_program_configs(self):
W
wenbin 已提交
31
        def generate_input1(attrs: List[Dict[str, Any]], batch, is_int):
32
            if self.dims == 4:
W
wenbin 已提交
33 34 35
                return np.ones([batch, 3, 24, 24]).astype(
                    np.int32 if is_int else np.float32
                )
36
            elif self.dims == 3:
W
wenbin 已提交
37 38 39
                return np.ones([batch, 3, 24]).astype(
                    np.int32 if is_int else np.float32
                )
40
            elif self.dims == 2:
W
wenbin 已提交
41 42 43
                return np.ones([batch, 24]).astype(
                    np.int32 if is_int else np.float32
                )
44
            elif self.dims == 1:
W
wenbin 已提交
45
                return np.ones([24]).astype(np.int32 if is_int else np.float32)
46 47
            elif self.dims == 0:
                return np.ones([]).astype(np.int32 if is_int else np.float32)
48

W
wenbin 已提交
49 50
        def generate_weight1(attrs: List[Dict[str, Any]], is_int):
            return np.ones([1]).astype(np.int32 if is_int else np.float32)
51 52

        for num_input in [0, 1]:
53
            for dims in [0, 1, 2, 3, 4]:
54
                for batch in [1, 2]:
55
                    for scale in [0.1, -1.0]:
56 57
                        for bias in [0.0, 1.2]:
                            for bias_after_scale in [False, True]:
W
wenbin 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                                for is_int in [False, True]:
                                    self.num_input = num_input
                                    self.dims = dims
                                    self.is_int = is_int
                                    dics = [
                                        {
                                            "scale": scale,
                                            "bias": bias,
                                            "bias_after_scale": bias_after_scale,
                                        },
                                        {},
                                    ]

                                    dics_intput = [
                                        {
                                            "X": ["scale_input"],
                                            "ScaleTensor": ["ScaleTensor"],
                                        },
                                        {"X": ["scale_input"]},
                                    ]
                                    dics_intputs = [
                                        {
                                            "ScaleTensor": TensorConfig(
                                                data_gen=partial(
                                                    generate_weight1,
                                                    dics,
                                                    is_int,
                                                )
86
                                            )
W
wenbin 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                                        },
                                        {},
                                    ]

                                    ops_config = [
                                        {
                                            "op_type": "scale",
                                            "op_inputs": dics_intput[num_input],
                                            "op_outputs": {
                                                "Out": ["scale_out"]
                                            },
                                            "op_attrs": dics[0],
                                        }
                                    ]
                                    ops = self.generate_op_config(ops_config)
                                    program_config = ProgramConfig(
                                        ops=ops,
                                        weights=dics_intputs[num_input],
                                        inputs={
                                            "scale_input": TensorConfig(
                                                data_gen=partial(
                                                    generate_input1,
                                                    dics,
                                                    batch,
                                                    is_int,
                                                )
113
                                            )
W
wenbin 已提交
114 115 116
                                        },
                                        outputs=["scale_out"],
                                    )
117

W
wenbin 已提交
118
                                    yield program_config
119 120

    def sample_predictor_configs(
121 122
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
123 124 125 126 127 128
        def generate_dynamic_shape(attrs):
            if self.dims == 4:
                self.dynamic_shape.min_input_shape = {
                    "scale_input": [1, 3, 24, 24]
                }
                self.dynamic_shape.max_input_shape = {
129
                    "scale_input": [4, 3, 24, 24]
130 131
                }
                self.dynamic_shape.opt_input_shape = {
132
                    "scale_input": [1, 3, 24, 24]
133 134 135
                }
            elif self.dims == 3:
                self.dynamic_shape.min_input_shape = {"scale_input": [1, 3, 24]}
136
                self.dynamic_shape.max_input_shape = {"scale_input": [4, 3, 24]}
137 138 139 140 141 142 143 144 145
                self.dynamic_shape.opt_input_shape = {"scale_input": [1, 3, 24]}
            elif self.dims == 2:
                self.dynamic_shape.min_input_shape = {"scale_input": [1, 24]}
                self.dynamic_shape.max_input_shape = {"scale_input": [9, 48]}
                self.dynamic_shape.opt_input_shape = {"scale_input": [1, 24]}
            elif self.dims == 1:
                self.dynamic_shape.min_input_shape = {"scale_input": [24]}
                self.dynamic_shape.max_input_shape = {"scale_input": [48]}
                self.dynamic_shape.opt_input_shape = {"scale_input": [24]}
146 147 148 149
            elif self.dims == 0:
                self.dynamic_shape.min_input_shape = {"scale_input": []}
                self.dynamic_shape.max_input_shape = {"scale_input": []}
                self.dynamic_shape.opt_input_shape = {"scale_input": []}
150 151 152 153 154 155 156

        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):
157 158
            if not dynamic_shape and (self.dims == 1 or self.dims == 0):
                return 0, 3
159 160 161
            return 1, 2

        attrs = [
162
            program_config.ops[i].attrs for i in range(len(program_config.ops))
163 164 165 166 167 168
        ]

        # for static_shape
        clear_dynamic_shape()
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
169 170
            attrs, False
        ), 1e-5
171 172
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
173 174
            attrs, False
        ), (1e-3, 1e-3)
175 176 177 178

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
179
        yield self.create_inference_config(), generate_trt_nodes_num(
180 181
            attrs, True
        ), 1e-5
182
        self.trt_param.precision = paddle_infer.PrecisionType.Half
183
        yield self.create_inference_config(), generate_trt_nodes_num(
184 185
            attrs, True
        ), (1e-3, 1e-3)
186 187 188

    def add_skip_trt_case(self):
        def teller1(program_config, predictor_config):
189
            if self.num_input == 0:
190 191 192
                return True
            return False

193 194 195 196 197
        self.add_skip_case(
            teller1,
            SkipReasons.TRT_NOT_SUPPORT,
            "INPUT ScaleTensor and Shape NOT SUPPORT",
        )
198 199

        def teller2(program_config, predictor_config):
W
wenbin 已提交
200 201 202 203 204
            if self.is_int and len(self.dynamic_shape.min_input_shape) == 0:
                return True
            return False

        self.add_skip_case(
205
            teller2,
W
wenbin 已提交
206 207 208 209
            SkipReasons.TRT_NOT_SUPPORT,
            "INTEGER INPUT OF STATIC SHAPE NOT SUPPORT",
        )

210 211 212 213 214 215 216
    def test(self):
        self.add_skip_trt_case()
        self.run_test()


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