scope.h 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qiaolongfei 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

Q
qiaolongfei 已提交
15 16
#pragma once

M
minqiyang 已提交
17
#include <functional>
Y
Yi Wang 已提交
18
#include <list>
M
minqiyang 已提交
19
#include <memory>
Q
qiaolongfei 已提交
20
#include <string>
M
minqiyang 已提交
21
#include <utility>
22
#include <vector>
Q
qiaolongfei 已提交
23

M
minqiyang 已提交
24 25
#include <tsl/robin_map.h>  // NOLINT

M
minqiyang 已提交
26
#include "paddle/fluid/framework/rw_lock.h"
Y
Yi Wang 已提交
27 28
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/platform/macros.h"
Q
qiaolongfei 已提交
29 30 31 32

namespace paddle {
namespace framework {

S
sneaxiy 已提交
33
int64_t GetEagerDeletionThreshold();
S
fix bug  
sneaxiy 已提交
34
bool IsFastEagerDeletionModeEnabled();
S
sneaxiy 已提交
35

Q
Qiao Longfei 已提交
36 37
class Scope;

Q
qiaolongfei 已提交
38
/**
39 40
 * @brief Scope that manage all variables.
 *
41
 * Scope is an association of a name to Variable. All variables belong to
42
 * Scope. You need to specify a scope to run a Net, i.e., `net.Run(&scope)`.
43 44
 * One net can run in different scopes and update different variable in the
 * scope.
Q
qiaolongfei 已提交
45 46
 */
class Scope {
Q
qiaolongfei 已提交
47
 public:
48
  Scope() {}
Y
Yi Wang 已提交
49
  ~Scope();
50

Y
Yu Yang 已提交
51 52 53 54 55 56
  /// Create a sub-scope. Returns a reference other than a pointer so
  /// to prevent from manual deletion.
  /// Mark it to const because that new kid scope cannot change parent scope.
  Scope& NewScope() const;

  /// Create a variable with given name if it doesn't exist.
57
  /// Caller doesn't own the returned Variable.
D
dongzhihong 已提交
58
  Variable* Var(const std::string& name);
Y
Yi Wang 已提交
59

Y
Yu Yang 已提交
60
  /// Create a variable with a scope-unique name.
61
  /// Caller doesn't own the returned Variable.
Y
Yu Yang 已提交
62
  Variable* Var(std::string* name = nullptr);
Y
Yi Wang 已提交
63

Y
Yancey1989 已提交
64
  void EraseVars(const std::vector<std::string>& var_names);
65

Y
Yu Yang 已提交
66 67
  /// Find a variable in the scope or any of its ancestors.  Returns
  /// nullptr if cannot find.
68
  /// Caller doesn't own the returned Variable.
Y
Yi Wang 已提交
69 70
  Variable* FindVar(const std::string& name) const;

S
sneaxiy 已提交
71 72 73 74 75
  /// Find a variable in the current scope.
  /// Return nullptr if cannot find.
  /// Caller doesn't own the returned Variable.
  Variable* FindLocalVar(const std::string& name) const;

76
  const Scope* parent() const { return parent_; }
Y
Yan Chunwei 已提交
77

Y
Yu Yang 已提交
78 79 80
  /// Find the scope or an ancestor scope that contains the given variable.
  const Scope* FindScope(const Variable* var) const;

81
  void DeleteScope(Scope* scope) const;
Y
Yu Yang 已提交
82

Y
Yu Yang 已提交
83 84
  /// Drop all kids scopes belonged to this scope.
  void DropKids();
85

M
minqiyang 已提交
86 87 88
  /// Find if a scope exists in the kid scopes
  bool HasKid(const Scope* scope) const;

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

91
  // enumerate all the variables current contains.
Y
Yang Yu 已提交
92
  std::vector<std::string> LocalVarNames() const;
93

Y
Yu Yang 已提交
94 95 96 97 98 99 100
  // Rename variable to a new name
  void Rename(const std::string& origin_name,
              const std::string& new_name) const;

  // Rename variable to a new name and return the new name
  std::string Rename(const std::string& origin_name) const;

101
 protected:
M
minqiyang 已提交
102 103 104 105 106
  mutable tsl::robin_map<
      std::string, std::unique_ptr<Variable>, std::hash<std::string>,
      std::equal_to<std::string>,
      std::allocator<std::pair<std::string, std::unique_ptr<Variable>>>, true>
      vars_;
107

108
 private:
Y
Yi Wang 已提交
109
  // Call Scope::NewScope for a sub-scope.
Y
Yu Yang 已提交
110
  explicit Scope(Scope const* parent) : parent_(parent) {}
Y
Yi Wang 已提交
111

T
tensor-tang 已提交
112 113 114 115 116 117 118 119 120 121
  // Called by Var.
  Variable* VarInternal(const std::string& name);

  // Called by FindScope.
  const Scope* FindScopeInternal(const Variable* var) const;

  // Called by Rename.
  void RenameInternal(const std::string& origin_name,
                      const std::string& new_name) const;

T
tensor-tang 已提交
122
  // Called by FindVar recursively.
T
tensor-tang 已提交
123 124
  Variable* FindVarInternal(const std::string& name) const;

T
tensor-tang 已提交
125
  // Called by FindVarInternal and Var.
T
tensor-tang 已提交
126 127
  Variable* FindVarLocally(const std::string& name) const;

128
  // Scope in `kids_` are owned by this class.
Y
Yu Yang 已提交
129
  mutable std::list<Scope*> kids_;
130
  const Scope* parent_{nullptr};
D
format  
dongzhihong 已提交
131 132

  DISABLE_COPY_AND_ASSIGN(Scope);
Y
Yancey1989 已提交
133 134

 private:
M
minqiyang 已提交
135 136
  mutable RWLock kids_lock_;
  mutable RWLock vars_lock_;
Q
qiaolongfei 已提交
137
};
138 139 140 141 142

// Generate some debug string about the inherience structure of scope, quite
// naive.
std::string GenScopeTreeDebugInfo(Scope*);

Q
qiaolongfei 已提交
143 144
}  // namespace framework
}  // namespace paddle