|
| 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 | + |
| 22 | + |
| 23 | +class TrtConvertHardSigmoidTest_dim_2(TrtLayerAutoScanTest): |
| 24 | + def is_program_valid(self, program_config: ProgramConfig) -> bool: |
| 25 | + return True |
| 26 | + |
| 27 | + def sample_program_configs(self): |
| 28 | + def generate_input(shape): |
| 29 | + return np.random.random(shape).astype(np.float32) |
| 30 | + |
| 31 | + for batch in [1, 2, 4]: |
| 32 | + for shape in [[batch, 64], [batch, 32, 64], [batch, 64, 32, 128]]: |
| 33 | + self.input_dim = len(shape) |
| 34 | + for slope in [0.1, 0.5]: |
| 35 | + for offset in [0.2, 0.7]: |
| 36 | + dics = [{"slope": slope, "offset": offset}] |
| 37 | + ops_config = [{ |
| 38 | + "op_type": "hard_sigmoid", |
| 39 | + "op_inputs": { |
| 40 | + "X": ["input_data"], |
| 41 | + }, |
| 42 | + "op_outputs": { |
| 43 | + "Out": ["output_data"] |
| 44 | + }, |
| 45 | + "op_attrs": dics[0] |
| 46 | + }] |
| 47 | + ops = self.generate_op_config(ops_config) |
| 48 | + |
| 49 | + program_config = ProgramConfig( |
| 50 | + ops=ops, |
| 51 | + weights={}, |
| 52 | + inputs={ |
| 53 | + "input_data": TensorConfig( |
| 54 | + data_gen=partial(generate_input, shape)) |
| 55 | + }, |
| 56 | + outputs=["output_data"]) |
| 57 | + |
| 58 | + yield program_config |
| 59 | + |
| 60 | + def sample_predictor_configs( |
| 61 | + self, program_config) -> (paddle_infer.Config, List[int], float): |
| 62 | + def generate_dynamic_shape(attrs): |
| 63 | + if self.input_dim == 2: |
| 64 | + self.dynamic_shape.min_input_shape = {"input_data": [1, 8]} |
| 65 | + self.dynamic_shape.max_input_shape = {"input_data": [64, 128]} |
| 66 | + self.dynamic_shape.opt_input_shape = {"input_data": [2, 16]} |
| 67 | + elif self.input_dim == 3: |
| 68 | + self.dynamic_shape.min_input_shape = {"input_data": [1, 8, 8]} |
| 69 | + self.dynamic_shape.max_input_shape = { |
| 70 | + "input_data": [64, 128, 256] |
| 71 | + } |
| 72 | + self.dynamic_shape.opt_input_shape = {"input_data": [2, 16, 64]} |
| 73 | + elif self.input_dim == 4: |
| 74 | + self.dynamic_shape.min_input_shape = { |
| 75 | + "input_data": [1, 8, 8, 4] |
| 76 | + } |
| 77 | + self.dynamic_shape.max_input_shape = { |
| 78 | + "input_data": [64, 128, 256, 512] |
| 79 | + } |
| 80 | + self.dynamic_shape.opt_input_shape = { |
| 81 | + "input_data": [2, 16, 64, 128] |
| 82 | + } |
| 83 | + |
| 84 | + def clear_dynamic_shape(): |
| 85 | + self.dynamic_shape.max_input_shape = {} |
| 86 | + self.dynamic_shape.min_input_shape = {} |
| 87 | + self.dynamic_shape.opt_input_shape = {} |
| 88 | + |
| 89 | + attrs = [ |
| 90 | + program_config.ops[i].attrs |
| 91 | + for i in range(len(program_config.ops)) |
| 92 | + ] |
| 93 | + |
| 94 | + # for static_shape |
| 95 | + clear_dynamic_shape() |
| 96 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 97 | + yield self.create_inference_config(), (1, 2), 1e-5 |
| 98 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 99 | + yield self.create_inference_config(), (1, 2), 1e-5 |
| 100 | + |
| 101 | + # for dynamic_shape |
| 102 | + generate_dynamic_shape(attrs) |
| 103 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 104 | + yield self.create_inference_config(), (1, 2), 1e-5 |
| 105 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 106 | + yield self.create_inference_config(), (1, 2), 1e-5 |
| 107 | + |
| 108 | + def add_skip_trt_case(self): |
| 109 | + def teller(program_config, predictor_config): |
| 110 | + if len(self.dynamic_shape. |
| 111 | + min_input_shape) == 0 and self.input_dim == 2: |
| 112 | + return True |
| 113 | + return False |
| 114 | + |
| 115 | + self.add_skip_case( |
| 116 | + teller, SkipReasons.TRT_NOT_SUPPORT, |
| 117 | + "Need to repair the case: the output of trt and GPU has diff when inputs' dims is 2 in static shape mode." |
| 118 | + ) |
| 119 | + |
| 120 | + def test(self): |
| 121 | + self.add_skip_trt_case() |
| 122 | + self.run_test() |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + unittest.main() |
0 commit comments