Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dygraph triple grad test #36814

Merged
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
80b884e
native commit for triple grad of sigmod
veyron95 Sep 22, 2021
d52b81c
Updated unittests files
veyron95 Sep 22, 2021
19d6b05
init functional jacobian api
Sep 22, 2021
f47b48f
merge upstream/develop
Sep 22, 2021
16c048a
Merge pull request #2 from veyron95/ops_derivative
JiabinYang Sep 22, 2021
a6a9053
Merge branch 'support_derivative' of /~https://github.com/JiabinYang/Pa…
JiabinYang Sep 22, 2021
4febae7
Updated trible_test func
veyron95 Sep 22, 2021
be9da74
Updated gradient_checker & test_script
veyron95 Sep 22, 2021
be2b30d
finish test with dtype float32
Sep 23, 2021
36b8c34
add float64 test case
Sep 23, 2021
35b1ce8
polish code
Sep 24, 2021
3a35a00
use atol=1e-5 with dtype float64
Sep 24, 2021
a3ea12e
fix for ci
Sep 24, 2021
8738cf8
set timeout for test_jacobian
Sep 24, 2021
d6e771e
fix dygraph grad to support high differential
JiabinYang Sep 24, 2021
0bd8287
polish API docstring
Sep 26, 2021
83c8395
Merge branch 'support_derivative' of /~https://github.com/JiabinYang/Pa…
veyron95 Sep 26, 2021
4109fc5
Updated gradient checker and some related files
veyron95 Sep 26, 2021
19e471c
Merge pull request #4 from veyron95/ops_derivative
JiabinYang Sep 26, 2021
1573b2c
Merge branch 'lml/jacobian' of /~https://github.com/levi131/Paddle into…
JiabinYang Sep 26, 2021
1408ef5
fix double grad strip error for high differential
JiabinYang Sep 26, 2021
ea78b6e
fix double grad strip error for high differential
JiabinYang Sep 26, 2021
2351a99
Add Sigmoid triple grad tests
veyron95 Sep 26, 2021
7a3fbd1
fix dygraph double grad dtype error when calling for high differentia…
JiabinYang Sep 26, 2021
42df611
Merge pull request #8 from veyron95/ops_derivative
JiabinYang Sep 26, 2021
a6dde75
Updated triple grad teses func
veyron95 Sep 27, 2021
848efcf
Use np.random to initialize ddx
veyron95 Sep 27, 2021
04eab89
Updated triple_grad_check func
veyron95 Sep 28, 2021
38ca20a
Merge pull request #9 from veyron95/ops_derivative
JiabinYang Sep 28, 2021
886d9fb
merge develop
JiabinYang Sep 28, 2021
e9f643d
add todo for gradient checker and refine some comments
JiabinYang Sep 28, 2021
2d6370b
remove additional code
JiabinYang Sep 28, 2021
a3b8e4e
add test for infer_var dtype warning
JiabinYang Sep 29, 2021
13af3ed
Merge branch 'support_derivative' of /~https://github.com/JiabinYang/Pa…
JiabinYang Sep 29, 2021
20ca8e7
add test for warnging in backward.py
JiabinYang Sep 29, 2021
a961e3c
format python code
JiabinYang Oct 11, 2021
ee5489d
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
JiabinYang Oct 11, 2021
a495960
support multi input in triple gradient checker
JiabinYang Oct 12, 2021
ebe8559
Add matmul triple grad kernel
veyron95 Oct 14, 2021
4f31159
Merge branch 'support_derivative' of /~https://github.com/JiabinYang/Pa…
veyron95 Oct 14, 2021
4d56a30
Updated comments of TODO
veyron95 Oct 14, 2021
15f2a32
Merge develop branch and all conflicts fixed
veyron95 Oct 14, 2021
07d1490
Supported some special tests
veyron95 Oct 14, 2021
d5fdd20
merge develop
JiabinYang Oct 15, 2021
0e44f39
merge jiabin/support_derivative branch
veyron95 Oct 15, 2021
b52794e
Change code-format to follow CI std
veyron95 Oct 18, 2021
4202d96
Updated gradient_checker.py
veyron95 Oct 19, 2021
91149a7
Fix conflicts
veyron95 Oct 19, 2021
e20ef17
Merge develop and fix conflicts
veyron95 Oct 19, 2021
d0741f4
Removed unnecessary printing log
veyron95 Oct 19, 2021
46dbd64
Change code style to follow CI std
veyron95 Oct 20, 2021
6d313ce
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
veyron95 Oct 25, 2021
1bc2eca
Add Dygraph Triple Grad test
veyron95 Oct 27, 2021
e7eb785
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
veyron95 Nov 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions python/paddle/fluid/tests/unittests/test_imperative_triple_grad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# 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 paddle.fluid as fluid
import paddle
from paddle.fluid.wrapped_decorator import wrap_decorator
from paddle.vision.models import resnet50, resnet101
import unittest
from unittest import TestCase
import numpy as np


