scope.h 2.6 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
#include "paddle/framework/variable.h"
22
#include "paddle/platform/assert.h"
Q
qiaolongfei 已提交
23 24 25 26 27

namespace paddle {
namespace framework {

/**
28
 * Scope is an association of a name to Variable. All variables belong to
29
 * Scope. You need to specify a scope to run a Net, i.e., `net.Run(&scope)`.
30 31
 * One net can run in different scopes and update different variable in the
 * scope.
Q
qiaolongfei 已提交
32 33
 */
class Scope {
Q
qiaolongfei 已提交
34
 private:
35 36 37
  explicit Scope(const std::shared_ptr<Scope>& parent = nullptr)
      : parent_(parent) {}

Q
qiaolongfei 已提交
38 39 40 41 42 43
 public:
  static std::shared_ptr<Scope> Create(
      const std::shared_ptr<Scope>& parent = nullptr) {
    return std::make_shared<Scope>(Scope(parent));
  }

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  /// Create Variable in this Scope. Failed if Variable already been
  /// created.
  Variable* CreateVariable(const std::string& name) {
    PADDLE_ASSERT(!HasVariable(name));
    vars_[name] = std::unique_ptr<Variable>(new Variable());
    return GetVariable(name);
  }

  /// Get Variable from this Scope, this function will recursive find Variable
  /// from it's parent scope. Return nullptr if not found.
  Variable* GetVariable(const std::string& name) const {
    auto it = vars_.find(name);
    if (it != vars_.end()) {
      return it->second.get();
    } else if (parent_ != nullptr) {
      return parent_->GetVariable(name);
    } else {
      return nullptr;
    }
  }

  /// Get Variable from scope, if Variable is not exist, creat one and return.
  Variable* GetOrCreateVariable(const std::string& name) {
    auto var = GetVariable(name);
    if (var) {
      return var;
    } else {
      return CreateVariable(name);
    }
  }

  /// Find if there is a Variable in this scope and it's parent scope
  bool HasVariable(const std::string& name) const {
    return (vars_.find(name) != vars_.end() ||
            (parent_ && parent_->HasVariable(name)));
  }
Q
qiaolongfei 已提交
80 81 82

 private:
  std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;
83
  std::shared_ptr<Scope> parent_{nullptr};
Q
qiaolongfei 已提交
84 85 86 87
};

}  // namespace framework
}  // namespace paddle