test_split_var.py 2.3 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
import math
import unittest
17
from paddle.fluid.transpiler.distribute_transpiler import split_variable
18 19
import paddle.fluid as fluid
import paddle.fluid.core as core
T
typhoonzero 已提交
20 21 22 23
import random


class TestSplitVar(unittest.TestCase):
24
    def check_split_output(self, shapes, expected_sizes, min_size):
T
typhoonzero 已提交
25 26 27 28
        var_list = []
        program = fluid.Program()
        for shape in shapes:
            var = program.global_block().create_var(
T
typhoonzero 已提交
29
                name=str(random.randint(10000, 99999)),
T
typhoonzero 已提交
30
                persistable=True,
T
typhoonzero 已提交
31
                # dtype=core.VarDesc.VarType.LOD_TENSOR,
T
typhoonzero 已提交
32 33
                shape=shape)
            var_list.append(var)
34
        blocks = split_variable(var_list, 10, min_size)
T
typhoonzero 已提交
35 36 37 38 39 40 41 42
        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])

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    def test_1k(self):
        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]
        ]

        self.check_split_output(shapes, expected_sizes, 1024)

    def test_check_output_8k(self):
        shapes = [[3, 5], [1024], [28, 784], [8, 1020], [800, 10],
                  [6, 33, 33, 33]]
        expected_sizes = [[15], [1024], [10976, 10976], [8160], [8000],
                          [35937, 35937, 35937, 35937, 35937, 35937]]

        self.check_split_output(shapes, expected_sizes, 8192)

T
typhoonzero 已提交
62 63 64

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