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

clear local scope every setp #37569

Merged
merged 3 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions paddle/fluid/framework/new_executor/interpretercore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ paddle::framework::FetchList InterpreterCore::Run(
ExecuteInstructionList(vec_instruction_);
}

if (create_local_scope_) {
ClearLoDTensorArrayInLocalScope();
}

// return Fetch Tensors
auto* fetch_var = global_scope_->Var(interpreter::kFetchVarName);
return std::move(*fetch_var->GetMutable<framework::FetchList>());
Expand Down Expand Up @@ -122,11 +126,28 @@ paddle::framework::FetchList InterpreterCore::Run(
ExecuteInstructionList(vec_instruction_);
}

if (create_local_scope_) {
ClearLoDTensorArrayInLocalScope();
}

// return Fetch Tensors
auto* fetch_var = global_scope_->Var(interpreter::kFetchVarName);
return std::move(*fetch_var->GetMutable<framework::FetchList>());
}

// At the end of each step, the holder of Tensor in LoDTensorArray is null.
// Clear these Tensors and leave LoDTensorArray empty, otherwise an exception
// will occur in the next step
void InterpreterCore::ClearLoDTensorArrayInLocalScope() {
auto vars = local_scope_->LocalVars();
for (auto var : vars) {
if (var->IsType<LoDTensorArray>()) {
auto* lod_tensor_arr = var->GetMutable<LoDTensorArray>();
lod_tensor_arr->clear();
}
}
}

void InterpreterCore::BuildOperatorDependences() {
// analysis the dependences between ops, set the dependecy_count_ and Call
// Schedule
Expand Down Expand Up @@ -609,6 +630,10 @@ interpreter::CostInfo InterpreterCore::DryRun(
platform::DeviceContextPool::Instance().Get(place_)->Wait();
}

if (create_local_scope_) {
ClearLoDTensorArrayInLocalScope();
}

return cost_info;
}

Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/framework/new_executor/interpretercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class InterpreterCore {

void SetFeedVarsInplaceSkip(const std::vector<std::string>& feed_names);

void ClearLoDTensorArrayInLocalScope();

bool is_build_;

const platform::Place& place_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ void InterpreterCoreGarbageCollector::Add(paddle::framework::Variable* var,
for (auto& t : *tensor_arr) {
Add(t.MoveMemoryHolder(), event, ctx);
}
tensor_arr->clear();
} else if (var->IsType<std::vector<Scope*>>()) {
// NOTE(@xiongkun03) conditional_op / while_op will create a STEP_SCOPE
// refer to executor.cc to see what old garbage collector does.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ void build_op_func_list(const platform::Place& place,
for (auto& t : *lod_tensor_arr) {
garbages->emplace_back(t.MoveMemoryHolder());
}
lod_tensor_arr->clear();
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Type %s of variable %s is not supported eager deletion.",
Expand Down
12 changes: 12 additions & 0 deletions paddle/fluid/framework/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ std::vector<std::string> Scope::LocalVarNames() const {
return known_vars;
}

std::vector<Variable*> Scope::LocalVars() {
std::vector<Variable*> known_vars;
{
SCOPE_VARS_READER_LOCK
known_vars.reserve(this->vars_.size());
for (auto& p : vars_) {
known_vars.emplace_back(p.second.get());
}
}
return known_vars;
}

void Scope::DeleteScope(Scope* scope) const {
{
SCOPE_KIDS_WRITER_LOCK
Expand Down
5 changes: 4 additions & 1 deletion paddle/fluid/framework/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ class Scope : public ScopeBase {

const std::list<Scope*>& kids() const { return kids_; }

// enumerate all the variables current contains.
// enumerate all the variable names current contains.
std::vector<std::string> LocalVarNames() const;

// enumerate all the variables current contains.
std::vector<Variable*> LocalVars();

// Rename variable to a new name
void Rename(const std::string& origin_name,
const std::string& new_name) const;
Expand Down