test_cond.py 24.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

15
import os
16
import unittest
17 18 19 20

import numpy as np
from simple_nets import batchnorm_fc_with_inputs, simple_fc_net_with_inputs

21
import paddle
22 23 24
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.framework as framework
25
import paddle.fluid.layers as layers
26
from paddle.fluid.backward import append_backward
27
from paddle.fluid.framework import Program, program_guard
28 29

np.random.seed(123)
30 31


32
class TestCondInputOutput(unittest.TestCase):
33 34 35 36 37 38 39 40 41 42
    def test_return_single_var(self):
        """
        pseudocode:

        if 0.23 < 0.1:
            return 2
        else:
            return -1
        """

43 44
        paddle.enable_static()

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        def true_func():
            return layers.fill_constant(shape=[2, 3], dtype='int32', value=2)

        def false_func():
            return layers.fill_constant(shape=[3, 2], dtype='int32', value=-1)

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            x = layers.fill_constant(shape=[1], dtype='float32', value=0.1)
            y = layers.fill_constant(shape=[1], dtype='float32', value=0.23)
            pred = layers.less_than(y, x)
            out = layers.cond(pred, true_func, false_func)
            # out is one tensor

60 61 62 63 64
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
65
        exe = fluid.Executor(place)
66 67 68 69
        (ret,) = exe.run(main_program, fetch_list=[out.name])
        np.testing.assert_allclose(
            np.asarray(ret), np.full((3, 2), -1, np.int32), rtol=1e-05
        )
70 71 72 73 74 75 76 77 78 79 80

    def test_return_var_tuple(self):
        """
        pseudocode:

        if True:
            return 1, True
        else:
            return 3, 2
        """

81 82
        paddle.enable_static()

83
        def true_func():
84 85 86
            return layers.fill_constant(
                shape=[1, 2], dtype='int32', value=1
            ), layers.fill_constant(shape=[2, 3], dtype='bool', value=True)
87 88

        def false_func():
89 90 91
            return layers.fill_constant(
                shape=[3, 4], dtype='float32', value=3
            ), layers.fill_constant(shape=[4, 5], dtype='int64', value=2)
92 93 94 95 96 97 98 99

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            pred = layers.fill_constant(shape=[1], dtype='bool', value=True)
            out = layers.cond(pred, true_func, false_func)
            # out is a tuple containing 2 tensors

100 101 102 103 104
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
105 106
        exe = fluid.Executor(place)
        ret = exe.run(main_program, fetch_list=out)
107 108 109 110 111 112
        np.testing.assert_allclose(
            np.asarray(ret[0]), np.full((1, 2), 1, np.int32), rtol=1e-05
        )
        np.testing.assert_allclose(
            np.asarray(ret[1]), np.full((2, 3), True, bool), rtol=1e-05
        )
113 114 115 116 117 118 119 120 121 122 123 124

    def test_pass_and_modify_var(self):
        """
        pseudocode:
        for i in range(5):
            a = 7
            if i % 2 == 0:
                a = a * (i + 1)
            else:
                a = a - (i - 1)
        """

125 126
        paddle.enable_static()

127 128 129 130 131 132 133 134 135 136 137 138 139
        def true_func(a, i):
            a = a * (i + 1)
            return a

        def false_func(a, i):
            a = a - (i - 1)
            return a

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            a = layers.fill_constant(shape=[3, 2, 1], dtype='int32', value=7)
            i = fluid.data(name="i", shape=[1], dtype='int32')
140 141 142 143 144 145 146 147 148
            pred = (i % 2) == 0
            a = layers.cond(
                pred, lambda: true_func(a, i), lambda: false_func(a, i)
            )
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
149 150 151
        exe = fluid.Executor(place)
        for feed_i in range(5):
            expected_a = 7 * (feed_i + 1) if feed_i % 2 == 0 else 8 - feed_i
