test_trt_convert_scale.py 8.9 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

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

        for num_input in [0, 1]:
            for dims in [1, 2, 3, 4]:
                for batch in [1, 2]:
53
                    for scale in [0.1, -1.0]:
54 55
                        for bias in [0.0, 1.2]:
                            for bias_after_scale in [False, True]:
W
wenbin 已提交
56 57 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
                                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,
                                                )
84
                                            )
W
wenbin 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                                        },
                                        {},
                                    ]

                                    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,
                                                )
111
                                            )
W
wenbin 已提交
112 113 114
                                        },
                                        outputs=["scale_out"],
                                    )
115

W
wenbin 已提交
116
                                    yield program_config
117 118

    def sample_predictor_configs(
119 120
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
121 122 123 124 125 126
        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 = {
127
                    "scale_input": [4, 3, 24, 24]
128 129
                }
                self.dynamic_shape.opt_input_shape = {
130
                    "scale_input": [1, 3, 24, 24]
131 132 133
                }
            elif self.dims == 3:
                self.dynamic_shape.min_input_shape = {"scale_input": [1, 3, 24]}
134
                self.dynamic_shape.max_input_shape = {"scale_input": [4, 3, 24]}
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                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]}

        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 = [
154
            program_config.ops[i].attrs for i in range(len(program_config.ops))
155 156 157 158 159 160
        ]

        # for static_shape
        clear_dynamic_shape()
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
161 162
            attrs, False
        ), 1e-5
163 164
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
165 166
            attrs, False
        ), (1e-3, 1e-3)
167 168 169 170

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
171
        yield self.create_inference_config(), generate_trt_nodes_num(
172 173
            attrs, True
        ), 1e-5
174
        self.trt_param.precision = paddle_infer.PrecisionType.Half
175
        yield self.create_inference_config(), generate_trt_nodes_num(
176 177
            attrs, True
        ), (1e-3, 1e-3)
178 179 180

    def add_skip_trt_case(self):
        def teller1(program_config, predictor_config):
181
            if self.num_input == 0:
182 183 184
                return True
            return False

185 186 187 188 189
        self.add_skip_case(
            teller1,
            SkipReasons.TRT_NOT_SUPPORT,
            "INPUT ScaleTensor and Shape NOT SUPPORT",
        )
190 191

        def teller2(program_config, predictor_config):
192
            if self.dims == 1 and len(self.dynamic_shape.min_input_shape) == 0:
193 194 195
                return True
            return False

196 197 198 199 200
        self.add_skip_case(
            teller2,
            SkipReasons.TRT_NOT_SUPPORT,
            "INPUT DIM EQUAL TO 1 OF STATIC SHAPE NOT SUPPORT",
        )
201

W
wenbin 已提交
202 203 204 205 206 207 208 209 210 211 212
        def teller3(program_config, predictor_config):
            if self.is_int and len(self.dynamic_shape.min_input_shape) == 0:
                return True
            return False

        self.add_skip_case(
            teller3,
            SkipReasons.TRT_NOT_SUPPORT,
            "INTEGER INPUT OF STATIC SHAPE NOT SUPPORT",
        )

213 214 215 216 217 218 219
    def test(self):
        self.add_skip_trt_case()
        self.run_test()


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