In MNN, Interpreter provides three functions for running Session, but in general, runSession is sufficient for most cases.

Run Session

  1. /**
  2. * @brief run session.
  3. * @param session given session.
  4. * @return result of running.
  5. */
  6. 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

  1. typedef std::function<bool(const std::vector<Tensor*>&,
  2. const std::string& /*opName*/)> TensorCallBack;
  3. /*
  4. * @brief run session.
  5. * @param session given session.
  6. * @param before callback before each op. return true to run the op; return false to skip the op.
  7. * @param after callback after each op. return true to continue running; return false to interrupt the session.
  8. * @param sync synchronously wait for finish of execution or not.
  9. * @return result of running.
  10. */
  11. ErrorCode runSessionWithCallBack(const Session* session,
  12. const TensorCallBack& before,
  13. const TensorCallBack& end,
  14. 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

  1. class MNN_PUBLIC OperatorInfo {
  2. struct Info;
  3. public:
  4. /** Operator's name*/
  5. const std::string& name() const;
  6. /** Operator's type*/
  7. const std::string& type() const;
  8. /** Operator's flops, in M*/
  9. float flops() const;
  10. protected:
  11. OperatorInfo();
  12. ~OperatorInfo();
  13. Info* mContent;
  14. };
  15. typedef std::function<bool(const std::vector<Tensor*>&, const OperatorInfo*)> TensorCallBackWithInfo;
  16. /*
  17. * @brief run session.
  18. * @param session given session.
  19. * @param before callback before each op. return true to run the op; return false to skip the op.
  20. * @param after callback after each op. return true to continue running; return false to interrupt the session.
  21. * @param sync synchronously wait for finish of execution or not.
  22. * @return result of running.
  23. */
  24. ErrorCode runSessionWithCallBackInfo(const Session* session,
  25. const TensorCallBackWithInfo& before,
  26. const TensorCallBackWithInfo& end,
  27. 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.