|
| 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 TrtConvertAnchorGeneratorTest(TrtLayerAutoScanTest): |
| 24 | + def is_program_valid(self, program_config: ProgramConfig) -> bool: |
| 25 | + return True |
| 26 | + |
| 27 | + def sample_program_configs(self): |
| 28 | + def generate_input1(batch, attrs: List[Dict[str, Any]]): |
| 29 | + return np.random.random([batch, 3, 64, 64]).astype(np.float32) |
| 30 | + |
| 31 | + for batch in [1, 2, 4]: |
| 32 | + for anchor_sizes in [[64.0, 128.0, 256.0, 512.0]]: |
| 33 | + for aspect_ratios in [[0.5, 1, 2], [0.4, 1.2, 3]]: |
| 34 | + for variances in [[1.0, 1.0, 1.0, 1.0], |
| 35 | + [0.5, 1.0, 0.5, 1.0]]: |
| 36 | + for stride in [[16.0, 16.0], [16.0, 32.0]]: |
| 37 | + for offset in [0.5, 0.8]: |
| 38 | + dics = [{ |
| 39 | + "anchor_sizes": anchor_sizes, |
| 40 | + "aspect_ratios": aspect_ratios, |
| 41 | + "variances": variances, |
| 42 | + "stride": stride, |
| 43 | + "offset": offset |
| 44 | + }] |
| 45 | + |
| 46 | + ops_config = [{ |
| 47 | + "op_type": "anchor_generator", |
| 48 | + "op_inputs": { |
| 49 | + "Input": ["input_data"] |
| 50 | + }, |
| 51 | + "op_outputs": { |
| 52 | + "Anchors": ["output_anchors"], |
| 53 | + "Variances": ["output_variances"] |
| 54 | + }, |
| 55 | + "op_attrs": dics[0] |
| 56 | + }] |
| 57 | + ops = self.generate_op_config(ops_config) |
| 58 | + |
| 59 | + program_config = ProgramConfig( |
| 60 | + ops=ops, |
| 61 | + weights={}, |
| 62 | + inputs={ |
| 63 | + "input_data": TensorConfig( |
| 64 | + data_gen=partial(generate_input1, |
| 65 | + batch, dics)) |
| 66 | + }, |
| 67 | + outputs=[ |
| 68 | + "output_anchors", "output_variances" |
| 69 | + ]) |
| 70 | + |
| 71 | + yield program_config |
| 72 | + |
| 73 | + def sample_predictor_configs( |
| 74 | + self, program_config) -> (paddle_infer.Config, List[int], float): |
| 75 | + def generate_dynamic_shape(attrs): |
| 76 | + self.dynamic_shape.min_input_shape = {"input_data": [1, 3, 32, 32]} |
| 77 | + self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]} |
| 78 | + self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]} |
| 79 | + |
| 80 | + def clear_dynamic_shape(): |
| 81 | + self.dynamic_shape.min_input_shape = {} |
| 82 | + self.dynamic_shape.max_input_shape = {} |
| 83 | + self.dynamic_shape.opt_input_shape = {} |
| 84 | + |
| 85 | + def generate_trt_nodes_num(attrs, dynamic_shape): |
| 86 | + return 1, 3 |
| 87 | + |
| 88 | + attrs = [ |
| 89 | + program_config.ops[i].attrs |
| 90 | + for i in range(len(program_config.ops)) |
| 91 | + ] |
| 92 | + |
| 93 | + # for static_shape |
| 94 | + clear_dynamic_shape() |
| 95 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 96 | + yield self.create_inference_config(), generate_trt_nodes_num( |
| 97 | + attrs, False), 1e-5 |
| 98 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 99 | + yield self.create_inference_config(), generate_trt_nodes_num( |
| 100 | + attrs, False), 1e-5 |
| 101 | + |
| 102 | + # for dynamic_shape |
| 103 | + generate_dynamic_shape(attrs) |
| 104 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 105 | + yield self.create_inference_config(), generate_trt_nodes_num(attrs, |
| 106 | + True), 1e-5 |
| 107 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 108 | + yield self.create_inference_config(), generate_trt_nodes_num(attrs, |
| 109 | + True), 1e-5 |
| 110 | + |
| 111 | + def test(self): |
| 112 | + self.run_test() |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + unittest.main() |
0 commit comments