系列文章目录
MNN createFromBuffer(一)
MNN createRuntime(二)
MNN createSession 之 Schedule(三)
MNN createSession 之创建流水线后端(四)
MNN Session 之维度计算(五)
MNN Session 之几何计算(六)
MNN Session 之 CPU 算子(七)
MNN Session 之 Vulkan 算子(八)
MNN 执行推理(九)
文章目录
- 系列文章目录
- 1、createSession
-
- 1.1 createMultiPathSession
- 1.1.1 Session::resize
- 1.1.1.1 Pipeline::encode
- 1.1.1.1.1 GeometryComputerUtils::shapeComputeAndGeometryTransform
- 1.1.1.1.1.1 GeometryComputer::search
- 1.1.1.1.1.1.1 GeometryComputerManager::search
- 1.1.1.1.1.1.1.1 几何计算初始化
- 1.1.1.1.1.2 GeometryComputer::onRecompute
- 1.1.1.1.1.3 GeometryComputer::onCompute
- 1.1.1.1.1.4 GeometryComputerUtils::makeRaster
- 1.1.1.1.1.5 GeometryComputer::Context::getRasterCacheCreateRecursive
1、createSession
依据 ScheduleConfig 和 RuntimeInfo 创建会话。
// source/core/Interpreter.cpp
Session* Interpreter::createSession(const ScheduleConfig& config, const RuntimeInfo& runtime) {
return createMultiPathSession({
config}, runtime);
}
1.1 createMultiPathSession
// source/core/Interpreter.cpp
Session* Interpreter::createMultiPathSession(const std::vector<ScheduleConfig>& configs, const RuntimeInfo& runtime) {
// ...
auto result = newSession.get();
auto validForResize = info.validForResize;
if (validForResize && mNet->modes.inputMode == Session_Input_Inside && mNet->modes.resizeMode == Session_Resize_Direct) {
result->resize();
}
// ...
return result;
}
1.1.1 Session::resize
// source/core/Session.cpp
ErrorCode Session::resize() {
// ...
if (mNeedResize) {
bool debug = mCallBackMode == Interpreter::Session_Debug;
// mPipelines 类型为 std::vector<std::shared_ptr<Pipeline>>
for (auto& iter : mPipelines) {
auto error = iter->encode(debug, permitCodegen);
if (NO_ERROR != error) {
return error;
}
}
mNeedResize = false;
mNeedMalloc = true;
firstMalloc = true;
}
// ...
}
1.1.1.1 Pipeline::encode
Pipeline::encode 完整代码
BackendCache、OpCacheInfo
// source/core/Pipeline.cpp
// typedef std::pair<BackendCache, std::vector<OpCacheInfo>> PipelineInfo;
//
// struct BackendCache {
// Backend::Info info;
// BackendConfig config;
// std::pair<std::shared_ptr<Backend>, std::shared_ptr<Backend>> cache;
// bool needComputeShape = true;
// bool needComputeGeometry = true;
// bool reportError = true;
// std::map<Tensor*, TENSORCACHE> inputTensorCopyCache;
// };
//
// /** pipeline info */
// struct OpCacheInfo {
// /** op */
// const Op* op;
// /** input tensors */
// std::vector<Tensor*> inputs;
// /** output tensors */
// std::vector<Tensor*> outputs;
// /** schedule type*/
// Schedule::Type type = Schedule::Type::SEPARATE;
//
// /**Command buffer for cache*/
// CommandBuffer cacheBuffer;
//
// /**Command buffer for execute*/
// CommandBuffer executeBuffer;
//
// std::map<const Op*, std::shared_ptr<Execution>> executionCache;
// };
//
ErrorCode Pipeline::encode(bool supportDebug, bool permitCodegen) {
// mInfo.first.cache 类型为 std::pair<std::shared_ptr<Backend>, std::shared_ptr<Backend>>
// mBackend 创建的后端如(VulkanBackend)
auto& mBackend = mInfo.first.cache.first;
// mBackupBackend 创建的后备(默认)后端如(CPUBackend)
auto& mBackupBackend = mInfo.first.cache.second;
// Static Model just copy info to command buffer
// mInfo.first 类型为 BackendCache
if (!mInfo.first.needComputeGeometry) {
// ...
} else {
#ifndef MNN_BUILD_MINI
// mContext 类型为 GeometryComputer::Context
mContext.clear();
/** Size Compute and compute Const Begin */
auto res = GeometryComputerUtils::shapeComputeAndGeometryTransform(mInfo.second, mContext, mInfo.first.cache.second, mUseGeometry, false, permitCodegen);
if (res != NO_ERROR) {
return res;
}
#endif
}
// ...
return NO_ERROR;
}
1.1.1.1.1 GeometryComputerUtils::shapeComputeAndGeometryTransform
GeometryComputerUtils::shapeComputeAndGeometryTransform 完整代码
OpCacheInfo
// source/geometry/GeometryComputerUtils.cpp
// /** pipeline info */
// struct OpCacheInfo {
// /** op */
// const Op* op;
// /** input tensors */
// std::vector<Tensor*> inputs;
// /** output tensors */
// std::vector<Tensor*> outputs;
// /** schedule type*/
// Schedule::Type type = Schedule::Type::SEPARATE;
//
// /**Command buffer for cache*/
// CommandBuffer cacheBuffer;
//
// /**Command buffer for execute*/
// CommandBuffer executeBuffer;
//
// std::map<const Op*, std::shared_ptr<Execution>> executionCache;
// };
//
ErrorCode GeometryComputerUtils::shapeComputeAndGeometryTransform(
std::vector<Schedule::OpCacheInfo>& infos,
GeometryComputer::Context& geoContext,
std::shared_ptr<Backend> backupBackend,
Runtime::CompilerType compileType,
bool skipShapeCompute,
bool permitCodegen) {
/** Size Compute and compute Const Begin */
GeometryComputer::Context ctx(backupBackend);
// Size Compute and compute Const
// infos 为算子缓存,大小为 171
for (int i=0; i<infos.size(); ++i) {
// info 类型为 OpCacheInfo
auto& info = infos[i];
auto& cmdBufferVir = info.executeBuffer;
auto&