test_slice_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.

15 16
from __future__ import print_function

T
typhoonzero 已提交
17 18
import math
import unittest
19
from paddle.fluid.transpiler.distribute_transpiler import slice_variable
20 21
import paddle.fluid as fluid
import paddle.fluid.core as core
T
typhoonzero 已提交
22 23 24
import random


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

44 45 46 47 48 49 50 51 52
    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]
        ]

53
        self.check_slice_output(shapes, expected_sizes, 1024)
54 55 56 57 58 59 60

    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]]

61
        self.check_slice_output(shapes, expected_sizes, 8192)
62

T
typhoonzero 已提交
63 64 65

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