test_while_loop_op.py 23.0 KB
Newer Older
G
guofei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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 numpy as np
import unittest

18
import paddle
G
guofei 已提交
19 20 21 22
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.layers as layers
from paddle.fluid.framework import Program, program_guard
23
from paddle.fluid.backward import append_backward
G
guofei 已提交
24

25 26
paddle.enable_static()

G
guofei 已提交
27 28

class TestApiWhileLoop(unittest.TestCase):
29

G
guofei 已提交
30
    def test_var_tuple(self):
31

G
guofei 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
        def cond(i):
            return layers.less_than(i, ten)

        def body(i):
            return layers.elementwise_add(x=i, y=one)

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = layers.fill_constant(shape=[1], dtype='int64', value=0)
            one = layers.fill_constant(shape=[1], dtype='int64', value=1)
            ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
            out = layers.while_loop(cond, body, (i, ))

46 47
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
G
guofei 已提交
48 49
        exe = fluid.Executor(place)
        res = exe.run(main_program, fetch_list=out)
50 51 52
        np.testing.assert_allclose(np.asarray(res[0]),
                                   np.full(1, 10, np.int64),
                                   rtol=1e-05)
G
guofei 已提交
53 54

    def test_var_list(self):
55

G
guofei 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
        def cond(i, mem):
            return layers.less_than(i, ten)

        def body(i, mem):
            mem = layers.elementwise_add(x=mem, y=one)
            i = layers.increment(i)
            return [i, mem]

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = layers.zeros(shape=[1], dtype='int64')
            ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
69
            mem = fluid.data(name='mem', shape=[10], dtype='float32')
G
guofei 已提交
70 71 72 73 74 75
            one = layers.fill_constant(shape=[10], dtype='float32', value=1)
            out = layers.while_loop(cond, body, [i, mem])

            data = np.random.rand(10).astype('float32')
            data_one = np.ones(10).astype('float32')

76 77
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
G
guofei 已提交
78 79 80 81
        exe = fluid.Executor(place)
        res = exe.run(main_program, feed={'mem': data}, fetch_list=out)
        for i in range(10):
            data = np.add(data, data_one)
82
        np.testing.assert_allclose(np.asarray(res[1]), data, rtol=1e-05)
G
guofei 已提交
83

84
    def test_var_dict(self):
85

86
        def cond(i, ten, test_dict, test_list, test_list_dict):
87 88
            return layers.less_than(i, ten)

89 90 91 92 93 94 95
        def body(i, ten, test_dict, test_list, test_list_dict):
            test_dict["test_key"] = i
            test_dict["test_key"] += 1

            test_list[0] = fluid.layers.reshape(test_list[0], [2, -1]) + 1

            test_list_dict[0]["test_key"] += 1
96 97
            test_list_dict[0]["test_key"] = fluid.layers.relu(
                test_list_dict[0]["test_key"])
98

99
            i = layers.increment(i)
100
            return [i, ten, test_dict, test_list, test_list_dict]
101 102 103 104 105 106 107

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = layers.zeros(shape=[1], dtype='int64')
            ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
            test_data = layers.fill_constant(shape=[1], dtype='int64', value=0)
108

109
            test_dict = {"test_key": test_data}
110
            test_list = [
111
                layers.fill_constant(shape=[1, 2], dtype='int64', value=0)
112 113
            ]
            test_list_dict = [{
114 115
                "test_key":
                layers.fill_constant(shape=[1], dtype='float32', value=0)
116 117 118 119
            }]

            i, ten, test_dict, test_list, test_list_dict = layers.while_loop(
                cond, body, [i, ten, test_dict, test_list, test_list_dict])
120 121
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
122
        exe = fluid.Executor(place)
123 124 125 126 127
        res = exe.run(main_program,
                      fetch_list=[
                          test_dict["test_key"], test_list[0],
                          test_list_dict[0]["test_key"]
                      ])
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        np.testing.assert_allclose(np.asarray(res[0]),
                                   np.full(shape=1,
                                           fill_value=10,
                                           dtype=np.int64),
                                   rtol=1e-05)
        np.testing.assert_allclose(np.asarray(res[1]),
                                   np.full(shape=(2, 1),
                                           fill_value=10,
                                           dtype=np.int64),
                                   rtol=1e-05)
        np.testing.assert_allclose(np.asarray(res[2]),
                                   np.full(shape=1,
                                           fill_value=10,
                                           dtype=np.float32),
                                   rtol=1e-05)
