-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
… change_backward_output
… change_backward_output
There was a problem hiding this 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()) {{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. thx
paddle/fluid/eager/utils.cc
Outdated
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()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove this check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
反向计算经过剪枝后,在高阶导的计算中可能会出现一些不需要的输入Tensor,这里原来的check逻辑会将这些Tensor拦截,导致不能往下计算。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
根据昨天的结论做了调整:
- 非optional的输入梯度维持原来的检查逻辑
- optional类型的输入梯度不进行该项检查,如果没有meta信息就跳过填0的操作。
There was a problem hiding this 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default value is true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paddle/fluid/eager/utils.cc
Outdated
VLOG(7) << "Should not set grad node twice, original node is:" | ||
<< autograd_meta->GradNode()->name() | ||
<< "current is: " << grad_node->name(); | ||
if (autograd_meta) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this be nullptr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
剪枝后输出的Tensor可能没有AutogradMeta
paddle/fluid/eager/utils.cc
Outdated
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this?
There was a problem hiding this comment.
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时也需要这样的操作
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
… change_backward_output
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…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
PR types
Others
PR changes
Others
Describe
新动态图反向计算支持剪枝优化:
前续工作(Part1):C++ 反向API输出格式调整:返回结果由之前的return调整为作为输入参数。#42677
以
matmul_grad
为例,matmul
反向一般需要计算输入x
和y
的梯度x_grad
和y_grad
,但有些情况下只需要计算x_grad
和y_grad
其中的一个,而另一个不需要计算。matmul_grad
接口会根据传入的x_grad
和y_grad
是否为空指针来决定是否进行计算,从而可以避免无效的计算。stop_gradient
属性来决定传入matmul_grad
的输出参数是否为空指针,即只有在stop_gradient
为false
时才会传入有效的Tensor指针。