mxnet
op_attr_types.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 
25 #ifndef MXNET_OP_ATTR_TYPES_H_
26 #define MXNET_OP_ATTR_TYPES_H_
27 
28 #include <mshadow/tensor.h>
29 #include <nnvm/op_attr_types.h>
30 
31 #include <vector>
32 #include <functional>
33 
34 #include "./base.h"
35 #include "./ndarray.h"
36 #include "./engine.h"
37 #include "./resource.h"
38 
39 namespace mxnet {
40 
41 using nnvm::NodeAttrs;
42 
44 enum OpReqType {
57 };
58 
65 struct OpContext {
67  int is_train;
73  std::vector<Resource> requested;
79  template<typename xpu>
80  inline mshadow::Stream<xpu>* get_stream() const {
81  return run_ctx.get_stream<xpu>();
82  }
83 };
84 
86 enum class ExecType {
88  kSync,
93  kAsync,
95  kLocal,
103 };
104 
106 enum class DispatchMode {
107  kUndefined = -1,
108  // dispatch on FCompute or FStatefulCompute
109  kFCompute,
110  // dispatch on FComputeEx or FStatefulComputeEx, if available
111  kFComputeEx,
112  // dispatch on FCompute or FStatefulCompute, and performs storage fallback
114  // special dispatch mode for variables
115  kVariable,
116 };
117 
122 class OpStatePtr {
123  public:
124  /* \brief Create a OpStatePtr with state of type T.
125  * \param args Arguments passed to T's constructor.
126  */
127  template<typename T, typename... Args>
128  static OpStatePtr Create(Args&&... args) {
129  OpStatePtr ret;
130  ret.ptr_ = std::make_shared<OpState>();
131  ret.ptr_->var_ = Engine::Get()->NewVariable();
132  ret.ptr_->state_.construct<T>(std::forward<Args>(args)...);
133 
134  return ret;
135  }
136  /* \brief Get engine variable associated with this state */
138  return ptr_->var_;
139  }
140  /* \brief Get state of type T */
141  template<typename T>
142  T& get_state() const {
143  return dmlc::get<T>(ptr_->state_);
144  }
145  /* \brief clear state */
146  void reset() {
147  ptr_.reset();
148  }
149  /* \brief Whether state is empty */
150  explicit operator bool() const {
151  return ptr_ ? true : false;
152  }
153 
154  private:
155  /* \brief state structure */
156  struct OpState {
157  OpState() {}
158  OpState(const OpState& other) = delete;
159  OpState& operator=(const OpState& other) = delete;
160 
161  ~OpState() {
162  Engine::Get()->DeleteVariable([](RunContext s) {}, Context::CPU(), var_);
163  }
164 
165  engine::VarHandle var_;
166  dmlc::any state_;
167  };
168  /* \brief shared pointer to state */
169  std::shared_ptr<OpState> ptr_;
170 };
171 
184 using FCreateOpState = std::function<OpStatePtr (const NodeAttrs& attrs,
185  Context ctx,
186  const std::vector<TShape>& in_shape,
187  const std::vector<int>& in_type)>;
191 using FExecType = std::function<ExecType (const NodeAttrs& attrs)>;
199 using FStatefulCompute = std::function<void (const OpStatePtr& state,
200  const OpContext& ctx,
201  const std::vector<TBlob>& inputs,
202  const std::vector<OpReqType>& req,
203  const std::vector<TBlob>& outputs)>;
211 using FStatefulComputeEx = std::function<void (const OpStatePtr& state,
212  const OpContext& ctx,
213  const std::vector<NDArray>& inputs,
214  const std::vector<OpReqType>& req,
215  const std::vector<NDArray>& outputs)>;
221 using FResourceRequest = std::function<
222  std::vector<ResourceRequest> (const NodeAttrs& n)>;
228 using FNDArrayFunction = std::function<void (const nnvm::NodeAttrs& attrs,
229  const std::vector<NDArray>& inputs,
230  std::vector<NDArray>* outputs)>;
236 using FCompute = std::function<void (const nnvm::NodeAttrs& attrs,
237  const OpContext& ctx,
238  const std::vector<TBlob>& inputs,
239  const std::vector<OpReqType>& req,
240  const std::vector<TBlob>& outputs)>;
247 using FComputeEx = std::function<void (const nnvm::NodeAttrs& attrs,
248  const OpContext& ctx,
249  const std::vector<NDArray>& inputs,
250  const std::vector<OpReqType>& req,
251  const std::vector<NDArray>& outputs)>;
252 
259 using FInferStorageType = std::function<bool (const NodeAttrs& attrs,
260  const int dev_mask,
261  DispatchMode* dispatch_mode,
262  std::vector<int>* in_attrs,
263  std::vector<int>* out_attrs)>;
264 
265 } // namespace mxnet
266 
267 #endif // MXNET_OP_ATTR_TYPES_H_
std::function< void(const OpStatePtr &state, const OpContext &ctx, const std::vector< TBlob > &inputs, const std::vector< OpReqType > &req, const std::vector< TBlob > &outputs)> FStatefulCompute
Resiger a compute function for stateful operator. OpStatePtr is a pointer type, it&#39;s content is mutab...
Definition: op_attr_types.h:203
void reset()
Definition: op_attr_types.h:146
Forward/Backward are synchronize calls.
Engine that schedules all the operations according to dependency.
no operation, do not write anything
Definition: op_attr_types.h:46
write gradient to provided space
Definition: op_attr_types.h:48
namespace of mxnet
Definition: base.h:126
std::function< std::vector< ResourceRequest >(const NodeAttrs &n)> FResourceRequest
The resource request from the operator.
Definition: op_attr_types.h:222
std::function< void(const OpStatePtr &state, const OpContext &ctx, const std::vector< NDArray > &inputs, const std::vector< OpReqType > &req, const std::vector< NDArray > &outputs)> FStatefulComputeEx
Resiger a compute function for stateful operator using NDArray interface. OpStatePtr is a pointer typ...
Definition: op_attr_types.h:215
mshadow::Stream< xpu > * get_stream() const
get mshadow stream from Context
Definition: base.h:251
Asynchronous function call.
engine::VarHandle get_var() const
Definition: op_attr_types.h:137
Cross device copy operation, this is a special operator That indicates copy across devices...
execution time context. The information needed in runtime for actual execution.
Definition: base.h:238
DispatchMode
the dispatch mode of the operator
Definition: op_attr_types.h:106
Run this operator on the scheduling thread without pushing to engine.
T & get_state() const
Definition: op_attr_types.h:142
engine::CallbackOnComplete async_on_complete
the callback when operation completes, used by asynchronize ops
Definition: op_attr_types.h:71
std::function< OpStatePtr(const NodeAttrs &attrs, Context ctx, const std::vector< TShape > &in_shape, const std::vector< int > &in_type)> FCreateOpState
Create a Layer style, forward/backward operator. This is easy to write code that contains state...
Definition: op_attr_types.h:187
All the possible information needed by Operator.Forward and Backward This is the superset of RunConte...
Definition: op_attr_types.h:65
int is_train
whether it is training phase
Definition: op_attr_types.h:67
std::function< ExecType(const NodeAttrs &attrs)> FExecType
Execution mode of this operator.
Definition: op_attr_types.h:191
virtual VarHandle NewVariable()=0
Allocate a new variable, the variable can then be used to schedule the operation concurrently via dep...
static OpStatePtr Create(Args &&...args)
Definition: op_attr_types.h:128
std::function< void(const nnvm::NodeAttrs &attrs, const std::vector< NDArray > &inputs, std::vector< NDArray > *outputs)> FNDArrayFunction
Register an operator called as a NDArray function.
Definition: op_attr_types.h:230
Global resource allocation handling.
virtual void DeleteVariable(SyncFn delete_fn, Context exec_ctx, VarHandle var)=0
Schedule the deletion of a variable.
Var * VarHandle
Variable pointer type, usually hold by user used to specify dependencies.
Definition: engine.h:46
perform an inplace write, Target shares memory with one of input arguments. This option only happen w...
Definition: op_attr_types.h:54
OpReqType
operation request type to Forward and Backward
Definition: op_attr_types.h:44
std::function< void(const nnvm::NodeAttrs &attrs, const OpContext &ctx, const std::vector< NDArray > &inputs, const std::vector< OpReqType > &req, const std::vector< NDArray > &outputs)> FComputeEx
Resiger an NDArray compute function for simple stateless forward only operator.
Definition: op_attr_types.h:251
std::vector< Resource > requested
Resources requested by the operator.
Definition: op_attr_types.h:73
RunContext run_ctx
RunContext related resources.
Definition: op_attr_types.h:69
static Context CPU(int32_t dev_id=0)
std::function< void(const nnvm::NodeAttrs &attrs, const OpContext &ctx, const std::vector< TBlob > &inputs, const std::vector< OpReqType > &req, const std::vector< TBlob > &outputs)> FCompute
Resiger a compute function for simple stateless forward only operator.
Definition: op_attr_types.h:240
OnComplete Callback to the engine, called by AsyncFn when action completes.
Definition: engine.h:55
static Engine * Get()
std::function< bool(const NodeAttrs &attrs, const int dev_mask, DispatchMode *dispatch_mode, std::vector< int > *in_attrs, std::vector< int > *out_attrs)> FInferStorageType
Resiger a storage and dispatch mode inference function based on storage types of the inputs and outpu...
Definition: op_attr_types.h:263
add to the provided space
Definition: op_attr_types.h:56
ExecType
the execution type of the operator
Definition: op_attr_types.h:86
Context information about the execution environment.
Definition: base.h:141
mshadow::Stream< xpu > * get_stream() const
get mshadow stream from Context
Definition: op_attr_types.h:80
Operator state. This is a pointer type, its content is mutable even if OpStatePtr is const...
Definition: op_attr_types.h:122