143

G
guofei 已提交
144 145

class TestApiWhileLoop_Nested(unittest.TestCase):
146

G
guofei 已提交
147
    def test_nested_net(self):
148

G
guofei 已提交
149 150 151 152
        def external_cond(i, j, init, sums):
            return layers.less_than(i, loop_len1)

        def external_body(i, j, init, sums):
153

G
guofei 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            def internal_cond(j, init, sums):
                return layers.less_than(j, loop_len2)

            def internal_body(j, init, sums):
                init = layers.elementwise_add(x=init, y=ones)
                sums = layers.elementwise_add(x=init, y=sums)
                j = layers.increment(j)
                return [j, init, sums]

            result = layers.while_loop(internal_cond, internal_body,
                                       [j, init, sums])
            j = result[0]
            init = result[1]
            sums = result[2]
            sums = layers.elementwise_add(x=init, y=sums)
            i = layers.increment(i)
            return [i, j, init, sums]

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = layers.zeros(shape=[1], dtype='int64')
            j = layers.zeros(shape=[1], dtype='int64')
177 178
            init = fluid.data(name='init', shape=[3, 3], dtype='float32')
            sums = fluid.data(name='sums', shape=[3, 3], dtype='float32')
G
guofei 已提交
179 180 181 182
            loop_len1 = layers.fill_constant(shape=[1], dtype='int64', value=2)
            loop_len2 = layers.fill_constant(shape=[1], dtype='int64', value=3)
            ones = layers.fill_constant(shape=[3, 3], dtype='float32', value=1)

183
            out = layers.while_loop(external_cond, external_body,
G
guofei 已提交
184 185 186 187 188
                                    [i, j, init, sums])

            data = np.random.rand(3, 3).astype('float32')
            data_sums = np.zeros([3, 3]).astype('float32')

189 190
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
G
guofei 已提交
191
        exe = fluid.Executor(place)
192
        res = exe.run(main_program,
193 194 195 196
                      feed={
                          'init': data,
                          'sums': data_sums
                      },
197
                      fetch_list=out)
G
guofei 已提交
198 199 200 201 202
        for i in range(3):
            data = np.add(data, 1)
            data_sums = np.add(data, data_sums)
        for j in range(2):
            data_sums = np.add(data, data_sums)
203
        np.testing.assert_allclose(np.asarray(res[3]), data_sums, rtol=1e-05)
204 205 206


class TestApiWhileLoop_Backward(unittest.TestCase):
207

208
    def test_while_loop_backward(self):
209

210 211 212
        def cond(i, x):
            return layers.less_than(i, eleven)

213
        def body(i, x):
214
            x = layers.elementwise_mul(x=i, y=i)
215 216
            i = layers.increment(i)
            return [i, x]
217 218 219 220

        main_program = Program()
        startup_program = Program()
        with fluid.program_guard(main_program, startup_program):
221
            i = fluid.data(name='i', shape=[1], dtype='float32')
222 223 224
            i.stop_gradient = False
            eleven = layers.fill_constant(shape=[1], dtype='float32', value=11)
            one = layers.fill_constant(shape=[1], dtype='float32', value=1)
225
            x = fluid.data(name='x', shape=[1], dtype='float32')
226 227 228
            x.stop_gradient = False

            out = layers.while_loop(cond, body, [i, x])
229
            mean = paddle.mean(out[1])
230 231
            append_backward(mean)

232 233
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
234 235 236 237 238 239 240 241
        exe = fluid.Executor(place)

        feed_i = np.ones(1).astype('float32')
        feed_x = np.ones(1).astype('float32')
        data = np.asarray([100]).astype('float32')
        i_grad = np.asarray([110]).astype('float32')

        res = exe.run(main_program,
242 243 244 245
                      feed={
                          'i': feed_i,
                          'x': feed_x
                      },
246
                      fetch_list=[mean.name, i.grad_name])
247 248
        np.testing.assert_allclose(np.asarray(res[0]), data, rtol=1e-05)
        np.testing.assert_allclose(np.asarray(res[1]), i_grad, rtol=1e-05)
249 250

    def test_while_loop_backward2(self):
251

252
        def cond(i, x):
253
            return i < 3
254 255

        def body(i, x):
256
            x = x * i
