|
| 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 numpy as np |
| 19 | +from inference_pass_test import InferencePassTest |
| 20 | +import paddle |
| 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 TRTTileTest(InferencePassTest): |
| 28 | + def setUp(self): |
| 29 | + with fluid.program_guard(self.main_program, self.startup_program): |
| 30 | + data = fluid.data( |
| 31 | + name="data", shape=[4, 3, 224, 256], dtype="float32") |
| 32 | + tile_out = paddle.tile(x=data, repeat_times=[1, 1, 1, 1]) |
| 33 | + out = fluid.layers.batch_norm(tile_out, is_test=True) |
| 34 | + |
| 35 | + self.feeds = { |
| 36 | + "data": np.random.random([4, 3, 224, 256]).astype("float32"), |
| 37 | + } |
| 38 | + self.enable_trt = True |
| 39 | + self.trt_parameters = TRTTileTest.TensorRTParam( |
| 40 | + 1 << 30, 16, 1, AnalysisConfig.Precision.Float32, False, False) |
| 41 | + self.fetch_list = [out] |
| 42 | + |
| 43 | + def test_check_output(self): |
| 44 | + if core.is_compiled_with_cuda(): |
| 45 | + use_gpu = True |
| 46 | + self.check_output_with_option(use_gpu, flatten=True) |
| 47 | + self.assertTrue( |
| 48 | + PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) |
| 49 | + |
| 50 | + |
| 51 | +class TRTTileExpandTest(InferencePassTest): |
| 52 | + def setUp(self): |
| 53 | + with fluid.program_guard(self.main_program, self.startup_program): |
| 54 | + data = fluid.data(name="data", shape=[1, 1, 1, 1], dtype="float32") |
| 55 | + tile_out = paddle.tile(x=data, repeat_times=[1, 4, 1080, 1920]) |
| 56 | + out = fluid.layers.batch_norm(tile_out, is_test=True) |
| 57 | + |
| 58 | + self.feeds = { |
| 59 | + "data": np.random.random([1, 1, 1, 1]).astype("float32"), |
| 60 | + } |
| 61 | + self.enable_trt = True |
| 62 | + self.trt_parameters = TRTTileExpandTest.TensorRTParam( |
| 63 | + 1 << 30, 1, 1, AnalysisConfig.Precision.Float32, False, False) |
| 64 | + self.fetch_list = [out] |
| 65 | + |
| 66 | + def test_check_output(self): |
| 67 | + if core.is_compiled_with_cuda(): |
| 68 | + use_gpu = True |
| 69 | + self.check_output_with_option(use_gpu, flatten=True) |
| 70 | + self.assertTrue( |
| 71 | + PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) |
| 72 | + |
| 73 | + |
| 74 | +class TRTTileExpandStaticTest(InferencePassTest): |
| 75 | + def setUp(self): |
| 76 | + with fluid.program_guard(self.main_program, self.startup_program): |
| 77 | + data = fluid.data(name="data", shape=[1, 1, 1, 1], dtype="float32") |
| 78 | + tile_out = paddle.tile(x=data, repeat_times=[1, 4, 1080, 1920]) |
| 79 | + out = fluid.layers.batch_norm(tile_out, is_test=True) |
| 80 | + |
| 81 | + self.feeds = { |
| 82 | + "data": np.random.random([1, 1, 1, 1]).astype("float32"), |
| 83 | + } |
| 84 | + self.enable_trt = True |
| 85 | + self.trt_parameters = TRTTileExpandStaticTest.TensorRTParam( |
| 86 | + 1 << 30, 1, 1, AnalysisConfig.Precision.Float32, True, False) |
| 87 | + self.fetch_list = [out] |
| 88 | + |
| 89 | + def test_check_output(self): |
| 90 | + if core.is_compiled_with_cuda(): |
| 91 | + use_gpu = True |
| 92 | + self.check_output_with_option(use_gpu, flatten=True) |
| 93 | + self.assertTrue( |
| 94 | + PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) |
| 95 | + |
| 96 | + |
| 97 | +class TRTTileExpandHalfTest(InferencePassTest): |
| 98 | + def setUp(self): |
| 99 | + with fluid.program_guard(self.main_program, self.startup_program): |
| 100 | + data = fluid.data(name="data", shape=[1, 1, 1, 1], dtype="float32") |
| 101 | + tile_out = paddle.tile(x=data, repeat_times=[1, 4, 1080, 1920]) |
| 102 | + out = fluid.layers.batch_norm(tile_out, is_test=True) |
| 103 | + |
| 104 | + self.feeds = { |
| 105 | + "data": np.random.random([1, 1, 1, 1]).astype("float32"), |
| 106 | + } |
| 107 | + self.enable_trt = True |
| 108 | + self.trt_parameters = TRTTileExpandHalfTest.TensorRTParam( |
| 109 | + 1 << 30, 1, 1, AnalysisConfig.Precision.Half, False, False) |
| 110 | + self.fetch_list = [out] |
| 111 | + |
| 112 | + def test_check_output(self): |
| 113 | + if core.is_compiled_with_cuda(): |
| 114 | + use_gpu = True |
| 115 | + self.check_output_with_option(use_gpu, flatten=True) |
| 116 | + self.assertTrue( |
| 117 | + PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + unittest.main() |
0 commit comments