mxnet
imperative.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef MXNET_IMPERATIVE_H_
21 #define MXNET_IMPERATIVE_H_
22 
23 #include <mxnet/op_attr_types.h>
24 #include <mxnet/graph_attr_types.h>
25 #include <mxnet/c_api.h>
26 #include <nnvm/symbolic.h>
27 #include <nnvm/op.h>
28 #include <nnvm/graph.h>
29 #include <vector>
30 #include <atomic>
31 #include <unordered_map>
32 
33 #include "./ndarray.h"
34 
35 namespace mxnet {
37 class Imperative {
38  public:
40  class AGInfo {
41  public:
45  std::vector<NDArray> outputs;
46  std::vector<NDArray> out_grads;
48 
49  AGInfo() :
50  grad_req(kNullOp), fresh_out_grad(false) {}
51 
52  static void Clear(const nnvm::NodePtr& node) {
53  if (node == nullptr || node->info.empty()) return;
54  AGInfo& info = Get(node);
55  if (info.grad_req != kNullOp) return;
56  node->info.clear();
57  }
58 
59  static AGInfo& Get(const nnvm::NodePtr& node) {
60  return dmlc::get<AGInfo>(node->info);
61  }
62 
63  static AGInfo& Create(const nnvm::NodePtr& node) {
64  node->info.construct<AGInfo>();
65  return Get(node);
66  }
67 
68  static bool IsNone(const NDArray& arr) {
69  return arr.entry_.node == nullptr || arr.entry_.node->info.empty();
70  }
71 
72  static bool IsVariable(const nnvm::NodePtr& node) {
73  AGInfo& info = Get(node);
74  return info.grad_req != kNullOp && info.outputs.size() == 1
75  && info.out_grads.size() == 1;
76  }
77  };
78  class CachedOp {
79  public:
80  explicit CachedOp(const nnvm::Symbol& sym);
81  uint32_t num_inputs() {
82  return fwd_graph_.indexed_graph().input_nodes().size();
83  }
84  uint32_t num_outputs() {
85  return fwd_graph_.outputs.size();
86  }
87  uint32_t num_backward_inputs() {
88  return bwd_ograd_dep_.size() + bwd_in_dep_.size() + bwd_out_dep_.size();
89  }
90  std::vector<bool>& save_inputs() {
91  return save_inputs_;
92  }
93  std::vector<bool>& save_outputs() {
94  return save_outputs_;
95  }
96  const std::unordered_set<uint32_t>& mutable_input_nodes() {
97  return fwd_graph_.indexed_graph().mutable_input_nodes();
98  }
99  nnvm::Graph GetForwardGraph(const bool recording,
100  const std::vector<NDArray*>& inputs);
101  nnvm::Graph GetBackwardGraph(const OpStatePtr& state,
102  const std::vector<OpReqType>& reqs,
103  const std::vector<NDArray*>& inputs);
104  std::vector<nnvm::NodeEntry> Gradient(const nnvm::NodePtr& node,
105  const std::vector<nnvm::NodeEntry>& ograds);
106  OpStatePtr Forward(const std::vector<NDArray*>& inputs,
107  const std::vector<NDArray*>& outputs);
108  void Backward(const bool retain_graph,
109  const OpStatePtr& state,
110  const std::vector<NDArray*>& inputs,
111  const std::vector<OpReqType>& reqs,
112  const std::vector<NDArray*>& outputs);
113 
114  private:
115  struct CachedOpState {
116  std::vector<NDArray> buff;
117  std::vector<OpStatePtr> states;
118  };
119  std::mutex mutex_;
120  nnvm::Graph fwd_graph_;
121  nnvm::Graph grad_graph_;
122  nnvm::Graph full_graph_;
123  std::vector<nnvm::NodeEntry> ograd_entries_;
124  std::vector<bool> curr_grad_req_;
125  std::vector<uint32_t> bwd_in_dep_, bwd_out_dep_, bwd_ograd_dep_;
126  std::vector<uint32_t> bwd_input_eid_;
127  std::vector<bool> save_inputs_, save_outputs_;
128  };
130  bool is_training() const {
131  return is_train_;
132  }
134  bool set_is_training(bool is_train) {
135  bool old = is_train_;
136  is_train_ = is_train;
137  return old;
138  }
140  bool is_recording() const {
141  return is_recording_;
142  }
145  bool old = is_recording_;
146  is_recording_ = is_recording;
147  return old;
148  }
150  void RecordOp(nnvm::NodeAttrs&& attrs,
151  const std::vector<NDArray*>& inputs,
152  const std::vector<NDArray*>& outputs,
153  const OpStatePtr& state = OpStatePtr(),
154  std::vector<bool>* p_save_inputs = nullptr,
155  std::vector<bool>* p_save_outputs = nullptr);
157  OpStatePtr Invoke(const Context& default_ctx,
158  const nnvm::NodeAttrs& attrs,
159  const std::vector<NDArray*>& inputs,
160  const std::vector<NDArray*>& outputs);
163  const nnvm::NodeAttrs& attrs,
164  const std::vector<NDArray*>& inputs,
165  const std::vector<NDArray*>& outputs,
166  const std::vector<OpReqType>& req,
167  const DispatchMode dispatch_mode,
170  void MarkVariables(const std::vector<NDArray*>& variables,
171  const std::vector<mx_uint>& grad_reqs,
172  const std::vector<NDArray*>& gradients);
174  std::vector<NDArray*> Backward(const std::vector<NDArray*>& outputs,
175  const std::vector<NDArray*>& ograds,
176  const std::vector<NDArray*>& variables,
177  bool is_train, bool retain_graph,
178  bool create_graph);
180  static Imperative* Get();
181 
182  private:
183  friend class NDArray;
185  Imperative() {}
187  void GetBackwardDependency(
188  const nnvm::NodePtr& node,
189  uint32_t num_inputs, uint32_t num_outputs,
190  std::vector<bool> *p_save_inputs,
191  std::vector<bool> *p_save_outputs);
192  void RunGraph(
193  const bool retain_graph,
194  const nnvm::IndexedGraph& idx,
195  const std::vector<NDArray*> arrays,
196  size_t node_start, size_t node_end,
197  std::vector<OpReqType>&& array_reqs,
198  std::vector<uint32_t>&& ref_count,
199  std::vector<OpStatePtr> *p_states,
200  const DispatchModeVector& dispatch_modes);
202 #if DMLC_CXX11_THREAD_LOCAL
203  static thread_local bool is_train_;
204  static thread_local bool is_recording_;
205 #else
206  static MX_THREAD_LOCAL bool is_train_;
207  static MX_THREAD_LOCAL bool is_recording_;
208 #endif
209 
210  std::atomic<uint64_t> node_count_{0};
212  std::atomic<uint64_t> variable_count_{0};
213 };
214 
215 using CachedOpPtr = std::shared_ptr<Imperative::CachedOp>;
216 
217 } // namespace mxnet
218 #endif // MXNET_IMPERATIVE_H_
C API of mxnet.
bool is_recording() const
whether operator recording is on.
Definition: imperative.h:140
static bool IsNone(const NDArray &arr)
Definition: imperative.h:68
uint32_t num_outputs()
Definition: imperative.h:84
static AGInfo & Create(const nnvm::NodePtr &node)
Definition: imperative.h:63
std::vector< DispatchMode > DispatchModeVector
The result holder of dispatch mode of each Node in the graph.
Definition: graph_attr_types.h:60
bool is_training() const
whether operator recording is on.
Definition: imperative.h:130
no operation, do not write anything
Definition: op_attr_types.h:47
bool set_is_training(bool is_train)
turn on or turn off operator recording for autograd.
Definition: imperative.h:134
namespace of mxnet
Definition: base.h:127
std::vector< NDArray * > Backward(const std::vector< NDArray * > &outputs, const std::vector< NDArray * > &ograds, const std::vector< NDArray * > &variables, bool is_train, bool retain_graph, bool create_graph)
compute the gradient of outputs w.r.t variables.
static void Clear(const nnvm::NodePtr &node)
Definition: imperative.h:52
Additional operator attributes beside the ones provided by NNVM.
OpStatePtr Invoke(const Context &default_ctx, const nnvm::NodeAttrs &attrs, const std::vector< NDArray * > &inputs, const std::vector< NDArray * > &outputs)
AGInfo()
Definition: imperative.h:49
DispatchMode
the dispatch mode of the operator
Definition: op_attr_types.h:105
std::vector< NDArray > outputs
Definition: imperative.h:45
Definition: imperative.h:40
uint32_t num_backward_inputs()
Definition: imperative.h:87
bool set_is_recording(bool is_recording)
turn on or turn off operator recording for autograd.
Definition: imperative.h:144
Definition: imperative.h:78
bool fresh_out_grad
Definition: imperative.h:47
OpStatePtr state
Definition: imperative.h:44
std::vector< NDArray > out_grads
Definition: imperative.h:46
Data structures that can appear in graph attributes.
void RecordOp(nnvm::NodeAttrs &&attrs, const std::vector< NDArray * > &inputs, const std::vector< NDArray * > &outputs, const OpStatePtr &state=OpStatePtr(), std::vector< bool > *p_save_inputs=nullptr, std::vector< bool > *p_save_outputs=nullptr)
to record operator, return corresponding node.
std::shared_ptr< Imperative::CachedOp > CachedOpPtr
Definition: imperative.h:215
OpReqType grad_req
Definition: imperative.h:43
Context ctx
Definition: imperative.h:42
OpReqType
operation request type to Forward and Backward
Definition: op_attr_types.h:45
std::vector< bool > & save_inputs()
Definition: imperative.h:90
OpStatePtr InvokeOp(const Context &ctx, const nnvm::NodeAttrs &attrs, const std::vector< NDArray * > &inputs, const std::vector< NDArray * > &outputs, const std::vector< OpReqType > &req, const DispatchMode dispatch_mode, OpStatePtr state=OpStatePtr())
static bool IsVariable(const nnvm::NodePtr &node)
Definition: imperative.h:72
runtime functions for NDArray
Definition: imperative.h:37
static AGInfo & Get(const nnvm::NodePtr &node)
Definition: imperative.h:59
const std::unordered_set< uint32_t > & mutable_input_nodes()
Definition: imperative.h:96
void MarkVariables(const std::vector< NDArray * > &variables, const std::vector< mx_uint > &grad_reqs, const std::vector< NDArray * > &gradients)
mark variables for computing gradients.
uint32_t num_inputs()
Definition: imperative.h:81
Context information about the execution environment.
Definition: base.h:142
ndarray interface
Definition: ndarray.h:79
std::vector< bool > & save_outputs()
Definition: imperative.h:93
Operator state. This is a pointer type, its content is mutable even if OpStatePtr is const...
Definition: op_attr_types.h:121