test_tensor.py 6.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

17
import paddle.fluid.core as core
Y
Yu Yang 已提交
18 19 20 21
import unittest
import numpy


22
class TestTensor(unittest.TestCase):
23
    def test_int_tensor(self):
Y
Yu Yang 已提交
24
        scope = core.Scope()
D
dongzhihong 已提交
25
        var = scope.var("test_tensor")
Q
qijun 已提交
26
        place = core.CPUPlace()
Q
qijun 已提交
27

Y
Yu Yang 已提交
28 29
        tensor = var.get_tensor()

Y
yuyang18 已提交
30
        tensor._set_dims([1000, 784])
Y
yuyang18 已提交
31
        tensor._alloc_int(place)
Y
Yu Yang 已提交
32 33 34 35
        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1
        tensor_array[19, 11] = 2
Q
qijun 已提交
36
        tensor.set(tensor_array, place)
Y
Yu Yang 已提交
37 38

        tensor_array_2 = numpy.array(tensor)
39 40
        self.assertEqual(1, tensor_array_2[3, 9])
        self.assertEqual(2, tensor_array_2[19, 11])
Y
Yu Yang 已提交
41

42
    def test_float_tensor(self):
Y
Yu Yang 已提交
43
        scope = core.Scope()
D
dongzhihong 已提交
44
        var = scope.var("test_tensor")
Q
qijun 已提交
45
        place = core.CPUPlace()
Q
qijun 已提交
46

Y
Yu Yang 已提交
47 48
        tensor = var.get_tensor()

Y
yuyang18 已提交
49
        tensor._set_dims([1000, 784])
Y
yuyang18 已提交
50
        tensor._alloc_float(place)
Y
Yu Yang 已提交
51 52 53 54 55

        tensor_array = numpy.array(tensor)
        self.assertEqual((1000, 784), tensor_array.shape)
        tensor_array[3, 9] = 1.0
        tensor_array[19, 11] = 2.0
Q
qijun 已提交
56
        tensor.set(tensor_array, place)
Y
Yu Yang 已提交
57 58 59 60 61

        tensor_array_2 = numpy.array(tensor)
        self.assertAlmostEqual(1.0, tensor_array_2[3, 9])
        self.assertAlmostEqual(2.0, tensor_array_2[19, 11])

Q
qingqing01 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    def test_int8_tensor(self):
        scope = core.Scope()
        var = scope.var("int8_tensor")
        cpu_tensor = var.get_tensor()
        tensor_array = numpy.random.randint(
            -127, high=128, size=[100, 200], dtype=numpy.int8)
        place = core.CPUPlace()
        cpu_tensor.set(tensor_array, place)
        cpu_tensor_array_2 = numpy.array(cpu_tensor)
        self.assertAlmostEqual(cpu_tensor_array_2.all(), tensor_array.all())

        if core.is_compiled_with_cuda():
            cuda_tensor = var.get_tensor()
            tensor_array = numpy.random.randint(
                -127, high=128, size=[100, 200], dtype=numpy.int8)
            place = core.CUDAPlace(0)
            cuda_tensor.set(tensor_array, place)
            cuda_tensor_array_2 = numpy.array(cuda_tensor)
            self.assertAlmostEqual(cuda_tensor_array_2.all(),
                                   tensor_array.all())

83
    def test_int_lod_tensor(self):
84
        place = core.CPUPlace()
85
        scope = core.Scope()
D
dongzhihong 已提交
86
        var_lod = scope.var("test_lod_tensor")
87 88
        lod_tensor = var_lod.get_tensor()

Y
yuyang18 已提交
89
        lod_tensor._set_dims([4, 4, 6])
Y
yuyang18 已提交
90
        lod_tensor._alloc_int(place)
91 92 93 94
        array = numpy.array(lod_tensor)
        array[0, 0, 0] = 3
        array[3, 3, 5] = 10
        lod_tensor.set(array, place)
95
        lod_tensor.set_recursive_sequence_lengths([[2, 2]])
96 97 98 99

        lod_v = numpy.array(lod_tensor)
        self.assertTrue(numpy.alltrue(array == lod_v))

100 101
        lod = lod_tensor.recursive_sequence_lengths()
        self.assertEqual(2, lod[0][0])