152 153 154 155 156 157 158 159 160 161
            (ret,) = exe.run(
                main_program,
                feed={'i': np.full((1), feed_i, np.int32)},
                fetch_list=[a],
            )
            np.testing.assert_allclose(
                np.asarray(ret),
                np.full((3, 2, 1), expected_a, np.int32),
                rtol=1e-05,
            )
162 163 164 165 166 167 168 169 170 171 172

    def test_return_none(self):
        """
        pseudocode: test doing nothing in branches
        for i in range(5):
            if i % 2 == 0:
                pass
            else:
                pass
        """

173 174
        paddle.enable_static()

175 176 177 178 179 180 181 182 183 184
        def true_func():
            pass

        def false_func():
            return None

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = fluid.data(name="i", shape=[1], dtype='int32')
185
            pred = (i % 2) == 0
186 187 188
            out1 = layers.cond(pred, true_func, false_func)
            out2 = layers.cond(pred, None, false_func)
            out3 = layers.cond(pred, true_func, None)
189 190 191 192 193
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
194 195 196 197 198 199 200 201 202 203 204 205 206
        exe = fluid.Executor(place)
        for feed_i in range(5):
            # Test that output is None is runnable
            exe.run(main_program, feed={'i': np.full((1), feed_i, np.int32)})
            self.assertIsNone(out1)
            self.assertIsNone(out2)
            self.assertIsNone(out3)

    def test_wrong_structure_exception(self):
        """
        test returning different number of tensors cannot merge into output
        """

207 208
        paddle.enable_static()

209 210 211 212 213 214 215
        def func_return_none():
            return None

        def func_return_one_tensor():
            return layers.fill_constant(shape=[2, 7], dtype='int32', value=3)

        def func_return_two_tensors():
216 217 218
            return layers.fill_constant(
                shape=[3, 1], dtype='int32', value=7
            ), layers.fill_constant(shape=[3, 1], dtype='int32', value=8)
219 220 221 222 223

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = fluid.data(name="i", shape=[1], dtype='int32')
224
            pred = (i % 2) == 0
225
            with self.assertRaises(TypeError):
226 227
                out = layers.cond(pred, i, func_return_one_tensor)

228
            with self.assertRaises(TypeError):
229 230 231
                out = layers.cond(pred, func_return_one_tensor, np.asarray([3]))

            with self.assertRaises(Exception) as e:
232 233 234
                out = layers.cond(
                    pred, func_return_none, func_return_one_tensor
                )
235
            self.assertTrue(
236 237 238
                "Incompatible return values of true_fn and false_fn in cond"
                in str(e.exception)
            )
239 240

            with self.assertRaises(Exception) as e:
241 242 243
                out = layers.cond(
                    pred, func_return_two_tensors, func_return_none
                )
244
            self.assertTrue(
245 246 247
                "Incompatible return values of true_fn and false_fn in cond"
                in str(e.exception)
            )
248 249

            with self.assertRaises(Exception) as e:
250 251 252
                out = layers.cond(
                    pred, func_return_one_tensor, func_return_two_tensors
                )
253
            self.assertTrue(
254
                "true fn returns 1 vars, but false fn returns 2 vars, which is not equals"
255 256
                in str(e.exception)
            )
257

258
    def test_extremely_simple_net_with_op_in_condition(self):
259
        paddle.enable_static()
260 261 262
        main_program = fluid.Program()
        startup_program = fluid.Program()
        with fluid.program_guard(main_program, startup_program):
263 264 265
            a = fluid.layers.fill_constant(
                shape=[1], dtype='float32', value=1.23
            )
266
            a.stop_gradient = False
267 268 269
            b = fluid.layers.fill_constant(
                shape=[1], dtype='float32', value=1.25
            )
270 271 272 273
            b.stop_gradient = False
            out = layers.cond(a - b < -1.0, lambda: a, lambda: b)
        append_backward(out)

274 275 276 277 278
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
279
        exe = fluid.Executor(place)
