test_expand_v2_op.py 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

import unittest
16 17

import gradient_checker
18
import numpy as np
19
from decorator_helper import prog_scope
20
from eager_op_test import OpTest
21 22

import paddle
23
import paddle.fluid as fluid
24
from paddle.fluid import Program, core, program_guard
25 26 27 28 29 30


# Situation 1: shape is a list(without tensor)
class TestExpandV2OpRank1(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
31
        self.prim_op_type = "prim"
32
        self.init_data()
H
hong 已提交
33
        self.python_api = paddle.expand
34
        self.public_python_api = paddle.expand
35 36 37 38
        self.inputs = {'X': np.random.random(self.ori_shape).astype("float64")}
        self.attrs = {'shape': self.shape}
        output = np.tile(self.inputs['X'], self.expand_times)
        self.outputs = {'Out': output}
39
        self.enable_cinn = True
40 41 42 43 44 45 46

    def init_data(self):
        self.ori_shape = [100]
        self.shape = [100]
        self.expand_times = [1]

    def test_check_output(self):
47
        self.check_output()
48 49

    def test_check_grad(self):
50
        self.check_grad(['X'], 'Out', check_prim=True)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84


class TestExpandV2OpRank2_DimExpanding(TestExpandV2OpRank1):
    def init_data(self):
        self.ori_shape = [120]
        self.shape = [2, 120]
        self.expand_times = [2, 1]


class TestExpandV2OpRank2(TestExpandV2OpRank1):
    def init_data(self):
        self.ori_shape = [1, 140]
        self.shape = [12, 140]
        self.expand_times = [12, 1]


class TestExpandV2OpRank3_Corner(TestExpandV2OpRank1):
    def init_data(self):
        self.ori_shape = (2, 10, 5)
        self.shape = (2, 10, 5)
        self.expand_times = (1, 1, 1)


class TestExpandV2OpRank4(TestExpandV2OpRank1):
    def init_data(self):
        self.ori_shape = (2, 4, 5, 7)
        self.shape = (-1, -1, -1, -1)
        self.expand_times = (1, 1, 1, 1)


# Situation 2: shape is a list(with tensor)
class TestExpandV2OpRank1_tensor_attr(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
85
        self.prim_op_type = "prim"
86
        self.python_api = paddle.expand
87
        self.public_python_api = paddle.expand
88 89 90
        self.init_data()
        expand_shapes_tensor = []
        for index, ele in enumerate(self.expand_shape):
91 92 93
            expand_shapes_tensor.append(
                ("x" + str(index), np.ones((1)).astype('int32') * ele)
            )
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

        self.inputs = {
            'X': np.random.random(self.ori_shape).astype("float64"),
            'expand_shapes_tensor': expand_shapes_tensor,
        }
        self.attrs = {"shape": self.infer_expand_shape}
        output = np.tile(self.inputs['X'], self.expand_times)
        self.outputs = {'Out': output}

    def init_data(self):
        self.ori_shape = [100]
        self.expand_times = [1]
        self.expand_shape = [100]
        self.infer_expand_shape = [-1]

    def test_check_output(self):
110
        self.check_output()
111 112

    def test_check_grad(self):
113
        self.check_grad(['X'], 'Out')
114 115 116 117 118 119 120 121 122 123 124 125 126 127


class TestExpandV2OpRank2_Corner_tensor_attr(TestExpandV2OpRank1_tensor_attr):
    def init_data(self):
        self.ori_shape = [12, 14]
        self.expand_times = [1, 1]
        self.expand_shape = [12, 14]
        self.infer_expand_shape = [12, -1]


# Situation 3: shape is a tensor
class TestExpandV2OpRank1_tensor(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
128
        self.prim_op_type = "prim"
129
        self.python_api = paddle.expand
130
        self.public_python_api = paddle.expand
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        self.init_data()

        self.inputs = {
            'X': np.random.random(self.ori_shape).astype("float64"),
            'Shape': np.array(self.expand_shape).astype("int32"),
        }
        self.attrs = {}
        output = np.tile(self.inputs['X'], self.expand_times)
        self.outputs = {'Out': output}

    def init_data(self):
        self.ori_shape = [100]
        self.expand_times = [2, 1]
        self.expand_shape = [2, 100]

    def test_check_output(self):
        self.check_output()

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


# Situation 4: input x is Integer
class TestExpandV2OpInteger(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
157
        self.prim_op_type = "prim"
158
        self.python_api = paddle.expand
159
        self.public_python_api = paddle.expand
160
        self.inputs = {
161
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int32")
162 163 164 165 166 167 168 169 170
        }
        self.attrs = {'shape': [2, 4, 5]}
        output = np.tile(self.inputs['X'], (1, 1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


171
#  Situation 5: input x is Bool
172 173 174
class TestExpandV2OpBoolean(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
175
        self.prim_op_type = "prim"
176
        self.python_api = paddle.expand
177
        self.public_python_api = paddle.expand
178 179 180 181 182 183 184 185 186
        self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")}
        self.attrs = {'shape': [2, 4, 5]}
        output = np.tile(self.inputs['X'], (1, 1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


187
#  Situation 6: input x is Integer
188 189 190
class TestExpandV2OpInt64_t(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
191
        self.prim_op_type = "prim"
192
        self.python_api = paddle.expand
193
        self.public_python_api = paddle.expand
194
        self.inputs = {
195
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int64")
196 197 198 199 200 201 202 203 204 205 206 207
        }
        self.attrs = {'shape': [2, 4, 5]}
        output = np.tile(self.inputs['X'], (1, 1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


class TestExpandV2Error(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
208 209 210
            x1 = fluid.create_lod_tensor(
                np.array([[-1]]), [[1]], fluid.CPUPlace()
            )
211 212
            shape = [2, 2]
            self.assertRaises(TypeError, paddle.tensor.expand, x1, shape)
G
GGBond8488 已提交
213
            x2 = paddle.static.data(name='x2', shape=[-1, 4], dtype="uint8")
214
            self.assertRaises(TypeError, paddle.tensor.expand, x2, shape)
G
GGBond8488 已提交
215
            x3 = paddle.static.data(name='x3', shape=[-1, 4], dtype="bool")
L
lilong12 已提交
216
            x3.stop_gradient = False
217 218 219 220 221 222 223
            self.assertRaises(ValueError, paddle.tensor.expand, x3, shape)


# Test python API
class TestExpandV2API(unittest.TestCase):
    def test_api(self):
        input = np.random.random([12, 14]).astype("float32")
G
GGBond8488 已提交
224
        x = paddle.static.data(name='x', shape=[12, 14], dtype="float32")
225

226
        positive_2 = paddle.tensor.fill_constant([1], "int32", 12)
G
GGBond8488 已提交
227
        expand_shape = paddle.static.data(
228 229 230 231
            name="expand_shape",
            shape=[2],
            dtype="int32",
        )
232 233 234 235 236 237 238 239

        out_1 = paddle.expand(x, shape=[12, 14])
        out_2 = paddle.expand(x, shape=[positive_2, 14])
        out_3 = paddle.expand(x, shape=expand_shape)

        g0 = fluid.backward.calc_gradient(out_2, x)

        exe = fluid.Executor(place=fluid.CPUPlace())
240 241 242 243 244 245 246 247
        res_1, res_2, res_3 = exe.run(
            fluid.default_main_program(),
            feed={
                "x": input,
                "expand_shape": np.array([12, 14]).astype("int32"),
            },
            fetch_list=[out_1, out_2, out_3],
        )
248 249 250 251 252
        assert np.array_equal(res_1, np.tile(input, (1, 1)))
        assert np.array_equal(res_2, np.tile(input, (1, 1)))
        assert np.array_equal(res_3, np.tile(input, (1, 1)))


253 254 255 256 257 258
class TestExpandInferShape(unittest.TestCase):
    def test_shape_with_var(self):
        with program_guard(Program(), Program()):
            x = paddle.static.data(shape=[-1, 1, 3], name='x')
            fake_var = paddle.randn([2, 3])
            target_shape = [
259 260 261
                -1,
                paddle.shape(fake_var)[0],
                paddle.shape(fake_var)[1],
262 263 264 265 266
            ]
            out = paddle.expand(x, shape=target_shape)
            self.assertListEqual(list(out.shape), [-1, -1, -1])


267
# Test python Dygraph API
268 269 270 271 272 273 274 275
class TestExpandV2DygraphAPI(unittest.TestCase):
    def test_expand_times_is_tensor(self):
        with paddle.fluid.dygraph.guard():
            paddle.seed(1)
            a = paddle.rand([2, 5])
            expand_1 = paddle.expand(a, shape=[2, 5])
            np_array = np.array([2, 5])
            expand_2 = paddle.expand(a, shape=np_array)
276
            np.testing.assert_array_equal(expand_1.numpy(), expand_2.numpy())
277 278


279 280 281 282 283 284 285 286 287 288
class TestExpandDoubleGradCheck(unittest.TestCase):
    def expand_wrapper(self, x):
        return paddle.expand(x[0], [2, 3])

    @prog_scope()
    def func(self, place):
        # the shape of input variable should be clearly specified, not inlcude -1.
        eps = 0.005
        dtype = np.float32

G
GGBond8488 已提交
289
        data = paddle.static.data('data', [2, 3], dtype)
290 291 292 293
        data.persistable = True
        out = paddle.expand(data, [2, 3])
        data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype)

294 295 296 297 298 299
        gradient_checker.double_grad_check(
            [data], out, x_init=[data_arr], place=place, eps=eps
        )
        gradient_checker.double_grad_check_for_dygraph(
            self.expand_wrapper, [data], out, x_init=[data_arr], place=place
        )
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

    def test_grad(self):
        paddle.enable_static()
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestExpandTripleGradCheck(unittest.TestCase):
    def expand_wrapper(self, x):
        return paddle.expand(x[0], [2, 3])

    @prog_scope()
    def func(self, place):
        # the shape of input variable should be clearly specified, not inlcude -1.
        eps = 0.005
        dtype = np.float32

G
GGBond8488 已提交
320
        data = paddle.static.data('data', [2, 3], dtype)
321 322 323 324
        data.persistable = True
        out = paddle.expand(data, [2, 3])
        data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype)

325 326 327 328 329 330
        gradient_checker.triple_grad_check(
            [data], out, x_init=[data_arr], place=place, eps=eps
        )
        gradient_checker.triple_grad_check_for_dygraph(
            self.expand_wrapper, [data], out, x_init=[data_arr], place=place
        )
331 332 333 334 335 336 337 338 339 340

    def test_grad(self):
        paddle.enable_static()
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


341 342 343 344 345 346 347
# Situation 7: comp case, shape is a list(without tensor)
class TestExpandV2CompOpRank1(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
        self.prim_op_type = "comp"
        self.init_data()
        self.python_api = paddle.expand
348
        self.public_python_api = paddle.expand
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        self.inputs = {'X': np.random.random(self.ori_shape).astype("float64")}
        self.attrs = {'shape': self.shape}
        output = np.tile(self.inputs['X'], self.expand_times)
        self.outputs = {'Out': output}
        self.enable_cinn = True

    def init_data(self):
        self.ori_shape = [100]
        self.shape = [100]
        self.expand_times = [1]

    def test_check_output(self):
        self.check_output(check_prim=True)

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


class TestExpandV2OpCompRank2_DimExpanding(TestExpandV2CompOpRank1):
    def init_data(self):
        self.ori_shape = [120]
        self.shape = [2, 120]
        self.expand_times = [2, 1]


class TestExpandV2CompOpRank2(TestExpandV2CompOpRank1):
    def init_data(self):
        self.ori_shape = [1, 140]
        self.shape = [12, 140]
        self.expand_times = [12, 1]


class TestExpandV2CompOpRank3_Corner(TestExpandV2CompOpRank1):
    def init_data(self):
        self.ori_shape = (2, 10, 5)
        self.shape = (2, 10, 5)
        self.expand_times = (1, 1, 1)


class TestExpandV2CompOpRank4(TestExpandV2CompOpRank1):
    def init_data(self):
        self.ori_shape = (2, 4, 5, 7)
        self.shape = (-1, -1, -1, -1)
        self.expand_times = (1, 1, 1, 1)


# Situation 8: comp case, input x is Integer
class TestExpandV2CompOpInteger(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
        self.prim_op_type = "comp"
        self.python_api = paddle.expand
401
        self.public_python_api = paddle.expand
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        self.inputs = {
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int32")
        }
        self.attrs = {'shape': [2, 4, 5]}
        output = np.tile(self.inputs['X'], (1, 1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output(check_prim=True)


#  Situation 9: comp case, input x is Bool
class TestExpandV2CompOpBoolean(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
        self.prim_op_type = "comp"
        self.python_api = paddle.expand
419
        self.public_python_api = paddle.expand
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")}
        self.attrs = {'shape': [2, 4, 5]}
        output = np.tile(self.inputs['X'], (1, 1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output(check_prim=True)


#  Situation 10: comp case, input x is Integer
class TestExpandV2CompOpInt64_t(OpTest):
    def setUp(self):
        self.op_type = "expand_v2"
        self.prim_op_type = "comp"
        self.python_api = paddle.expand
435
        self.public_python_api = paddle.expand
436 437 438 439 440 441 442 443 444 445 446
        self.inputs = {
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int64")
        }
        self.attrs = {'shape': [2, 4, 5]}
        output = np.tile(self.inputs['X'], (1, 1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output(check_prim=True)


447
if __name__ == "__main__":
H
hong 已提交
448
    paddle.enable_static()
449
    unittest.main()