gradient_checker.py 30.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2019 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.
J
Jiawei Wang 已提交
14
"""This is the lib for gradient checker unittest."""
15 16 17

import numpy as np
from itertools import product
18
import paddle
19 20 21 22

import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.backward import _append_grad_suffix_, _as_list
23
from paddle.fluid.framework import _test_eager_guard
24

25
from collections.abc import Sequence
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


def _product(t):
    if isinstance(t, int):
        return t
    else:
        return np.product(t)


def dtype_to_np_dtype(dtype):
    if dtype == core.VarDesc.VarType.FP32:
        return np.float32
    elif dtype == core.VarDesc.VarType.FP64:
        return np.float64
    elif dtype == core.VarDesc.VarType.FP16:
        return np.float16
    else:
        raise ValueError("Not supported data type " + str(dtype))


def _get_item(t, i, np_dtype):
    if np_dtype == np.float16:
        np_t = np.array(t).astype(np.float16)
        np_t = np_t.flatten()
        return np_t[i]
    elif np_dtype == np.float32:
        return t._get_float_element(i)
    elif np_dtype == np.float64:
        return t._get_double_element(i)
    else:
        raise ValueError("Not supported data type " + str(np_dtype))


59
def _set_item(t, i, e, np_dtype, place):
60 61 62 63 64
    if np_dtype == np.float16:
        np_t = np.array(t).astype(np.float16)
        shape = np_t.shape
        np_t = np_t.flatten()
        np_t[i] = e
65
        np_t = np_t.reshape(shape)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        t.set(np_t, place)
    elif np_dtype == np.float32:
        t._set_float_element(i, e)
    elif np_dtype == np.float64:
        t._set_double_element(i, e)
    else:
        raise ValueError("Not supported data type " + str(np_dtype))


def set_var_in_scope(scope, place, name, value, recursive_seq_len=None):
    t = scope.var(name).get_tensor()
    t.set(value, place)
    if recursive_seq_len:
        t.set_recursive_sequence_lengths(recursive_seq_len)
    return t


Q
qingqing01 已提交
83 84 85 86
def var_to_np_array_in_scope(scope, place, name):
    return np.array(scope.var(name).get_tensor())


87 88 89
def make_jacobian(x, y_size, np_dtype):
    if isinstance(x, fluid.framework.Variable):
        return np.zeros((_product(x.shape), y_size), dtype=np_dtype)
90
    elif isinstance(x, Sequence):
91
        jacobians = list(
92 93 94 95 96
            filter(
                lambda t: t is not None,
                (make_jacobian(item, y_size, np_dtype) for item in x),
            )
        )
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        return jacobians
    else:
        None


def _compute_numerical_jacobian(program, x, y, place, scope, delta):
    """Computes the numeric Jacobian for dy/dx.

    Computes the numeric Jacobian by slightly perturbing the inputs and
    measuring the differences on the output.

    Args:
        program (Program): the network program.
        x (Variable): the input variables.
        y (list[Variable]): the output variables.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        scope (Scope): the scope used to run program.
        delta: the amount of perturbation we give to the input

    Returns:
        A list of 2-D numpy array, the list length is len(y).
        Each 2-D numpy array represents the Jacobian for dy_i/dx.
        It has "x_size" rows and "y_size" columns
        where "x_size" is the number of elements in x and
        "y_size" is the number of elements in each y_i.
    """
    if not isinstance(x, fluid.framework.Variable):
        raise TypeError('x is not Variable')

    # To compute the jacobian, treat x and y as one-dimensional vectors.
    y = _as_list(y)
    exe = fluid.Executor(place)

    def run():
        y_res = exe.run(program, scope=scope, fetch_list=y)
        return [yi.flatten() for yi in y_res]

    x_name = x.name
    x_shape = x.shape
    x_size = _product(x_shape)
    x_t = scope.find_var(x_name).get_tensor()

    np_type = dtype_to_np_dtype(x.dtype)
    jacobian = [make_jacobian(x, _product(yi.shape), np_type) for yi in y]

