MNN/source/core/Pipeline.hpp

93 lines
3.0 KiB
C++
Raw Permalink Normal View History

2019-04-17 10:49:11 +08:00
//
// Pipeline.hpp
// MNN
//
// Created by MNN on 2019/01/14.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef Pipeline_hpp
#define Pipeline_hpp
#include "Schedule.hpp"
2020-11-05 16:41:56 +08:00
#include "core/Execution.hpp"
#include "geometry/GeometryComputer.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
struct OperatorInfo::Info {
std::string name;
std::string type;
float flops = 0.0f;
};
class SizeComputer;
/** pipeline. one session may contains multiple pipeline, and one pipeline may contains more than one unit. */
class Pipeline : public NonCopyable {
public:
2022-01-04 10:50:40 +08:00
struct TuningAttr {
bool autoSetOpType;
int maxTuningNumber;
};
2024-04-19 11:58:21 +08:00
Pipeline(const std::string& externalFile, Schedule::PipelineInfo&& info, bool allocInput, bool outputStatic, const TuningAttr& tune, const Runtime* rt, const Runtime* cpuRt);
2020-11-05 16:41:56 +08:00
~Pipeline();
2024-04-19 11:58:21 +08:00
ErrorCode fixResizeCache();
void openResizeCheck();
2020-11-05 16:41:56 +08:00
class UnitInfo : public OperatorInfo {
public:
UnitInfo() = default;
virtual ~UnitInfo() = default;
void setUp(const Command& cmd, int index, const Op* originOp, int totalIndex);
2020-11-05 16:41:56 +08:00
};
2019-04-17 10:49:11 +08:00
public:
2020-11-05 16:41:56 +08:00
/** encode :
1. compute shape for every op's inputs and outputs;
2. geometry transform;
3. copy op, inputs and outputs tensor info to mBuffer
static_model: 3; dynamic_model: 1,2,3
*/
2023-07-18 09:36:26 +08:00
ErrorCode encode(bool supportDebug = false, bool permitCodegen = false);
2020-11-05 16:41:56 +08:00
/** allocMemory: create Execution and alloc memory for every op */
2023-07-18 09:36:26 +08:00
ErrorCode allocMemory(bool firstMalloc, bool permitCodegen);
2020-11-05 16:41:56 +08:00
/** execute this pipline */
2019-04-17 10:49:11 +08:00
ErrorCode execute();
ErrorCode executeCallBack(const TensorCallBackWithInfo& before, const TensorCallBackWithInfo& after);
2022-12-30 15:18:58 +08:00
Schedule::PipelineInfo& getPipelineInfo() {
return mInfo;
}
2019-04-17 10:49:11 +08:00
float flops() const {
return mFlops;
}
friend class Session;
2022-01-04 10:50:40 +08:00
MNNForwardType getMainForwardType() const {
2022-12-30 15:18:58 +08:00
return mInfo.first.cache.first->type();
2022-01-04 10:50:40 +08:00
}
typedef std::map<std::pair<Tensor::InsideDescribe::NativeInsideDescribe*, Backend*>, std::pair<std::weak_ptr<Tensor::InsideDescribe::NativeInsideDescribe>, std::shared_ptr<Tensor>>> WrapTensorCache;
2019-04-17 10:49:11 +08:00
private:
2024-04-19 11:58:21 +08:00
ErrorCode _allocForTensor(int index, bool allocInput);
2022-12-30 15:18:58 +08:00
void _copyInputs();
void _pushTuningTask(std::vector<Schedule::OpCacheInfo>&& initInfos);
2022-01-04 10:50:40 +08:00
void _recycleDynamicMemory(Command* command);
2022-12-30 15:18:58 +08:00
Schedule::PipelineInfo mInfo;
2020-11-05 16:41:56 +08:00
bool mAllocInput;
bool mOutputStatic;
2022-01-04 10:50:40 +08:00
TuningAttr mTuneAttr;
float mFlops = 0.0f;
bool mIsQuantModel = false;
// For gpu or other backend
std::map<Tensor*, std::shared_ptr<Tensor>> mCacheConstTensors;
WrapTensorCache mWrapTensors;
2020-11-05 16:41:56 +08:00
#ifndef MNN_BUILD_MINI
GeometryComputer::Context mContext;
Runtime::CompilerType mUseGeometry;
2020-11-05 16:41:56 +08:00
#endif
2022-01-04 10:50:40 +08:00
const Runtime* mRuntime;
const Runtime* mCpuRuntime;
2024-04-19 11:58:21 +08:00
std::string mExternalFile;
std::vector<std::shared_ptr<BufferStorage>> mExternalStorage;
2019-04-17 10:49:11 +08:00
};
} // namespace MNN
#endif /* Pipeline_hpp */