test_xpu_reduce_ops_fuse_pass.py 3.3 KB
Newer Older
W
wz1qqx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Copyright (c) 2023 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
import numpy as np
from auto_scan_test import PassAutoScanTest
from program_config import OpConfig, ProgramConfig, TensorConfig


W
wz1qqx 已提交
23
class TestReduceMaxFusePass(PassAutoScanTest):
W
wz1qqx 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    def sample_predictor_configs(self, program_config):
        config = self.create_inference_config(use_xpu=True)
        yield config, ["reduce_max"], (1e-3, 1e-3)

    def sample_program_config(self, draw):
        s_axes = [2]
        batch_size = draw(st.integers(min_value=1, max_value=4))
        H = draw(st.integers(min_value=1, max_value=64))
        W = draw(st.integers(min_value=1, max_value=64))
        in_shape = [batch_size, H, W]

        transpose_op1 = OpConfig(
            type='transpose2',
            inputs={
                "X": ["transpose_in"],
            },
            outputs={"Out": ["transpose_out1"]},
            attrs={"axis": [0, 2, 1]},
        )
        unsqueeze2_op = OpConfig(
            type="unsqueeze2",
            inputs={"X": ["transpose_out1"]},
            outputs={"Out": ["unsqueeze_out"]},
            attrs={
                "axes": s_axes,
            },
        )
        pool_op = OpConfig(
            "pool2d",
            inputs={"X": ["unsqueeze_out"]},
            outputs={"Out": ["pool_out"]},
            ksize=[1, H],
            adaptive=False,
            pooling_type="max",
            data_format="NCHW",
            strides=[1, H],
            paddings=[0, 0],
            ceil_mode=False,
            global_pooling=False,
            padding_algorithm="EXPLICIT",
            exclusive=True,
        )
        squeeze2_op = OpConfig(
            "squeeze2",
            inputs={
                "X": ["pool_out"],
            },
            axes=s_axes,
            outputs={"Out": ["squeeze2_out"], "XShape": ["xshape"]},
        )
        transpose_op2 = OpConfig(
            type='transpose2',
            inputs={
                "X": ["squeeze2_out"],
            },
            outputs={"Out": ["transpose_out2"]},
            attrs={"axis": [0, 2, 1]},
        )
        ops = [
            transpose_op1,
            unsqueeze2_op,
            pool_op,
            squeeze2_op,
            transpose_op2,
        ]

        program_config = ProgramConfig(
            ops=ops,
            weights={},
            inputs={
                "transpose_in": TensorConfig(shape=in_shape),
            },
            outputs=ops[-1].outputs["Out"],
        )
        return program_config

    def test(self):
        self.run_and_statis(
            quant=False,
            max_examples=25,
W
wz1qqx 已提交
104
            passes=["reduce_ops_fuse_pass"],
W
wz1qqx 已提交
105 106 107 108 109 110
        )


if __name__ == "__main__":
    np.random.seed(200)
    unittest.main()