test_multihead_matmul_fuse_pass_v3.py 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2022 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
16 17 18 19 20
from functools import partial

import numpy as np
from auto_scan_test import PassAutoScanTest
from program_config import OpConfig, ProgramConfig, TensorConfig
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35


class TestMultiheadMatmulFusePass(PassAutoScanTest):
    def sample_predictor_configs(self, program_config):
        # gpu
        config = self.create_inference_config(use_gpu=True)
        yield config, ["multihead_matmul", "mul"], (1e-2, 1e-3)

    def sample_program_config(self, draw):
        def generate_mul_input():
            return np.random.random([1, 128, 768]).astype(np.float32) - 0.5

        def generate_elewise_input():
            return np.random.random([1, 12, 128, 128]).astype(np.float32)

36 37 38
        def generate_weight(shape):
            return np.random.random(shape).astype(np.float32)

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        mul_0 = OpConfig(
            "mul",
            inputs={"X": ["mul_x"], "Y": ["mul_0_w"]},
            outputs={"Out": ["mul_0_out"]},
            x_num_col_dims=2,
            y_num_col_dims=1,
        )
        mul_1 = OpConfig(
            "mul",
            inputs={"X": ["mul_x"], "Y": ["mul_1_w"]},
            outputs={"Out": ["mul_1_out"]},
            x_num_col_dims=2,
            y_num_col_dims=1,
        )
        mul_2 = OpConfig(
            "mul",
            inputs={"X": ["mul_x"], "Y": ["mul_2_w"]},
            outputs={"Out": ["mul_2_out"]},
            x_num_col_dims=2,
            y_num_col_dims=1,
        )
        ele_0 = OpConfig(
            "elementwise_add",
            inputs={"X": [mul_0.outputs["Out"][0]], "Y": ["ele_0_w"]},
            outputs={"Out": ["ele_0_out"]},
            axis=-1,
        )
        ele_1 = OpConfig(
            "elementwise_add",
            inputs={"X": [mul_1.outputs["Out"][0]], "Y": ["ele_1_w"]},
            outputs={"Out": ["ele_1_out"]},
            axis=-1,
        )
        ele_2 = OpConfig(
            "elementwise_add",
            inputs={"X": [mul_2.outputs["Out"][0]], "Y": ["ele_2_w"]},
            outputs={"Out": ["ele_2_out"]},
            axis=-1,
        )
        reshape_0 = OpConfig(
            "reshape2",
            inputs={"X": [ele_0.outputs["Out"][0]]},
            outputs={"Out": ["reshape_0_out"], "XShape": ["reshape_0_Xout"]},
            shape=(1, 128, 12, 64),
        )
        reshape_1 = OpConfig(
            "reshape2",
            inputs={"X": [ele_1.outputs["Out"][0]]},
            outputs={"Out": ["reshape_1_out"], "XShape": ["reshape_1_Xout"]},
            shape=(1, 128, 12, 64),
        )
        reshape_2 = OpConfig(
            "reshape2",
            inputs={"X": [ele_2.outputs["Out"][0]]},
            outputs={"Out": ["reshape_2_out"], "XShape": ["reshape_2_Xout"]},
            shape=(1, 128, 12, 64),
        )
        transpose_0 = OpConfig(
            "transpose2",
            inputs={"X": [reshape_0.outputs["Out"][0]]},
            outputs={"Out": ["transpose_0_out"]},
            axis=(0, 2, 1, 3),
        )
        transpose_1 = OpConfig(
            "transpose2",
            inputs={"X": [reshape_1.outputs["Out"][0]]},
            outputs={"Out": ["transpose_1_out"]},
            axis=(0, 2, 3, 1),
        )
        transpose_2 = OpConfig(
            "transpose2",
            inputs={"X": [reshape_2.outputs["Out"][0]]},
            outputs={"Out": ["transpose_2_out"]},
            axis=(0, 2, 1, 3),
        )
        matmul_0 = OpConfig(
            "matmul",
            inputs={
                "X": [transpose_0.outputs["Out"][0]],
                "Y": [transpose_1.outputs["Out"][0]],
            },
            outputs={"Out": ["matmul_0_out"]},
            alpha=0.125,
            transpose_X=False,
            transpose_Y=False,
        )
        ele_3 = OpConfig(
            "elementwise_add",
            inputs={
                "X": [matmul_0.outputs["Out"][0]],
                "Y": ["eltadd_qk_b_var"],
            },
            outputs={"Out": ["ele_3_out"]},
            axis=-1,
        )
        softmax_op = OpConfig(
            "softmax",
            inputs={"X": [ele_3.outputs["Out"][0]]},
            outputs={"Out": ["softmax_out"]},
            axis=3,
            is_test=True,
        )
        matmul_1 = OpConfig(
            "matmul",
            inputs={
                "X": [softmax_op.outputs["Out"][0]],
                "Y": [transpose_2.outputs["Out"][0]],
            },
            outputs={"Out": ["matmul_1_out"]},
            alpha=1.0,
            transpose_X=False,
            transpose_Y=False,
        )
        transpose_3 = OpConfig(
            "transpose2",
            inputs={"X": [matmul_1.outputs["Out"][0]]},
            outputs={"Out": ["transpose_3_out"]},
            axis=(0, 2, 1, 3),
        )
        reshape_3 = OpConfig(
            "reshape2",
            inputs={"X": [transpose_3.outputs["Out"][0]]},
            outputs={"Out": ["reshape_3_out"], "XShape": ["reshape_3_Xout"]},
            shape=(1, 128, 768),
        )
        mul_3 = OpConfig(
            "mul",
            inputs={"X": [reshape_3.outputs["Out"][0]], "Y": ["mul_3_w"]},
            outputs={"Out": ["mul_3_out"]},
            x_num_col_dims=2,
            y_num_col_dims=1,
        )
171
        ops = [
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            mul_0,
            mul_1,
            mul_2,
            ele_0,
            ele_1,
            ele_2,
            reshape_0,
            reshape_1,
            reshape_2,
            transpose_0,
            transpose_1,
            transpose_2,
            matmul_0,
            ele_3,
            softmax_op,
            matmul_1,
            transpose_3,
            reshape_3,
            mul_3,
191 192 193 194
        ]
        program_config = ProgramConfig(
            ops=ops,
            inputs={
195 196 197 198
                "mul_x": TensorConfig(data_gen=partial(generate_mul_input)),
                "eltadd_qk_b_var": TensorConfig(
                    data_gen=partial(generate_elewise_input)
                ),
199 200
            },
            weights={
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
                "mul_0_w": TensorConfig(
                    data_gen=partial(generate_weight, [768, 768])
                ),
                "mul_1_w": TensorConfig(
                    data_gen=partial(generate_weight, [768, 768])
                ),
                "mul_2_w": TensorConfig(
                    data_gen=partial(generate_weight, [768, 768])
                ),
                "mul_3_w": TensorConfig(
                    data_gen=partial(generate_weight, [768, 768])
                ),
                "ele_0_w": TensorConfig(
                    data_gen=partial(generate_weight, [768])
                ),
                "ele_1_w": TensorConfig(
                    data_gen=partial(generate_weight, [768])
                ),
                "ele_2_w": TensorConfig(
                    data_gen=partial(generate_weight, [768])
                ),
222
            },
223 224
            outputs=[ops[-1].outputs["Out"][0]],
        )
225 226 227
        return program_config

    def test(self):
228 229 230 231 232 233
        self.run_and_statis(
            quant=False,
            max_examples=100,
            min_success_num=1,
            passes=["multihead_matmul_fuse_pass_v3"],
        )
234 235 236 237


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