mxnet
executor.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_EXECUTOR_H_
26 #define MXNET_EXECUTOR_H_
27 
28 #include <dmlc/base.h>
29 #include <vector>
30 #include <memory>
31 #include <map>
32 #include <string>
33 #include <utility>
34 #include "./base.h"
35 #include "./c_api.h"
36 #include "./ndarray.h"
37 #include "./operator.h"
38 
39 // check c++11
40 #if DMLC_USE_CXX11 == 0
41 #error "CXX11 was required for symbolic module"
42 #endif
43 
44 namespace mxnet {
46 using nnvm::Symbol;
47 
52 class Executor {
53  public:
55  virtual ~Executor() {}
60  virtual void Forward(bool is_train) = 0;
69  virtual void PartialForward(bool is_train, int step, int *step_left) = 0;
79  virtual void Backward(const std::vector<NDArray> &head_grads, bool is_train = true) = 0;
84  virtual void Print(std::ostream &os) const {} // NOLINT(*)
89  virtual const std::vector<NDArray> &outputs() const = 0;
94  virtual const std::unordered_map<std::string, NDArray>& in_arg_map() const = 0;
99  virtual const std::unordered_map<std::string, NDArray>& arg_grad_map() const = 0;
104  virtual const std::unordered_map<std::string, NDArray>& aux_state_map() const = 0;
119  static Executor *Bind(nnvm::Symbol symbol,
120  const Context& default_ctx,
121  const std::map<std::string, Context>& group2ctx,
122  const std::vector<NDArray> &in_args,
123  const std::vector<NDArray> &arg_grad_store,
124  const std::vector<OpReqType> &grad_req_type,
125  const std::vector<NDArray> &aux_states,
126  Executor* shared_exec = NULL);
127 
128  static Executor* SimpleBind(nnvm::Symbol symbol,
129  const Context& default_ctx,
130  const std::map<std::string, Context>& group2ctx,
131  const std::vector<Context>& in_arg_ctxes,
132  const std::vector<Context>& arg_grad_ctxes,
133  const std::vector<Context>& aux_state_ctxes,
134  const std::unordered_map<std::string, TShape>& arg_shape_map,
135  const std::unordered_map<std::string, int>& arg_dtype_map,
136  const std::vector<OpReqType>& grad_req_types,
137  const std::unordered_set<std::string>& param_names,
138  std::vector<NDArray>* in_args,
139  std::vector<NDArray>* arg_grads,
140  std::vector<NDArray>* aux_states,
141  std::unordered_map<std::string, NDArray>*
142  shared_data_arrays = nullptr,
143  Executor* shared_exec = nullptr);
147  typedef std::function<void(const char*, void*)> MonitorCallback;
151  virtual void SetMonitorCallback(const MonitorCallback& callback) {}
152 }; // class executor
153 } // namespace mxnet
154 #endif // MXNET_EXECUTOR_H_
Executor of a computation graph. Executor can be created by Binding a symbol.
Definition: executor.h:52
C API of mxnet.
virtual ~Executor()
destructor
Definition: executor.h:55
std::function< void(const char *, void *)> MonitorCallback
the prototype of user-defined monitor callback
Definition: executor.h:147
virtual void Backward(const std::vector< NDArray > &head_grads, bool is_train=true)=0
Perform a Backward operation of the Operator. This must be called after Forward. After this operation...
namespace of mxnet
Definition: base.h:126
static Executor * Bind(nnvm::Symbol symbol, const Context &default_ctx, const std::map< std::string, Context > &group2ctx, const std::vector< NDArray > &in_args, const std::vector< NDArray > &arg_grad_store, const std::vector< OpReqType > &grad_req_type, const std::vector< NDArray > &aux_states, Executor *shared_exec=NULL)
Create an operator by bind symbol with context and arguments. If user do not want to compute the grad...
virtual void Print(std::ostream &os) const
print the execution plan info to output stream.
Definition: executor.h:84
virtual const std::unordered_map< std::string, NDArray > & in_arg_map() const =0
get input argument map, key is arg name, value is arg&#39;s NDArray.
virtual const std::vector< NDArray > & outputs() const =0
get array of outputs in the executor.
virtual void SetMonitorCallback(const MonitorCallback &callback)
Install a callback to notify the completion of operation.
Definition: executor.h:151
virtual void Forward(bool is_train)=0
Perform a Forward operation of Operator After this operation, user can get the result by using functi...
virtual void PartialForward(bool is_train, int step, int *step_left)=0
Perform a Partial Forward operation of Operator. Only issue operation specified by step...
static Executor * SimpleBind(nnvm::Symbol symbol, const Context &default_ctx, const std::map< std::string, Context > &group2ctx, const std::vector< Context > &in_arg_ctxes, const std::vector< Context > &arg_grad_ctxes, const std::vector< Context > &aux_state_ctxes, const std::unordered_map< std::string, TShape > &arg_shape_map, const std::unordered_map< std::string, int > &arg_dtype_map, const std::vector< OpReqType > &grad_req_types, const std::unordered_set< std::string > &param_names, std::vector< NDArray > *in_args, std::vector< NDArray > *arg_grads, std::vector< NDArray > *aux_states, std::unordered_map< std::string, NDArray > *shared_data_arrays=nullptr, Executor *shared_exec=nullptr)
virtual const std::unordered_map< std::string, NDArray > & arg_grad_map() const =0
get input argument graident map, key is arg name, value is gradient&#39;s NDArray.
virtual const std::unordered_map< std::string, NDArray > & aux_state_map() const =0
get aux state map, key is arg name, value is aux state&#39;s NDArray.
Context information about the execution environment.
Definition: base.h:141