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

[Bug fixes] Add default arg to enhance varbase ClearGradient func #36837

Merged
merged 6 commits into from
Nov 11, 2021

Conversation

veyron95
Copy link
Contributor

@veyron95 veyron95 commented Oct 28, 2021

PR types

Bug fixes

PR changes

APIs

Describe

1. Add arg to enhance varbase ClearGradient func,

use case: 1

 linear.weight.clear_gradient() #  same as the original version.

use case: 2

 linear.weight.clear_gradient(False)   # False means to real clear gradient
 # default args: set_to_zero = True

2. Expose two func to set and get the " is_empty_ " attribute of grad_var_

  • _is_gradient_set_empty()
  • _gradient_set_empty(is_empty = False) or _gradient_set_empty(is_empty = True) , default=True
@dygraph_guard
    def test_tensor_method_clear_gradient_case2(self):
        input = paddle.uniform(self.input_shape)
        linear = paddle.nn.Linear(2, 3)
        out = linear(input)
        out.backward()
        # default arg set_to_zero is true
        # so, False means real clear gradient
        linear.weight.clear_gradient(False)

        # before ._gradient_set_empty(False), 
        # the return of ._is_gradient_set_empty() should be True
        self.assertTrue(linear.weight._is_gradient_set_empty())

        # reset, because ClearGradient will call SetIsEmpty(True), but this is not our expectation.
        linear.weight._gradient_set_empty(False)
        # after ._gradient_set_empty(False), 
        # the return of ._is_gradient_set_empty() should be False
        self.assertFalse(linear.weight._is_gradient_set_empty())

        # actual result
        gradient_actual = linear.weight.grad
        # expected result
        self.assertTrue(np.empty(gradient_actual))

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Baibaifan
Baibaifan previously approved these changes Nov 2, 2021
Copy link
Contributor

@Baibaifan Baibaifan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for collective

@paddle-bot-old
Copy link

paddle-bot-old bot commented Nov 5, 2021

Sorry to inform you that 9c458ff's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@veyron95 veyron95 changed the title 【Bug fixes】Add default arg to enhance varbase ClearGradient func 【Bug fixes】Add FLAGS to enhance varbase ClearGradient func Nov 5, 2021
@veyron95 veyron95 changed the title 【Bug fixes】Add FLAGS to enhance varbase ClearGradient func [Bug fixes] Add FLAGS to enhance varbase ClearGradient func Nov 5, 2021
@veyron95 veyron95 changed the title [Bug fixes] Add FLAGS to enhance varbase ClearGradient func [Bug fixes] Add default arg to enhance varbase ClearGradient func Nov 8, 2021
Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments

paddle/fluid/imperative/layer.cc Outdated Show resolved Hide resolved
paddle/fluid/imperative/layer.cc Outdated Show resolved Hide resolved
paddle/fluid/imperative/layer.cc Outdated Show resolved Hide resolved
Copy link
Contributor

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JiabinYang JiabinYang merged commit 63f5c2d into PaddlePaddle:develop Nov 11, 2021
@veyron95 veyron95 deleted the to_fix_ClearGradient branch November 12, 2021 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants