scope.h 3.6 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

Y
Yi Wang 已提交
17
#include <list>
Y
Yancey1989 已提交
18
#include <mutex>  // NOLINT
Q
qiaolongfei 已提交
19
#include <string>
Y
Yu Yang 已提交
20
#include <unordered_map>
21
#include <vector>
Q
qiaolongfei 已提交
22

Y
Yi Wang 已提交
23 24
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/platform/macros.h"
Q
qiaolongfei 已提交
25 26 27 28

namespace paddle {
namespace framework {

Q
Qiao Longfei 已提交
29 30
class Scope;

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

Y
Yu Yang 已提交
44 45 46 47 48 49
  /// 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.
50
  /// Caller doesn't own the returned Variable.
D
dongzhihong 已提交
51
  Variable* Var(const std::string& name);
Y
Yi Wang 已提交
52

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

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

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

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

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

69
  void DeleteScope(Scope* scope) const;
Y
Yu Yang 已提交
70

Y
Yu Yang 已提交
71 72
  /// Drop all kids scopes belonged to this scope.
  void DropKids();
73

M
minqiyang 已提交
74 75 76
  /// Find if a scope exists in the kid scopes
  bool HasKid(const Scope* scope) const;

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

Y
Yu Yang 已提交
80 81 82 83 84 85 86
  // 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;

87 88 89
 protected:
  mutable std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;

90
 private:
Y
Yi Wang 已提交
91
  // Call Scope::NewScope for a sub-scope.
Y
Yu Yang 已提交
92
  explicit Scope(Scope const* parent) : parent_(parent) {}
Y
Yi Wang 已提交
93

T
tensor-tang 已提交
94 95 96 97 98 99 100 101 102 103
  // 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 已提交
104
  // Called by FindVar recursively.
T
tensor-tang 已提交
105 106
  Variable* FindVarInternal(const std::string& name) const;

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

110
  // Scope in `kids_` are owned by this class.
M
minqiyang 已提交
111
  mutable std::list<Scope*> kids_;
Y
Yu Yang 已提交
112
  Scope const* parent_{nullptr};
D
format  
dongzhihong 已提交
113 114

  DISABLE_COPY_AND_ASSIGN(Scope);
Y
Yancey1989 已提交
115 116 117

 private:
  mutable std::mutex mutex_;
Q
qiaolongfei 已提交
118 119 120
};
}  // namespace framework
}  // namespace paddle