|
| 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.fluid as fluid |
| 21 | +import paddle.fluid.core as core |
| 22 | +from paddle.fluid.core import AnalysisConfig |
| 23 | + |
| 24 | + |
| 25 | +#normal starts && ends |
| 26 | +class SlicePluginTRTDynamicTest(SlicePluginTRTTest): |
| 27 | + def setUpSliceParams(self): |
| 28 | + self.params_axes = [1, 3] |
| 29 | + self.params_starts = [0, 1] |
| 30 | + self.params_ends = [2, 3] |
| 31 | + |
| 32 | + def setUpTensorRTParams(self): |
| 33 | + self.trt_parameters = SlicePluginTRTTest.TensorRTParam( |
| 34 | + 1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False) |
| 35 | + self.enable_trt = True |
| 36 | + self.dynamic_shape_params = SlicePluginTRTDynamicTest.DynamicShapeParam( |
| 37 | + { |
| 38 | + 'data': [1, 1, 1, 1] |
| 39 | + }, {'data': [8, 8, 8, 8]}, {'data': [8, 8, 8, 8]}, False) |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + self.setUpSliceParams() |
| 43 | + self.setUpTensorRTParams() |
| 44 | + with fluid.program_guard(self.main_program, self.startup_program): |
| 45 | + data = fluid.data(name="data", shape=[3, 3, 3, 3], dtype="float32") |
| 46 | + axes = self.params_axes |
| 47 | + starts = self.params_starts |
| 48 | + ends = self.params_ends |
| 49 | + slice_out = fluid.layers.slice( |
| 50 | + data, axes=axes, starts=starts, ends=ends) |
| 51 | + |
| 52 | + self.feeds = { |
| 53 | + "data": np.random.random((3, 3, 3, 3)).astype("float32"), |
| 54 | + } |
| 55 | + self.fetch_list = [slice_out] |
| 56 | + |
| 57 | + def test_check_output(self): |
| 58 | + use_gpu = [False] |
| 59 | + if core.is_compiled_with_cuda(): |
| 60 | + use_gpu.append(True) |
| 61 | + for i in range(len(use_gpu)): |
| 62 | + atol = 1e-5 |
| 63 | + if self.trt_parameters.precision == AnalysisConfig.Precision.Half: |
| 64 | + atol = 1e-3 |
| 65 | + self.check_output_with_option(use_gpu[i], atol) |
| 66 | + |
| 67 | + |
| 68 | +class SlicePluginTRTDynamicBoundTest(SlicePluginTRTDynamicTest): |
| 69 | + def setUpSliceParams(self): |
| 70 | + self.params_axes = [1, 3] |
| 71 | + self.params_starts = [0, 1] |
| 72 | + self.params_ends = [2, 1000] |
| 73 | + |
| 74 | + def setUpTensorRTParams(self): |
| 75 | + self.trt_parameters = SlicePluginTRTDynamicBoundTest.TensorRTParam( |
| 76 | + 1 << 30, 32, 1, AnalysisConfig.Precision.Half, False, False) |
| 77 | + self.enable_trt = True |
| 78 | + self.dynamic_shape_params = SlicePluginTRTDynamicBoundTest.DynamicShapeParam( |
| 79 | + { |
| 80 | + 'data': [1, 1, 1, 1] |
| 81 | + }, {'data': [8, 8, 8, 8]}, {'data': [8, 8, 8, 8]}, False) |
| 82 | + |
| 83 | + |
| 84 | +class SlicePluginTRTDynamicNegativeBoundTest(SlicePluginTRTDynamicTest): |
| 85 | + def setUpSliceParams(self): |
| 86 | + self.params_axes = [1, 3] |
| 87 | + self.params_starts = [-5, 1] |
| 88 | + self.params_ends = [2, 1000] |
| 89 | + |
| 90 | + def setUpTensorRTParams(self): |
| 91 | + self.trt_parameters = SlicePluginTRTDynamicNegativeBoundTest.TensorRTParam( |
| 92 | + 1 << 30, 32, 1, AnalysisConfig.Precision.Half, False, False) |
| 93 | + self.enable_trt = True |
| 94 | + self.dynamic_shape_params = SlicePluginTRTDynamicNegativeBoundTest.DynamicShapeParam( |
| 95 | + { |
| 96 | + 'data': [1, 1, 1, 1] |
| 97 | + }, {'data': [8, 8, 8, 8]}, {'data': [8, 8, 8, 8]}, False) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + unittest.main() |
0 commit comments