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

Implement the composition of minimum_double_grad #62342

Merged
merged 3 commits into from
Mar 21, 2024

Conversation

YibinLiu666
Copy link
Contributor

@YibinLiu666 YibinLiu666 commented Mar 3, 2024

PR types

Others

PR changes

Others

Description

实现minimum的二阶微分组合,测试代码为:

import paddle
import numpy as np
import torch
def test_minimum_double_grad():
    shape = [1024, 512]
    x_np = np.random.randn(*shape)
    y_np = np.random.randn(*shape)
    x = paddle.to_tensor(x_np, dtype="float32")
    x.stop_gradient=False
    y = paddle.to_tensor(y_np, dtype="float32")
    y.stop_gradient=False
    paddle.framework.core.set_prim_eager_enabled(True)
    paddle.framework.core._set_prim_backward_blacklist("minimum_grad")
    z_composite_res = paddle.tanh(paddle.minimum(x, y))
    g = paddle.grad(z_composite_res, [x, y], create_graph=True, retain_graph=False)
    ddx_composite_res, ddy_composite_res = paddle.grad(g, [x,y], create_graph=False, retain_graph=False)
    
    x_torch = torch.tensor(x_np, dtype=torch.float32, requires_grad=True)
    y_torch = torch.tensor(y_np, dtype=torch.float32, requires_grad=True)
    z_torch = torch.tanh(torch.minimum(x_torch, y_torch))
    dx_torch, dy_torch = torch.autograd.grad(z_torch.sum(), [x_torch, y_torch], create_graph=True, retain_graph=True)
    ddx, ddy = torch.autograd.grad([dx_torch.sum(), dy_torch.sum()], [x_torch, y_torch], create_graph=False, retain_graph=False)
    np.testing.assert_allclose(ddx, ddx_composite_res.numpy(), 1e-6, 1e-6)
    np.testing.assert_allclose(ddy, ddy_composite_res.numpy(), 1e-6, 1e-6)

@HydrogenSulfate

Copy link

paddle-bot bot commented Mar 3, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Mar 3, 2024
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

LGTM

麻烦 @cyber-pioneer 卓哥也review一下

Copy link
Contributor

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

加一下二阶组合单测

Comment on lines +76 to +78
} else {
grad_out_grad = nullptr;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的else分支可以删除,不会出现grad_x_grad和grad_y_grad同时不存在的情况

Copy link

paddle-ci-bot bot commented Mar 15, 2024

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

Copy link
Contributor

@heavyrain-lzy heavyrain-lzy 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 YAML

@Charles-hit Charles-hit merged commit 714ddbe into PaddlePaddle:develop Mar 21, 2024
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants