test_fleet_ps.py 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   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.

import unittest
16

17
from paddle.fluid.framework import default_main_program
18
from paddle.incubate.distributed.fleet.parameter_server.ir.pserver_pass import (
19 20
    _get_optimizer_input_shape,
)
21

22 23 24 25 26
main_program = default_main_program()


class TestFleetPS(unittest.TestCase):
    def test_version(self):
27
        from paddle.incubate.distributed.fleet.parameter_server import version
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        transpiler = version.is_transpiler()
        self.assertEqual(transpiler, True)

    def test_optimizer_shape(self):
        optimizers = []
        optimizers.append(("adam", "Moment1", [100, 1], [50, 1]))
        optimizers.append(("adam", "Moment2", [100, 1], [50, 1]))
        optimizers.append(("adagrad", "Moment", [100, 1], [50, 1]))
        optimizers.append(("adamax", "Moment", [100, 1], [50, 1]))
        optimizers.append(("adamax", "InfNorm", [100, 1], [50, 1]))
        optimizers.append(("momentum", "Velocity", [100, 1], [50, 1]))
        optimizers.append(("lars_momentum", "Velocity", [100, 1], [50, 1]))
        optimizers.append(("decayed_adagrad", "Moment", [100, 1], [50, 1]))
        optimizers.append(("rmsprop", "Moment", [100, 1], [50, 1]))
        optimizers.append(("rmsprop", "MeanSquare", [100, 1], [50, 1]))
        optimizers.append(("ftrl", "SquaredAccumulator", [100, 1], [50, 1]))
        optimizers.append(("ftrl", "LinearAccumulator", [100, 1], [50, 1]))

        for attrs in optimizers:
            op_type, varkey, orig_shape, param_shape = attrs
49 50 51
            new_shape = _get_optimizer_input_shape(
                op_type, varkey, orig_shape, param_shape
            )
52 53 54 55 56 57 58
            self.assertListEqual(new_shape, param_shape)

        optimizers = []
        optimizers.append(("sgd", "", [100, 1], [50, 1]))

        for attrs in optimizers:
            op_type, varkey, orig_shape, param_shape = attrs
59 60 61
            new_shape = _get_optimizer_input_shape(
                op_type, varkey, orig_shape, param_shape
            )
62 63 64 65 66 67 68 69
            self.assertListEqual(new_shape, orig_shape)

        with self.assertRaises(ValueError):
            optimizers = []
            optimizers.append(("new_opti", "", [100, 1], [50, 1]))

            for attrs in optimizers:
                op_type, varkey, orig_shape, param_shape = attrs
70 71 72
                _get_optimizer_input_shape(
                    op_type, varkey, orig_shape, param_shape
                )
73 74 75 76


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