test_variable.py 2.4 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 19
from paddle.fluid.framework import default_main_program, Program, convert_np_dtype_to_dtype_
import paddle.fluid.core as core
Y
Yu Yang 已提交
20 21 22 23 24
import numpy as np


class TestVariable(unittest.TestCase):
    def test_np_dtype_convert(self):
25
        DT = core.VarDesc.VarType
26
        convert = convert_np_dtype_to_dtype_
Y
Yu Yang 已提交
27 28 29 30 31 32 33
        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 已提交
34 35
        self.assertEqual(DT.INT8, convert("int8"))
        self.assertEqual(DT.UINT8, convert("uint8"))
Y
Yu Yang 已提交
36

Y
Yu Yang 已提交
37
    def test_var(self):
Y
Yu Yang 已提交
38
        b = default_main_program().current_block()
Y
Yu Yang 已提交
39 40
        w = b.create_var(
            dtype="float64", shape=[784, 100], lod_level=0, name="fc.w")
41
        self.assertNotEqual(str(w), "")
42
        self.assertEqual(core.VarDesc.VarType.FP64, w.dtype)
Y
Yu Yang 已提交
43 44 45 46 47
        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')
48
        self.assertEqual(core.VarDesc.VarType.FP64, w.dtype)
Y
Yu Yang 已提交
49 50 51 52 53 54 55
        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 已提交
56 57 58 59 60 61 62
    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)

Y
Yu Yang 已提交
63 64 65

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