test_autograd_dynamic.py 20.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 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 85 86 87 88 89 90 91 92
# Copyright (c) 2021 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 collections
import typing
import unittest

import config
import numpy as np
import utils

import paddle
import paddle.nn.functional as F
from paddle.incubate.autograd.utils import as_tensors


def make_v(f, inputs):
    outputs = as_tensors(f(*inputs))
    return [paddle.ones_like(x) for x in outputs]


@utils.place(config.DEVICES)
@utils.parameterize(
    (utils.TEST_CASE_NAME, 'func', 'xs'),
    (
        ('1d_in_1d_out', utils.square, np.array([2.0, 3.0])),
        (
            'single_in_single_out',
            utils.square,
            np.random.rand(
                6,
            ),
        ),
        (
            'multi_in_single_out',
            paddle.matmul,
            (
                np.random.rand(
                    4,
                ),
                np.random.rand(
                    4,
                ),
            ),
        ),
    ),
)
class TestJacobianNoBatch(unittest.TestCase):
    def setUp(self):
        self._dtype = (
            self.xs[0].dtype
            if isinstance(self.xs, typing.Sequence)
            else self.xs.dtype
        )
        self._eps = (
            config.TOLERANCE.get(str(self._dtype))
            .get("first_order_grad")
            .get("eps")
        )
        self._rtol = (
            config.TOLERANCE.get(str(self._dtype))
            .get("first_order_grad")
            .get("rtol")
        )
        self._atol = (
            config.TOLERANCE.get(str(self._dtype))
            .get("first_order_grad")
            .get("atol")
        )

    def test_jacobian(self):
        xs = (
            [paddle.to_tensor(x, stop_gradient=False) for x in self.xs]
            if isinstance(self.xs, typing.Sequence)
            else paddle.to_tensor(self.xs, stop_gradient=False)
        )
        ys = (
            self.func(*xs) if isinstance(xs, typing.Sequence) else self.func(xs)
        )
        self._actual = paddle.autograd.jacobian(ys, xs, batch_axis=None)
        if isinstance(self._actual, (tuple, list)):
93
            self._actual = paddle.concat([x[:] for x in self._actual], axis=0)
94 95
        self._expected = self._get_expected()

96 97 98 99 100 101
        self.assertEqual(self._actual.numpy().dtype, self._expected.dtype)
        np.testing.assert_allclose(
            self._actual.flatten(),
            self._expected.flatten(),
            rtol=self._rtol,
            atol=self._atol,
102 103 104 105 106 107 108 109 110 111 112 113 114
        )

    def test_jacobian_attribute_operator(self):
        xs = (
            [paddle.to_tensor(x, stop_gradient=False) for x in self.xs]
            if isinstance(self.xs, typing.Sequence)
            else paddle.to_tensor(self.xs, stop_gradient=False)
        )
        ys = (
            self.func(*xs) if isinstance(xs, typing.Sequence) else self.func(xs)
        )
        self._actual = paddle.autograd.jacobian(ys, xs, batch_axis=None)
        if isinstance(self._actual, (tuple, list)):
115
            self._actual = paddle.concat([x[:] for x in self._actual], axis=0)
116 117 118
        self._expected = self._get_expected()

        self.assertEqual(self._actual.numpy().dtype, self._expected.dtype)
119 120 121 122 123 124
        np.testing.assert_allclose(
            self._actual.flatten(),
            self._expected.flatten(),
            rtol=self._rtol,
            atol=self._atol,
        )
125 126 127 128 129 130 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 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

    def _get_expected(self):
        xs = (
            [paddle.to_tensor(x, stop_gradient=False) for x in self.xs]
            if isinstance(self.xs, typing.Sequence)
            else paddle.to_tensor(self.xs, stop_gradient=False)
        )
        jac = utils._compute_numerical_jacobian(
            self.func, xs, self._eps, self._dtype
        )
        return utils._np_concat_matrix_sequence(jac, utils.MatrixFormat.NM)