280 281 282
        ret = exe.run(
            main_program, fetch_list=[out, b, a.grad_name, b.grad_name]
        )
283 284
        # Note: fill_constant has loss of precision, you have to assertEqual
        # with values doens't lose precision in float-point number.
285 286 287
        self.assertEqual(ret[0][0], ret[1][0])
        self.assertEqual(ret[2][0], 0.0)
        self.assertEqual(ret[3][0], 1.0)
288

289

290 291 292 293 294 295 296 297
class TestCondNestedControlFlow(unittest.TestCase):
    def test_cond_inside_cond(self):
        """
        pseudocode:
        for i in range(1, 10):
            a = 2 * i
            if i < 5:
                if i >= 3:
298
                    return a + a
299 300 301 302 303 304 305 306 307
                else:
                    return a - a
            else:
                if i < 8:
                    return a * a
                else:
                    return a / a
        """

308 309
        paddle.enable_static()

310
        def less_than_branch(i, a):
311 312 313 314 315
            return layers.cond(
                i >= 3.0,
                lambda: layers.elementwise_add(a, a),
                lambda: layers.elementwise_sub(a, a),
            )
316 317

        def greater_equal_branch(i, a):
318 319 320 321 322
            return layers.cond(
                i < 8.0,
                lambda: layers.elementwise_mul(a, a),
                lambda: layers.elementwise_div(a, a),
            )
323 324 325 326 327 328

        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            i = fluid.data(name="i", shape=[1], dtype='float32')
            a = 2.0 * i
329 330 331 332 333
            out = layers.cond(
                i < 5.0,
                lambda: less_than_branch(i, a),
                lambda: greater_equal_branch(i, a),
            )
334
            mean = paddle.mean(out)
335 336
            append_backward(mean)

337 338 339 340 341
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
342 343 344 345 346 347 348 349 350
        exe = fluid.Executor(place)
        for feed_i in range(0, 10):
            expected_a = 2.0 * feed_i
            if feed_i < 5:
                expected_ret = expected_a + expected_a if feed_i >= 3 else 0.0
                expected_a_grad = 2.0 if feed_i >= 3 else 0.0
            else:
                expected_ret = expected_a * expected_a if feed_i < 8 else 1.0
                expected_a_grad = 2.0 * expected_a if feed_i < 8 else 0.0
351 352 353 354 355
            ret = exe.run(
                main_program,
                feed={'i': np.full((1), feed_i, np.float32)},
                fetch_list=[out.name, a.grad_name],
            )
356 357 358
            self.assertEqual(ret[0][0], expected_ret)
            self.assertEqual(ret[1][0], expected_a_grad)

359
    def test_cond_op_in_condition(self):
360
        paddle.enable_static()
361 362 363 364
        main_program = fluid.Program()
        startup_program = fluid.Program()

        with fluid.program_guard(main_program, startup_program):
365 366 367
            a = fluid.layers.fill_constant(
                shape=[1], dtype='float32', value=1.23
            )
368
            a.stop_gradient = False
369 370 371
            b = fluid.layers.fill_constant(
                shape=[1], dtype='float32', value=1.24
            )
372 373
            b.stop_gradient = False
            out = fluid.layers.cond(
374 375 376 377 378 379 380 381 382
                a < b,
                lambda: fluid.layers.cond(
                    a - b < -1.0,
                    lambda: fluid.layers.elementwise_add(a, b),
                    lambda: fluid.layers.elementwise_mul(a, b),
                ),
                lambda: fluid.layers.cond(
                    a == b,
                    lambda: fluid.layers.elementwise_sub(a, b),
383
                    lambda: paddle.pow(a, b),
384 385
                ),
            )
386 387
            append_backward(out)

388 389 390 391 392
        place = (
            fluid.CUDAPlace(0)
            if core.is_compiled_with_cuda()
            else fluid.CPUPlace()
        )
