test_map_matmul_to_mul_pass.py 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2021 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.

import unittest

import hypothesis.strategies as st
18 19
from auto_scan_test import IgnoreReasons, PassAutoScanTest
from program_config import OpConfig, ProgramConfig, TensorConfig
20 21 22


class TestMapMatmulToMulPass(PassAutoScanTest):
23
    r"""
24 25 26
    x_var    y_var(persistable)
      \       /
        matmul
27 28 29 30 31
    """

    def sample_predictor_configs(self, program_config):
        # cpu
        config = self.create_inference_config(use_gpu=False)
32 33 34
        yield config, [
            "mul",
        ], (1e-5, 1e-5)
35 36 37

        # for gpu
        config = self.create_inference_config(use_gpu=True)
38 39 40
        yield config, [
            "mul",
        ], (1e-5, 1e-5)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        # TRT
        # config = self.create_trt_inference_config()
        # config.enable_tensorrt_engine(
        #     max_batch_size=10,
        #     workspace_size=10240,
        #     min_subgraph_size=0,
        #     precision_mode=paddle_infer.PrecisionType.Float32,
        #     use_static=False,
        #     use_calib_mode=False)
        # yield config, ["mul", ], (1e-5, 1e-5)

    def add_ignore_pass_case(self):
        # Here we put some skip rules to avoid known bugs
        def teller1(program_config, predictor_config):
            if predictor_config.use_gpu():
                # On 3080, the results of MatMul and Mul are different
                return True

            if predictor_config.tensorrt_engine_enabled():
                # On 3080, the results of MatMul and Mul are different
                return True

                x_shape = list(program_config.inputs["matmul_x"].shape)
                if len(x_shape) > 5:
                    return True
            return False

        self.add_ignore_check_case(
70 71 72 73
            teller1,
            IgnoreReasons.PASS_ACCURACY_ERROR,
            "The pass error on TRT while shape of mul_x > 5.",
        )
74 75 76 77

    def sample_program_config(self, draw):
        # 1. Generate shape and attr of matmul
        x_shape = draw(
78 79 80 81
            st.lists(
                st.integers(min_value=1, max_value=8), min_size=2, max_size=5
            )
        )
82
        y_shape = draw(
83 84 85 86
            st.lists(
                st.integers(min_value=1, max_value=8), min_size=2, max_size=2
            )
        )
87 88 89 90 91 92 93
        y_shape[0] = x_shape[-1]
        alpha = 1.0
        transpose_X = False
        transpose_Y = False

        matmul_op = OpConfig(
            "matmul",
94
            inputs={"X": ["matmul_x"], "Y": ["matmul_y"]},
95 96 97 98
            outputs={"Out": ["matmul_out"]},
            alpha=alpha,
            transpose_X=transpose_X,
            transpose_Y=transpose_Y,
99 100 101 102 103 104 105 106 107 108 109
        )

        ops = [
            matmul_op,
        ]
        weights = {
            "matmul_y": TensorConfig(shape=y_shape),
        }
        inputs = {
            "matmul_x": TensorConfig(shape=x_shape),
        }
110 111 112 113
        program_config = ProgramConfig(
            ops=ops,
            weights=weights,
            inputs=inputs,
114 115
            outputs=ops[-1].outputs["Out"],
        )
116 117 118
        return program_config

    def test(self):
119 120 121 122 123 124
        self.run_and_statis(
            quant=False,
            max_examples=100,
            passes=["gpu_cpu_map_matmul_to_mul_pass"],
            max_duration=180,
        )
125 126 127 128


if __name__ == "__main__":
    unittest.main()