142
    for i in range(x_size):
143 144
        orig = _get_item(x_t, i, np_type)
        x_pos = orig + delta
145
        _set_item(x_t, i, x_pos, np_type, place)
146 147 148
        y_pos = run()

        x_neg = orig - delta
149
        _set_item(x_t, i, x_neg, np_type, place)
150 151
        y_neg = run()

152
        _set_item(x_t, i, orig, np_type, place)
153

154
        for j in range(len(y)):
155
            jacobian[j][i, :] = (y_pos[j] - y_neg[j]) / delta / 2.0
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

    return jacobian


def _compute_analytical_jacobian(program, x, y, place, scope):
    """Computes the analytical Jacobian for dy/dx.

    Args:
        program (Program): a Program with forward pass.
        x (Variable|list[Variable]): a variable or list of variable
        y (Variable): the target variable.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        scope (Scope): the scope used to run program.

    Returns:
        A list of 2-D numpy array. The list length is len(x).
        Each 2-D numpy array represents the Jacobian for dy/dx_i.
        It has "xi_size" rows and "dy_size" columns
        where "x_size" is the number of elements in x_i and
        "dy_size" is the number of elements in y.
    """
    if not isinstance(y, fluid.framework.Variable):
        raise TypeError('y is not Variable')

    dy_name = _append_grad_suffix_(y.name)

    np_type = dtype_to_np_dtype(y.dtype)
    # create dy Variable in Program
184 185 186
    dy = program.global_block().create_var(
        name=dy_name, shape=y.shape, dtype=np_type, persistable=True
    )
187
    # append backward
188
    dx = fluid.gradients(y, x, dy)
189 190 191 192 193 194 195 196 197 198 199 200

    # init dy tensor in scope
    value = np.zeros(y.shape, dtype=np_type)
    dy_t = set_var_in_scope(scope, place, dy_name, value)

    exe = fluid.Executor(place)

    y_size = _product(y.shape)

    x = _as_list(x)
    jacobian = make_jacobian(x, y_size, np_type)

201 202 203 204 205
    # filter None in dx for DX/DY may be None in kernel
    # only fetch not None dx in exe.run
    filted = [(i, dxi) for i, dxi in enumerate(dx) if dxi is not None]
    filted_idx, filted_dx = zip(*filted)

206
    for i in range(y_size):
207
        _set_item(dy_t, i, 1, np_type, place)
208

209
        dx_res = exe.run(program, scope=scope, fetch_list=filted_dx)
210

211
        for j in range(len(filted_dx)):
212
            dx_idx = filted_idx[j]
Q
qingqing01 已提交
213
            if dx_res[j] is not None:
214
                jacobian[dx_idx][:, i] = dx_res[j].flatten()
Q
qingqing01 已提交
215
            else:
216 217 218
                jacobian[dx_idx][:, i] = np.zeros(
                    dx[dx_idx].shape, dtype=np_type
                ).flatten()
Q
qingqing01 已提交
219

220
        _set_item(dy_t, i, 0, np_type, place)
221 222 223 224

    return jacobian


225 226 227 228 229 230 231 232 233 234 235
def grad_check(
    x,
    y,
    x_init=None,
    place=None,
    program=None,
    eps=1e-6,
    atol=1e-5,
    rtol=1e-3,
    raise_exception=True,
):
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    """
    Check numerical and analytical gradients for dy/dx.
    Each Jacobian gradients is a 2-D array with shape [xi_size, yi_size].

    Args:
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        program (Program|None): a Program with forward pass.
            If None, use fluid.default_main_program().
        eps (float): perturbation for finite differences.
        atol (float): absolute tolerance.
        rtol (float): relative tolerance.
        raise_exception (bool): whether to raise an exception if
            the check fails. Default is True.
    Returns:
        True if all differences satisfy numpy.allclose condition.
    """

    def fail_test(msg):
        if raise_exception:
            raise RuntimeError(msg)
        return False

    # check input arguments
    x = _as_list(x)
    y = _as_list(y)
