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

[Phi] Change the output format of C++ backward api (Part2) #42545

Merged
merged 20 commits into from
May 19, 2022

Conversation

zyfncg
Copy link
Contributor

@zyfncg zyfncg commented May 6, 2022

PR types

Others

PR changes

Others

Describe

新动态图反向计算支持剪枝优化:

前续工作(Part1):C++ 反向API输出格式调整:返回结果由之前的return调整为作为输入参数。#42677

matmul_grad为例,matmul反向一般需要计算输入xy的梯度x_grady_grad,但有些情况下只需要计算x_grady_grad其中的一个,而另一个不需要计算。

  • Part1(#42677)完成后,matmul_grad接口会根据传入的x_grady_grad是否为空指针来决定是否进行计算,从而可以避免无效的计算。
  • Part2(本PR)会在调用反向API前根据返回Tensor的stop_gradient属性来决定传入matmul_grad的输出参数是否为空指针,即只有在stop_gradientfalse时才会传入有效的Tensor指针。

@paddle-bot-old
Copy link

paddle-bot-old bot commented May 6, 2022

你的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.

@zyfncg zyfncg changed the title [Phi] Change the output format of C++ backward api [Phi] Change the output format of C++ backward api (Part2) May 12, 2022
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

@@ -1321,7 +1321,11 @@ def GenerateNodeDefinition(self, grad_node_creation_str,
}}
api_output[i].reserve(returns[i].size());
for (size_t j = 0; j < returns[i].size(); ++j) {{
api_output[i].push_back(&returns[i][j]);
if (out_metas[i][j].IsStopGradient()) {{
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe only one for loop can do this all, because we have the same size for out_metas, returns, api_output

Copy link
Contributor Author

@zyfncg zyfncg May 12, 2022

Choose a reason for hiding this comment

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

Done. thx

auto tensor_with_zero = paddle::experimental::full(
phi::vectorize(tensor_meta.dims), 0.0, tensor_meta.dtype, place);
grad.set_impl(tensor_with_zero.impl());
if (grad_in_meta.HasTensorMeta()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why remove this check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

反向计算经过剪枝后,在高阶导的计算中可能会出现一些不需要的输入Tensor,这里原来的check逻辑会将这些Tensor拦截,导致不能往下计算。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

根据昨天的结论做了调整:

  1. 非optional的输入梯度维持原来的检查逻辑
  2. optional类型的输入梯度不进行该项检查,如果没有meta信息就跳过填0的操作。

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

@@ -218,6 +218,8 @@ void GradNodeBase::SetGradOutMeta(const paddle::experimental::Tensor& fwd_in,
// Set Stop_gradient
if (fwd_in_meta) {
meta.SetStopGradient(fwd_in_meta->StopGradient());
} else {
meta.SetStopGradient(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

default value is true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GradSlotMeta的默认值目前是false
image

VLOG(7) << "Should not set grad node twice, original node is:"
<< autograd_meta->GradNode()->name()
<< "current is: " << grad_node->name();
if (autograd_meta) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this be nullptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

剪枝后输出的Tensor可能没有AutogradMeta

@@ -181,7 +183,7 @@ void EagerUtils::SetOutRankWithSlot(std::vector<AutogradMeta*>* targets,
}
}
void EagerUtils::SetOutRankWithSlot(AutogradMeta* target, size_t slot_id) {
target->SetSingleOutRankWithSlot(slot_id, 0);
if (target) target->SetSingleOutRankWithSlot(slot_id, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

同上

for name, (ttype, fwd_position,
grad_api_position) in backward_grad_inputs_map.items():
if name in self.optional_inputs:
if IsPlainTensorType(ttype):
Copy link
Contributor

Choose a reason for hiding this comment

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

why this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

高阶反向在使用paddle.grad时会出现某些反向节点分支不处理的情况,这种情况会给对应下一个节点的grad_in填0,在对应的输入为optional时也需要这样的操作

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

@chenwhql chenwhql 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

@zyfncg zyfncg merged commit 4427f1b into PaddlePaddle:develop May 19, 2022
@zyfncg zyfncg deleted the change_backward_output branch May 19, 2022 06:52
YaoCheng8667 pushed a commit to WorgenZhang/Paddle that referenced this pull request May 19, 2022
…dle#42545)

* change the output format of C++ backward api

* fix merge conflict

* fix sparse api code auto-gen

* fix eager_gen bug

* fix bug of output is null

* fix bug of conv2d_grad_impl

* fix optional grad

* fix bug of eager-gen double_grad

* fix bug

* fix multiply_double_grad bug

* fix bug of higher order derivative

* fix bug of FillZeroForEmptyGradInput

* remove redundant vector in grad_node

* fix bug of test_deformable_conv_v1_op

* fix bug of test_deformable_conv_v1_op

* some refacotr
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