Skip to content

Commit

Permalink
fix scope reuse problem (#52119)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiu authored Mar 27, 2023
1 parent d92c647 commit 97fc2a0
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions paddle/fluid/eager/to_static/run_program_op_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ inline void RunProgramAPI(

paddle::framework::Scope *global_inner_scope = out_scope_vec->front();

VLOG(4) << "global_inner_scope:" << global_inner_scope;

auto input_names = details::GetTensorsName(x);
auto output_names = details::GetTensorsName(out);
auto param_names = details::GetTensorsName(params);
Expand Down Expand Up @@ -479,6 +481,7 @@ inline void RunProgramGradAPI(
VLOG(2) << "RunProgramGradOp use interpretercore to execute program.";

paddle::framework::Scope *global_inner_scope = out_scope_vec->front();
VLOG(4) << "global_inner_scope:" << global_inner_scope;

auto *forward_global_block = PADDLE_GET_CONST(
paddle::framework::BlockDesc *, attrs.at("forward_global_block"));
Expand Down Expand Up @@ -579,8 +582,7 @@ inline void RunProgramGradAPI(
*backward_global_block,
global_inner_scope);
VLOG(4) << "after backward gc all vars";
global_inner_scope->SetCanReuesd(
false); // can't reuse util call `~GradNodeRunProgram`
global_inner_scope->SetCanReuesd(true);
details::GcScope(global_inner_scope);
}
}
Expand All @@ -591,12 +593,16 @@ class GradNodeRunProgram : public egr::GradNodeBase {
: egr::GradNodeBase(bwd_in_slot_num, bwd_out_slot_num) {}

~GradNodeRunProgram() {
auto *out_scope_vec = &step_scope_;
// Normally out_scope_vec.size() == 1. for safty, we add for-loop here.
for (size_t i = 0; i < out_scope_vec->size(); ++i) {
paddle::framework::Scope *global_inner_scope = out_scope_vec->at(i);
global_inner_scope->SetCanReuesd(true); // set this to reuse scope.
details::GcScope(global_inner_scope);
if (!executed_) {
auto *out_scope_vec = &step_scope_;
VLOG(4) << "~GradNodeRunProgram";
// Normally out_scope_vec.size() == 1. for safty, we add for-loop here.
for (size_t i = 0; i < out_scope_vec->size(); ++i) {
paddle::framework::Scope *global_inner_scope = out_scope_vec->at(i);
global_inner_scope->SetCanReuesd(true);
details::GcScope(global_inner_scope);
VLOG(4) << "global_inner_scope SetCanReuesd";
}
}
}
// Functor: perform backward computations
Expand Down Expand Up @@ -658,6 +664,8 @@ class GradNodeRunProgram : public egr::GradNodeBase {
x_grad_ptr,
params_grad_ptr);
VLOG(3) << "End Eager Backward Node: GradNodeRunProgram";

executed_ = true;
return {x_grad, params_grad};
}

Expand Down Expand Up @@ -734,4 +742,6 @@ class GradNodeRunProgram : public egr::GradNodeBase {

// Attribute Map
paddle::framework::AttributeMap attrs_;

bool executed_{false};
};

0 comments on commit 97fc2a0

Please sign in to comment.