Q
qingqing01 已提交
264

265 266 267
    for v in x:
        v.stop_gradient = False
        v.persistable = True
268 269 270
    for u in y:
        u.stop_gradient = False
        u.persistable = True
271 272 273 274 275 276 277 278 279 280 281 282 283 284
    if place is None:
        place = fluid.CPUPlace()
    if program is None:
        program = fluid.default_main_program()

    # init variable in strtup program
    scope = fluid.executor.global_scope()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    x_init = _as_list(x_init)
    # init inputs if x_init is not None
    if x_init:
        if len(x_init) != len(x):
285 286 287 288
            raise ValueError(
                'len(x_init) (=%d) is not the same'
                ' as len(x) (= %d)' % (len(x_init), len(x))
            )
289 290 291 292 293 294 295 296 297 298 299 300 301
        # init variable in main program
        for var, arr in zip(x, x_init):
            assert var.shape == arr.shape
        feeds = {k.name: v for k, v in zip(x, x_init)}
        exe.run(program, feed=feeds, scope=scope)

    # [x_idx, y_idx]
    numerical = [
        _compute_numerical_jacobian(program, xi, y, place, scope, eps)
        for xi in x
    ]

    # [y_idx, x_idx]
Q
qingqing01 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    analytical = []
    for yi in y:
        prog = program.clone()

        clone_x = []
        clone_y = None
        for b in prog.blocks:
            if b.has_var(yi.name):
                clone_y = b.var(yi.name)
                break
        for xi in x:
            for b in prog.blocks:
                if b.has_var(xi.name):
                    clone_x.append(b.var(xi.name))
                    break
        analytical.append(
318 319
            _compute_analytical_jacobian(prog, clone_x, clone_y, place, scope)
        )
320

321
    for i, (x_idx, y_idx) in enumerate(
322 323
        product(*[range(len(x)), range(len(y))])
    ):
324 325 326
        a = analytical[y_idx][x_idx]
        n = numerical[x_idx][y_idx]
        if not np.allclose(a, n, rtol, atol):
327 328 329 330 331 332
            msg = (
                'Jacobian mismatch for output %s '
                'with respect to input %s on %s,\n'
                'numerical:%s\nanalytical:%s\n'
                % (y[y_idx].name, x[x_idx].name, str(place), n, a)
            )
333 334 335 336
            return fail_test(msg)
    return True


337 338 339 340 341 342 343 344 345 346 347 348
def double_grad_check(
    x,
    y,
    x_init=None,
    y_grads=None,
    place=None,
    program=None,
    eps=1e-6,
    atol=1e-5,
    rtol=1e-3,
    raise_exception=True,
):
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
    """
    Check gradients of gradients. This function will append backward to the
    program before second order gradient check.

    Args:
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        y_grads (numpy.array|list[numpy.array]|None): the gradients with respect to y.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        program (Program|None): a Program with forward pass.
            If None, use fluid.default_main_program().
        eps (float): perturbation for finite differences.
        atol (float): absolute tolerance.
        rtol (float): relative tolerance.
        raise_exception (bool): whether to raise an exception if
            the check fails. Default is True.
    Returns:
        True if all differences satisfy numpy.allclose condition.
    """
    # check input arguments
    x = _as_list(x)
    for v in x:
        v.stop_gradient = False
        v.persistable = True
    y = _as_list(y)
375 376 377
    for u in y:
        u.stop_gradient = False
        u.persistable = True
378 379 380 381 382 383 384

    if program is None:
        program = fluid.default_main_program()

    if y_grads is None:
        scope = fluid.executor.global_scope()
        y_grads = []
Q
qingqing01 已提交
385
        y_grads_init = []
