From 336531955d11f1692cd49bc01670c8a40c63cc4f Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Tue, 23 Nov 2021 14:58:17 +0800 Subject: [PATCH] [new-exec] sync scope and variable_scope when init executor (#37445) * sync scope and variable_scope when init executor * set var_desc for new var --- .../framework/new_executor/data_transfer.cc | 21 ++++++++------- .../new_executor/standalone_executor.cc | 27 +++++++++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/framework/new_executor/data_transfer.cc b/paddle/fluid/framework/new_executor/data_transfer.cc index 7b6ea752b1..5d0db4b402 100644 --- a/paddle/fluid/framework/new_executor/data_transfer.cc +++ b/paddle/fluid/framework/new_executor/data_transfer.cc @@ -123,9 +123,10 @@ std::shared_ptr TransferLayout(const std::string& var_name, auto var_type = var_scope->Var(var_name)->Type(); InitializeVariable(ptr, static_cast(var_type)); - VLOG(3) << "Create Variable " << var_name << " locally, which pointer is " - << ptr << "Variable Type " << var_type; - var_scope->SetVarDesc(var_name, nullptr); + VLOG(3) << "Create Variable " << *new_var_name + << " locally, which pointer is " << ptr << "Variable Type " + << var_type; + var_scope->SetVarDesc(*new_var_name, nullptr); // 2. Construct VariableNameMap VariableNameMap in_name_map = {{"X", {var_name}}}; @@ -156,9 +157,10 @@ std::shared_ptr TransferDtype(const std::string& var_name, auto var_type = var_scope->Var(var_name)->Type(); InitializeVariable(ptr, static_cast(var_type)); - VLOG(3) << "Create Variable " << var_name << " locally, which pointer is " - << ptr << "Variable Type " << var_type; - var_scope->SetVarDesc(var_name, nullptr); + VLOG(3) << "Create Variable " << *new_var_name + << " locally, which pointer is " << ptr << "Variable Type " + << var_type; + var_scope->SetVarDesc(*new_var_name, nullptr); // 2. Construct VariableNameMap VariableNameMap in_name_map = {{"X", {var_name}}}; @@ -194,9 +196,10 @@ std::shared_ptr TransferDevice(const std::string& var_name, auto var_type = var_scope->Var(var_name)->Type(); InitializeVariable(ptr, static_cast(var_type)); - VLOG(3) << "Create Variable " << var_name << " locally, which pointer is " - << ptr << "Variable Type " << var_type; - var_scope->SetVarDesc(var_name, nullptr); + VLOG(3) << "Create Variable " << *new_var_name + << " locally, which pointer is " << ptr << "Variable Type " + << var_type; + var_scope->SetVarDesc(*new_var_name, nullptr); // 2. Construct VariableNameMap VariableNameMap in_name_map = {{"X", {var_name}}}; diff --git a/paddle/fluid/framework/new_executor/standalone_executor.cc b/paddle/fluid/framework/new_executor/standalone_executor.cc index 1cef303a05..56999e4463 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor.cc +++ b/paddle/fluid/framework/new_executor/standalone_executor.cc @@ -24,23 +24,28 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place, startup_prog_(startup_prog), main_prog_(main_prog), global_scope_(VariableScope(scope)) { + // NOTE(zhiqiu): it is needed to sync thhe variables in scope to + // variable_scope, + // since the some variable only exists in startup program, e.g, + // lod_tensor_blocking_queue_0 used in dataloader. + // These variables may be created in scope during runing startup program with + // original executor. + if (scope) { + auto name_list = scope->LocalVarNames(); + for (auto name : name_list) { + auto v = scope->Var(name); + if (!global_scope_.HasVar(name)) { + global_scope_.AddVar(name, *v); + } + } + } + // NOTE(zhiqiu): for startup_program, initialize scope and run once // if startup_program is empty, the scope is initialize during first run if (startup_prog.Block(0).AllOps().size() > 0) { VLOG(4) << "Run startup program"; // init scope BuildVariableScope(startup_prog, &global_scope_); - - if (scope != nullptr) { - auto name_list = scope->LocalVarNames(); - for (auto name : name_list) { - auto v = scope->Var(name); - if (!global_scope_.HasVar(name)) { - global_scope_.AddVar(name, *v); - } - } - } - std::vector vec_func_list; // No need to use_local_scope for startup_program, its variables are // persistable -- GitLab