257 258 259 260 261 262 263 264 265 266 267 268
            i = i + 1
            return [i, x]

        main_program = Program()
        startup_program = Program()
        with fluid.program_guard(main_program, startup_program):
            i = fluid.data(name='i', shape=[1], dtype='float32')
            i.stop_gradient = False
            x = fluid.data(name='x', shape=[1], dtype='float32')
            x.stop_gradient = False

            out = layers.while_loop(cond, body, [i, x])
269
            mean = paddle.mean(out[1])
270 271
            append_backward(mean)

272 273
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
274 275 276 277
        exe = fluid.Executor(place)

        feed_i = np.ones(1).astype('float32')
        feed_x = np.ones(1).astype('float32')
278 279 280
        data = np.asarray([2]).astype('float32')
        i_grad = np.asarray([3]).astype('float32')
        x_grad = np.asarray([2]).astype('float32')
281 282

        res = exe.run(main_program,
283 284 285 286
                      feed={
                          'i': feed_i,
                          'x': feed_x
                      },
287
                      fetch_list=[mean.name, i.grad_name, x.grad_name])
288 289 290
        np.testing.assert_allclose(np.asarray(res[0]), data, rtol=1e-05)
        np.testing.assert_allclose(np.asarray(res[1]), i_grad, rtol=1e-05)
        np.testing.assert_allclose(np.asarray(res[2]), x_grad, rtol=1e-05)
291 292


293
class TestApiWhileLoop_NestedWithBackwardAndLoDTensorArray(unittest.TestCase):
294

295
    def test_nested_net_with_backward_and_lodtensor(self):
296

297 298 299 300
        def external_cond(i, j, x, mem_array):
            return layers.less_than(i, array_len)

        def external_body(i, j, x, mem_array):
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
            def internal_cond(j, x, mem_array):
                return layers.less_than(j, array_len2)

            def internal_body(j, x, mem_array):
                inner_data = layers.array_read(array=data_array, i=j)
                inner_prev = layers.array_read(array=mem_array, i=j)
                inner_sum_0 = layers.elementwise_add(x=inner_data, y=inner_prev)
                inner_sum_1 = layers.elementwise_add(x=x, y=inner_sum_0)
                j = layers.increment(x=j, in_place=True)
                layers.array_write(inner_sum_1, i=j, array=mem_array)
                return [j, x, mem_array]

            outer_data = layers.array_read(array=data_array, i=i)
            outer_prev = layers.array_read(array=mem_array, i=i)
            outer_sum_0 = layers.elementwise_add(x=outer_data, y=outer_prev)
            outer_sum_1 = layers.elementwise_add(x=x, y=outer_sum_0)
            i = layers.increment(x=i, in_place=True)
            layers.array_write(outer_sum_1, i=i, array=mem_array)
            j, x, mem_array = layers.while_loop(internal_cond, internal_body,
                                                [j, x, mem_array])
            return [i, j, x, mem_array]
323 324 325 326

        main_program = Program()
        startup_program = Program()
        with fluid.program_guard(main_program, startup_program):
327 328 329 330
            d0 = fluid.data(name='d0', shape=[10], dtype='float32')
            d1 = fluid.data(name='d1', shape=[10], dtype='float32')
            d2 = fluid.data(name='d2', shape=[10], dtype='float32')
            x = fluid.data(name='x', shape=[10], dtype='float32')
331
            x.stop_gradient = False
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
            i = layers.zeros(shape=[1], dtype='int64')
            i.stop_gradient = True
            init = layers.zeros(shape=[10], dtype='float32')
            mem_array = layers.array_write(x=init, i=i)
            data_array = layers.array_write(x=d0, i=i)
            i = layers.increment(i)
            layers.array_write(d1, i, array=data_array)
            i = layers.increment(i)
            layers.array_write(d2, i, array=data_array)
            i = layers.zeros(shape=[1], dtype='int64')
            i.stop_gradient = True
            array_len = layers.fill_constant(shape=[1], dtype='int64', value=1)
            j = layers.fill_constant(shape=[1], dtype='int64', value=1)
            j.stop_gradient = True
            array_len2 = layers.fill_constant(shape=[1], dtype='int64', value=3)
347

348 349
            out = layers.while_loop(external_cond, external_body,
                                    [i, j, x, mem_array])
350

351
            sum_result = layers.array_read(array=mem_array, i=j)
352
            mean = paddle.mean(sum_result)
353
            append_backward(mean)
354

