You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/framework/scope.cc

56 lines
1.1 KiB

#include "paddle/framework/scope.h"
namespace paddle {
namespace framework {
Error Scope::CreateVariable(const std::string& name) {
if (name == "") {
return Error("Variable name should not be empty");
}
if (HaveVariable(name)) {
return AlreadyCreated;
}
vars_[name] = std::unique_ptr<Variable>(new Variable());
return Error();
}
Variable* Scope::GetVarLocally(const std::string& name) const {
if (vars_.count(name)) {
return vars_.at(name).get();
}
return nullptr;
}
Variable* Scope::GetVariable(const std::string& name) const {
Variable* var = GetVarLocally(name);
if (var != nullptr) {
return var;
} else if (parent_ != nullptr) {
return parent_->GetVariable(name);
} else {
return nullptr;
}
}
Variable* Scope::GetOrCreateVariable(const std::string& name) {
Variable* var = GetVariable(name);
if (var != nullptr) {
return var;
}
Error err = CreateVariable(name);
if (!err.isOK()) {
return nullptr;
} else {
return GetVariable(name);
}
}
bool Scope::HaveVariable(const std::string& name) {
return vars_.count(name) != 0;
}
} // namespace framework
} // namespace paddle