test_split_var.py 1.9 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

T
typhoonzero 已提交
15 16 17 18
import math
import unittest
from paddle.v2.fluid.distribute_transpiler import split_dense_variable
import paddle.v2.fluid as fluid
T
typhoonzero 已提交
19
import paddle.v2.fluid.core as core
T
typhoonzero 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import random


class TestSplitVar(unittest.TestCase):
    def test_check_output(self):
        # split below shapes to 10 servers
        shapes = [[3, 5], [1024], [28, 784], [8, 1020], [800, 10]]
        expected_sizes = [
            [15], [1024],
            [2352, 2352, 2352, 2352, 2352, 2352, 2352, 2352, 2352, 784],
            [2040, 2040, 2040, 2040],
            [1150, 1150, 1150, 1150, 1150, 1150, 1100]
        ]
        var_list = []
        program = fluid.Program()
        for shape in shapes:
            var = program.global_block().create_var(
T
typhoonzero 已提交
37
                name=str(random.randint(10000, 99999)),
T
typhoonzero 已提交
38
                persistable=True,
T
typhoonzero 已提交
39
                # dtype=core.VarDesc.VarType.LOD_TENSOR,
T
typhoonzero 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
                shape=shape)
            var_list.append(var)
        blocks = split_dense_variable(var_list, 10)
        all_sizes = []
        for s in expected_sizes:
            for s2 in s:
                all_sizes.append(s2)
        for i, block_str in enumerate(blocks):
            varname, block_id, size = block_str.split(":")
            self.assertEqual(int(size), all_sizes[i])


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