def _dygraph_guard_(func):
def __impl__(*args, **kwargs):
if fluid.in_dygraph_mode():
return func(*args, **kwargs)
else:
with fluid.dygraph.guard():
return func(*args, **kwargs)

return __impl__


dygraph_guard = wrap_decorator(_dygraph_guard_)


def random_var(size, low=-1, high=1, dtype='float32'):
np.random.seed(2021)
x_np = np.random.uniform(low=low, high=high, size=size).astype(dtype)
return fluid.dygraph.to_variable(x_np)


class TestDygraphTripleGrad(TestCase):
def setUp(self):
self.sort_sum_gradient = False
self.shape = [5, 5]

def grad(self,
outputs,
inputs,
grad_outputs=None,
no_grad_vars=None,
retain_graph=None,
create_graph=False,
allow_unused=False):
fluid.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient})
return fluid.dygraph.grad(
outputs=outputs,
inputs=inputs,
grad_outputs=grad_outputs,
no_grad_vars=no_grad_vars,
retain_graph=retain_graph,
create_graph=create_graph,
allow_unused=allow_unused)

@dygraph_guard
def test_exception(self):
with self.assertRaises(AssertionError):
self.grad(None, None)

shape = self.shape

with self.assertRaises(AssertionError):
self.grad(1, random_var(shape))

with self.assertRaises(AssertionError):
self.grad(random_var(shape), 1)

with self.assertRaises(AssertionError):
self.grad([1], [random_var(shape)])

with self.assertRaises(AssertionError):
self.grad([random_var(shape)], [1])

with self.assertRaises(AssertionError):
self.grad([random_var(shape), random_var(shape)],
[random_var(shape)], [random_var(shape)])

with self.assertRaises(AssertionError):
self.grad(
[random_var(shape)], [random_var(shape)], no_grad_vars=[1])

with self.assertRaises(AssertionError):
self.grad([random_var(shape)], [random_var(shape)], no_grad_vars=1)

@dygraph_guard
def test_example_with_gradient_and_create_graph(self):
x = random_var(self.shape)
x_np = x.numpy()
x.stop_gradient = False

y = random_var(self.shape)
y_np = y.numpy()
y.stop_gradient = False

z = random_var(self.shape)
z_np = z.numpy()
numel = z_np.size
z.stop_gradient = False

out = fluid.layers.sigmoid(paddle.matmul(x, y) + z)
out_np = out.numpy()

dx_actual, = self.grad([out], [x], create_graph=True)
# Theoritical result based on math calculation
dout = np.ones(self.shape).astype('float32')
dx_expected = np.matmul(dout * out_np * (1 - out_np),
np.transpose(y_np))
self.assertTrue(np.allclose(dx_actual.numpy(), dx_expected))

ddx_actual, = self.grad([dx_actual], [x], create_graph=True)
# Theoritical result based on math calculation
DDY = np.zeros(self.shape).astype('float32')
DDX = np.ones(self.shape).astype('float32')
double_grad_tmp1 = np.matmul(dout * out_np * (1 - out_np),
np.transpose(DDY))
double_grad_tmp2 = np.matmul(DDX, y_np) + np.matmul(x_np, DDY)
double_grad_tmp3 = (
1 - 2 * out_np) * dout * double_grad_tmp2 * out_np * (1 - out_np)
ddx_expected = double_grad_tmp1 + np.matmul(double_grad_tmp3,
np.transpose(y_np))
self.assertTrue(np.allclose(ddx_actual.numpy(), ddx_expected))

# Theoritical result based on math calculation
d_ddout = np.zeros(self.shape).astype('float32')
tmp0 = np.matmul(DDX, y_np) + np.matmul(x_np, DDY)
tmp1 = (1 - 2 * out_np) * ((1 - 2 * out_np) * dout * tmp0 * tmp0)
tmp2 = tmp0 * (1 - 2 * out_np) * d_ddout - 2 * dout * (
1 - out_np) * out_np * tmp0 * tmp0
dddx_expected = np.matmul(((tmp1 + tmp2) * out_np * (1 - out_np)),
np.transpose(y_np))

ddx_actual.backward()
dddx_grad_actual = x.gradient()
self.assertTrue(np.allclose(dddx_grad_actual, dddx_expected))


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