sharding_pass_unittest.py 3.7 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 random
16 17
import unittest

18
import numpy as np
19
from get_gpt_model import FakeDataset, generate_model
20

21
import paddle
22
from paddle.distributed.fleet import auto
23 24 25 26 27 28 29 30 31 32 33

paddle.enable_static()


def apply_pass(use_sharding=False, stage=None):
    strategy = auto.Strategy()
    strategy.auto_mode = "semi"
    strategy.reinit = True
    if use_sharding:
        sharding = strategy.sharding
        sharding.enable = True
Z
zhaoyingli 已提交
34
        sharding.degree = 2
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        sharding.stage = 1

    return strategy


def reset_prog():
    paddle.fluid.framework.switch_main_program(paddle.static.Program())
    paddle.fluid.framework.switch_startup_program(paddle.static.Program())


class TestShardingPass(unittest.TestCase):
    def setUp(self):
        self.rtol = 1e-6
        self.atol = 1e-8
        self.batch_size = 2
        self.batch_num = 10
        self.clip_norm = 0.2
        self.dataset = FakeDataset(self.batch_size * self.batch_num)

    def init(self, engine):
        paddle.seed(2022)
        np.random.seed(2022)
        random.seed(2022)
58
        place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        engine._executor = paddle.static.Executor(place)

    def get_engine(self, use_sharding=False, stage=None):
        reset_prog()

        strategy = apply_pass(use_sharding, stage)
        clip = paddle.nn.ClipGradByGlobalNorm(self.clip_norm)
        opt = paddle.optimizer.AdamW(learning_rate=0.00001, grad_clip=clip)
        model, loss = generate_model("dp")

        engine = auto.Engine(model, loss, opt, strategy=strategy)
        self.init(engine)
        return engine

    def check_results(self, ref_losses, check_losses):
        np.testing.assert_allclose(
            ref_losses,
            check_losses,
            rtol=self.rtol,
            atol=self.atol,
            err_msg='pass {} has wrong results!, \nu={}\nv={}\ndiff={}'.format(
80 81 82
                __class__, ref_losses, check_losses, ref_losses - check_losses
            ),
        )
83 84 85 86

    def test_sharding_pass(self):
        # dp2 training
        dp_engine = self.get_engine()
Z
zhaoyingli 已提交
87 88
        history = dp_engine.fit(self.dataset, 3, batch_size=self.batch_size)
        dp_losses = np.array(history.history["loss"])
89 90 91

        # sharding2 stage1 training
        sharding1_engine = self.get_engine(True, 1)
92 93 94
        history = sharding1_engine.fit(
            self.dataset, 3, batch_size=self.batch_size
        )
Z
zhaoyingli 已提交
95
        sharding1_losses = np.array(history.history["loss"])
96 97 98 99
        self.check_results(dp_losses, sharding1_losses)

        # sharding2 stage2 training
        sharding2_engine = self.get_engine(True, 2)
100 101 102
        history = sharding2_engine.fit(
            self.dataset, 3, batch_size=self.batch_size
        )
Z
zhaoyingli 已提交
103
        sharding2_losses = np.array(history.history["loss"])
104 105 106 107
        self.check_results(dp_losses, sharding2_losses)

        # sharding2 stage3 training
        sharding3_engine = self.get_engine(True, 3)
108 109 110
        history = sharding3_engine.fit(
            self.dataset, 3, batch_size=self.batch_size
        )
Z
zhaoyingli 已提交
111
        sharding3_losses = np.array(history.history["loss"])
112 113 114 115 116
        self.check_results(dp_losses, sharding3_losses)


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