diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index ef0a4779dca2a260eef4f7b7af48210d7146395a..3959728a2071eb91fc7e1ff0cfd70d9884d668e5 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -919,15 +919,16 @@ std::vector* OperatorWithKernel::GetKernelConfig( void OperatorWithKernel::RunImpl(const Scope& scope, const platform::Place& place) const { const Scope* cur_scope = &scope; - if (!runtime_ctx_ || pre_scope_ != cur_scope || - scope.FindVar(details::kLocalExecScopeName)) { - // RuntimeContext is used to relate input/output names of Operator with - // the corresponding variables in Scope. - // In a same Scope, since the input/output names of Operator do not change - // in the execution, RuntimeContext could be created only at the first - // iteration of the execution to save the elapsed time. - // Note that the Scope should not be the local scope, since local scope - // would be cleaned regularly. + // RuntimeContext is used to relate input/output names of Operator with + // the corresponding variables in Scope. + // In a same Scope, since the input/output names of Operator do not change + // in the execution, RuntimeContext could be created only at the first + // iteration of the execution to save the elapsed time. + // Note that the Scope should not be the local scope, since local scope + // would be cleaned regularly. + if (scope.FindVar(details::kLocalExecScopeName)) { + runtime_ctx_.reset(new RuntimeContext(Inputs(), Outputs(), scope)); + } else if (!runtime_ctx_ || pre_scope_ != cur_scope) { runtime_ctx_.reset(new RuntimeContext(Inputs(), Outputs(), scope)); pre_scope_ = cur_scope; } diff --git a/paddle/fluid/framework/scope.cc b/paddle/fluid/framework/scope.cc index 87f0f307d30bc90a43a698c3766b16c975f0635e..e6de4771711400721ac544b185f44627c49d93eb 100644 --- a/paddle/fluid/framework/scope.cc +++ b/paddle/fluid/framework/scope.cc @@ -107,6 +107,10 @@ const Scope* Scope::FindScope(const Variable* var) const { return FindScopeInternal(var); } +bool Scope::HasLocalVar(const std::string& name) const { + return vars_.find(name) != vars_.end(); +} + void Scope::DropKids() { SCOPE_KIDS_WRITER_LOCK for (Scope* s : kids_) delete s; diff --git a/paddle/fluid/framework/scope.h b/paddle/fluid/framework/scope.h index f0915d2eee072b0bcd53f37dad5ef9d801c87172..38d3b4d6cea03c1e309afcf5e3b8d84aed1f586c 100644 --- a/paddle/fluid/framework/scope.h +++ b/paddle/fluid/framework/scope.h @@ -75,6 +75,10 @@ class Scope { /// Caller doesn't own the returned Variable. Variable* FindLocalVar(const std::string& name) const; + /// Find whether a variable in the current scope. + /// Return false if cannot find. + bool HasLocalVar(const std::string& name) const; + const Scope* parent() const { return parent_; } /// Find the scope or an ancestor scope that contains the given variable.