test_trt_inspector.py 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import subprocess
16 17
import sys
import unittest
18

19 20
import numpy as np
from inference_pass_test import InferencePassTest
21

22
import paddle
23 24
from paddle import fluid
from paddle.fluid import core
25 26 27 28 29 30 31
from paddle.fluid.core import AnalysisConfig


class TensorRTInspectorTest(InferencePassTest):
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
32 33 34
            data = paddle.static.data(
                name="data", shape=[1, 16, 16], dtype="float32"
            )
K
kangguangli 已提交
35
            matmul_out = paddle.matmul(
36 37 38 39 40
                x=data,
                y=data,
                transpose_x=self.transpose_x,
                transpose_y=self.transpose_y,
            )
K
kangguangli 已提交
41
            matmul_out = paddle.scale(matmul_out, scale=self.alpha)
42
            out = paddle.static.nn.batch_norm(matmul_out, is_test=True)
43

44 45 46
        self.feeds = {
            "data": np.ones([1, 16, 16]).astype("float32"),
        }
47 48
        self.enable_trt = True
        self.trt_parameters = InferencePassTest.TensorRTParam(
49 50
            1 << 30, 1, 0, AnalysisConfig.Precision.Float32, False, False, True
        )
51 52 53 54 55 56 57 58 59 60 61
        self.fetch_list = [out]

    def set_params(self):
        self.transpose_x = True
        self.transpose_y = True
        self.alpha = 2.0

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            build_engine = subprocess.run(
                [sys.executable, 'test_trt_inspector.py', '--build-engine'],
62 63
                stderr=subprocess.PIPE,
            )
64 65 66 67
            engine_info = build_engine.stderr.decode('ascii')
            trt_compile_version = paddle.inference.get_trt_compile_version()
            trt_runtime_version = paddle.inference.get_trt_runtime_version()
            valid_version = (8, 2, 0)
68 69 70 71
            if (
                trt_compile_version >= valid_version
                and trt_runtime_version >= valid_version
            ):
72 73 74 75 76 77 78
                self.assertTrue('====== engine info ======' in engine_info)
                self.assertTrue('====== engine info end ======' in engine_info)
                self.assertTrue('matmul' in engine_info)
                self.assertTrue('LayerType: Scale' in engine_info)
                self.assertTrue('batch_norm' in engine_info)
            else:
                self.assertTrue(
79 80 81
                    'Inspector needs TensorRT version 8.2 and after.'
                    in engine_info
                )
82 83 84 85 86 87 88 89 90 91


if __name__ == "__main__":
    if '--build-engine' in sys.argv:
        test = TensorRTInspectorTest()
        test.setUp()
        use_gpu = True
        test.check_output_with_option(use_gpu)
    else:
        unittest.main()