386 387 388
        for yi in y:
            dyi_name = _append_grad_suffix_(yi.name)
            np_type = dtype_to_np_dtype(yi.dtype)
389 390 391
            dy = program.global_block().create_var(
                name=dyi_name, shape=yi.shape, dtype=np_type, persistable=True
            )
392 393 394 395
            dy.stop_gradient = False
            v = np.random.random(size=yi.shape).astype(np_type)
            set_var_in_scope(scope, place, dyi_name, v)
            y_grads.append(dy)
Q
qingqing01 已提交
396
            y_grads_init.append(v)
397 398
    else:
        y_grads = _as_list(y_grads)
Q
qingqing01 已提交
399 400 401
        y_grads_init = [
            var_to_np_array_in_scope(scope, place, v.name) for v in y_grads
        ]
402 403

    # append first order grads
404
    target_grads = fluid.gradients(y, x, y_grads)
Q
qingqing01 已提交
405 406 407 408 409 410 411

    # y_grads are the input of first-order backward,
    # so, they are also the input of second-order backward.
    x += y_grads
    x_init = _as_list(x_init)
    x_init += y_grads_init

412
    grad_check(x, target_grads, x_init, place, program, eps, atol, rtol)
413 414


415
# TODO(jiabin): We currently support only triple grad check here, extend this to support
416 417 418 419
# higher order differenciation later.


# check triple grad and two outputs of the triple Kernel
420 421 422 423 424 425 426 427 428 429 430 431 432
def triple_grad_check(
    x,
    y,
    x_init=None,
    y_grads=None,
    x_grads_grads=None,
    place=None,
    program=None,
    eps=1e-6,
    atol=1e-5,
    rtol=1e-3,
    raise_exception=True,
):
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    """
    Check triple gradients. This function will append backward to the
    program before third order gradient check.

    Args:
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        y_grads (numpy.array|list[numpy.array]|None): the gradients with respect to y.
        x_grads_grads (numpy.array|list[numpy.array]|None): the gradients with respect to your input.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        program (Program|None): a Program with forward pass.
            If None, use fluid.default_main_program().
        eps (float): perturbation for finite differences.
        atol (float): absolute tolerance.
        rtol (float): relative tolerance.
        raise_exception (bool): whether to raise an exception if
            the check fails. Default is True.
    Returns:
        True if all differences satisfy numpy.allclose condition.
    """
    # check input arguments
    x = _as_list(x)
    for v in x:
        v.stop_gradient = False
        v.persistable = True
    y = _as_list(y)
460 461 462
    for u in y:
        u.stop_gradient = False
        u.persistable = True
463 464 465 466 467 468 469 470 471 472 473

    if program is None:
        program = fluid.default_main_program()

    if y_grads is None:
        scope = fluid.executor.global_scope()
        y_grads = []
        y_grads_init = []
        for yi in y:
            dyi_name = _append_grad_suffix_(yi.name)
            np_type = dtype_to_np_dtype(yi.dtype)
474 475 476
            dy = program.global_block().create_var(
                name=dyi_name, shape=yi.shape, dtype=np_type, persistable=True
            )
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
            dy.stop_gradient = False
            v = np.random.random(size=yi.shape).astype(np_type)
            set_var_in_scope(scope, place, dyi_name, v)
            y_grads.append(dy)
            y_grads_init.append(v)
    else:
        y_grads = _as_list(y_grads)
        y_grads_init = [
            var_to_np_array_in_scope(scope, place, v.name) for v in y_grads
        ]

    # append first order grads
    target_grads = fluid.gradients(y, x, y_grads)

    if x_grads_grads is None:
        scope = fluid.executor.global_scope()
        x_grads_grads = []
        x_grads_grads_init = []
        for dxi in target_grads:
            ddxi_name = _append_grad_suffix_(dxi.name)
            np_type = dtype_to_np_dtype(dxi.dtype)
