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

【pir】deal with if build stop gradient #59585

Merged
merged 4 commits into from
Dec 4, 2023

Conversation

xiaoguoguo626807
Copy link
Contributor

PR types

others

PR changes

others

Description

pcard-67164

  1. if build op 的输出stopgradient 处理为yield 输入的stopgradient
  2. 修改if op 输入cond 的stopgradient 为 True, 相应修改 ir_backward 中众多逻辑适配中途停止梯度传播的case.
  3. 整理相同代码提取为函数get_real_op_inputs

TODO

for (size_t i = 0; i < op.num_operands(); ++i) {
argument.AddOutput(op.operand(i).type());
bool input_stop_gradient = true;
auto input = op.operand_source(i).dyn_cast<pir::OpResult>();
Copy link
Contributor

Choose a reason for hiding this comment

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

如果input不是OpResult,而是BlockArgument的话,这句返回的是空的OpResult。 下一句的defining_op返回的也是空指针。再后面会直接奔溃。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

input_stop_gradient =
attrs[input.index()].dyn_cast<pir::BoolAttribute>().data();
}
outs_stop_gradient.push_back(pir::BoolAttribute::get(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
outs_stop_gradient.push_back(pir::BoolAttribute::get(
outs_stop_gradient.push_back(builder.bool_attr(input_stop_gradient));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

argument.AddAttribute(kStopGradientAttrName,
pir::ArrayAttribute::get(pir::IrContext::Instance(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pir::ArrayAttribute::get(pir::IrContext::Instance(),
pir::ArrayAttribute::get(builder.ir_context(),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


auto cond_ = cond.dyn_cast<pir::OpResult>();
auto cond_op = cond_.owner();
if (cond_op->HasAttribute(kStopGradientAttrName)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

同理,如果这儿的cond是BlockArgument, 而不是OpResult, 这儿也会崩溃,且没有提示。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

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

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

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

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

LGTM

@xiaoguoguo626807 xiaoguoguo626807 merged commit 8ddc4a6 into PaddlePaddle:develop Dec 4, 2023
29 checks passed
@xiaoguoguo626807 xiaoguoguo626807 deleted the ir_grad2 branch December 4, 2023 05:58
SigureMo pushed a commit to gouzil/Paddle that referenced this pull request Dec 5, 2023
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.

4 participants