@utils.place(config.DEVICES)
@utils.parameterize(
    (utils.TEST_CASE_NAME, 'func', 'xs'),
    (
        (
            '1d_in_1d_out',
            utils.square,
            np.array([[1.0, 2.0, 3.0], [3.0, 4.0, 3.0]]),
        ),
        ('multi_in_single_out', utils.square, np.random.rand(2, 3)),
    ),
)
class TestJacobianBatchFirst(unittest.TestCase):
    def setUp(self):
        self._dtype = (
            self.xs[0].dtype
            if isinstance(self.xs, typing.Sequence)
            else self.xs.dtype
        )
        self._eps = (
            config.TOLERANCE.get(str(self._dtype))
            .get("first_order_grad")
            .get("eps")
        )
        self._rtol = (
            config.TOLERANCE.get(str(self._dtype))
            .get("first_order_grad")
            .get("rtol")
        )
        self._atol = (
            config.TOLERANCE.get(str(self._dtype))
            .get("first_order_grad")
            .get("atol")
        )

    def test_jacobian(self):
        xs = (
            [paddle.to_tensor(x, stop_gradient=False) for x in self.xs]
            if isinstance(self.xs, typing.Sequence)
            else paddle.to_tensor(self.xs, stop_gradient=False)
        )
        ys = (
            self.func(*xs) if isinstance(xs, typing.Sequence) else self.func(xs)
        )
        self._actual = paddle.autograd.jacobian(ys, xs, batch_axis=0)
        self._expected = self._get_expected()

        Index = collections.namedtuple('Index', ('type', 'value'))
        indexes = (
            Index(
                'all',
                (
                    slice(0, None, None),
                    slice(0, None, None),
                    slice(0, None, None),
                ),
            ),
            Index('row', (slice(0, None, None), 0, slice(0, None, None))),
            Index('col', (slice(0, None, None), slice(0, None, None), 0)),
            Index(
                'batch',
                (slice(0, 2, None), slice(0, None, None), slice(0, None, None)),
            ),
            Index(
                'multi_row',
                (slice(0, 1, None), slice(0, 2, 1), slice(0, None, None)),
            ),
        )
        self.assertEqual(self._actual[:].numpy().dtype, self._expected.dtype)
        for index in indexes:
            np.testing.assert_allclose(
                self._actual.__getitem__(index.value),
                self._expected.__getitem__(index.value),
                rtol=self._rtol,
                atol=self._atol,
                err_msg=f'Testcase {index.type} index not passed, value is {index.value}',
            )

    def test_jacobian_attribute_operator(self):
        # test for attribute operator "."
        xs = (
            [paddle.to_tensor(x, stop_gradient=False) for x in self.xs]
            if isinstance(self.xs, typing.Sequence)
            else paddle.to_tensor(self.xs, stop_gradient=False)
        )
        ys = (
            self.func(*xs) if isinstance(xs, typing.Sequence) else self.func(xs)
        )
        self._actual = paddle.autograd.jacobian(ys, xs, batch_axis=0)
        self._expected = self._get_expected()

        Index = collections.namedtuple('Index', ('type', 'value'))
        indexes = (
            Index(
                'all',
                (
                    slice(0, None, None),
                    slice(0, None, None),
                    slice(0, None, None),
                ),
            ),
            Index('row', (slice(0, None, None), 0, slice(0, None, None))),
            Index('col', (slice(0, None, None), slice(0, None, None), 0)),
            Index(
                'batch',
                (slice(0, 2, None), slice(0, None, None), slice(0, None, None)),
            ),
            Index(
                'multi_row',
                (slice(0, 1, None), slice(0, 2, 1), slice(0, None, None)),
            ),
        )
        self.assertEqual(self._actual.numpy().dtype, self._expected.dtype)
        for index in indexes:
            np.testing.assert_allclose(
                self._actual.__getitem__(index.value),
                self._expected.__getitem__(index.value),
                rtol=self._rtol,
                atol=self._atol,
                err_msg=f'Testcase {index.type} index not passed, value is {index.value}',
            )

    def _get_expected(self):
        xs = (
            [paddle.to_tensor(x, stop_gradient=False) for x in self.xs]
            if isinstance(self.xs, typing.Sequence)
            else paddle.to_tensor(self.xs, stop_gradient=False)
        )
        jac = utils._compute_numerical_batch_jacobian(
            self.func, xs, self._eps, self._dtype, False
        )
        jac = utils._np_concat_matrix_sequence(jac, utils.MatrixFormat.NBM)
        return utils._np_transpose_matrix_format(
            jac, utils.MatrixFormat.NBM, utils.MatrixFormat.BNM
        )