393 394
        exe = fluid.Executor(place)
        ret = exe.run(main_program, fetch_list=[out, a.grad_name, b.grad_name])
395
        # Note: fill_constant has loss of precision, so we assertAlmostEqual.
396 397 398 399
        self.assertAlmostEqual(ret[0][0], 1.5252)
        self.assertAlmostEqual(ret[1][0], 1.24)
        self.assertAlmostEqual(ret[2][0], 1.23)

400

401
class TestCondBackward(unittest.TestCase):
402
    def backward_value_helper(self, cond_func, use_cuda, use_parallel_exe):
403 404 405
        """
        Helper function that compares calculated backward value is close to dy/dx
        """
406
        paddle.enable_static()
407 408 409 410 411 412 413 414 415 416 417
        main_program = Program()
        main_program.random_seed = 123
        startup_program = Program()
        startup_program.random_seed = 123
        with program_guard(main_program, startup_program):
            img = fluid.data(name='image', shape=[-1, 9], dtype='float32')
            img.stop_gradient = False
            label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
            i = fluid.data(name="i", shape=[1], dtype='int32')
            loss = cond_func(i, img, label)
            append_backward(loss)
418
        place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
419 420 421
        exe = fluid.Executor(place)
        exe.run(startup_program)

422 423 424
        num_devices = 1
        if use_parallel_exe:
            os.environ['CPU_NUM'] = str(2)
425 426 427 428 429
            exe = fluid.ParallelExecutor(
                use_cuda=use_cuda,
                main_program=main_program,
                loss_name=loss.name,
            )
430 431
            num_devices = exe.device_count

432 433 434
        delta = 0.005
        for feed_i in range(0, 10):
            feed_img = np.random.random(size=[1, 9]).astype(np.float32)
435 436 437
            feed_label = np.random.randint(
                low=0, high=10, size=[1, 1], dtype=np.int64
            )
438 439 440 441
            if use_parallel_exe:
                img_grad, loss_value = exe.run(
                    feed={
                        'i': np.full((num_devices), feed_i, np.int32),
442
                        'image': np.repeat(feed_img, num_devices, axis=0),
443
                        'label': np.repeat(feed_label, num_devices, axis=0),
444
                    },
445 446
                    fetch_list=[img.grad_name, loss.name],
                )
447 448 449 450 451 452
            else:
                img_grad, loss_value = exe.run(
                    main_program,
                    feed={
                        'i': np.full((1), feed_i, np.int32),
                        'image': feed_img,
453
                        'label': feed_label,
454
                    },
455 456
                    fetch_list=[img.grad_name, loss.name],
                )
457

458
            numerical_grad = np.zeros(shape=[num_devices, 9], dtype=np.float32)
459 460 461
            feed_img_delta = np.copy(feed_img)
            for j in range(9):
                feed_img_delta[0][j] = feed_img[0][j] + delta
462
                if use_parallel_exe:
463 464 465 466 467 468 469 470 471 472 473 474 475
                    loss_delta = exe.run(
                        feed={
                            'i': np.full((num_devices), feed_i, np.int32),
                            'image': np.repeat(
                                feed_img_delta, num_devices, axis=0
                            ),
                            'label': np.repeat(feed_label, num_devices, axis=0),
                        },
                        fetch_list=[loss.name],
                    )
                    multi_device_grad = (
                        (loss_delta[0] - loss_value[0]) / delta / num_devices
                    )
476 477 478
                    for d in range(num_devices):
                        numerical_grad[d][j] = multi_device_grad[d]
                else:
479 480 481 482 483 484 485 486 487 488 489 490
                    loss_delta = exe.run(
                        main_program,
                        feed={
                            'i': np.full((1), feed_i, np.int32),
                            'image': feed_img_delta,
                            'label': feed_label,
                        },
                        fetch_list=[loss.name],
                    )
                    numerical_grad[0][j] = (
                        loss_delta[0] - loss_value[0]
                    ) / delta