102 103 104
        self.assertEqual(2, lod[0][1])

    def test_float_lod_tensor(self):
105
        place = core.CPUPlace()
106
        scope = core.Scope()
D
dongzhihong 已提交
107
        var_lod = scope.var("test_lod_tensor")
108 109

        lod_tensor = var_lod.get_tensor()
Y
yuyang18 已提交
110
        lod_tensor._set_dims([5, 2, 3, 4])
Y
yuyang18 已提交
111
        lod_tensor._alloc_float(place)
112 113 114

        tensor_array = numpy.array(lod_tensor)
        self.assertEqual((5, 2, 3, 4), tensor_array.shape)
115 116
        tensor_array[0, 0, 0, 0] = 1.0
        tensor_array[0, 0, 0, 1] = 2.0
117 118 119 120 121
        lod_tensor.set(tensor_array, place)

        lod_v = numpy.array(lod_tensor)
        self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0])
        self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1])
122
        self.assertEqual(len(lod_tensor.recursive_sequence_lengths()), 0)
123

124 125 126
        lod_py = [[2, 1], [1, 2, 2]]
        lod_tensor.set_recursive_sequence_lengths(lod_py)
        lod = lod_tensor.recursive_sequence_lengths()
127 128 129 130
        self.assertListEqual(lod_py, lod)

    def test_lod_tensor_init(self):
        place = core.CPUPlace()
131
        lod_py = [[2, 1], [1, 2, 2]]
D
dzhwinter 已提交
132
        lod_tensor = core.LoDTensor()
133

Y
yuyang18 已提交
134
        lod_tensor._set_dims([5, 2, 3, 4])
135
        lod_tensor.set_recursive_sequence_lengths(lod_py)
Y
yuyang18 已提交
136
        lod_tensor._alloc_float(place)
D
dzhwinter 已提交
137 138 139 140 141 142 143 144
        tensor_array = numpy.array(lod_tensor)
        tensor_array[0, 0, 0, 0] = 1.0
        tensor_array[0, 0, 0, 1] = 2.0
        lod_tensor.set(tensor_array, place)

        lod_v = numpy.array(lod_tensor)
        self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0])
        self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1])
145
        self.assertListEqual(lod_py, lod_tensor.recursive_sequence_lengths())
D
dzhwinter 已提交
146 147 148 149 150

    def test_lod_tensor_gpu_init(self):
        if not core.is_compiled_with_cuda():
            return
        place = core.CUDAPlace(0)
151
        lod_py = [[2, 1], [1, 2, 2]]
D
dzhwinter 已提交
152 153
        lod_tensor = core.LoDTensor()

Y
yuyang18 已提交
154
        lod_tensor._set_dims([5, 2, 3, 4])
155
        lod_tensor.set_recursive_sequence_lengths(lod_py)
Y
yuyang18 已提交
156
        lod_tensor._alloc_float(place)
157 158 159 160
        tensor_array = numpy.array(lod_tensor)
        tensor_array[0, 0, 0, 0] = 1.0
        tensor_array[0, 0, 0, 1] = 2.0
        lod_tensor.set(tensor_array, place)
161

162
        lod_v = numpy.array(lod_tensor)
163 164
        self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0])
        self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1])
165
        self.assertListEqual(lod_py, lod_tensor.recursive_sequence_lengths())
166

Q
Qiao Longfei 已提交
167 168 169 170 171 172 173
    def test_empty_tensor(self):
        place = core.CPUPlace()
        scope = core.Scope()
        var = scope.var("test_tensor")

        tensor = var.get_tensor()

Y
yuyang18 已提交
174
        tensor._set_dims([0, 1])
Y
yuyang18 已提交
175
        tensor._alloc_float(place)
Q
Qiao Longfei 已提交
176 177 178 179 180 181

        tensor_array = numpy.array(tensor)
        self.assertEqual((0, 1), tensor_array.shape)

        if core.is_compiled_with_cuda():
            gpu_place = core.CUDAPlace(0)
Y
yuyang18 已提交
182
            tensor._alloc_float(gpu_place)
Q
Qiao Longfei 已提交
183 184 185
            tensor_array = numpy.array(tensor)
            self.assertEqual((0, 1), tensor_array.shape)

Y
Yu Yang 已提交
186 187 188

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