498 499 500
            ddx = program.global_block().create_var(
                name=ddxi_name, shape=dxi.shape, dtype=np_type, persistable=True
            )
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
            ddx.stop_gradient = False
            v = np.random.random(size=dxi.shape).astype(np_type)
            set_var_in_scope(scope, place, ddxi_name, v)
            x_grads_grads.append(ddx)
            x_grads_grads_init.append(v)
    else:
        x_grads_grads = _as_list(x_grads_grads)
        x_grads_grads_init = [
            var_to_np_array_in_scope(scope, place, v.name)
            for v in x_grads_grads
        ]
    x += y_grads
    x_init = _as_list(x_init)
    x_init += y_grads_init

516 517 518 519
    # append second order grads
    target_grads_grads = fluid.gradients(target_grads, x, x_grads_grads)

    # filter None in target_grads_grads for Dy/Dx may be None in kernel
520 521 522
    filted = [
        (i, dyi) for i, dyi in enumerate(target_grads_grads) if dyi is not None
    ]
523 524
    filted_idx, filted_target_grads_grads = zip(*filted)

525 526 527 528
    x += x_grads_grads
    x_init += x_grads_grads_init

    # x <=> [x, dout, ddx]
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    grad_check(
        x=x,
        y=filted_target_grads_grads,
        x_init=x_init,
        place=place,
        program=program,
        eps=eps,
        atol=atol,
        rtol=rtol,
    )


def get_static_double_grad(
    x, y, x_init=None, dy_init=None, place=None, program=None
):
544 545 546 547 548 549 550 551 552
    """
    Get Double Grad result of static graph.

    Args:
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        dy_init (numpy.array|list[numpy.array]|None): the init value for output y.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
553 554
        program (Program|None): a Program with forward pass.
            If None, use fluid.default_main_program().
555 556 557 558
    Returns:
        A list of numpy array that stores second derivative result calulated by static graph.
    """

559 560
    if program is None:
        program = fluid.default_main_program()
561 562
    scope = fluid.executor.global_scope()
    y_grads = []
563
    for i in range(len(y)):
564 565 566
        yi = y[i]
        dyi_name = _append_grad_suffix_(yi.name)
        np_type = dtype_to_np_dtype(yi.dtype)
567 568 569
        dy = program.global_block().create_var(
            name=dyi_name, shape=yi.shape, dtype=np_type, persistable=True
        )
570 571 572 573 574 575 576 577 578 579 580
        dy.stop_gradient = False
        set_var_in_scope(scope, place, dyi_name, dy_init[i])
        y_grads.append(dy)

    # append first order grads
    dx = fluid.gradients(y, x, y_grads)

    # y_grads are the input of first-order backward,
    # so, they are also the input of second-order backward.
    x += y_grads
    x_init += dy_init
581 582 583 584

    # filter None in dx for DX/DY may be None in kernel
    filted_dx = [dxi for dxi in dx if dxi is not None]
    y = filted_dx
585 586 587 588 589 590 591 592

    # check input arguments
    x = _as_list(x)
    y = _as_list(y)

    for v in x:
        v.stop_gradient = False
        v.persistable = True
593 594 595
    for u in y:
        u.stop_gradient = False
        u.persistable = True
596 597 598 599 600 601 602 603 604 605 606 607 608 609
    if place is None:
        place = fluid.CPUPlace()
    if program is None:
        program = fluid.default_main_program()

    # init variable in strtup program
    scope = fluid.executor.global_scope()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    x_init = _as_list(x_init)
    # init inputs if x_init is not None
    if x_init:
        if len(x_init) != len(x):
610 611 612 613
            raise ValueError(
                'len(x_init) (=%d) is not the same'
                ' as len(x) (= %d)' % (len(x_init), len(x))
            )
