test_expand_op.py 5.8 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.

Y
yangyaming 已提交
15 16
import unittest
import numpy as np
17
from op_test import OpTest
L
liym27 已提交
18
import paddle.fluid as fluid
Y
yangyaming 已提交
19 20


L
liym27 已提交
21
# Situation 1: expand_times is a list(without tensor)
Y
yangyaming 已提交
22 23 24
class TestExpandOpRank1(OpTest):
    def setUp(self):
        self.op_type = "expand"
L
liym27 已提交
25
        self.init_data()
26 27 28
        self.dtype = (
            "float32" if fluid.core.is_compiled_with_rocm() else "float64"
        )
L
liym27 已提交
29

R
ronnywang 已提交
30
        self.inputs = {'X': np.random.random(self.ori_shape).astype(self.dtype)}
L
liym27 已提交
31 32
        self.attrs = {'expand_times': self.expand_times}
        output = np.tile(self.inputs['X'], self.expand_times)
Y
yangyaming 已提交
33 34
        self.outputs = {'Out': output}

L
liym27 已提交
35
    def init_data(self):
36
        self.ori_shape = [100]
L
liym27 已提交
37 38
        self.expand_times = [2]

Y
yangyaming 已提交
39 40 41 42 43 44 45
    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


L
liym27 已提交
46 47
class TestExpandOpRank2_Corner(TestExpandOpRank1):
    def init_data(self):
Z
zhupengyang 已提交
48
        self.ori_shape = [120]
L
liym27 已提交
49
        self.expand_times = [2]
50 51


L
liym27 已提交
52 53 54 55
class TestExpandOpRank2(TestExpandOpRank1):
    def init_data(self):
        self.ori_shape = [12, 14]
        self.expand_times = [2, 3]
56 57


L
liym27 已提交
58 59
class TestExpandOpRank3_Corner(TestExpandOpRank1):
    def init_data(self):
Z
zhupengyang 已提交
60
        self.ori_shape = (2, 10, 5)
L
liym27 已提交
61
        self.expand_times = (1, 1, 1)
Y
yangyaming 已提交
62 63


L
liym27 已提交
64 65
class TestExpandOpRank3(TestExpandOpRank1):
    def init_data(self):
Z
zhupengyang 已提交
66
        self.ori_shape = (2, 4, 15)
L
liym27 已提交
67 68
        self.expand_times = (2, 1, 4)

Y
yangyaming 已提交
69

L
liym27 已提交
70 71 72 73
class TestExpandOpRank4(TestExpandOpRank1):
    def init_data(self):
        self.ori_shape = (2, 4, 5, 7)
        self.expand_times = (3, 2, 1, 2)
Y
yangyaming 已提交
74

L
liym27 已提交
75 76 77

# Situation 2: expand_times is a list(with tensor)
class TestExpandOpRank1_tensor_attr(OpTest):
78 79
    def setUp(self):
        self.op_type = "expand"
L
liym27 已提交
80
        self.init_data()
81 82 83
        self.dtype = (
            "float32" if fluid.core.is_compiled_with_rocm() else "float64"
        )
R
ronnywang 已提交
84

L
liym27 已提交
85 86
        expand_times_tensor = []
        for index, ele in enumerate(self.expand_times):
87 88 89
            expand_times_tensor.append(
                ("x" + str(index), np.ones((1)).astype('int32') * ele)
            )
L
liym27 已提交
90

91
        self.inputs = {
R
ronnywang 已提交
92
            'X': np.random.random(self.ori_shape).astype(self.dtype),
L
liym27 已提交
93
            'expand_times_tensor': expand_times_tensor,
94
        }
L
liym27 已提交
95 96
        self.attrs = {"expand_times": self.infer_expand_times}
        output = np.tile(self.inputs['X'], self.expand_times)
97 98
        self.outputs = {'Out': output}

L
liym27 已提交
99
    def init_data(self):
Z
zhupengyang 已提交
100
        self.ori_shape = [100]
L
liym27 已提交
101 102 103
        self.expand_times = [2]
        self.infer_expand_times = [-1]

104 105 106 107 108 109 110
    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


L
liym27 已提交
111 112 113 114 115
class TestExpandOpRank2_Corner_tensor_attr(TestExpandOpRank1_tensor_attr):
    def init_data(self):
        self.ori_shape = [12, 14]
        self.expand_times = [1, 1]
        self.infer_expand_times = [1, -1]
Y
yangyaming 已提交
116 117


L
liym27 已提交
118 119 120 121 122
class TestExpandOpRank2_attr_tensor(TestExpandOpRank1_tensor_attr):
    def init_data(self):
        self.ori_shape = [12, 14]
        self.expand_times = [2, 3]
        self.infer_expand_times = [-1, 3]
Y
yangyaming 已提交
123 124


L
liym27 已提交
125 126
# Situation 3: expand_times is a tensor
class TestExpandOpRank1_tensor(OpTest):
127 128
    def setUp(self):
        self.op_type = "expand"
L
liym27 已提交
129
        self.init_data()
130 131 132
        self.dtype = (
            "float32" if fluid.core.is_compiled_with_rocm() else "float64"
        )
L
liym27 已提交
133

134
        self.inputs = {
R
ronnywang 已提交
135
            'X': np.random.random(self.ori_shape).astype(self.dtype),
L
liym27 已提交
136
            'ExpandTimes': np.array(self.expand_times).astype("int32"),
137 138
        }
        self.attrs = {}
L
liym27 已提交
139
        output = np.tile(self.inputs['X'], self.expand_times)
140 141
        self.outputs = {'Out': output}

L
liym27 已提交
142
    def init_data(self):
Z
zhupengyang 已提交
143
        self.ori_shape = [100]
L
liym27 已提交
144
        self.expand_times = [2]
Y
yangyaming 已提交
145 146 147 148 149 150 151 152

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


L
liym27 已提交
153 154 155 156
class TestExpandOpRank2_tensor(TestExpandOpRank1_tensor):
    def init_data(self):
        self.ori_shape = [12, 14]
        self.expand_times = [2, 3]
Y
yangyaming 已提交
157 158


L
liym27 已提交
159
# Situation 4: input x is Integer
160 161 162
class TestExpandOpInteger(OpTest):
    def setUp(self):
        self.op_type = "expand"
J
jerrywgz 已提交
163
        self.inputs = {
164
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int32")
J
jerrywgz 已提交
165
        }
166 167 168 169 170 171 172 173
        self.attrs = {'expand_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


L
liym27 已提交
174
# Situation 5: input x is Bool
175 176 177
class TestExpandOpBoolean(OpTest):
    def setUp(self):
        self.op_type = "expand"
J
jerrywgz 已提交
178
        self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")}
179 180 181 182 183 184 185 186
        self.attrs = {'expand_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


W
wangchaochaohu 已提交
187 188 189 190 191
# Situation 56: input x is Integer
class TestExpandOpInt64_t(OpTest):
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {
192
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int64")
W
wangchaochaohu 已提交
193 194 195 196 197 198 199 200 201
        }
        self.attrs = {'expand_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


Y
yangyaming 已提交
202 203
if __name__ == "__main__":
    unittest.main()