class TestHessianNoBatch(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.shape = (4,)
        self.dtype = 'float32'
        self.np_dtype = np.float32
        self.numerical_delta = (
            config.TOLERANCE.get(self.dtype).get("second_order_grad").get("eps")
        )
        self.rtol = (
            config.TOLERANCE.get(self.dtype)
            .get("second_order_grad")
            .get("rtol")
        )
        self.atol = (
            config.TOLERANCE.get(self.dtype)
            .get("second_order_grad")
            .get("atol")
        )
        self.x = paddle.rand(shape=self.shape, dtype=self.dtype)
        self.y = paddle.rand(shape=self.shape, dtype=self.dtype)

    def func_create_graph_true(self):
        def func(x):
            return paddle.sum(F.sigmoid(x))

        numerical_hessian = utils._compute_numerical_hessian(
            func, self.x, self.numerical_delta, self.np_dtype
        )
        numerical_hessian = utils._np_concat_matrix_sequence(numerical_hessian)
        self.x.stop_gradient = False
        hessian = paddle.autograd.hessian(func(self.x), self.x, batch_axis=None)
        assert not hessian[:].stop_gradient
        np.testing.assert_allclose(
            hessian[:].numpy(), numerical_hessian, self.rtol, self.atol
        )

    def func_out_not_single(self):
        def func(x):
            return x * x

        with self.assertRaises(ValueError):
            x = paddle.ones([3])
            paddle.autograd.hessian(func(x), x, batch_axis=None)

    def func_add(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected + 1.0
        actual = H + 1.0
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_sub(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected - 1.0
        actual = H - 1.0
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_mul(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected * 2.0
        actual = H * 2.0
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_div(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected / 2.0
        actual = H / 2.0
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_truediv(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected / 2.0
        actual = H / 2.0
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_pow(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected**3.0
        actual = H**3.0
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_mod(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected % 1.2
        actual = H % 1.2
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_matmul(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected @ expected
        actual = H @ H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_eq(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected == expected
        actual = H == H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_ne(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected != expected
        actual = H != H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_lt(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected < expected
        actual = H < H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_le(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected <= expected
        actual = H <= H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_gt(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected > expected
        actual = H > H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_ge(self):
        def func(x):
            return (x * x).sum()

        H = paddle.autograd.hessian(func(self.x), self.x)
        expected = np.diag(np.full((self.x.size,), 2.0))

        expected = expected >= expected
        actual = H >= H
        np.testing.assert_allclose(actual, expected, self.rtol, self.atol)

    def func_0Dtensor_index(self):
        x_0d = self.x[0].reshape([])

        def func(x):
            return x * x

        with self.assertRaises(IndexError):
            H = paddle.autograd.hessian(func(x_0d), x_0d)
            H = H[:]

    def func_2Dtensor(self):
        x_2d = self.x.reshape([self.x.shape[0] // 2, 2])

        def func(x):
            return (x * x).sum()

        with self.assertRaises(ValueError):
            H = paddle.autograd.hessian(func(x_2d), x_2d)

    def test_all_cases(self):
        self.setUpClass()
        self.func_create_graph_true()
        self.func_out_not_single()
        self.func_add()
        self.func_sub()
        self.func_mul()
        self.func_div()
        self.func_truediv()
        self.func_pow()
        self.func_mod()
        self.func_matmul()
        self.func_eq()
        self.func_ne()
        self.func_lt()
        self.func_le()
        self.func_gt()
        self.func_ge()
        self.func_0Dtensor_index()
        self.func_2Dtensor()


class TestHessianBatchFirst(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.x_shape = (5, 2)
        self.weight_shape = (2, 4)
        self.y_shape = (5, 2)
        self.nbatch, self.nrow = 5, 2
        self.dtype = 'float32'
        self.np_dtype = np.float32
        self.numerical_delta = (
            config.TOLERANCE.get(self.dtype).get('second_order_grad').get('eps')
        )
        self.rtol = (
            config.TOLERANCE.get(self.dtype)
            .get('second_order_grad')
            .get('rtol')
        )
        self.atol = (
            config.TOLERANCE.get(self.dtype)
            .get('second_order_grad')
            .get('atol')
        )
        self.x = paddle.rand(shape=self.x_shape, dtype=self.dtype)
        self.x.stop_gradient = False
        self.weight = paddle.rand(shape=self.weight_shape, dtype=self.dtype)
        self.weight.stop_gradient = False
        self.y = paddle.rand(shape=self.y_shape, dtype=self.dtype)
        self.y.stop_gradient = False

    def func_allow_unused(self):
        def func(x, y):
            return paddle.matmul(x * x, self.weight)[:, 0:1]

        xs_len = 2
        expected = utils._compute_numerical_batch_hessian(
            func, [self.x, self.y], self.numerical_delta, self.np_dtype
        )
        expected = np.reshape(
            np.array(expected),
            (xs_len, xs_len, self.nrow, self.nbatch, self.nrow),
        )
        expected = [list(row) for row in expected]
        expected = utils._np_concat_matrix_sequence(expected)
        expected = utils._np_transpose_matrix_format(
            expected, utils.MatrixFormat.NBM, utils.MatrixFormat.BNM
        )

        actual = paddle.autograd.hessian(
            func(self.x, self.y), [self.x, self.y], batch_axis=0
        )
        actual = paddle.concat(
            [
                paddle.concat([actual[i][j][:] for j in range(2)], axis=2)
                for i in range(2)
            ],
            axis=1,
        )

        np.testing.assert_allclose(
            actual.shape, expected.shape, rtol=self.rtol, atol=self.atol
        )

    def func_stop_gradient(self):
        def func(x):
            return paddle.matmul(x * x, self.weight)[:, 0:1]

        expected = utils._compute_numerical_batch_hessian(
            func, self.x, self.numerical_delta, self.np_dtype
        )

        x = self.x.clone()
        x.stop_gradient = True
        H = paddle.autograd.hessian(func(self.x), self.x, batch_axis=0)[:]
        actual = utils._np_transpose_matrix_format(
            H[:].numpy(), utils.MatrixFormat.BNM, utils.MatrixFormat.NBM
        )
        actual = actual.reshape((H.shape[1], -1))

        np.testing.assert_allclose(
            actual.shape, np.asarray(expected).shape, self.rtol, self.atol
        )

    def func_out_not_single(self):
        def func(x):
            return x * x

        with self.assertRaises(ValueError):
            x = paddle.ones((3, 3))
            paddle.autograd.hessian(func(x), x, batch_axis=0)

    def func_batch_axis_except_0(self):
        def func(x):
            return x * x

        with self.assertRaises(ValueError):
            x = paddle.ones([3])
            paddle.autograd.hessian(func(x), x, batch_axis=2)

    def func_ndim_bigger_than_2(self):
        def func(x):
615
            return (x * x).sum([1, 2, 3])
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

        with self.assertRaises(ValueError):
            x = paddle.ones([3, 3, 3, 3])
            paddle.autograd.hessian(func(x), x, batch_axis=0)

    def func_batch_axis_str(self):
        def func(x):
            return (x * x).sum()

        with self.assertRaises(ValueError):
            x = paddle.ones([3, 3, 3, 3])
            paddle.autograd.hessian(func(x), x, batch_axis="0")

    def func_ellipsis_index(self):
        def func(x):
            return (x * x).sum()

        with self.assertRaises(IndexError):
            x = paddle.ones([2, 3])
            H = paddle.autograd.hessian(func(x), x, batch_axis=0)[..., 1]

    def test_all_cases(self):
        self.setUpClass()
        self.func_allow_unused()
        self.func_stop_gradient()
        self.func_out_not_single()
        self.func_batch_axis_except_0()
        self.func_ndim_bigger_than_2()
        self.func_batch_axis_str()
        self.func_ellipsis_index()


if __name__ == "__main__":
    np.random.seed(2022)
    unittest.main()