test_trt_convert_leaky_relu.py 5.1 KB
Newer Older
已提交
1 2
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

3
#
已提交
4 5 6
# 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
7
#
已提交
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
已提交
10 11 12 13 14 15
# 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.

16
import unittest
已提交
17
from functools import partial
18
from typing import List
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
已提交
25 26 27 28 29 30 31


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

    def sample_program_configs(self):
32 33
        def generate_input1(shape):
            return np.random.random(shape).astype(np.float32)
已提交
34

35 36 37 38 39
        for batch in [1, 2]:
            for shape in [[batch, 64], [batch, 32, 64], [batch, 8, 32, 32]]:
                self.input_dim = len(shape)
                for alpha in [0.02, 1.0, 100.0, -1.0, 0.0]:
                    dics = [{"alpha": alpha}]
40 41 42 43 44 45 46 47 48 49 50 51
                    ops_config = [
                        {
                            "op_type": "leaky_relu",
                            "op_inputs": {
                                "X": ["input_data"],
                            },
                            "op_outputs": {
                                "Out": ["y_data"],
                            },
                            "op_attrs": dics[0],
                        }
                    ]
52 53 54 55 56
                    ops = self.generate_op_config(ops_config)
                    program_config = ProgramConfig(
                        ops=ops,
                        weights={},
                        inputs={
57 58 59
                            "input_data": TensorConfig(
                                data_gen=partial(generate_input1, shape)
                            )
60
                        },
61 62
                        outputs=["y_data"],
                    )
已提交
63

64
                    yield program_config
已提交
65 66

    def sample_predictor_configs(
67 68
        self, program_config
    ) -> (paddle_infer.Config, List[int], float):
已提交
69
        def generate_dynamic_shape(attrs):
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            if self.input_dim == 2:
                self.dynamic_shape.min_input_shape = {"input_data": [1, 8]}
                self.dynamic_shape.max_input_shape = {"input_data": [64, 128]}
                self.dynamic_shape.opt_input_shape = {"input_data": [2, 16]}
            elif self.input_dim == 3:
                self.dynamic_shape.min_input_shape = {"input_data": [1, 8, 8]}
                self.dynamic_shape.max_input_shape = {
                    "input_data": [64, 128, 256]
                }
                self.dynamic_shape.opt_input_shape = {"input_data": [2, 16, 64]}
            elif self.input_dim == 4:
                self.dynamic_shape.min_input_shape = {
                    "input_data": [1, 8, 8, 4]
                }
                self.dynamic_shape.max_input_shape = {
                    "input_data": [64, 64, 128, 128]
                }
                self.dynamic_shape.opt_input_shape = {
                    "input_data": [2, 16, 64, 32]
                }
已提交
90 91 92 93 94 95 96 97 98 99

        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 = [
100
            program_config.ops[i].attrs for i in range(len(program_config.ops))
已提交
101 102 103 104 105 106
        ]

        # for static_shape
        clear_dynamic_shape()
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
        yield self.create_inference_config(), generate_trt_nodes_num(
107 108
            attrs, False
        ), 1e-5
已提交
109 110
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
111 112
            attrs, False
        ), (1e-3, 1e-3)
已提交
113 114
        self.trt_param.precision = paddle_infer.PrecisionType.Int8
        yield self.create_inference_config(), generate_trt_nodes_num(
115 116
            attrs, False
        ), (1e-3, 1e-3)
已提交
117 118 119 120

        # for dynamic_shape
        generate_dynamic_shape(attrs)
        self.trt_param.precision = paddle_infer.PrecisionType.Float32
121
        yield self.create_inference_config(), generate_trt_nodes_num(
122 123
            attrs, True
        ), 1e-5
已提交
124 125
        self.trt_param.precision = paddle_infer.PrecisionType.Half
        yield self.create_inference_config(), generate_trt_nodes_num(
126 127
            attrs, True
        ), (1e-3, 1e-3)
已提交
128 129
        self.trt_param.precision = paddle_infer.PrecisionType.Int8
        yield self.create_inference_config(), generate_trt_nodes_num(
130 131
            attrs, True
        ), (1e-3, 1e-3)
已提交
132 133 134 135 136 137 138

    def test(self):
        self.run_test()


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