scope.md 1.5 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7
# Scope

### Define

Scope is a context to manage Variables. It mainly contains a map from Variable name to Variable. Net will get and update variable throw scope.

```cpp
Y
Yu Yang 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21
class Variable;
using VariablePtr = std::shared_ptr<Variable>;

class Scope final {
public:
  Scope();
  Scope(const std::shared_ptr<Scope>& parent);

  //! Get Variable in this scope.
  //! @return nullptr if no such variable.
  const VariablePtr& getVar(const std::string& name) const;

  //! Create or get a variable in this scope.
  VariablePtr& createOrGetVar(const std::string& name);
Q
qiaolongfei 已提交
22 23

private:
Y
Yu Yang 已提交
24 25 26 27
  /// variable name -> variable
  std::unordered_map<std::string, VariablePtr> vars_;
  std::shared_ptr<Scope> parent_{nullptr};
};
Q
qiaolongfei 已提交
28 29 30
```

You need to specify a scope to run a Net. One net can run in different scopes and update different variable in the scope. If you did not specify one, It will run in a default scope.
Y
Yu Yang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

```cpp
Scope global;
auto x = newVar("X");  // x is created in scope global, implicitly.
auto y = newVar("Y");
Net net1;
net1.addOp("add", {x, y}, {x});  // x = x + y;
net1.run();

for (size_t i=0; i<10; ++i) {
  Scope local;
  auto tmp = newVar("tmp");  // tmp is created in scope local.
  Net net2;
  net2.addOp("add", {x, y}, {tmp});
  net2.run();  // tmp = x + y;
}

Net net3;
net3.addOp("add", {x, y}, {"tmp"});  // error! cannot found "tmp" in global scope.

Q
qiaolongfei 已提交
51 52 53 54 55 56 57
```

### Chain structure

Scope has a pointer point to it's parent scope, this is mainly used in RNN when it need to create many stepNet.


Y
Yu Yang 已提交
58
### Scope Guard