test_fleet_rolemaker.py 4.8 KB
Newer Older
T
tangwei12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2018 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.
X
xujiaqi01 已提交
14
"""Test cloud role maker."""
T
tangwei12 已提交
15 16 17

import os
import unittest
18

19
import paddle
T
tangwei12 已提交
20 21 22 23
import paddle.fluid.incubate.fleet.base.role_maker as role_maker


class TestCloudRoleMaker(unittest.TestCase):
X
xujiaqi01 已提交
24 25 26 27
    """
    Test cases for PaddleCloudRoleMaker.
    """

T
tangwei12 已提交
28
    def setUp(self):
X
xujiaqi01 已提交
29
        """Set up, set envs."""
T
tangwei12 已提交
30
        os.environ["PADDLE_TRAINERS_NUM"] = "2"
C
Chengmo 已提交
31
        os.environ[
32 33
            "PADDLE_PSERVERS_IP_PORT_LIST"
        ] = "127.0.0.1:36001,127.0.0.2:36001"
T
tangwei12 已提交
34 35

    def test_tr_rolemaker(self):
X
xujiaqi01 已提交
36
        """Test tr rolenamer."""
T
tangwei12 已提交
37 38 39 40 41 42 43 44 45 46
        os.environ["TRAINING_ROLE"] = "TRAINER"
        os.environ["PADDLE_TRAINER_ID"] = "0"

        ro = role_maker.PaddleCloudRoleMaker(is_collective=False)
        ro.generate_role()
        self.assertTrue(ro.is_worker())
        self.assertFalse(ro.is_server())
        self.assertEqual(ro.worker_num(), 2)

    def test_ps_rolemaker(self):
X
xujiaqi01 已提交
47
        """Test ps rolemaker."""
T
tangwei12 已提交
48 49
        os.environ["TRAINING_ROLE"] = "PSERVER"
        os.environ["POD_IP"] = "127.0.0.1"
C
Chengmo 已提交
50
        os.environ["PADDLE_PORT"] = "36001"
T
tangwei12 已提交
51 52 53 54 55 56
        ro = role_maker.PaddleCloudRoleMaker(is_collective=False)
        ro.generate_role()
        self.assertFalse(ro.is_worker())
        self.assertTrue(ro.is_server())
        self.assertEqual(ro.worker_num(), 2)

57
    def test_training_role(self):
X
xujiaqi01 已提交
58
        """Test training role."""
T
tangwei12 已提交
59 60 61 62
        os.environ["TRAINING_ROLE"] = "TEST"
        ro = role_maker.PaddleCloudRoleMaker(is_collective=False)
        self.assertRaises(ValueError, ro.generate_role)

X
xujiaqi01 已提交
63 64 65 66
    def test_pslib_1(self):
        """Test cases for pslib."""
        import paddle.fluid as fluid
        from paddle.fluid.incubate.fleet.base.role_maker import GeneralRoleMaker
67
        from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
1
123malin 已提交
68

X
xujiaqi01 已提交
69 70 71 72 73 74 75
        os.environ["POD_IP"] = "127.0.0.1"
        os.environ["PADDLE_PORT"] = "36001"
        os.environ["TRAINING_ROLE"] = "TRAINER"
        os.environ["PADDLE_TRAINER_ENDPOINTS"] = "127.0.0.1:36001"
        os.environ["PADDLE_PSERVERS_IP_PORT_LIST"] = "127.0.0.1:36002"
        os.environ["PADDLE_TRAINER_ID"] = "0"
        role_maker = GeneralRoleMaker()
76 77
        # print("init rolemaker")
        # role_maker.generate_role()
X
xujiaqi01 已提交
78 79
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
80
        # fleet.init(role_maker)
X
xujiaqi01 已提交
81 82 83 84
        train_program = fluid.Program()
        startup_program = fluid.Program()
        scope = fluid.Scope()
        with fluid.program_guard(train_program, startup_program):
85 86 87 88 89 90 91
            show = fluid.layers.data(
                name="show",
                shape=[-1, 1],
                dtype="float32",
                lod_level=1,
                append_batch_size=False,
            )
C
Charles-hit 已提交
92
            fc = paddle.static.nn.fc(x=show, size=1, activation=None)
93 94 95 96 97 98 99
            label = fluid.layers.data(
                name="click",
                shape=[-1, 1],
                dtype="int64",
                lod_level=1,
                append_batch_size=False,
            )
X
xujiaqi01 已提交
100
            label_cast = fluid.layers.cast(label, dtype='float32')
101
            cost = paddle.nn.functional.log_loss(fc, label_cast)
X
xujiaqi01 已提交
102 103 104 105 106 107 108 109
        try:
            adam = fluid.optimizer.Adam(learning_rate=0.000005)
            adam = fleet.distributed_optimizer(adam)
            adam.minimize([cost], [scope])
            fleet.run_server()
        except:
            print("do not support pslib test, skip")
            return
X
xujiaqi01 已提交
110
        fleet.clear_one_table(0)
111 112 113 114
        from paddle.fluid.incubate.fleet.base.role_maker import (
            MPISymetricRoleMaker,
        )

X
xujiaqi01 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        try:
            role = MPISymetricRoleMaker()
            role._all_reduce([1], [2])
        except:
            print("catch expected error of not inited")
        try:
            role = MPISymetricRoleMaker()
            role._all_reduce([1], [2], "min")
        except:
            print("catch expected error of not inited")
        try:
            role = MPISymetricRoleMaker()
            role._all_reduce([1], [2], "max")
        except:
            print("catch expected error of not inited")
        try:
            role = MPISymetricRoleMaker()
            role._all_reduce([1], [2], "unknown")
        except:
            print("catch expected error of unknown type")

T
tangwei12 已提交
136 137 138

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