test_trt_convert_activation.py 7.7 KB
Newer Older
J
JingZhuangzhuang 已提交
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
J
JingZhuangzhuang 已提交
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
#
J
JingZhuangzhuang 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
J
JingZhuangzhuang 已提交
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.

W
Wilber 已提交
15
import unittest
J
JingZhuangzhuang 已提交
16
from functools import partial
17
from typing import Any, Dict, List
J
JingZhuangzhuang 已提交
18

19 20 21 22 23 24
import numpy as np
from program_config import ProgramConfig, TensorConfig
from trt_layer_auto_scan_test import TrtLayerAutoScanTest

import paddle.inference as paddle_infer

J
JingZhuangzhuang 已提交
25 26 27

class TrtConvertActivationTest(TrtLayerAutoScanTest):
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
28 29 30 31
        ver = paddle_infer.get_trt_compile_version()
        if ver[0] * 1000 + ver[1] * 100 + ver[0] * 10 < 8200:
            if program_config.ops[0].type == "round":
                return False
J
JingZhuangzhuang 已提交
32 33 34 35
        return True

    def sample_program_configs(self):
        def generate_input1(dims, batch, attrs: List[Dict[str, Any]]):
36 37 38
            if dims == 0:
                return np.random.random([]).astype(np.float32)
            elif dims == 1:
39
                return np.random.random([32]).astype(np.float32)
J
JingZhuangzhuang 已提交
40
            elif dims == 2:
41
                return np.random.random([3, 32]).astype(np.float32)
J
JingZhuangzhuang 已提交
42
            elif dims == 3:
43
                return np.random.random([3, 32, 32]).astype(np.float32)
J
JingZhuangzhuang 已提交
44
            else:
45
                return np.random.random([batch, 3, 32, 32]).astype(np.float32)
J
JingZhuangzhuang 已提交
46

47
        for dims in [0, 1, 2, 3, 4]:
48
            for batch in [1, 4]:
49
                for op_type in [
50 51 52 53 54
                    "relu",
                    "sigmoid",
                    "relu6",
                    "elu",
                    "selu",
55
                    "silu",
56 57 58
                    "softsign",
                    "stanh",
                    "thresholded_relu",
59 60 61
                    "celu",
                    "logsigmoid",
                    "tanh_shrink",
62
                    "softplus",
63 64
                ]:
                    # few samples to reduce time
65
                    # for beta in [-0.2, 0.5, 0.67, 3]:
66 67 68 69 70
                    #    for alpha in [-0.2, 0.5, 0.67, 3]:
                    for beta in [0.67]:
                        for alpha in [0.67]:
                            self.dims = dims
                            dics = [{}]
71 72
                            if op_type == "celu":
                                dics = [{"alpha": 1.0}]
73 74 75 76 77 78 79 80 81 82
                            if op_type == "elu":
                                dics = [{"alpha": alpha}]
                            if op_type == "selu":
                                dics = [{"alpha": beta, "scale": alpha}]
                            if op_type == "stanh":
                                dics = [{"scale_a": beta, "scale_b": alpha}]
                            if op_type == "thresholded_relu":
                                dics = [{"threshold": alpha}]
                            if op_type == "softplus":
                                dics = [{"beta": beta}]
J
JingZhuangzhuang 已提交
83

84 85 86 87 88 89 90 91
                            ops_config = [
                                {
                                    "op_type": op_type,
                                    "op_inputs": {"X": ["input_data"]},
                                    "op_outputs": {"Out": ["output_data"]},
                                    "op_attrs": dics[0],
                                }
                            ]
92
                            ops = self.generate_op_config(ops_config)
J
JingZhuangzhuang 已提交
93

94 95 96 97
                            program_config = ProgramConfig(
                                ops=ops,
                                weights={},
                                inputs={
98 99 100 101 102
                                    "input_data": TensorConfig(
                                        data_gen=partial(
                                            generate_input1, dims, batch, dics
                                        )
                                    )
103
                                },
104 105
                                outputs=["output_data"],
                            )
J
JingZhuangzhuang 已提交
106

107
                            yield program_config
J
JingZhuangzhuang 已提交
108 109

    def sample_predictor_configs(
110 111
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
J
JingZhuangzhuang 已提交
112
        def generate_dynamic_shape(attrs):
113 114 115 116 117
            if self.dims == 0:
                self.dynamic_shape.min_input_shape = {"input_data": []}
                self.dynamic_shape.max_input_shape = {"input_data": []}
                self.dynamic_shape.opt_input_shape = {"input_data": []}
            elif self.dims == 1:
J
JingZhuangzhuang 已提交
118
                self.dynamic_shape.min_input_shape = {"input_data": [1]}
119 120
                self.dynamic_shape.max_input_shape = {"input_data": [64]}
                self.dynamic_shape.opt_input_shape = {"input_data": [32]}
J
JingZhuangzhuang 已提交
121
            elif self.dims == 2:
122 123 124
                self.dynamic_shape.min_input_shape = {"input_data": [1, 16]}
                self.dynamic_shape.max_input_shape = {"input_data": [4, 32]}
                self.dynamic_shape.opt_input_shape = {"input_data": [3, 32]}
J
JingZhuangzhuang 已提交
125
            elif self.dims == 3:
126 127 128
                self.dynamic_shape.min_input_shape = {"input_data": [1, 16, 16]}
                self.dynamic_shape.max_input_shape = {"input_data": [4, 32, 32]}
                self.dynamic_shape.opt_input_shape = {"input_data": [3, 32, 32]}
J
JingZhuangzhuang 已提交
129 130
            else:
                self.dynamic_shape.min_input_shape = {
131
                    "input_data": [1, 3, 16, 16]
J
JingZhuangzhuang 已提交
132 133
                }
                self.dynamic_shape.max_input_shape = {
134
                    "input_data": [4, 3, 32, 32]
J
JingZhuangzhuang 已提交
135 136
                }
                self.dynamic_shape.opt_input_shape = {
137
                    "input_data": [1, 3, 32, 32]
J
JingZhuangzhuang 已提交
138 139 140 141 142 143 144 145
                }

        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):
146
            if not dynamic_shape and (self.dims == 1 or self.dims == 0):
J
JingZhuangzhuang 已提交
147
                return 0, 3
148 149 150 151 152 153 154 155 156
            runtime_version = paddle_infer.get_trt_runtime_version()
            if (
                runtime_version[0] * 1000
                + runtime_version[1] * 100
                + runtime_version[2] * 10
                < 8600
                and self.dims == 0
            ) and program_config.ops[0].type in ["celu", "logsigmoid"]:
                return 0, 3
J
JingZhuangzhuang 已提交
157 158 159
            return 1, 2

        attrs = [
160
            program_config.ops[i].attrs for i in range(len(program_config.ops))
J
JingZhuangzhuang 已提交
161 162 163 164 165 166
        ]

        # for static_shape
        clear_dynamic_shape()
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
167 168
            attrs, False
        ), 1e-5
J
JingZhuangzhuang 已提交
169 170
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
171 172
            attrs, False
        ), 1e-3
J
JingZhuangzhuang 已提交
173 174 175 176

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

    def test(self):
        self.run_test()


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