hybrid_parallel_mp_random.py 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 63 64 65 66 67 68 69 70 71 72 73 74
# 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.

from __future__ import division
from __future__ import print_function

import unittest

import paddle
import numpy as np
import paddle.distributed as dist
import paddle.fluid as fluid
import paddle.distributed.fleet as fleet
import random


class TestDistTraning(unittest.TestCase):
    def setUp(self):
        strategy = fleet.DistributedStrategy()
        self.model_parallel_size = 2
        strategy.hybrid_configs = {
            "dp_degree": 1,
            "mp_degree": self.model_parallel_size,
            "pp_degree": 1
        }
        fleet.init(is_collective=True, strategy=strategy)

    def test_cuda_rng_tracker(self):
        seed_1 = 2021
        seed_2 = 1024

        size = [20, 15]

        paddle.seed(seed_1)
        target_11 = paddle.randn(size, "float32")
        target_12 = paddle.randn(size, "float32")

        paddle.seed(seed_2)
        target_21 = paddle.randn(size, "float32")
        target_22 = paddle.randn(size, "float32")

        paddle.seed(seed_1)

        fleet.meta_parallel.get_rng_state_tracker().add("test", seed_2)

        result_11 = paddle.randn(size, "float32")

        with fleet.meta_parallel.get_rng_state_tracker().rng_state("test"):
            result_21 = paddle.randn(size, "float32")

        result_12 = paddle.randn(size, "float32")

        with fleet.meta_parallel.get_rng_state_tracker().rng_state("test"):
            result_22 = paddle.randn(size, "float32")

        np.testing.assert_allclose(result_11.numpy(), target_11.numpy())
        np.testing.assert_allclose(result_12.numpy(), target_12.numpy())
        np.testing.assert_allclose(result_21.numpy(), target_21.numpy())
        np.testing.assert_allclose(result_22.numpy(), target_22.numpy())


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