|
|
|
@ -15,7 +15,9 @@ limitations under the License. */
|
|
|
|
|
#include "paddle/fluid/framework/scope.h"
|
|
|
|
|
|
|
|
|
|
#include <memory> // for unique_ptr
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
#include "glog/logging.h"
|
|
|
|
|
#include "paddle/fluid/framework/threadpool.h"
|
|
|
|
|
#include "paddle/fluid/string/printf.h"
|
|
|
|
@ -36,6 +38,16 @@ DEFINE_double(
|
|
|
|
|
"Memory size threshold (GB) when the garbage collector clear tensors."
|
|
|
|
|
"Disabled when this value is less than 0");
|
|
|
|
|
|
|
|
|
|
// When in inference scenario, the scopes will not be written by two threads in
|
|
|
|
|
// a mean time, but a scope may be read by multiple threads concurrently, and
|
|
|
|
|
// the mutex will cause serious performance issue.
|
|
|
|
|
// So the mutex is disabled when `ON_INFER`.
|
|
|
|
|
#ifdef ON_INFER
|
|
|
|
|
#define SCOPE_LOCK_GUARD
|
|
|
|
|
#else
|
|
|
|
|
#define SCOPE_LOCK_GUARD std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
|
@ -49,18 +61,18 @@ int64_t GetEagerDeletionThreshold() {
|
|
|
|
|
Scope::~Scope() { DropKids(); }
|
|
|
|
|
|
|
|
|
|
Scope& Scope::NewScope() const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
kids_.push_back(new Scope(this));
|
|
|
|
|
return *kids_.back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::Var(const std::string& name) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
return VarInternal(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::Var(std::string* name) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
|
|
|
|
|
if (name != nullptr) {
|
|
|
|
|
*name = new_name;
|
|
|
|
@ -69,34 +81,34 @@ Variable* Scope::Var(std::string* name) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::FindVar(const std::string& name) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
return FindVarInternal(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::FindLocalVar(const std::string& name) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
return FindVarLocally(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Scope* Scope::FindScope(const Variable* var) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
return FindScopeInternal(var);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Scope::DropKids() {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
for (Scope* s : kids_) delete s;
|
|
|
|
|
kids_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Scope::HasKid(const Scope* scope) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
|
|
|
|
|
return it != this->kids_.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> Scope::LocalVarNames() const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
std::vector<std::string> known_vars;
|
|
|
|
|
known_vars.reserve(this->vars_.size());
|
|
|
|
|
for (auto& p : vars_) {
|
|
|
|
@ -106,9 +118,10 @@ std::vector<std::string> Scope::LocalVarNames() const {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Scope::DeleteScope(Scope* scope) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
|
|
|
|
|
PADDLE_ENFORCE(it != this->kids_.end(), "Cannot find %p as kid scope", scope);
|
|
|
|
|
PADDLE_ENFORCE(it != this->kids_.end(), "%p Cannot find %p as kid scope",
|
|
|
|
|
this, scope);
|
|
|
|
|
this->kids_.erase(it);
|
|
|
|
|
// When making memory benchmark on Fluid, we have to delete scope sync.
|
|
|
|
|
if (FLAGS_benchmark || FLAGS_eager_delete_scope) {
|
|
|
|
@ -119,7 +132,7 @@ void Scope::DeleteScope(Scope* scope) const {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Scope::EraseVars(const std::vector<std::string>& var_names) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
std::set<std::string> var_set(var_names.begin(), var_names.end());
|
|
|
|
|
for (auto it = vars_.begin(); it != vars_.end();) {
|
|
|
|
|
if (var_set.find(it->first) != var_set.end()) {
|
|
|
|
@ -132,12 +145,12 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
|
|
|
|
|
|
|
|
|
|
void Scope::Rename(const std::string& origin_name,
|
|
|
|
|
const std::string& new_name) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
RenameInternal(origin_name, new_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Scope::Rename(const std::string& origin_name) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
SCOPE_LOCK_GUARD
|
|
|
|
|
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
|
|
|
|
|
RenameInternal(origin_name, new_name);
|
|
|
|
|
return new_name;
|
|
|
|
@ -189,5 +202,46 @@ Variable* Scope::FindVarLocally(const std::string& name) const {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GenScopeTreeDebugInfo(Scope* root) {
|
|
|
|
|
std::stringstream os;
|
|
|
|
|
|
|
|
|
|
if (!root) return "";
|
|
|
|
|
|
|
|
|
|
// level traversal
|
|
|
|
|
std::queue<Scope*> queue;
|
|
|
|
|
queue.push(root);
|
|
|
|
|
|
|
|
|
|
std::vector<Scope*> scopes;
|
|
|
|
|
|
|
|
|
|
while (!queue.empty()) {
|
|
|
|
|
auto* end = queue.back();
|
|
|
|
|
Scope* q = nullptr;
|
|
|
|
|
while (q != end) {
|
|
|
|
|
q = queue.front();
|
|
|
|
|
queue.pop();
|
|
|
|
|
os << q << " ";
|
|
|
|
|
scopes.push_back(q);
|
|
|
|
|
|
|
|
|
|
for (auto* c : q->kids()) {
|
|
|
|
|
queue.push(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// end of a level
|
|
|
|
|
os << "\n------------------------------------------\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os << "\nDetails:\n\n";
|
|
|
|
|
|
|
|
|
|
for (Scope* q : scopes) {
|
|
|
|
|
os << "====\n";
|
|
|
|
|
os << q << ":\n";
|
|
|
|
|
for (auto& var : q->LocalVarNames()) {
|
|
|
|
|
os << " - " << var << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
|
} // namespace paddle
|
|
|
|
|