|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from trt_layer_auto_scan_test import TrtLayerAutoScanTest, SkipReasons |
| 16 | +from program_config import TensorConfig, ProgramConfig |
| 17 | +import numpy as np |
| 18 | +import paddle.inference as paddle_infer |
| 19 | +from functools import partial |
| 20 | +from typing import Optional, List, Callable, Dict, Any, Set |
| 21 | +import logging |
| 22 | + |
| 23 | + |
| 24 | +class TrtConvertGatherTest(TrtLayerAutoScanTest): |
| 25 | + def is_program_valid(self, program_config: ProgramConfig) -> bool: |
| 26 | + inputs = program_config.inputs |
| 27 | + attrs = [ |
| 28 | + program_config.ops[i].attrs |
| 29 | + for i in range(len(program_config.ops)) |
| 30 | + ] |
| 31 | + if len(inputs['input_data'].shape) <= attrs[0]['axis']: |
| 32 | + return False |
| 33 | + |
| 34 | + return True |
| 35 | + |
| 36 | + def sample_program_configs(self): |
| 37 | + def generate_input1(shape): |
| 38 | + return np.random.random(shape).astype(np.float32) |
| 39 | + |
| 40 | + def generate_input2(index): |
| 41 | + return np.array(index).astype(np.int32) |
| 42 | + |
| 43 | + def generate_input3(axis): |
| 44 | + return np.array([axis]).astype(np.int32) |
| 45 | + |
| 46 | + for shape in [[32], [16, 64], [32, 16, 16], [32, 64, 16, 32]]: |
| 47 | + for index in [[1, 4], [4, 8]]: |
| 48 | + for axis in [0, 1, 2, 3]: |
| 49 | + for overwrite in [True, False]: |
| 50 | + for input in [{ |
| 51 | + "X": ["input_data"], |
| 52 | + "Index": ["index_data"] |
| 53 | + }, { |
| 54 | + "X": ["input_data"], |
| 55 | + "Index": ["index_data"], |
| 56 | + "Axis": ["axis_data"] |
| 57 | + }]: |
| 58 | + self.shape = shape |
| 59 | + self.axis = axis |
| 60 | + self.input_num = len(input) |
| 61 | + dics = [{"overwrite": overwrite, "axis": axis}] |
| 62 | + ops_config = [{ |
| 63 | + "op_type": "gather", |
| 64 | + "op_inputs": input, |
| 65 | + "op_outputs": { |
| 66 | + "Out": ["output_data"] |
| 67 | + }, |
| 68 | + "op_attrs": dics[0] |
| 69 | + }] |
| 70 | + ops = self.generate_op_config(ops_config) |
| 71 | + |
| 72 | + program_config = ProgramConfig( |
| 73 | + ops=ops, |
| 74 | + weights={}, |
| 75 | + inputs={ |
| 76 | + "input_data": TensorConfig(data_gen=partial( |
| 77 | + generate_input1, shape)), |
| 78 | + "index_data": TensorConfig(data_gen=partial( |
| 79 | + generate_input2, index)), |
| 80 | + } if len(input) == 2 else { |
| 81 | + "input_data": TensorConfig(data_gen=partial( |
| 82 | + generate_input1, shape)), |
| 83 | + "index_data": TensorConfig(data_gen=partial( |
| 84 | + generate_input2, index)), |
| 85 | + "axis_data": TensorConfig(data_gen=partial( |
| 86 | + generate_input3, axis)), |
| 87 | + }, |
| 88 | + outputs=["output_data"]) |
| 89 | + |
| 90 | + yield program_config |
| 91 | + |
| 92 | + def sample_predictor_configs( |
| 93 | + self, program_config) -> (paddle_infer.Config, List[int], float): |
| 94 | + def generate_dynamic_shape(attrs): |
| 95 | + if len(self.shape) == 1: |
| 96 | + self.dynamic_shape.min_input_shape = { |
| 97 | + "input_data": [4], |
| 98 | + "index_data": [1] |
| 99 | + } |
| 100 | + self.dynamic_shape.max_input_shape = { |
| 101 | + "input_data": [128], |
| 102 | + "index_data": [4] |
| 103 | + } |
| 104 | + self.dynamic_shape.opt_input_shape = { |
| 105 | + "input_data": [16], |
| 106 | + "index_data": [2] |
| 107 | + } |
| 108 | + elif len(self.shape) == 2: |
| 109 | + self.dynamic_shape.min_input_shape = { |
| 110 | + "input_data": [2, 4], |
| 111 | + "index_data": [1] |
| 112 | + } |
| 113 | + self.dynamic_shape.max_input_shape = { |
| 114 | + "input_data": [256, 256], |
| 115 | + "index_data": [4] |
| 116 | + } |
| 117 | + self.dynamic_shape.opt_input_shape = { |
| 118 | + "input_data": [64, 32], |
| 119 | + "index_data": [2] |
| 120 | + } |
| 121 | + elif len(self.shape) == 3: |
| 122 | + self.dynamic_shape.min_input_shape = { |
| 123 | + "input_data": [2, 4, 4], |
| 124 | + "index_data": [1] |
| 125 | + } |
| 126 | + self.dynamic_shape.max_input_shape = { |
| 127 | + "input_data": [128, 256, 256], |
| 128 | + "index_data": [4] |
| 129 | + } |
| 130 | + self.dynamic_shape.opt_input_shape = { |
| 131 | + "input_data": [16, 64, 32], |
| 132 | + "index_data": [2] |
| 133 | + } |
| 134 | + elif len(self.shape) == 4: |
| 135 | + self.dynamic_shape.min_input_shape = { |
| 136 | + "input_data": [2, 4, 4, 2], |
| 137 | + "index_data": [1] |
| 138 | + } |
| 139 | + self.dynamic_shape.max_input_shape = { |
| 140 | + "input_data": [128, 256, 128, 256], |
| 141 | + "index_data": [4] |
| 142 | + } |
| 143 | + self.dynamic_shape.opt_input_shape = { |
| 144 | + "input_data": [16, 64, 16, 32], |
| 145 | + "index_data": [2] |
| 146 | + } |
| 147 | + |
| 148 | + def clear_dynamic_shape(): |
| 149 | + self.dynamic_shape.max_input_shape = {} |
| 150 | + self.dynamic_shape.min_input_shape = {} |
| 151 | + self.dynamic_shape.opt_input_shape = {} |
| 152 | + |
| 153 | + def generate_trt_nodes_num(dynamic_shape): |
| 154 | + if self.input_num == 3: |
| 155 | + return 0, 5 |
| 156 | + else: |
| 157 | + if dynamic_shape and self.axis == 0: |
| 158 | + return 1, 3 |
| 159 | + else: |
| 160 | + return 0, 4 |
| 161 | + |
| 162 | + attrs = [ |
| 163 | + program_config.ops[i].attrs |
| 164 | + for i in range(len(program_config.ops)) |
| 165 | + ] |
| 166 | + |
| 167 | + # for static_shape |
| 168 | + clear_dynamic_shape() |
| 169 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 170 | + yield self.create_inference_config(), generate_trt_nodes_num( |
| 171 | + False), 1e-5 |
| 172 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 173 | + yield self.create_inference_config(), generate_trt_nodes_num( |
| 174 | + False), 1e-5 |
| 175 | + |
| 176 | + # for dynamic_shape |
| 177 | + generate_dynamic_shape(attrs) |
| 178 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 179 | + yield self.create_inference_config(), generate_trt_nodes_num(True), 1e-5 |
| 180 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 181 | + yield self.create_inference_config(), generate_trt_nodes_num(True), 1e-5 |
| 182 | + |
| 183 | + def add_skip_trt_case(self): |
| 184 | + def teller1(program_config, predictor_config): |
| 185 | + if len(self.dynamic_shape.min_input_shape) != 0: |
| 186 | + inputs = program_config.inputs |
| 187 | + if len(inputs['input_data'].shape) == 1 or len(inputs[ |
| 188 | + 'index_data'].shape) == 1: |
| 189 | + return True |
| 190 | + return False |
| 191 | + |
| 192 | + self.add_skip_case( |
| 193 | + teller1, SkipReasons.TRT_NOT_SUPPORT, |
| 194 | + "Need to repair the case: trt reshape out failed for dynamic shape mode when inputs' dims==1." |
| 195 | + ) |
| 196 | + |
| 197 | + def teller2(program_config, predictor_config): |
| 198 | + inputs = program_config.inputs |
| 199 | + if "axis_data" in inputs.keys(): |
| 200 | + return True |
| 201 | + return False |
| 202 | + |
| 203 | + self.add_skip_case( |
| 204 | + teller2, SkipReasons.TRT_NOT_SUPPORT, |
| 205 | + "Need to repair the case: trt do not support axis tensor input.") |
| 206 | + |
| 207 | + def test(self): |
| 208 | + self.add_skip_trt_case() |
| 209 | + self.run_test() |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + unittest.main() |
0 commit comments