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

[NewIR] Support Ir run program node #56791

Merged
merged 41 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2430d99
support build model in python
YuanRisheng Aug 15, 2023
096cd0d
deal with conflict
YuanRisheng Aug 15, 2023
ba9fb53
fix ci bugs
YuanRisheng Aug 16, 2023
c0d9558
fix ci bugs
YuanRisheng Aug 16, 2023
da6fd48
merge develop
YuanRisheng Aug 16, 2023
6eb1893
fix compile bugs
YuanRisheng Aug 17, 2023
68d1613
fix ci bugs
YuanRisheng Aug 17, 2023
1a56b7d
add infermeta for data
YuanRisheng Aug 17, 2023
d84506c
Merge branch 'develop' of github.com:YuanRisheng/Paddle into bind_ir
YuanRisheng Aug 17, 2023
bb36572
fix ci bugs
YuanRisheng Aug 18, 2023
840f93e
deal with conflict
YuanRisheng Aug 18, 2023
00ccd45
deal with conflict
YuanRisheng Aug 21, 2023
2c8df4a
fix ci bugs
YuanRisheng Aug 21, 2023
dac0b24
fix ci bugs
YuanRisheng Aug 21, 2023
a7cba09
deal with conflict
YuanRisheng Aug 22, 2023
9e05e8e
fix bugs when run ir program mutiple times
YuanRisheng Aug 22, 2023
464b253
perfect code
YuanRisheng Aug 22, 2023
2c7419a
frontend demo debugging
2742195759 Aug 23, 2023
903a663
merge rishen
2742195759 Aug 23, 2023
be3d209
support program split and go into run program node.
2742195759 Aug 24, 2023
6cecc00
simple run the dy2static test in newir_api mode.
2742195759 Aug 30, 2023
97c68bb
Merge
2742195759 Aug 30, 2023
0f61410
remove frame.proto changes
2742195759 Aug 30, 2023
5550797
merge
2742195759 Aug 30, 2023
4a0815a
fix ir-run-program-node
2742195759 Aug 31, 2023
150e8b5
fix some code
2742195759 Sep 5, 2023
9d033ab
Merge remote-tracking branch 'upstream/develop' into ir-run-program-node
2742195759 Sep 5, 2023
4d65f37
fix output error
2742195759 Sep 6, 2023
c25e0af
fix some errors
2742195759 Sep 7, 2023
a195b67
fix
2742195759 Sep 8, 2023
b11b829
merge
2742195759 Sep 8, 2023
af98cb8
fix
2742195759 Sep 8, 2023
0a97e32
Merge remote-tracking branch 'upstream/develop' into ir-run-program-node
2742195759 Sep 8, 2023
751829e
fix
2742195759 Sep 8, 2023
f9890fb
merge
2742195759 Sep 11, 2023
78c80f9
fix conflict
2742195759 Sep 12, 2023
0f42e6f
fix files
2742195759 Sep 12, 2023
4de0215
fix some errors
2742195759 Sep 12, 2023
b912fac
fix conflict
2742195759 Sep 12, 2023
7575338
Merge remote-tracking branch 'upstream/develop' into ir-run-program-node
2742195759 Sep 13, 2023
18c08f8
merge and solve conflict
2742195759 Sep 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions paddle/fluid/eager/to_static/run_program_op_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ inline void run_program_ad_func(
std::vector<paddle::Tensor*>& dout, // NOLINT
const paddle::framework::AttributeMap& attrs) {
// Prepare Autograd Meta
VLOG(2) << "start run run_program ad function.";
auto deref_out = details::DereferenceTensors(out);
std::vector<egr::AutogradMeta*> p_autograd_x =
egr::EagerUtils::nullable_autograd_meta(x);
Expand Down Expand Up @@ -174,3 +175,107 @@ inline void run_program_ad_func(
egr::EagerUtils::SetHistory(&p_autograd_outs, grad_node);
}
}

inline void newir_run_program_ad_func(
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
inline void newir_run_program_ad_func(
inline void pir_run_program_ad_func(

const std::vector<paddle::Tensor>& x,
const std::vector<paddle::Tensor>& params,
std::vector<paddle::Tensor*>& out, // NOLINT
std::vector<paddle::framework::Scope*>& step_scope, // NOLINT
std::vector<paddle::Tensor*>& dout, // NOLINT
const paddle::framework::AttributeMap& attrs) {
// Prepare Autograd Meta
VLOG(2) << "start run newir run_program ad function.";
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
VLOG(2) << "start run newir run_program ad function.";
VLOG(2) << "start run pir run_program ad function.";

auto deref_out = details::DereferenceTensors(out);
std::vector<egr::AutogradMeta*> p_autograd_x =
egr::EagerUtils::nullable_autograd_meta(x);
std::vector<egr::AutogradMeta*> p_autograd_params =
egr::EagerUtils::nullable_autograd_meta(params);
std::vector<egr::AutogradMeta*> p_autograd_outs =
egr::EagerUtils::nullable_autograd_meta(deref_out);

bool trace_backward = egr::Controller::Instance().HasGrad();
bool require_any_grad = egr::EagerUtils::ComputeRequireGrad(
trace_backward, &p_autograd_x, &p_autograd_params);

// Create Middle Output for GradNode.
auto middle_size =
PADDLE_GET_CONST(std::vector<::pir::Value>, attrs.at("fm")).size();
Copy link
Contributor

Choose a reason for hiding this comment

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

这里后续需要解耦run_program_op maker

auto output_size =
PADDLE_GET_CONST(std::vector<::pir::Value>, attrs.at("fo")).size();
auto middles = std::vector<paddle::Tensor*>();
std::shared_ptr<NewIRGradNodeRunProgram> grad_node;
VLOG(2) << "start run run_program with require_any_grad = "
<< require_any_grad;

if (require_any_grad) {
// Create GradOpNode (1 means [out_grad], 2 means [x_grad, paramx_grad])
grad_node = std::make_shared<NewIRGradNodeRunProgram>(1, 2);
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
grad_node = std::make_shared<NewIRGradNodeRunProgram>(1, 2);
grad_node = std::make_shared<GradNodeRunProgramPir>(1, 2);

这里或许可以使用namespace 来隔离,pir::GradNodeRunProgram

grad_node->GetMiddle().resize(middle_size);
grad_node->GetOutputs().resize(output_size);
for (size_t i = 0; i < middle_size; ++i) {
grad_node->GetMiddle()[i] =
paddle::Tensor(std::make_shared<phi::DenseTensor>());
middles.push_back(&grad_node->GetMiddle()[i]);
}
for (size_t i = 0; i < output_size; ++i) {
grad_node->GetOutputs()[i] = *out[i];
}
}

// Call forward function
// if require_any_grad is False, don't save any middle vars.
NewIRRunProgramAPI(
x, params, out, middles, step_scope, dout, require_any_grad, attrs);
if (require_any_grad) {
// auto x_names =
// PADDLE_GET_CONST(std::vector<std::string>, attrs.at("x_names"));

egr::EagerUtils::PassStopGradient(false, &p_autograd_outs);

// Set Attributes
grad_node->SetAttrMap(attrs);

// auto* forward_global_block = PADDLE_GET_CONST(
// paddle::framework::BlockDesc*, attrs.at("forward_global_block"));
// auto* backward_global_block = PADDLE_GET_CONST(
// paddle::framework::BlockDesc*, attrs.at("backward_global_block"));
// Clear unused x vars
// auto filter_x =
// filter_unused_input_var_in_backward(x, x_names, backward_global_block);
// Set TensorWrappers
grad_node->SetFwdX(x);
// Clear unused out vars
// clear_unused_out_var_in_backward(out, backward_global_block,
// step_scope[0]);

grad_node->SetFwdParams(params);
grad_node->SetStepScope(step_scope); // just for set useable.

// Set Grad out rank as same as fwd input and set stop gradient to bwd
// NOTE(@xiongkun): Not every tensor in x(list of tensor) is required
// gradient. for example: x[1] is not used for output, the x[1] is ignored.

// TODO(@xiongkun): rewrite by new ir representation.
std::vector<const paddle::Tensor*> x_require_grad;
for (size_t i = 0; i < x.size(); ++i) {
x_require_grad.push_back(&x[i]);
}

grad_node->SetGradOutMeta(x_require_grad, /*slot id*/ 0);
grad_node->SetGradOutMeta(params, /*slot id*/ 1);

// VLOG(2) << "clear_no_grad_edges.";
// clear_no_grad_edges_with_partial_block(params,
// forward_global_block,
// backward_global_block,
// grad_node.get(),
// [>slot id<] 1);

grad_node->SetGradInMeta(deref_out, 0);

egr::EagerUtils::SetOutRankWithSlot(&p_autograd_outs, 0);

// Set History for output set current Grad Node for
egr::EagerUtils::SetHistory(&p_autograd_outs, grad_node);
}
}
Loading