test_fleet_ps.py 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   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
from paddle.fluid.framework import default_main_program
17 18 19
from paddle.fluid.incubate.fleet.parameter_server.ir.pserver_pass import (
    _get_optimizer_input_shape,
)
20

21 22 23 24 25 26
main_program = default_main_program()


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

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        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
48 49 50
            new_shape = _get_optimizer_input_shape(
                op_type, varkey, orig_shape, param_shape
            )
51 52 53 54 55 56 57
            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
58 59 60
            new_shape = _get_optimizer_input_shape(
                op_type, varkey, orig_shape, param_shape
            )
61 62 63 64 65 66 67 68
            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
69 70 71
                _get_optimizer_input_shape(
                    op_type, varkey, orig_shape, param_shape
                )
72 73 74 75


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