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

Support backward during backward. #32355

Merged

Conversation

hbwx24
Copy link
Contributor

@hbwx24 hbwx24 commented Apr 19, 2021

PR types

Others

PR changes

OPs

Describe

  • 在backward中使用一个临时trace,而非全局的tracer,在backward中构建一个局部动态图时不对全局动态图造成影响。这样做的目的是:若用户需要在backward中再次backward(构建一个局部的动态图模型),不需要用户手动在backward中创建一个tracer。
    示例:
def test_backward_in_backward(self):
        class cus_tanh(PyLayer):
            @staticmethod
            def forward(ctx, x):
                temp = x.detach()
                ctx.inputs = temp
                return x.mean()

            @staticmethod
            def backward(ctx, dy):
                with paddle.enable_grad():
                    temp = ctx.inputs
                    temp.stop_gradient = False
                    z = paddle.tanh(temp)
                    z.backward()
                    return paddle.to_tensor(temp.grad)

        for i in range(2):
            data = paddle.ones([2, 3], dtype="float32") / (i + 1)
            data.stop_gradient = False
            data = paddle.nn.functional.relu(data)
            z = paddle.tanh(data)
            z = cus_tanh.apply(data)

@paddle-bot-old
Copy link

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

@hbwx24 hbwx24 changed the title PyLayer use a temp tracer in forward/backward. PyLayer use a temp tracer in backward. Apr 19, 2021
@hbwx24 hbwx24 changed the title PyLayer use a temp tracer in backward. add 'enable_grad' to support backward during backward. Apr 20, 2021
@hbwx24 hbwx24 changed the title add 'enable_grad' to support backward during backward. Add 'enable_grad' to support backward during backward. Apr 20, 2021
@hbwx24 hbwx24 force-pushed the custom-op/backward_during_backward branch from 307e875 to 784cccc Compare April 20, 2021 06:34
@hbwx24 hbwx24 force-pushed the custom-op/backward_during_backward branch from 784cccc to f0b4c6a Compare April 22, 2021 11:14
@hbwx24 hbwx24 force-pushed the custom-op/backward_during_backward branch from f0b4c6a to e9d935e Compare April 23, 2021 03:41
@hbwx24 hbwx24 changed the title Add 'enable_grad' to support backward during backward. Support backward during backward. Apr 23, 2021
Copy link
Member

@ForFishes ForFishes left a comment

Choose a reason for hiding this comment

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

LGTM

@hbwx24 hbwx24 closed this Apr 24, 2021
@hbwx24 hbwx24 reopened this Apr 24, 2021
@hbwx24 hbwx24 force-pushed the custom-op/backward_during_backward branch from 68df539 to e9d935e Compare April 24, 2021 11:37
@chenwhql chenwhql merged commit 83580ee into PaddlePaddle:develop Apr 25, 2021
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.

3 participants