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

from pass_test import PassTest
18

19 20 21 22 23 24
import paddle


class PrelnResidualBiasFusePassTest(PassTest):
    def setUp(self):
        paddle.enable_static()
25 26 27 28 29 30
        with paddle.static.program_guard(
            self.main_program, self.startup_program
        ):
            x = paddle.static.data(
                name="x", shape=[128, 768], dtype="float32", lod_level=0
            )
31
            bias = paddle.static.create_parameter(shape=[768], dtype='float32')
32 33 34
            y = paddle.static.data(
                name="y", shape=[128, 768], dtype="float32", lod_level=0
            )
35 36 37 38 39 40
            x = x + bias
            elementwise_out = x + y
            out = paddle.static.nn.layer_norm(input=elementwise_out)

        self.fetch_list = [out, elementwise_out]
        self.pass_names = "preln_residual_bias_fuse_pass"
W
Wang Bojun 已提交
41
        self.fused_op_type = "fused_bias_dropout_residual_layer_norm"
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        self.num_fused_ops = 1
        # self.graph_attrs = {
        #     "embedding_eltwise_layernorm_fuse_pass_flag": True,
        #     "multihead_matmul_fuse_pass_flag": True
        # }

    def test_check_program(self):
        use_gpu_set = [False]
        if paddle.device.is_compiled_with_cuda():
            use_gpu_set.append(True)
        for use_gpu in use_gpu_set:
            place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
            opt_program = self._apply_ir_passes()
            self.check_program(opt_program)


W
wenbin 已提交
58 59 60
class PrelnResidualBiasFusePassNoBiasTest(PassTest):
    def setUp(self):
        paddle.enable_static()
61 62 63 64 65 66 67 68 69
        with paddle.static.program_guard(
            self.main_program, self.startup_program
        ):
            x = paddle.static.data(
                name="x", shape=[128, 768], dtype="float32", lod_level=0
            )
            y = paddle.static.data(
                name="y", shape=[128, 768], dtype="float32", lod_level=0
            )
W
wenbin 已提交
70 71 72 73 74
            elementwise_out = x + y
            out = paddle.static.nn.layer_norm(input=elementwise_out)

        self.fetch_list = [out, elementwise_out]
        self.pass_names = "preln_residual_bias_fuse_pass"
W
Wang Bojun 已提交
75
        self.fused_op_type = "fused_bias_dropout_residual_layer_norm"
W
wenbin 已提交
76 77 78 79 80 81 82 83 84 85 86 87
        self.num_fused_ops = 1

    def test_check_program(self):
        use_gpu_set = [False]
        if paddle.device.is_compiled_with_cuda():
            use_gpu_set.append(True)
        for use_gpu in use_gpu_set:
            place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
            opt_program = self._apply_ir_passes()
            self.check_program(opt_program)


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