test_trt_convert_activation.py 6.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 36
        return True

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

        for dims in [1, 2, 3, 4]:
46
            for batch in [1, 4]:
47
                for op_type in [
48 49 50 51 52 53 54 55 56 57
                    "relu",
                    "sigmoid",
                    "tanh",
                    "relu6",
                    "elu",
                    "selu",
                    "softsign",
                    "stanh",
                    "thresholded_relu",
                    "softplus",
58 59
                ]:
                    # few samples to reduce time
60
                    # for beta in [-0.2, 0.5, 0.67, 3]:
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
                    #    for alpha in [-0.2, 0.5, 0.67, 3]:
                    for beta in [0.67]:
                        for alpha in [0.67]:
                            self.dims = dims
                            dics = [{}]
                            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 已提交
76

77 78 79 80 81 82 83 84
                            ops_config = [
                                {
                                    "op_type": op_type,
                                    "op_inputs": {"X": ["input_data"]},
                                    "op_outputs": {"Out": ["output_data"]},
                                    "op_attrs": dics[0],
                                }
                            ]
85
                            ops = self.generate_op_config(ops_config)
J
JingZhuangzhuang 已提交
86

87 88 89 90
                            program_config = ProgramConfig(
                                ops=ops,
                                weights={},
                                inputs={
91 92 93 94 95
                                    "input_data": TensorConfig(
                                        data_gen=partial(
                                            generate_input1, dims, batch, dics
                                        )
                                    )
96
                                },
97 98
                                outputs=["output_data"],
                            )
J
JingZhuangzhuang 已提交
99

100
                            yield program_config
J
JingZhuangzhuang 已提交
101 102

    def sample_predictor_configs(
103 104
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
J
JingZhuangzhuang 已提交
105 106 107
        def generate_dynamic_shape(attrs):
            if self.dims == 1:
                self.dynamic_shape.min_input_shape = {"input_data": [1]}
108 109
                self.dynamic_shape.max_input_shape = {"input_data": [64]}
                self.dynamic_shape.opt_input_shape = {"input_data": [32]}
J
JingZhuangzhuang 已提交
110
            elif self.dims == 2:
111 112 113
                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 已提交
114
            elif self.dims == 3:
115 116 117
                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 已提交
118 119
            else:
                self.dynamic_shape.min_input_shape = {
120
                    "input_data": [1, 3, 16, 16]
J
JingZhuangzhuang 已提交
121 122
                }
                self.dynamic_shape.max_input_shape = {
123
                    "input_data": [4, 3, 32, 32]
J
JingZhuangzhuang 已提交
124 125
                }
                self.dynamic_shape.opt_input_shape = {
126
                    "input_data": [1, 3, 32, 32]
J
JingZhuangzhuang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
                }

        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):
            if self.dims == 1:
                return 0, 3
            return 1, 2

        attrs = [
140
            program_config.ops[i].attrs for i in range(len(program_config.ops))
J
JingZhuangzhuang 已提交
141 142 143 144 145 146
        ]

        # for static_shape
        clear_dynamic_shape()
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
147 148
            attrs, False
        ), 1e-5
J
JingZhuangzhuang 已提交
149 150
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
151 152
            attrs, False
        ), 1e-3
J
JingZhuangzhuang 已提交
153 154 155 156

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
157
        yield self.create_inference_config(), generate_trt_nodes_num(
158 159
            attrs, True
        ), 1e-5
J
JingZhuangzhuang 已提交
160
        self.trt_param.precision = paddle_infer.PrecisionType.Half
161
        yield self.create_inference_config(), generate_trt_nodes_num(
162 163
            attrs, True
        ), 1e-3
J
JingZhuangzhuang 已提交
164 165 166 167 168 169 170

    def test(self):
        self.run_test()


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