-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,14 +20,17 @@ paddle::dialect::IfOp, paddle::dialect::WhileOp | |||||
|
||||||
#include "paddle/phi/core/enforce.h" | ||||||
#include "paddle/pir/core/builder.h" | ||||||
#include "paddle/pir/core/builtin_attribute.h" | ||||||
#include "paddle/pir/core/builtin_type.h" | ||||||
#include "paddle/pir/core/ir_printer.h" | ||||||
#include "paddle/pir/core/op_trait.h" | ||||||
#include "paddle/pir/core/operation_utils.h" | ||||||
#include "paddle/pir/core/utils.h" | ||||||
#include "paddle/pir/dialect/control_flow/ir/cf_op.h" | ||||||
|
||||||
using pir::TuplePopOp; | ||||||
using pir::TuplePushOp; | ||||||
constexpr char kStopGradientAttrName[] = "stop_gradient"; | ||||||
namespace paddle { | ||||||
namespace dialect { | ||||||
|
||||||
|
@@ -52,9 +55,26 @@ void IfOp::Build(pir::Builder &builder, // NOLINT | |||||
true_block->back().isa<pir::YieldOp>()) { | ||||||
auto &op = true_block->back(); | ||||||
|
||||||
std::vector<pir::Attribute> outs_stop_gradient; | ||||||
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>(); | ||||||
auto *defining_op = input.owner(); | ||||||
if (defining_op->HasAttribute(kStopGradientAttrName)) { | ||||||
auto attrs = defining_op->attribute(kStopGradientAttrName) | ||||||
.dyn_cast<pir::ArrayAttribute>() | ||||||
.AsVector(); | ||||||
input_stop_gradient = | ||||||
attrs[input.index()].dyn_cast<pir::BoolAttribute>().data(); | ||||||
} | ||||||
outs_stop_gradient.push_back(pir::BoolAttribute::get( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
pir::IrContext::Instance(), input_stop_gradient)); | ||||||
} | ||||||
|
||||||
argument.AddAttribute(kStopGradientAttrName, | ||||||
pir::ArrayAttribute::get(pir::IrContext::Instance(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
outs_stop_gradient)); | ||||||
} | ||||||
if (false_block && !false_block->empty() && | ||||||
false_block->back().isa<pir::YieldOp>()) { | ||||||
|
@@ -85,6 +105,20 @@ void IfOp::Build(pir::Builder &builder, // NOLINT | |||||
argument.AddRegion().push_back(true_block.release()); | ||||||
argument.AddRegion().push_back(false_block.release()); | ||||||
argument.AddInput(cond); | ||||||
|
||||||
auto cond_ = cond.dyn_cast<pir::OpResult>(); | ||||||
auto cond_op = cond_.owner(); | ||||||
if (cond_op->HasAttribute(kStopGradientAttrName)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同理,如果这儿的cond是BlockArgument, 而不是OpResult, 这儿也会崩溃,且没有提示。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
auto attrs = cond_op->attribute(kStopGradientAttrName) | ||||||
.dyn_cast<pir::ArrayAttribute>() | ||||||
.AsVector(); | ||||||
attrs[cond_.index()] = | ||||||
pir::BoolAttribute::get(pir::IrContext::Instance(), true); | ||||||
|
||||||
cond_op->set_attribute( | ||||||
kStopGradientAttrName, | ||||||
pir::ArrayAttribute::get(pir::IrContext::Instance(), attrs)); | ||||||
} | ||||||
} | ||||||
|
||||||
pir::Block *IfOp::true_block() { | ||||||
|
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.
如果input不是OpResult,而是BlockArgument的话,这句返回的是空的OpResult。 下一句的defining_op返回的也是空指针。再后面会直接奔溃。
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