test_variable.py 5.7 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

Y
Yu Yang 已提交
17
import unittest
18
from paddle.fluid.framework import default_main_program, Program, convert_np_dtype_to_dtype_
W
wopeizl 已提交
19
import paddle.fluid as fluid
20
import paddle.fluid.core as core
Y
Yu Yang 已提交
21 22 23 24 25
import numpy as np


class TestVariable(unittest.TestCase):
    def test_np_dtype_convert(self):
26
        DT = core.VarDesc.VarType
27
        convert = convert_np_dtype_to_dtype_
Y
Yu Yang 已提交
28 29 30 31 32 33 34
        self.assertEqual(DT.FP32, convert(np.float32))
        self.assertEqual(DT.FP16, convert("float16"))
        self.assertEqual(DT.FP64, convert("float64"))
        self.assertEqual(DT.INT32, convert("int32"))
        self.assertEqual(DT.INT16, convert("int16"))
        self.assertEqual(DT.INT64, convert("int64"))
        self.assertEqual(DT.BOOL, convert("bool"))
Q
qingqing01 已提交
35 36
        self.assertEqual(DT.INT8, convert("int8"))
        self.assertEqual(DT.UINT8, convert("uint8"))
Y
Yu Yang 已提交
37

Y
Yu Yang 已提交
38
    def test_var(self):
Y
Yu Yang 已提交
39
        b = default_main_program().current_block()
Y
Yu Yang 已提交
40 41
        w = b.create_var(
            dtype="float64", shape=[784, 100], lod_level=0, name="fc.w")
42
        self.assertNotEqual(str(w), "")
43
        self.assertEqual(core.VarDesc.VarType.FP64, w.dtype)
Y
Yu Yang 已提交
44 45 46 47 48
        self.assertEqual((784, 100), w.shape)
        self.assertEqual("fc.w", w.name)
        self.assertEqual(0, w.lod_level)

        w = b.create_var(name='fc.w')
49
        self.assertEqual(core.VarDesc.VarType.FP64, w.dtype)
Y
Yu Yang 已提交
50 51 52 53 54 55 56
        self.assertEqual((784, 100), w.shape)
        self.assertEqual("fc.w", w.name)
        self.assertEqual(0, w.lod_level)

        self.assertRaises(ValueError,
                          lambda: b.create_var(name="fc.w", shape=(24, 100)))

Y
Yu Yang 已提交
57 58 59 60 61 62 63
    def test_step_scopes(self):
        prog = Program()
        b = prog.current_block()
        var = b.create_var(
            name='step_scopes', type=core.VarDesc.VarType.STEP_SCOPES)
        self.assertEqual(core.VarDesc.VarType.STEP_SCOPES, var.type)

W
wopeizl 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    def _test_slice(self):
        b = default_main_program().current_block()
        w = b.create_var(dtype="float64", shape=[784, 100, 100], lod_level=0)

        for i in range(3):
            nw = w[i]
            self.assertEqual((1, 100, 100), nw.shape)

        nw = w[:]
        self.assertEqual((784, 100, 100), nw.shape)

        nw = w[:, :, ...]
        self.assertEqual((784, 100, 100), nw.shape)

        nw = w[::2, ::2, :]
        self.assertEqual((392, 50, 100), nw.shape)

        nw = w[::-2, ::-2, :]
        self.assertEqual((392, 50, 100), nw.shape)

        self.assertEqual(0, nw.lod_level)

        place = fluid.CPUPlace()
        main = fluid.Program()
        with fluid.program_guard(main):
            exe = fluid.Executor(place)
            tensor_array = np.array(
                [[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                 [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                 [[19, 20, 21], [22, 23, 24], [25, 26, 27]]]).astype('float32')
            var = fluid.layers.assign(tensor_array)
            var1 = var[0, 1, 1]
            var2 = var[1:]
            var3 = var[0:1]
            var4 = var[..., ]
            var5 = var[2::-2]
            var6 = var[1, 1:, 1:]
            var7 = var[1, ..., 1:]
            var8 = var[1, ...]
            local_out = exe.run(main,
                                fetch_list=[
                                    var, var1, var2, var3, var4, var5, var6,
                                    var7, var8
                                ])

            self.assertTrue((np.array(local_out[1]) == np.array(tensor_array[
                0, 1, 1])).all())
            self.assertTrue((np.array(local_out[2]) == np.array(tensor_array[
                1:])).all())
            self.assertTrue((np.array(local_out[3]) == np.array(tensor_array[
                0:1])).all())
            self.assertTrue((np.array(local_out[4]) == np.array(
                tensor_array[..., ])).all())
            self.assertTrue((np.array(local_out[5]) == np.array(tensor_array[
                2::-2])).all())
            self.assertTrue((np.array(local_out[6]) == np.array(tensor_array[
                1, 1:, 1:])).all())
            self.assertTrue((np.array(local_out[7]) == np.array(tensor_array[
                1, ..., 1:])).all())
            self.assertTrue((np.array(local_out[8]) == np.array(tensor_array[
                1, ...])).all())

    def test_slice(self):
        self._test_slice()


class TestVariableImperative(unittest.TestCase):
    def _test_slice(self):
        b = default_main_program().current_block()
        w = b.create_var(dtype="float64", shape=[784, 100, 100], lod_level=0)

        for i in range(3):
            nw = w[i]
            self.assertEqual([1, 100, 100], nw.shape)

        nw = w[:]
        self.assertEqual([784, 100, 100], nw.shape)

        nw = w[:, :, :]
        self.assertEqual([784, 100, 100], nw.shape)

        nw = w[::2, ::2, :]
        self.assertEqual([392, 50, 100], nw.shape)

        nw = w[::-2, ::-2, :]
        self.assertEqual([392, 50, 100], nw.shape)

        nw = w[0::-2, 0::-2, :]
        self.assertEqual([1, 1, 100], nw.shape)

    def test_slice(self):
L
lujun 已提交
155
        with fluid.dygraph.guard():
W
wopeizl 已提交
156 157
            self._test_slice()

Y
Yu Yang 已提交
158 159 160

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