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

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

Q
qiaolongfei 已提交
17
#include <string>
Q
qiaolongfei 已提交
18
#include <unordered_map>
19
#include <vector>
Q
qiaolongfei 已提交
20

Q
qiaolongfei 已提交
21 22 23 24 25
#include "paddle/framework/variable.h"

namespace paddle {
namespace framework {

Q
Qiao Longfei 已提交
26 27
class Scope;

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

  /**
   * @brief Initialize a Scope with parent.
   */
Y
Yu Yang 已提交
46
  explicit Scope(const std::shared_ptr<Scope>& parent) : parent_(parent) {}
Q
qiaolongfei 已提交
47

48 49 50 51 52 53
  /**
   * @brief Create Variable
   *
   * Create Variable in this Scope. Return the exist one if Variable already
   * been created.
   */
54
  Variable* CreateVariable(const std::string& name) {
Q
qiaolongfei 已提交
55 56 57 58
    auto var = GetVariable(name);
    if (var) {
      return var;
    } else {
59
      auto ptr = new Variable();
Y
Yu Yang 已提交
60 61
      name_to_var_[name] = std::unique_ptr<Variable>(ptr);
      var_to_name_[ptr] = name;
Q
qiaolongfei 已提交
62 63
      return GetVariable(name);
    }
64 65
  }

66 67 68 69 70 71
  /**
   * @brief Get Variable.
   *
   * Get Variable from this Scope, this function will recursive find Variable
   * from it's parent scope. Return nullptr if not found.
   */
72
  Variable* GetVariable(const std::string& name) const {
Y
Yu Yang 已提交
73 74
    auto it = name_to_var_.find(name);
    if (it != name_to_var_.end()) {
75 76 77 78 79 80 81 82
      return it->second.get();
    } else if (parent_ != nullptr) {
      return parent_->GetVariable(name);
    } else {
      return nullptr;
    }
  }

83 84 85 86 87
  /**
   * @brief If this scope has a Var named name.
   *
   * Find if there is a Variable in this scope and it's parent scope
   */
88
  bool HasVariable(const std::string& name) const {
Y
Yu Yang 已提交
89
    return (name_to_var_.find(name) != name_to_var_.end() ||
90 91
            (parent_ && parent_->HasVariable(name)));
  }
Q
qiaolongfei 已提交
92

93 94
  std::string GetVariableName(Variable* const var) const {
    try {
Y
Yu Yang 已提交
95
      return var_to_name_.at(var);
96 97 98 99 100
    } catch (...) {
      return "";
    }
  }

Q
qiaolongfei 已提交
101
 private:
Y
Yu Yang 已提交
102 103
  std::unordered_map<Variable*, std::string> var_to_name_;
  std::unordered_map<std::string, std::unique_ptr<Variable>> name_to_var_;
Y
Yu Yang 已提交
104
  std::shared_ptr<Scope> parent_{nullptr};
Q
qiaolongfei 已提交
105 106 107 108
};

}  // namespace framework
}  // namespace paddle