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 inner register backward hook method for Tensor #32171

Merged

Conversation

chenwhql
Copy link
Contributor

@chenwhql chenwhql commented Apr 9, 2021

PR types

New features

PR changes

APIs

Describe

Add inner register backward hook method for Tensor

  • remove InplaceVariableWrapperHook class and related method
  • add simple void Hook for reducing in distributed training
  • add Tensor._register_backward_hook method in python
    • used for reducer like in c++ for distributed training
var->GradVarBase()->AddVoidHook(std::make_shared<std::function<void()>>(
        [=]() { this->AddDistHook(global_var_index); }));
  • example
HOOK_INIT_VALUE = 10

def global_void_hook():
    global HOOK_INIT_VALUE
    HOOK_INIT_VALUE *= 2

x = paddle.to_tensor(5., stop_gradient=False)
x._register_backward_hook(global_void_hook)

for i in range(5):
    y = paddle.pow(x, 4.0)
    y.backward()

@paddle-bot-old
Copy link

paddle-bot-old bot commented Apr 9, 2021

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

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

@chenwhql chenwhql merged commit 7ba85ac into PaddlePaddle:develop Apr 14, 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.

2 participants