614 615 616 617 618 619 620 621 622 623 624
        # init variable in main program
        for var, arr in zip(x, x_init):
            assert var.shape == arr.shape
        feeds = {k.name: v for k, v in zip(x, x_init)}
        exe.run(program, feed=feeds, scope=scope)

    dys = []
    for yi in y:
        np_type = dtype_to_np_dtype(yi.dtype)
        dy_name = _append_grad_suffix_(yi.name)
        # create dy Variable in Program
625 626 627
        dy = program.global_block().create_var(
            name=dy_name, shape=yi.shape, dtype=np_type, persistable=True
        )
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
        # init dy tensor in scope
        value = np.ones(yi.shape, dtype=np_type)
        dy_t = set_var_in_scope(scope, place, dy_name, value)
        dys.append(dy)

    # append second order backward
    ddx = fluid.gradients(y, x, dys)
    exe = fluid.Executor(place)

    # filter None in dx for DX/DY may be None in kernel
    # only fetch not None dx in exe.run
    filted = [(i, dxi) for i, dxi in enumerate(ddx) if dxi is not None]
    filted_idx, filted_ddx = zip(*filted)
    ddx_res = exe.run(program, scope=scope, fetch_list=filted_ddx)

    return ddx_res


646 647 648
def get_eager_double_grad(
    func, x_init=None, dy_init=None, place=None, return_mid_result=False
):
649 650 651 652 653 654 655
    """
    Get Double Grad result of dygraph.

    Args:
        func: A wrapped dygraph function that its logic is equal to static program
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        dy_init (numpy.array|list[numpy.array]|None): the init value for gradient of output.
656
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
657
        return_mid_result (bool): A flag that controls the return content.
658
    Returns:
659
        If 'return_mid_result' set True.
660 661
        the second order derivative and the inputs of second order derivative's calculation
        will be returned for higher order derivative's calculation.
662
        If 'return_mid_result' set False.
663
        A list of numpy array that stores second derivative result calulated by dygraph.
664
    """
665 666 667 668
    if isinstance(place, fluid.CPUPlace):
        paddle.set_device("cpu")
    if isinstance(place, fluid.CUDAPlace):
        paddle.set_device("gpu")
669 670 671 672 673 674 675 676 677 678 679 680
    inputs = []
    dys = []
    for x in x_init:
        input_tensor = paddle.to_tensor(x)
        input_tensor.stop_gradient = False
        inputs.append(input_tensor)
    for dy in dy_init:
        dy_tensor = paddle.to_tensor(dy)
        dy_tensor.stop_gradient = False
        dys.append(dy_tensor)
    # calculate first derivative
    outputs = func(inputs)
681 682 683 684 685 686 687
    d_inputs = paddle.grad(
        outputs=outputs,
        inputs=inputs,
        grad_outputs=dys,
        create_graph=True,
        allow_unused=True,
    )
688
    d_inputs = [d_input for d_input in d_inputs if d_input is not None]
689 690 691 692

    # calcluate second derivative
    inputs = inputs + dys
    ddys = []
693 694 695 696 697
    if return_mid_result:
        create_graph = True
    else:
        create_graph = False

698 699 700 701 702
    for d_input in d_inputs:
        d_input.stop_gradient = False
        ddy = paddle.ones(shape=d_input.shape, dtype=d_input.dtype)
        ddy.stop_gradient = False
        ddys.append(ddy)
703

704 705 706 707 708 709 710
    dd_inputs = paddle.grad(
        outputs=d_inputs,
        inputs=inputs,
        grad_outputs=ddys,
        create_graph=create_graph,
        allow_unused=True,
    )
711

712
    if return_mid_result:
713 714 715
        return [
            dd_input for dd_input in dd_inputs if dd_input is not None
        ], inputs + ddys
716
    else:
717 718 719
        return [
            dd_input.numpy() for dd_input in dd_inputs if dd_input is not None
        ]
720 721


722 723 724 725 726 727 728 729 730 731
def double_grad_check_for_dygraph(
    func,
    x,
    y,
    x_init=None,
    place=None,
    atol=1e-5,
    rtol=1e-3,
    raise_exception=True,
):
732
    """
733 734
    Check second order gradients of dygraph. This function will compare the
    second order gradients of dygraph and second order gradients of static graph
735
    to validate dygraph's correctness
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

    Args:
        func: A wrapped dygraph function that its logic is equal to static program
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        atol (float): absolute tolerance.
        rtol (float): relative tolerance.
        raise_exception (bool): whether to raise an exception if
            the check fails. Default is True.
    """

    def fail_test(msg):
        if raise_exception:
            raise RuntimeError(msg)
        return False

    # check input arguments
    x = _as_list(x)
    for v in x:
        v.stop_gradient = False
        v.persistable = True
    y = _as_list(y)
760 761 762
    for u in y:
        u.stop_gradient = False
        u.persistable = True
763 764 765 766 767 768 769 770 771 772
    y_grads_init = []
    for yi in y:
        np_type = dtype_to_np_dtype(yi.dtype)
        v = np.random.random(size=yi.shape).astype(np_type)
        y_grads_init.append(v)

    x_init = _as_list(x_init)

    paddle.disable_static()
    with _test_eager_guard():
773 774 775
        eager_double_grad = get_eager_double_grad(
            func, x_init, y_grads_init, place
        )
776 777
    paddle.enable_static()

778 779 780
    static_double_grad = get_static_double_grad(
        x, y, x_init, y_grads_init, place
    )
781

782
    if len(static_double_grad) != len(eager_double_grad):
783 784
        msg = (
            "The output grad tensor's number of static graph is different with dygraph, "
785
            "please check the python api unit test used."
786
        )
787 788
        raise RuntimeError(msg)

789
    for i in range(len(static_double_grad)):
790 791 792 793 794 795 796
        if not np.allclose(
            static_double_grad[i], eager_double_grad[i], rtol, atol
        ):
            msg = (
                'Check eager double result fail. Mismatch between static_graph double grad '
                'and eager double grad on %s, the output double grad tensor\'s index is : %d \n'
                'static:%s\n eager:%s\n'
797
                % (str(place), i, static_double_grad[i], eager_double_grad[i])
798
            )
799
            return fail_test(msg)
800 801


802 803 804
def get_static_triple_grad(
    x, y, x_init=None, dy_init=None, place=None, program=None
):
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    """
    Get Triple Grad result of static graph.

    Args:
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        dy_init (numpy.array|list[numpy.array]|None): the init value for output y.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        program (Program|None): a Program with forward pass.
            If None, use fluid.default_main_program().
    Returns:
        A list of numpy array that stores third derivative result calulated by static graph.
    """
    if program is None:
        program = fluid.default_main_program()
    scope = fluid.executor.global_scope()
    y_grads = []
823
    for i in range(len(y)):
824 825 826
        yi = y[i]
        dyi_name = _append_grad_suffix_(yi.name)
        np_type = dtype_to_np_dtype(yi.dtype)
827 828 829
        dy = program.global_block().create_var(
            name=dyi_name, shape=yi.shape, dtype=np_type, persistable=True
        )
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
        dy.stop_gradient = False
        set_var_in_scope(scope, place, dyi_name, dy_init[i])
        y_grads.append(dy)

    # append first order grads
    dx = fluid.gradients(y, x, y_grads)

    # y_grads are the input of first-order backward,
    # so, they are also the input of second-order backward.
    x += y_grads
    x_init += dy_init
    y = dx

    x_grads_grads_init = []
    for dxi in dx:
        np_type = dtype_to_np_dtype(dxi.dtype)
        value = np.ones(dxi.shape, dtype=np_type)
        x_grads_grads_init.append(value)

849 850 851
    return get_static_double_grad(
        x, y, x_init, dy_init=x_grads_grads_init, place=place, program=program
    )
852 853


