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

Y
Yi Wang 已提交
17
#include <list>
Q
qiaolongfei 已提交
18
#include <string>
Y
Yu Yang 已提交
19
#include <unordered_map>
Q
qiaolongfei 已提交
20

Q
init  
qijun 已提交
21
#include "paddle/framework/lod_tensor.h"
Q
qiaolongfei 已提交
22
#include "paddle/framework/variable.h"
D
format  
dongzhihong 已提交
23
#include "paddle/platform/macros.h"
Q
init  
qijun 已提交
24
#include "paddle/platform/place.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.
D
dongzhihong 已提交
50
  Variable* Var(const std::string& name);
Y
Yi Wang 已提交
51

Y
Yu Yang 已提交
52
  /// Create a variable with a scope-unique name.
D
dongzhihong 已提交
53
  Variable* Var();
Y
Yi Wang 已提交
54

Y
Yu Yang 已提交
55 56
  /// Find a variable in the scope or any of its ancestors.  Returns
  /// nullptr if cannot find.
Y
Yi Wang 已提交
57 58
  Variable* FindVar(const std::string& name) const;

Y
Yan Chunwei 已提交
59 60
  const Scope& parent() const { return *parent_; }

Y
Yu Yang 已提交
61 62 63 64 65
  /// Find the scope or an ancestor scope that contains the given variable.
  const Scope* FindScope(const Variable* var) const;

  /// Drop all kids scopes belonged to this scope.
  void DropKids();
66

Q
qiaolongfei 已提交
67
 private:
Y
Yi Wang 已提交
68
  // Call Scope::NewScope for a sub-scope.
Y
Yu Yang 已提交
69
  explicit Scope(Scope const* parent) : parent_(parent) {}
Y
Yi Wang 已提交
70

Y
Yu Yang 已提交
71 72 73
  std::unordered_map<std::string, Variable*> vars_;
  mutable std::list<Scope*> kids_;
  Scope const* parent_{nullptr};
D
format  
dongzhihong 已提交
74 75

  DISABLE_COPY_AND_ASSIGN(Scope);
Q
qiaolongfei 已提交
76 77
};

Q
qijun 已提交
78
framework::Scope& GetGlobalScope();
Q
qijun 已提交
79

Q
init  
qijun 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
// template <typename T>
// void SetFeedVariable(const std::vector<T>& input, const Lod& lod,
//   const std::vector<int64_t>& dims,
//   const std::string& var_name, size_t index) {
//   Variable* g_feed_value = GetGlobalScope().Var("var_name");
//   // feed variable holds vector<LodTensor>
//   auto& feed_inputs =
//       *(g_feed_value->GetMutable<
// std::vector<paddle::framework::LoDTensor>>());
//   if (index >= feed_inputs.size()) {
//     feed_inputs.resize(index);
//   }
//   // copy tensor
//   T* dst = feed_inputs[index].mutable_data<T>(make_ddim(dims),
//     platform::CPUPlace());
//   memcpy(dst, inputs[i].data(), inputs[i].size() * sizeof(T));
//   // copy lod
//   feed_inputs[index].set_lod(lod);
// }

template <typename T>
void SetFeedVariable(const LoDTensor& input, const std::string& var_name,
                     size_t index) {
  std::cout << "into SetFeedVariable" << std::endl;
  std::cout << var_name << std::endl;
  std::cout << index << std::endl;
  Variable* g_feed_value = GetGlobalScope().Var(var_name);
  auto& feed_inputs =
      *(g_feed_value->GetMutable<std::vector<paddle::framework::LoDTensor>>());
  if (index >= feed_inputs.size()) {
    feed_inputs.resize(index + 1);
  }
  // shared data with input tensor
  feed_inputs[index].ShareDataWith<T>(input);
  // set lod
  feed_inputs[index].set_lod(input.lod());
}

// template <typename T>
// std::vector<T> GetFetchVariable(const std::string& var_name, size_t index) {
//   Variable* g_fetch_value = GetGlobalScope().Var(var_name);
//   auto& fetch_outputs =
//       *(g_fetch_value->GetMutable<
// std::vector<paddle::framework::LoDTensor>>());
//   std::vector<T> result;
//   result.resize(fetch_outputs[index].numel());
//   memcpy(result.data(), fetch_outputs[i].data<T>(),
//            fetch_outputs[i].numel() * sizeof(T));
// }

template <typename T>
LoDTensor& GetFetchVariable(const std::string& var_name, size_t index) {
  Variable* g_fetch_value = GetGlobalScope().Var(var_name);
  auto& fetch_outputs =
      *(g_fetch_value->GetMutable<std::vector<paddle::framework::LoDTensor>>());
  std::cout << "into GetFetchVariable" << std::endl;
  PADDLE_ENFORCE_LT(index, fetch_outputs.size());
  return fetch_outputs[index];
}

Q
qiaolongfei 已提交
140 141
}  // namespace framework
}  // namespace paddle