355 356
            place = fluid.CUDAPlace(
                0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
357 358 359 360 361 362 363 364
            exe = fluid.Executor(place)

            d = []
            for i in range(3):
                d.append(np.random.random(size=[10]).astype('float32'))
            feed_x = np.ones(10).astype('float32')
            data_sum = d[0] + d[1] + d[2] + 3 * feed_x
            x_grad = [0.3] * 10
365 366 367 368 369 370 371 372
            res = exe.run(main_program,
                          feed={
                              'd0': d[0],
                              'd1': d[1],
                              'd2': d[2],
                              'x': feed_x
                          },
                          fetch_list=[sum_result.name, x.grad_name])
373 374
            np.testing.assert_allclose(res[0], data_sum, rtol=1e-05)
            np.testing.assert_allclose(res[1], x_grad, rtol=1e-05)
375 376 377


class TestApiWhileLoopWithSwitchCase(unittest.TestCase):
378

379
    def test_with_switch_case(self):
380

381 382 383 384
        def cond(i):
            return layers.less_than(i, ten)

        def body(i):
385

386 387 388 389 390 391 392 393 394 395 396 397
            def fn_add_three():
                data_add_three = layers.elementwise_add(x=i, y=three)
                return data_add_three

            def fn_square():
                data_mul_data = layers.elementwise_mul(x=i, y=i)
                return data_mul_data

            def fn_add_one():
                data_add_one = layers.elementwise_add(x=i, y=one)
                return data_add_one

398 399 400 401 402 403
            return layers.switch_case(branch_index=i,
                                      branch_fns={
                                          2: fn_add_three,
                                          5: fn_square
                                      },
                                      default=fn_add_one)
404 405 406 407 408 409 410 411 412 413

        main_program = Program()
        startup_program = Program()
        with fluid.program_guard(main_program, startup_program):
            i = layers.fill_constant(shape=[1], dtype='int64', value=1)
            ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
            three = layers.fill_constant(shape=[1], dtype='int64', value=3)
            one = layers.fill_constant(shape=[1], dtype='int64', value=1)
            out = layers.while_loop(cond, body, [i])

414 415
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
416 417 418 419
        exe = fluid.Executor(place)
        res = exe.run(main_program, fetch_list=out)

        data = np.asarray([25]).astype('int64')
420
        np.testing.assert_allclose(np.asarray(res[0]), data, rtol=1e-05)
G
guofei 已提交
421 422 423


class TestApiWhileLoop_Error(unittest.TestCase):
424

G
guofei 已提交
425
    def test_error(self):
426

G
guofei 已提交
427 428 429 430 431 432 433 434 435 436 437 438
        def cond_returns_constant(i):
            return 1

        def cond_returns_not_bool_tensor(i):
            return layers.increment(i)

        def cond_returns_bool_tensor(i):
            return layers.less_than(i, ten)

        def cond_returns_2d_tensor(i):
            return layers.less_than(i, ten_2d)

439 440 441
        def cond_receives_two_args(i, ten):
            return layers.less_than(i, ten)

G
guofei 已提交
442 443 444
        def body(i):
            return layers.increment(i)

445 446 447 448 449 450 451
        def body_returns_error_length(i):
            i = layers.increment(i)
            return [i, i]

        def body_returns_error_type(i, ten):
            return layers.increment(i)

452 453 454 455
        def cond_returns_with_mutable_dict(i, test_dict):
            return i > 0

        def body_returns_with_mutable_dict(i, test_dict):
456 457 458
            test_dict['new_key'] = layers.fill_constant(shape=[1],
                                                        dtype='int64',
                                                        value=1)
459 460 461 462 463 464 465
            return layers.increment(i), test_dict

        def cond_returns_with_mutable_list(i, test_list):
            return i > 0

        def body_returns_with_mutable_list(i, test_list):
            test_list.append(
466
                layers.fill_constant(shape=[1], dtype='int64', value=1))
467 468
            return layers.increment(i), test_list

G
guofei 已提交
469 470 471 472 473 474 475 476 477
        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            data = layers.fill_constant(shape=[1], dtype='int64', value=1)
            data_1d = layers.fill_constant(shape=[1], dtype='int64', value=1)
            data_2d = layers.fill_constant(shape=[2, 2], dtype='int64', value=1)
            ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
            ten_2d = layers.fill_constant(shape=[2, 2], dtype='int64', value=10)

478
            # The type of `cond` in Op(while_loop) must be callable
G
guofei 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
            def type_error_cond():
                out = layers.while_loop(data, body, [data_1d])

            self.assertRaises(TypeError, type_error_cond)

            # The type of `body` in Op(while_loop) must be callable
            def type_error_body():
                out = layers.while_loop(cond_returns_bool_tensor, data,
                                        [data_1d])

            self.assertRaises(TypeError, type_error_body)

            # The type of `loop_vars` in Op(while_loop) must be list or tuple
            def type_error_loop_vars():
                out = layers.while_loop(cond_returns_bool_tensor, body, data_1d)

            self.assertRaises(TypeError, type_error_loop_vars)

            # The value of `loop_vars` is empty
            def value_error_loop_vars():
                out = layers.while_loop(cond_returns_bool_tensor, body, [])

            self.assertRaises(ValueError, value_error_loop_vars)

            # The type of `cond` returns in Op(while_loop) must be Variable
            def type_error_cond_returns_not_variable():
                out = layers.while_loop(cond_returns_constant, body, [data_1d])

            self.assertRaises(TypeError, type_error_cond_returns_not_variable)

            # The type of `cond` returns in Op(while_loop) must be a bollean variable
            def type_error_cond_returns_not_boolean():
                out = layers.while_loop(cond_returns_not_bool_tensor, body,
                                        [data_1d])

            self.assertRaises(TypeError, type_error_cond_returns_not_boolean)

            # The shape of `cond` returns in Op(while_loop) must be 1
            def type_error_shape_cond_returns_2d():
                out = layers.while_loop(cond_returns_2d_tensor, body, [data_2d])

            self.assertRaises(TypeError, type_error_shape_cond_returns_2d)

522 523 524 525 526 527 528 529 530 531 532 533 534 535
            # The length of `body` returns in Op(while_loop) must be same as `loop_vars`
            def value_error_body_returns_error_length():
                out = layers.while_loop(cond_returns_bool_tensor,
                                        body_returns_error_length, [data])

            self.assertRaises(ValueError, value_error_body_returns_error_length)

            # The type of `body` returns in Op(while_loop) must be same as `loop_vars`
            def value_error_body_returns_error_type():
                out = layers.while_loop(cond_receives_two_args,
                                        body_returns_error_type, [data, ten])

            self.assertRaises(ValueError, value_error_body_returns_error_type)

536 537 538
            # The length of `output_vars` with mutable value should keep same with `loop_vars`
            def value_error_body_returns_with_mutable_dict():
                test_dict = {
539 540
                    "int_constant":
                    layers.fill_constant(shape=[2, 2], dtype='int64', value=1)
541 542 543 544 545 546 547 548 549 550
                }
                out = layers.while_loop(cond_returns_with_mutable_dict,
                                        body_returns_with_mutable_dict,
                                        [data, test_dict])

            self.assertRaises(ValueError,
                              value_error_body_returns_with_mutable_dict)

            def value_error_body_returns_with_mutable_list():
                test_list = [
551
                    layers.fill_constant(shape=[2, 2], dtype='int64', value=1)
552 553 554 555 556 557 558 559
                ]
                out = layers.while_loop(cond_returns_with_mutable_list,
                                        body_returns_with_mutable_list,
                                        [data, test_list])

            self.assertRaises(ValueError,
                              value_error_body_returns_with_mutable_list)

G
guofei 已提交
560

561
class TestApiWhileLoopSliceInBody(unittest.TestCase):
562

563
    def test_var_slice(self):
564

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        def cond(z, i):
            return i + 1 <= x_shape[0]

        def body(z, i):
            z = z + x[i]
            i += 1
            return z, i

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            x = fluid.layers.data(name='x', shape=[5], dtype='int32')
            z = fluid.layers.fill_constant([1], 'int32', 0)
            x_shape = fluid.layers.shape(x)
            i = fluid.layers.fill_constant([1], 'int32', 0)
            z, _ = fluid.layers.while_loop(cond, body, [z, i])

582 583
        place = fluid.CUDAPlace(
            0) if core.is_compiled_with_cuda() else fluid.CPUPlace()
584 585 586 587
        exe = fluid.Executor(place)

        np_x = np.array([1, 2, 3, 4, 5], dtype='int32')
        res = exe.run(main_program, feed={'x': np_x}, fetch_list=[z])
588
        np.testing.assert_array_equal(res[0], [np.sum(np_x)])
589 590


G
guofei 已提交
591 592
if __name__ == '__main__':
    unittest.main()