854 855 856
def get_eager_triple_grad(
    func, x_init=None, dy_init=None, place=None, return_mid_result=False
):
857 858 859 860 861 862 863
    """
    Get triple Grad result of dygraph.

    Args:
        func: A wrapped dygraph function that its logic is equal to static program
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        dy_init (numpy.array|list[numpy.array]|None): the init value for gradient of output.
864
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
865
        return_mid_result (list[Tensor], list[Tensor]): If set True, the
866 867 868
    Returns:
        A list of numpy array that stores second derivative result calulated by dygraph
    """
869 870 871
    dd_y, dd_x = get_eager_double_grad(
        func, x_init, dy_init, place, return_mid_result=True
    )
872 873 874 875 876 877 878 879

    # calcluate third derivative
    dddys = []
    for dd_yi in dd_y:
        dd_yi.stop_gradient = False
        dddy = paddle.ones(shape=dd_yi.shape, dtype=dd_yi.dtype)
        dddy.stop_gradient = False
        dddys.append(dddy)
880 881 882
    ddd_inputs = paddle.grad(
        outputs=dd_y, inputs=dd_x, grad_outputs=dddys, allow_unused=True
    )
883 884 885
    return [
        ddd_input.numpy() for ddd_input in ddd_inputs if ddd_input is not None
    ]
886 887


888 889 890 891 892 893 894 895 896 897
def triple_grad_check_for_dygraph(
    func,
    x,
    y,
    x_init=None,
    place=None,
    atol=1e-5,
    rtol=1e-3,
    raise_exception=True,
):
898
    """
899 900
    Check third order gradients of dygraph. This function will compare the
    third order gradients of dygraph and third order gradients of static graph
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
    to validate dygraph's correctness

    Args:
        func: A wrapped dygraph function that its logic is equal to static program
        x (Variable|list[Variable]): input variables to the program.
        y (Variable|list[Variable]): output variables to the program.
        x_init (numpy.array|list[numpy.array]|None): the init value for input x.
        place (fluid.CPUPlace or fluid.CUDAPlace): the device.
        atol (float): absolute tolerance.
        rtol (float): relative tolerance.
        raise_exception (bool): whether to raise an exception if
            the check fails. Default is True.
    """

    def fail_test(msg):
        if raise_exception:
            raise RuntimeError(msg)
        return False

    # check input arguments
    x = _as_list(x)
    for v in x:
        v.stop_gradient = False
        v.persistable = True
    y = _as_list(y)
926 927 928
    for u in y:
        u.stop_gradient = False
        u.persistable = True
929 930 931 932 933 934 935 936 937 938
    y_grads_init = []
    for yi in y:
        np_type = dtype_to_np_dtype(yi.dtype)
        v = np.random.random(size=yi.shape).astype(np_type)
        y_grads_init.append(v)

    x_init = _as_list(x_init)

    paddle.disable_static()
    with _test_eager_guard():
939 940 941
        eager_triple_grad = get_eager_triple_grad(
            func, x_init, y_grads_init, place
        )
942 943
    paddle.enable_static()

944 945 946
    static_triple_grad = get_static_triple_grad(
        x, y, x_init, y_grads_init, place
    )
947

948
    if len(static_triple_grad) != len(eager_triple_grad):
949 950
        msg = (
            "The output grad tensor's number of static graph is different with dygraph, "
951
            "please check the python api unit test used."
952
        )
953 954
        raise RuntimeError(msg)

955
    for i in range(len(static_triple_grad)):
956 957 958 959 960 961 962
        if not np.allclose(
            static_triple_grad[i], eager_triple_grad[i], rtol, atol
        ):
            msg = (
                'Check eager double result fail. Mismatch between static_graph double grad '
                'and eager double grad on %s, the output double grad tensor\'s index is : %d \n'
                'static:%s\n eager:%s\n'
963
                % (str(place), i, static_triple_grad[i], eager_triple_grad[i])
964
            )
965
            return fail_test(msg)