In MNN, Interpreter
provides three functions for running Session
, but in general, runSession
is sufficient for most cases.
Run Session
/**
* @brief run session.
* @param session given session.
* @return result of running.
*/
ErrorCode runSession(Session* session) const;
Just pass in the previously created Session
.
The time consumption of the function is not always equal to the time consumption of the inference - for CPU backend, they are equal; for other backends, the function may not wait synchronously for the completion of the inference. For example, the time consumption of the function is equal to the time consumption for shaders encoding and committing for GPU backend.
Run Session with Callbacks
typedef std::function<bool(const std::vector<Tensor*>&,
const std::string& /*opName*/)> TensorCallBack;
/*
* @brief run session.
* @param session given session.
* @param before callback before each op. return true to run the op; return false to skip the op.
* @param after callback after each op. return true to continue running; return false to interrupt the session.
* @param sync synchronously wait for finish of execution or not.
* @return result of running.
*/
ErrorCode runSessionWithCallBack(const Session* session,
const TensorCallBack& before,
const TensorCallBack& end,
bool sync = false) const;
Compared to runSession
, runSessionWithCallback
provides additional:
- Callbacks before each op, which could be used to skip the execution;
- Callback after each op, which could be used to interrupt the inference;
- Synchronization option, defaults off; when enabled, all backends will wait for inference to complete, ie the function time cost is equal to the inference time cost;
Run Session with Flops
class MNN_PUBLIC OperatorInfo {
struct Info;
public:
/** Operator's name*/
const std::string& name() const;
/** Operator's type*/
const std::string& type() const;
/** Operator's flops, in M*/
float flops() const;
protected:
OperatorInfo();
~OperatorInfo();
Info* mContent;
};
typedef std::function<bool(const std::vector<Tensor*>&, const OperatorInfo*)> TensorCallBackWithInfo;
/*
* @brief run session.
* @param session given session.
* @param before callback before each op. return true to run the op; return false to skip the op.
* @param after callback after each op. return true to continue running; return false to interrupt the session.
* @param sync synchronously wait for finish of execution or not.
* @return result of running.
*/
ErrorCode runSessionWithCallBackInfo(const Session* session,
const TensorCallBackWithInfo& before,
const TensorCallBackWithInfo& end,
bool sync = false) const;
In general, runSessionWithCallbackInfo
is only used when evaluating the amount of computation. Compared to runSessionWithCallback
, the Op type and the calculation amount information are added during the callback.