test_auto_parallel_data_parallel_optimization_pass.py 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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 random
16 17 18
import sys
import unittest

19
import numpy as np
20
from auto_parallel_pass_test_base import AutoPallelPassTestBase
21 22 23

import paddle
import paddle.distributed.fleet as fleet
24 25 26
from paddle.distributed.auto_parallel.dist_context import (
    get_default_distributed_context,
)
27 28 29
from paddle.distributed.auto_parallel.operators.common import (
    is_data_parallel_reduce_op,
)
30
from paddle.distributed.passes import PassContext, new_pass
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

sys.path.append("..")


class TestDataParallelPassWithScale1(AutoPallelPassTestBase):
    def init(self):
        if paddle.is_compiled_with_cuda():
            paddle.set_flags({'FLAGS_cudnn_deterministic': 1})
        self.rtol = 1e-5
        self.atol = 1e-8
        # NOTE a hack to compare pass apply or not, since there is no
        # setting of this pass in dist_strategy
        self._apply_pass = False

        rank = paddle.distributed.get_rank()
        paddle.seed(rank + 2021)
        random.seed(rank + 2021)
        np.random.seed(rank + 2021)

    def apply_passes(self):
        dist_strategy = fleet.DistributedStrategy()
        dist_strategy.semi_auto = True
        fleet.init(is_collective=True, strategy=dist_strategy)
        self._apply_pass = True

    def apply_no_passes(self):
        dist_strategy = fleet.DistributedStrategy()
        dist_strategy.semi_auto = True
        fleet.init(is_collective=True, strategy=dist_strategy)
        self._apply_pass = False

    def test_bs_8(self):
63 64 65
        self.check_main(
            gpus=[0, 1], batch_size=8, sequence_len=512, vocab_size=1000
        )
66 67 68 69

    # test scaling with fillconstant
    def get_model(self, place, batch_size, sequence_len, vocab_size):

70 71 72 73 74 75 76 77 78
        (
            dist_main_prog,
            dist_startup_prog,
            data_holder,
            [loss],
            gen_data,
        ) = self.get_gpt_model(
            'dp', place, batch_size, sequence_len, vocab_size
        )
79 80 81 82
        if self._apply_pass:
            config = {}
            config["dist_context"] = get_default_distributed_context()
            config["global_rank"] = paddle.distributed.get_rank()
83 84 85
            dp_pass = new_pass(
                "auto_parallel_data_parallel_optimization", config
            )
86 87 88 89 90 91 92 93 94 95
            dp_pass.apply([dist_main_prog], [dist_startup_prog], PassContext())

        return dist_main_prog, dist_startup_prog, data_holder, [loss], gen_data


class TestDataParallelPassWithScale2(TestDataParallelPassWithScale1):

    # test scaling with optimizer rescale_grad
    def get_model(self, place, batch_size, sequence_len, vocab_size):

96 97 98 99 100 101 102 103 104 105 106 107 108 109
        (
            dist_main_prog,
            dist_startup_prog,
            data_holder,
            [loss],
            gen_data,
        ) = self.get_gpt_model(
            'dp',
            place,
            batch_size,
            sequence_len,
            vocab_size,
            optimizer='LarsMomentum',
        )
110 111 112 113
        if self._apply_pass:
            config = {}
            config["dist_context"] = get_default_distributed_context()
            config["global_rank"] = paddle.distributed.get_rank()
114 115 116
            dp_pass = new_pass(
                "auto_parallel_data_parallel_optimization", config
            )
117 118 119 120 121
            dp_pass.apply([dist_main_prog], [dist_startup_prog], PassContext())

        return dist_main_prog, dist_startup_prog, data_holder, [loss], gen_data


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 171 172 173 174 175 176 177 178
class TestDataParallelPassWithStandaloneEXE(TestDataParallelPassWithScale1):
    def init(self):
        if paddle.is_compiled_with_cuda():
            paddle.set_flags({'FLAGS_cudnn_deterministic': 1})
        self.rtol = 1e-5
        self.atol = 1e-8
        # NOTE a hack to compare pass apply or not, since there is no
        # setting of this pass in dist_strategy
        self._apply_pass = False

        rank = paddle.distributed.get_rank()
        paddle.seed(rank + 2021)
        random.seed(rank + 2021)
        np.random.seed(rank + 2021)

    # test scaling with optimizer rescale_grad
    def get_model(self, place, batch_size, sequence_len, vocab_size):

        (
            dist_main_prog,
            dist_startup_prog,
            data_holder,
            [loss],
            gen_data,
        ) = self.get_gpt_model(
            'dp',
            place,
            batch_size,
            sequence_len,
            vocab_size,
            optimizer='LarsMomentum',
        )
        if self._apply_pass:
            config = {}
            config["dist_context"] = get_default_distributed_context()
            config["global_rank"] = paddle.distributed.get_rank()
            dp_pass = new_pass(
                "auto_parallel_data_parallel_optimization", config
            )
            dp_pass.apply([dist_main_prog], [dist_startup_prog], PassContext())

            ops = dist_main_prog.global_block().ops
            allreduce_op_idx = -1
            for idx in range(len(ops)):
                if is_data_parallel_reduce_op(ops[idx]):
                    allreduce_op_idx = idx
                    break
            assert allreduce_op_idx > 0
            allreduce_op = ops[allreduce_op_idx]
            assert allreduce_op.attr('use_calc_stream') is True
            assert allreduce_op.dist_attr.execution_stream is not None
            assert ops[allreduce_op_idx - 1].type == "nop"
            assert ops[allreduce_op_idx + 1].type == "nop"

        return dist_main_prog, dist_startup_prog, data_holder, [loss], gen_data


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