491
                feed_img_delta[0][j] = feed_img[0][j]
492 493 494
            np.testing.assert_allclose(
                img_grad, numerical_grad, rtol=0.05, atol=0.05
            )
495

496
    def add_optimizer_helper(self, cond_func, use_cuda, use_parallel_exe):
497 498 499 500 501 502 503 504 505 506 507 508 509
        """
        Test that program is runnable when add optimizer
        """
        main_program = Program()
        startup_program = Program()
        with program_guard(main_program, startup_program):
            img = fluid.data(name='image', shape=[-1, 784], dtype='float32')
            label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
            i = fluid.data(name="i", shape=[1], dtype='int32')
            loss = cond_func(i, img, label)
            optimizer = fluid.optimizer.SGD(learning_rate=0.1)
            optimizer.minimize(loss)

510
        place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
511 512
        exe = fluid.Executor(place)
        exe.run(startup_program)
513 514
        if use_parallel_exe:
            os.environ['CPU_NUM'] = str(2)
515 516 517 518 519
            exe = fluid.ParallelExecutor(
                use_cuda=use_cuda,
                main_program=main_program,
                loss_name=loss.name,
            )
520
            num_devices = exe.device_count
521 522 523

        for feed_i in range(0, 10):
            feed_img = np.random.random(size=[16, 784]).astype(np.float32)
524 525 526
            feed_label = np.random.randint(
                low=0, high=10, size=[16, 1], dtype=np.int64
            )
527
            if use_parallel_exe:
528 529 530 531 532 533 534 535
                exe.run(
                    feed={
                        'i': np.full((num_devices), feed_i, np.int32),
                        'image': np.repeat(feed_img, num_devices, axis=0),
                        'label': np.repeat(feed_label, num_devices, axis=0),
                    },
                    fetch_list=[loss.name],
                )
536
            else:
537 538 539 540 541 542 543 544 545
                exe.run(
                    main_program,
                    feed={
                        'i': np.full((1), feed_i, np.int32),
                        'image': feed_img,
                        'label': feed_label,
                    },
                    fetch_list=[loss],
                )
546 547

    def test_cond_backward(self):
548

549 550
        paddle.enable_static()

551
        def cond_func(i, img, label):
552
            predicate = (i % 2) == 0
553 554 555
            return layers.cond(
                predicate,
                lambda: simple_fc_net_with_inputs(img, label, class_num=10),
556 557
                lambda: batchnorm_fc_with_inputs(img, label, class_num=10),
            )
558

559
        for use_parallel_exe in [False, True]:
560 561 562 563 564 565
            if use_parallel_exe and os.name == "nt":
                print(
                    "Skip use_parallel_exe=True in Windows because of flaky test when using PE under old Windows machine"
                )
                continue

566 567 568 569 570 571
            self.backward_value_helper(
                cond_func, core.is_compiled_with_cuda(), use_parallel_exe
            )
            self.add_optimizer_helper(
                cond_func, core.is_compiled_with_cuda(), use_parallel_exe
            )
572 573

    def test_half_nested_cond_backward(self):
574
        paddle.enable_static()
575

576
        def branch(i, img, label):
577 578 579
            return layers.cond(
                (i % 2) == 0,
                lambda: simple_fc_net_with_inputs(img, label, class_num=10),
580 581
                lambda: batchnorm_fc_with_inputs(img, label, class_num=10),
            )
582 583

        def cond_func_simple_net_at_true(i, img, label):
584 585 586
            return layers.cond(
                i < 5, lambda: branch(i, img, label), lambda: paddle.mean(img)
            )
587 588

        def cond_func_simple_net_at_false(i, img, label):
589 590 591
            return layers.cond(
                i < 5, lambda: paddle.mean(img), lambda: branch(i, img, label)
            )
592

593
        for use_parallel_exe in [False, True]:
594 595 596 597 598 599
            if use_parallel_exe and os.name == "nt":
                print(
                    "Skip use_parallel_exe=True in Windows because of flaky test when using PE under old Windows machine"
                )
                continue

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
            self.backward_value_helper(
                cond_func_simple_net_at_true,
                core.is_compiled_with_cuda(),
                use_parallel_exe,
            )
            self.add_optimizer_helper(
                cond_func_simple_net_at_true,
                core.is_compiled_with_cuda(),
                use_parallel_exe,
            )
            self.backward_value_helper(
                cond_func_simple_net_at_false,
                core.is_compiled_with_cuda(),
                use_parallel_exe,
            )
            self.add_optimizer_helper(
                cond_func_simple_net_at_false,
                core.is_compiled_with_cuda(),
                use_parallel_exe,
            )
620 621

    def test_nested_cond_backward(self):
622
        paddle.enable_static()
623

624 625
        def branch(i, img, label, mod_two):
            if mod_two:
626
                predicate = (i % 2) == 0
627
            else:
628
                predicate = (i % 2) != 0
629 630 631
            return layers.cond(
                predicate,
                lambda: simple_fc_net_with_inputs(img, label, class_num=10),
632 633
                lambda: batchnorm_fc_with_inputs(img, label, class_num=10),
            )
634 635

        def cond_func(i, img, label):
636 637 638 639 640
            return layers.cond(
                i < 5,
                lambda: branch(i, img, label, True),
                lambda: branch(i, img, label, False),
            )
641

642
        for use_parallel_exe in [False, True]:
643 644 645 646 647
            if use_parallel_exe and os.name == "nt":
                print(
                    "Skip use_parallel_exe=True in Windows because of flaky test when using PE under old Windows machine"
                )
                continue
648 649 650 651 652 653
            self.backward_value_helper(
                cond_func, core.is_compiled_with_cuda(), use_parallel_exe
            )
            self.add_optimizer_helper(
                cond_func, core.is_compiled_with_cuda(), use_parallel_exe
            )
654 655


656 657
class TestCondWithError(unittest.TestCase):
    def test_input_type_error(self):
658
        paddle.enable_static()
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
        main_program = framework.Program()
        startup_program = framework.Program()
        with framework.program_guard(main_program, startup_program):
            pred = fluid.data(name='y', shape=[1], dtype='bool')

            def func():
                return pred

            with self.assertRaises(TypeError):
                layers.cond(None, func, func)

            with self.assertRaises(TypeError):
                layers.cond(pred, func, set())

            with self.assertRaises(TypeError):
                layers.cond(pred, set(), func)

            with self.assertRaises(TypeError):
                layers.cond(pred, func, func, set())


680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
class TestCondWithDict(unittest.TestCase):
    def test_input_with_dict(self):
        paddle.enable_static()
        main_program = framework.Program()
        startup_program = framework.Program()
        with framework.program_guard(main_program, startup_program):

            def true_func():
                return {
                    '1': paddle.full(shape=[3, 2], dtype='int32', fill_value=1),
                    '2': paddle.full(
                        shape=[2, 3], dtype='bool', fill_value=True
                    ),
                }

            def false_func():
                return {
                    '1': paddle.full(
                        shape=[3, 4], dtype='float32', fill_value=3
                    ),
                    '2': paddle.full(shape=[4, 5], dtype='int64', fill_value=2),
                }

            x = paddle.full(shape=[1], dtype='float32', fill_value=0.1)
            y = paddle.full(shape=[1], dtype='float32', fill_value=0.23)
            pred = paddle.less_than(x=x, y=y, name=None)
            ret = paddle.static.nn.cond(pred, true_func, false_func)
            self.assertEqual(
                ret['1'].shape,
                (3, -1),
                f"The shape is not correct, expects (3, -1) but gets {ret['1'].shape}.",
            )
            self.assertEqual(
                ret['2'].shape,
                (-1, -1),
                f"The shape is not correct, expects (-1, -1) but gets {ret['2'].shape}.",
            )


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