MNN/source/core/Session.hpp

149 lines
3.9 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// Session.hpp
// MNN
//
// Created by MNN on 2018/07/30.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef Session_hpp
#define Session_hpp
#include <map>
#include <memory>
#include <vector>
#include "Backend.hpp"
#include "Macro.h"
#include "Pipeline.hpp"
#include "Schedule.hpp"
#include "SizeComputer.hpp"
#include "Tensor.hpp"
namespace MNN {
struct Net;
2019-04-17 10:49:11 +08:00
/** infer unit. multiple sessions could share one net. */
- build: - unify schema building in core and converter; - add more build script for android; - add linux build script for python; - ops impl: - add floor mod support in binary; - use eltwise impl in add/max/sub/mul binary for optimization; - remove fake double support in cast; - fix 5d support for concat; - add adjX and adjY support for batch matmul; - optimize conv2d back prop filter; - add pad mode support for conv3d; - fix bug in conv2d & conv depthwise with very small feature map; - optimize binary without broacast; - add data types support for gather; - add gather ND support; - use uint8 data type in gather v2; - add transpose support for matmul; - add matrix band part; - add dim != 4 support for padding, reshape & tensor convert; - add pad type support for pool3d; - make ops based on TensorFlow Lite quantization optional; - add all & any support for reduction; - use type in parameter as output type in reduction; - add int support for unary; - add variable weight support for conv2d; - fix conv2d depthwise weights initialization; - fix type support for transpose; - fix grad outputs count for reduce grad and reshape grad; - fix priorbox & detection output; - fix metal softmax error; - python: - add runSessionWithCallBackInfo interface; - add max nodes limit (1400) for visualization tool; - fix save error in python3; - align default dim; - convert: - add extra design for optimization; - add more post converting optimizers; - add caffe v1 weights blob support; - add cast, unary, conv transpose support for onnx model; - optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model; - add cos/sin/atan/tan support for unary for tensorflow model; - add any/all support for reduction for tensorflow model; - add elu, conv3d, pool3d support for tensorflow model; - optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model; - others: - fix size computer lock; - fix thread pool deadlock; - add express & parameters in express; - rewrite blitter chooser without static map; - add tests for expr;
2019-10-29 13:37:26 +08:00
class MNN_PUBLIC Session {
2019-04-17 10:49:11 +08:00
public:
/**
* @breif initializ with schedule info.
* @param info given schedule info.
*/
Session(const Schedule::ScheduleInfo& info);
~Session();
public:
/**
* @brief infer.
* @return result code.
*/
ErrorCode run() const;
/**
* @brief infer with callbacks and sync option.
* @param enterCallback callback before each op.
* @param exitCallback callback after each op.
* @param sync wait until all ops done before return or not.
* @return result code.
*/
ErrorCode runWithCallBack(const TensorCallBackWithInfo& enterCallback, const TensorCallBackWithInfo& exitCallback,
bool sync = false) const;
/**
* @brief infer with loops. used for profiling only.
* @param loops run times.
* @return result code.
*/
ErrorCode runWithProfiler(int loops) const;
public:
/**
* @brief resize tensors and buffers responding to input changes.
* @return result code.
*/
ErrorCode resize();
/**
* @brief check if needs resize.
* @return needs resize or not.
*/
bool getNeedResize() const {
return mNeedResize;
}
/**
* @brief set if needs resize.
* @param flag needs resize or not.
*/
void setNeedResize(bool flag = true) {
mNeedResize = flag;
}
public:
/**
* @brief get backend that create the tensor.
* @param tensor given tensor.
* @return backend that create the tensor, NULL if the tensor is created by default backend (CPU backend).
*/
const Backend* getBackEnd(const Tensor* tensor) const;
/**
* @brief get input tensor for given op name.
* @param name given op name. if NULL, return first input tensor.
* @return input tensor if found, NULL otherwise.
*/
Tensor* getInput(const char* name) const;
/**
* @brief get output tensor for given op name.
* @param name given op name. if NULL, return first output tensor.
* @return output tensor if found, NULL otherwise.
*/
Tensor* getOutput(const char* name) const;
/**
* @brief get output tensors map.
* @return get output tensors map.
*/
const std::map<std::string, Tensor*>& getOutputAll() const;
const std::map<std::string, Tensor*>& getInputAll() const;
/**
* @brief check session is valid or not.
* @return session is valid or not.
*/
inline bool valid() const {
return mValid;
}
2019-04-17 10:49:11 +08:00
/**
* @brief the session will not be resized any more, release all cache used for resize.
* @return errorcode
*/
ErrorCode releaseCache();
/**
* @brief update the session's const value to origin model's const blob.
* @return errorcode
*/
ErrorCode updateToModel(Net* net) const;
2019-04-17 10:49:11 +08:00
protected:
const std::vector<std::unique_ptr<Pipeline>>& getPipelines() const {
return this->mPipelines;
}
private:
void _clearCache();
void _setUpTensorInfo(const Schedule::ScheduleInfo& info);
Backend* _getDefaultBackend();
private:
std::map<MNNForwardType, std::unique_ptr<Backend>> mBackends;
std::vector<std::unique_ptr<Pipeline>> mPipelines;
std::vector<std::pair<int, std::shared_ptr<Tensor>>> mTensors;
std::map<std::string, Tensor*> mInputs;
std::map<std::string, Tensor*> mOutputs;
bool mNeedResize = false;
bool mValid = true;
Backend* mFirstBackend = nullptr;
};
} // namespace MNN
#endif /* Session_hpp */