MNN  1.0
Interpreter.hpp
浏览该文件的文档.
1 //
2 // Interpreter.hpp
3 // MNN
4 //
5 // Created by MNN on 2018/07/23.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8 
9 #ifndef Interpreter_hpp
10 #define Interpreter_hpp
11 
12 #include <functional>
13 #include <map>
14 #include <string>
15 #include "ErrorCode.hpp"
16 #include "MNNForwardType.h"
17 #include "Tensor.hpp"
18 
19 namespace MNN {
20 
24  std::vector<std::string> saveTensors;
28  int numThread = 4;
29 
31  struct Path {
32  std::vector<std::string> inputs;
33  std::vector<std::string> outputs;
34 
35  enum Mode {
43  Op = 0,
44 
51  Tensor = 1
52  };
53 
56  };
58 
61 
63  BackendConfig* backendConfig = nullptr;
64 };
65 
66 class Session;
67 struct Content;
68 class Tensor;
69 class Backend;
70 
72  struct Info;
73 
74 public:
76  const std::string& name() const;
77 
79  const std::string& type() const;
80 
82  float flops() const;
83 
84 protected:
85  OperatorInfo();
86  ~OperatorInfo();
87  Info* mContent;
88 };
89 
90 typedef std::function<bool(const std::vector<Tensor*>&, const std::string& /*opName*/)> TensorCallBack;
91 typedef std::function<bool(const std::vector<Tensor*>&, const OperatorInfo*)> TensorCallBackWithInfo;
92 
95 public:
101  static Interpreter* createFromFile(const char* file);
108  static Interpreter* createFromBuffer(const void* buffer, size_t size);
109  ~Interpreter();
110 
111 public:
117  Session* createSession(const ScheduleConfig& config);
118 
124  Session* createMultiPathSession(const std::vector<ScheduleConfig>& configs);
125 
131  bool releaseSession(Session* session);
132 
138  void resizeSession(Session* session);
139 
144  void releaseModel();
145 
151  ErrorCode runSession(Session* session) const;
152 
153  /*
154  * @brief run session.
155  * @param session given session.
156  * @param before callback before each op. return true to run the op; return false to skip the op.
157  * @param after callback after each op. return true to continue running; return false to interrupt the session.
158  * @param sync synchronously wait for finish of execution or not.
159  * @return result of running.
160  */
161  ErrorCode runSessionWithCallBack(const Session* session, const TensorCallBack& before, const TensorCallBack& end,
162  bool sync = false) const;
163 
164  /*
165  * @brief run session.
166  * @param session given session.
167  * @param before callback before each op. return true to run the op; return false to skip the op.
168  * @param after callback after each op. return true to continue running; return false to interrupt the session.
169  * @param sync synchronously wait for finish of execution or not.
170  * @return result of running.
171  */
172  ErrorCode runSessionWithCallBackInfo(const Session* session, const TensorCallBackWithInfo& before,
173  const TensorCallBackWithInfo& end, bool sync = false) const;
174 
181  Tensor* getSessionInput(const Session* session, const char* name);
188  Tensor* getSessionOutput(const Session* session, const char* name);
189 
195  const std::map<std::string, Tensor*>& getSessionOutputAll(const Session* session) const;
201  const std::map<std::string, Tensor*>& getSessionInputAll(const Session* session) const;
202 
203 public:
209  void resizeTensor(Tensor* tensor, const std::vector<int>& dims);
210 
217  const Backend* getBackend(const Session* session, const Tensor* tensor) const;
218 
223  const char* bizCode() const;
224 
225 private:
226  static Interpreter* createFromBufferInternal(Content* net);
227 
228  Content* mNet = nullptr;
229  Interpreter(Content* net);
230 
231  Interpreter(const Interpreter&) = delete;
232  Interpreter(const Interpreter&&) = delete;
233  Interpreter& operator=(const Interpreter&) = delete;
234  Interpreter& operator=(const Interpreter&&) = delete;
235 };
236 } // namespace MNN
237 
238 #endif /* Interpreter_hpp */
Mode
Definition: Interpreter.hpp:35
MNNForwardType
Definition: MNNForwardType.h:12
std::vector< std::string > saveTensors
Definition: Interpreter.hpp:24
MNNForwardType backupType
Definition: Interpreter.hpp:60
Info * mContent
Definition: Interpreter.hpp:87
MNNForwardType type
Definition: Interpreter.hpp:26
Definition: Interpreter.hpp:71
Definition: Interpreter.hpp:43
Definition: Tensor.hpp:25
Definition: Interpreter.hpp:31
#define MNN_PUBLIC
Definition: MNNDefine.h:53
Definition: Interpreter.hpp:22
Definition: Interpreter.hpp:94
Definition: MNNForwardType.h:13
std::function< bool(const std::vector< Tensor * > &, const OperatorInfo *)> TensorCallBackWithInfo
Definition: Interpreter.hpp:91
BackendConfig * backendConfig
Definition: Interpreter.hpp:63
Definition: AutoTime.hpp:16
int numThread
Definition: Interpreter.hpp:28
Path path
Definition: Interpreter.hpp:57
ErrorCode
Definition: ErrorCode.hpp:13
std::function< bool(const std::vector< Tensor * > &, const std::string &)> TensorCallBack
Definition: Interpreter.hpp:90
std::vector< std::string > inputs
Definition: Interpreter.hpp:32
Mode mode
Definition: Interpreter.hpp:55
std::vector< std::string > outputs
Definition: Interpreter.hpp:33