test_ir_skip_layernorm_pass.py 1.9 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
import paddle
20 21
from paddle import fluid
from paddle.fluid import core
22 23 24 25


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

        self.fetch_list = [out]
        self.pass_names = "skip_layernorm_fuse_pass"
        self.fused_op_type = "skip_layernorm"
        self.num_fused_ops = 1
41 42
        self.graph_attrs = {
            "embedding_eltwise_layernorm_fuse_pass_flag": True,
43
            "multihead_matmul_fuse_pass_flag": True,
44
        }
45 46 47 48 49 50 51 52 53 54 55 56 57

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


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