|
| 1 | +# Copyright (c) 2020 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 __future__ import print_function |
| 16 | + |
| 17 | +import unittest |
| 18 | +import itertools |
| 19 | +import numpy as np |
| 20 | +from inference_pass_test import InferencePassTest |
| 21 | +import paddle.fluid as fluid |
| 22 | +import paddle.fluid.core as core |
| 23 | +from paddle.fluid.core import PassVersionChecker |
| 24 | +from paddle.fluid.core import AnalysisConfig |
| 25 | + |
| 26 | + |
| 27 | +class TRTAffineChannelTest(InferencePassTest): |
| 28 | + def setUp(self): |
| 29 | + self.bs = 2 |
| 30 | + self.channel = 8 |
| 31 | + self.height = 16 |
| 32 | + self.width = 16 |
| 33 | + self.data_layout = 'NCHW' |
| 34 | + self.precision = AnalysisConfig.Precision.Float32 |
| 35 | + self.serialize = False |
| 36 | + self.enable_trt = True |
| 37 | + |
| 38 | + def build(self): |
| 39 | + # set min_graph_size to 2, |
| 40 | + # because affine channel doesn't support nhwc format |
| 41 | + self.trt_parameters = InferencePassTest.TensorRTParam( |
| 42 | + 1 << 30, self.bs, 2, self.precision, self.serialize, False) |
| 43 | + |
| 44 | + with fluid.program_guard(self.main_program, self.startup_program): |
| 45 | + if self.data_layout == 'NCHW': |
| 46 | + shape = [-1, self.channel, self.height, self.width] |
| 47 | + else: |
| 48 | + shape = [-1, self.height, self.width, self.channel] |
| 49 | + |
| 50 | + data = fluid.data(name='in', shape=shape, dtype='float32') |
| 51 | + # set scale, bias by constant |
| 52 | + scale = fluid.layers.create_parameter( |
| 53 | + shape=[self.channel], |
| 54 | + dtype='float32', |
| 55 | + default_initializer=fluid.initializer.Constant(2.)) |
| 56 | + bias = fluid.layers.create_parameter( |
| 57 | + shape=[self.channel], |
| 58 | + dtype='float32', |
| 59 | + default_initializer=fluid.initializer.Constant(.5)) |
| 60 | + affine_channel_out = fluid.layers.affine_channel( |
| 61 | + data, scale=scale, bias=bias, data_layout=self.data_layout) |
| 62 | + out = fluid.layers.batch_norm(affine_channel_out, is_test=True) |
| 63 | + |
| 64 | + shape[0] = self.bs |
| 65 | + self.feeds = {'in': np.random.random(shape).astype('float32'), } |
| 66 | + self.fetch_list = [out] |
| 67 | + |
| 68 | + def check_output(self): |
| 69 | + if core.is_compiled_with_cuda(): |
| 70 | + use_gpu = True |
| 71 | + atol = 1e-5 |
| 72 | + if self.trt_parameters.precision == AnalysisConfig.Precision.Half: |
| 73 | + atol = 1e-3 |
| 74 | + self.check_output_with_option(use_gpu, atol, flatten=True) |
| 75 | + self.assertTrue( |
| 76 | + PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) |
| 77 | + |
| 78 | + def run_test(self): |
| 79 | + self.build() |
| 80 | + self.check_output() |
| 81 | + |
| 82 | + def run_test_all(self): |
| 83 | + precision_opt = [ |
| 84 | + AnalysisConfig.Precision.Float32, AnalysisConfig.Precision.Half |
| 85 | + ] |
| 86 | + serialize_opt = [False, True] |
| 87 | + |
| 88 | + if self.data_layout == 'NCHW': |
| 89 | + min_shape = [ |
| 90 | + self.bs, self.channel, self.height // 2, self.width // 2 |
| 91 | + ] |
| 92 | + max_shape = [self.bs, self.channel, self.height * 2, self.width * 2] |
| 93 | + opt_shape = [self.bs, self.channel, self.height, self.width] |
| 94 | + |
| 95 | + if self.data_layout == 'NHWC': |
| 96 | + min_shape = [ |
| 97 | + self.bs, self.height // 2, self.width // 2, self.channel |
| 98 | + ] |
| 99 | + max_shape = [self.bs, self.height * 2, self.width * 2, self.channel] |
| 100 | + opt_shape = [self.bs, self.height, self.width, self.channel] |
| 101 | + |
| 102 | + dynamic_shape_profile = InferencePassTest.DynamicShapeParam({ |
| 103 | + 'in': min_shape |
| 104 | + }, {'in': max_shape}, {'in': opt_shape}, False) |
| 105 | + dynamic_shape_opt = [None, dynamic_shape_profile] |
| 106 | + |
| 107 | + for precision, serialize, dynamic_shape in itertools.product( |
| 108 | + precision_opt, serialize_opt, dynamic_shape_opt): |
| 109 | + self.precision = precision |
| 110 | + self.serialize = serialize |
| 111 | + self.dynamic_shape_params = dynamic_shape |
| 112 | + self.run_test() |
| 113 | + |
| 114 | + def test_base(self): |
| 115 | + self.run_test() |
| 116 | + |
| 117 | + def test_fp16(self): |
| 118 | + self.precision = AnalysisConfig.Precision.Half |
| 119 | + self.run_test() |
| 120 | + |
| 121 | + def test_serialize(self): |
| 122 | + self.serialize = True |
| 123 | + self.run_test() |
| 124 | + |
| 125 | + def test_dynamic(self): |
| 126 | + self.dynamic_shape_params = InferencePassTest.DynamicShapeParam({ |
| 127 | + 'in': [self.bs, self.channel, self.height // 2, self.width // 2] |
| 128 | + }, {'in': [self.bs, self.channel, self.height * 2, self.width * 2] |
| 129 | + }, {'in': [self.bs, self.channel, self.height, self.width]}, False) |
| 130 | + self.run_test() |
| 131 | + |
| 132 | + def test_nchw_all(self): |
| 133 | + self.run_test_all() |
| 134 | + |
| 135 | + def test_nhwc(self): |
| 136 | + self.data_layout = 'NHWC' |
| 137 | + self.run_test_all() |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + unittest.main() |
0 commit comments