|
| 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 TrtConvertGroupNormTest(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(attrs: List[Dict[str, Any]], batch): |
| 29 | + if attrs[0]['data_layout'] == 'NCHW': |
| 30 | + return np.random.random([batch, 32, 64, 64]).astype(np.float32) |
| 31 | + else: |
| 32 | + return np.random.random([batch, 64, 64, 32]).astype(np.float32) |
| 33 | + |
| 34 | + def generate_scale(): |
| 35 | + return np.random.randn(32).astype(np.float32) |
| 36 | + |
| 37 | + def generate_bias(): |
| 38 | + return np.random.randn(32).astype(np.float32) |
| 39 | + |
| 40 | + for batch in [1, 2, 4]: |
| 41 | + for group in [1, 4, 32]: |
| 42 | + for epsilon in [0.1, 0.7]: |
| 43 | + for data_layout in ['NCHW', 'NHWC']: |
| 44 | + for i in [0, 1]: |
| 45 | + dics = [{ |
| 46 | + "epsilon": epsilon, |
| 47 | + "groups": group, |
| 48 | + "data_layout": data_layout |
| 49 | + }, { |
| 50 | + "groups": group, |
| 51 | + "data_layout": data_layout |
| 52 | + }] |
| 53 | + ops_config = [{ |
| 54 | + "op_type": "group_norm", |
| 55 | + "op_inputs": { |
| 56 | + "X": ["input_data"], |
| 57 | + "Scale": ["scale_weight"], |
| 58 | + "Bias": ["bias_weight"] |
| 59 | + }, |
| 60 | + "op_outputs": { |
| 61 | + "Y": ["y_output"], |
| 62 | + "Mean": ["mean_output"], |
| 63 | + "Variance": ["variance_output"] |
| 64 | + }, |
| 65 | + "op_attrs": dics[i] |
| 66 | + }] |
| 67 | + ops = self.generate_op_config(ops_config) |
| 68 | + |
| 69 | + program_config = ProgramConfig( |
| 70 | + ops=ops, |
| 71 | + weights={ |
| 72 | + "scale_weight": TensorConfig( |
| 73 | + data_gen=partial(generate_scale)), |
| 74 | + "bias_weight": TensorConfig( |
| 75 | + data_gen=partial(generate_bias)) |
| 76 | + }, |
| 77 | + inputs={ |
| 78 | + "input_data": TensorConfig(data_gen=partial( |
| 79 | + generate_input, dics, batch)) |
| 80 | + }, |
| 81 | + outputs=["y_output"]) |
| 82 | + |
| 83 | + yield program_config |
| 84 | + |
| 85 | + def sample_predictor_configs( |
| 86 | + self, program_config) -> (paddle_infer.Config, List[int], float): |
| 87 | + def generate_dynamic_shape(attrs): |
| 88 | + self.dynamic_shape.min_input_shape = {"input_data": [1, 16, 32, 32]} |
| 89 | + self.dynamic_shape.max_input_shape = { |
| 90 | + "input_data": [4, 64, 128, 64] |
| 91 | + } |
| 92 | + self.dynamic_shape.opt_input_shape = {"input_data": [2, 32, 64, 64]} |
| 93 | + |
| 94 | + def clear_dynamic_shape(): |
| 95 | + self.dynamic_shape.max_input_shape = {} |
| 96 | + self.dynamic_shape.min_input_shape = {} |
| 97 | + self.dynamic_shape.opt_input_shape = {} |
| 98 | + |
| 99 | + def generate_trt_nodes_num(attrs, dynamic_shape): |
| 100 | + if len(attrs[0]) == 3: |
| 101 | + if dynamic_shape: |
| 102 | + return 1, 2 |
| 103 | + else: |
| 104 | + return 0, 3 |
| 105 | + else: |
| 106 | + return 0, 3 |
| 107 | + |
| 108 | + attrs = [ |
| 109 | + program_config.ops[i].attrs |
| 110 | + for i in range(len(program_config.ops)) |
| 111 | + ] |
| 112 | + |
| 113 | + # for static_shape |
| 114 | + clear_dynamic_shape() |
| 115 | + self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 116 | + yield self.create_inference_config(), generate_trt_nodes_num( |
| 117 | + attrs, False), 1e-5 |
| 118 | + self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 119 | + yield self.create_inference_config(), generate_trt_nodes_num( |
| 120 | + attrs, False), 1e-5 |
| 121 | + |
| 122 | + # for dynamic_shape |
| 123 | + generate_dynamic_shape(attrs) |
| 124 | + # self.trt_param.precision = paddle_infer.PrecisionType.Float32 |
| 125 | + # yield self.create_inference_config(), generate_trt_nodes_num(attrs, True), 1e-5 |
| 126 | + # self.trt_param.precision = paddle_infer.PrecisionType.Half |
| 127 | + # yield self.create_inference_config(), generate_trt_nodes_num(attrs, True), 1e-5 |
| 128 | + |
| 129 | + def test(self): |
| 130 | + self.run_test() |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + unittest.main() |
0 commit comments