2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// CPUBackend.hpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2018/07/06.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef CPUBackend_hpp
|
|
|
|
#define CPUBackend_hpp
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2024-07-22 19:51:53 +08:00
|
|
|
#include <MNN/AutoTime.hpp>
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Backend.hpp"
|
|
|
|
#include "core/Execution.hpp"
|
2023-09-04 10:42:11 +08:00
|
|
|
#include "core/BufferAllocator.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
#include "MNN_generated.h"
|
|
|
|
|
2025-07-16 13:11:57 +08:00
|
|
|
#ifdef MNN_USE_THREAD_POOL
|
|
|
|
#include "ThreadPool.hpp"
|
|
|
|
#endif
|
|
|
|
|
2024-08-16 09:05:35 +08:00
|
|
|
#ifdef MNN_KLEIDIAI_ENABLED
|
2024-12-10 18:24:00 +08:00
|
|
|
#include "arm/mnn_kleidiai.h"
|
2024-08-16 09:05:35 +08:00
|
|
|
#endif
|
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
namespace MNN {
|
2025-03-12 11:35:16 +08:00
|
|
|
class WorkerThread;
|
2020-11-05 16:41:56 +08:00
|
|
|
class CPURuntime : public Runtime {
|
|
|
|
public:
|
2024-09-12 12:57:57 +08:00
|
|
|
struct DynamicAllocator {
|
|
|
|
std::shared_ptr<BufferAllocator> mDynamicAllocator;
|
|
|
|
std::shared_ptr<BufferAllocator> mDynamicAllocatorBackup;
|
|
|
|
BufferAllocator* mCurrentDynamicAllocator = nullptr;
|
|
|
|
};
|
2020-11-05 16:41:56 +08:00
|
|
|
friend class CPUBackend;
|
|
|
|
CPURuntime(const Backend::Info& info);
|
|
|
|
virtual ~ CPURuntime();
|
2021-11-30 10:10:53 +08:00
|
|
|
int onGetRuntimeStatus(RuntimeStatus statusEnum) const override;
|
2024-09-12 12:57:57 +08:00
|
|
|
virtual Backend* onCreate(const BackendConfig* config, Backend* origin) const override;
|
2024-08-24 15:46:21 +08:00
|
|
|
virtual void onReset(int numberThread, const BackendConfig* config, bool full) override;
|
2020-11-05 16:41:56 +08:00
|
|
|
virtual void onGabageCollect(int level) override;
|
|
|
|
virtual float onGetMemoryInMB() override;
|
2021-06-11 17:17:13 +08:00
|
|
|
virtual CompilerType onGetCompilerType() const override {
|
|
|
|
return Compiler_Loop;
|
|
|
|
}
|
2025-05-23 15:21:41 +08:00
|
|
|
virtual void onConcurrencyBegin() const override;
|
|
|
|
virtual void onConcurrencyEnd() const override;
|
2023-05-18 19:11:50 +08:00
|
|
|
virtual bool onCheckInfo(Backend::Info& info) const override;
|
|
|
|
|
2024-09-12 12:57:57 +08:00
|
|
|
SingleBufferWithAllocator* buffer(int index) const;
|
|
|
|
BufferAllocator* createDynamicBufferAlloctor(int index) const;
|
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
private:
|
2024-07-22 19:51:53 +08:00
|
|
|
void _bindCPUCore() const;
|
2025-07-18 17:42:59 +08:00
|
|
|
void _resetThreadPool() const;
|
|
|
|
void _validateCpuIds() const;
|
2024-09-12 12:57:57 +08:00
|
|
|
mutable std::shared_ptr<EagerBufferAllocator> mStaticAllocator;
|
2025-07-18 17:42:59 +08:00
|
|
|
mutable int mThreadNumber;
|
|
|
|
mutable std::vector<int> mCpuIds;
|
|
|
|
mutable unsigned long mCpuMask;
|
2024-07-22 19:51:53 +08:00
|
|
|
#ifdef MNN_USE_THREAD_POOL
|
|
|
|
mutable int mTaskIndex = -1;
|
2025-05-23 15:21:41 +08:00
|
|
|
mutable int mThreadOpen = 0;
|
2025-07-18 17:42:59 +08:00
|
|
|
mutable ThreadPool* mThreadPool = nullptr;
|
2024-07-22 19:51:53 +08:00
|
|
|
#endif
|
2020-11-05 16:41:56 +08:00
|
|
|
BackendConfig::MemoryMode mMemory;
|
|
|
|
BackendConfig::PowerMode mPower;
|
|
|
|
BackendConfig::PrecisionMode mPrecision;
|
|
|
|
|
|
|
|
// Backend features
|
|
|
|
// CPU features
|
|
|
|
static Backend*(*gExtraCreate)(const Runtime* runtime);
|
2021-06-11 17:17:13 +08:00
|
|
|
size_t mFlags = 0;
|
2024-07-22 19:51:53 +08:00
|
|
|
mutable int mCurrentTID = 0;
|
2024-09-12 12:57:57 +08:00
|
|
|
mutable std::vector<SingleBufferWithAllocator> mDynamic;
|
|
|
|
mutable std::vector<SingleBufferWithAllocator> mDynamicMmap;
|
|
|
|
mutable std::shared_ptr<DynamicAllocator> mSharedDmaInfo;
|
2025-05-23 15:21:41 +08:00
|
|
|
mutable std::shared_ptr<EagerBufferAllocator> mStaticAllocatorRaw;
|
|
|
|
mutable std::shared_ptr<EagerBufferAllocator> mStaticAllocatorMMap;
|
2020-11-05 16:41:56 +08:00
|
|
|
};
|
2021-04-08 15:34:23 +08:00
|
|
|
struct CoreFunctions;
|
2021-06-11 17:17:13 +08:00
|
|
|
struct CoreInt8Functions;
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2021-11-30 10:10:53 +08:00
|
|
|
class CPUResizeCache;
|
2023-09-04 10:42:11 +08:00
|
|
|
class CPUMemObj : public Backend::MemObj {
|
|
|
|
public:
|
|
|
|
CPUMemObj(BufferAllocator* allocator, MemChunk chunk, int size) : mAllocator(allocator), mChunk(chunk), mSize(size) {}
|
|
|
|
virtual ~ CPUMemObj() {
|
|
|
|
if (mAllocator) {
|
|
|
|
mAllocator->free(mChunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual MemChunk chunk() {
|
|
|
|
return mChunk;
|
|
|
|
}
|
|
|
|
inline int getSize() const {
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
BufferAllocator* mAllocator;
|
|
|
|
MemChunk mChunk;
|
|
|
|
int mSize;
|
|
|
|
};
|
2020-11-05 16:41:56 +08:00
|
|
|
class CPUBackend : public Backend {
|
2019-04-17 10:49:11 +08:00
|
|
|
public:
|
2025-07-16 13:16:24 +08:00
|
|
|
CPUBackend(const CPURuntime* runtime, BackendConfig::PrecisionMode precision, BackendConfig::MemoryMode memory, MNNForwardType type = MNN_FORWARD_CPU, size_t flags = 0);
|
2019-04-17 10:49:11 +08:00
|
|
|
virtual ~CPUBackend();
|
|
|
|
|
2020-02-26 09:57:17 +08:00
|
|
|
// Return sizeDivide, scheduleNumber aligned memory
|
|
|
|
std::pair<int, int> multiThreadDivide(int size) const;
|
2024-04-19 11:58:21 +08:00
|
|
|
virtual bool onSelectDynamicAllocator(int index, int maxIndex) override;
|
2024-10-14 19:26:28 +08:00
|
|
|
// dividedSize's length should be larger than threadNumber
|
2024-12-02 10:12:08 +08:00
|
|
|
void computeDivideSizes(int size, int* dst, float computeI = 0.f) const;
|
2024-04-19 11:58:21 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
public:
|
2021-11-30 10:10:53 +08:00
|
|
|
virtual MemObj* onAcquire(const Tensor* nativeTensor, StorageType storageType) override;
|
2019-04-17 10:49:11 +08:00
|
|
|
virtual bool onClearBuffer() override;
|
|
|
|
virtual void onCopyBuffer(const Tensor* srcTensor, const Tensor* dstTensor) const override;
|
|
|
|
|
|
|
|
virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
|
|
|
|
const MNN::Op* op) override;
|
2022-08-12 10:30:48 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
virtual void onExecuteBegin() const override;
|
2019-07-19 17:09:09 +08:00
|
|
|
virtual void onExecuteEnd() const override;
|
2023-12-04 11:12:20 +08:00
|
|
|
virtual void* onMapTensor(Tensor::MapType mtype, Tensor::DimensionType dtype, const Tensor* srcTensor) override;
|
|
|
|
|
|
|
|
virtual bool onUnmapTensor(Tensor::MapType mtype, Tensor::DimensionType dtype, const Tensor* dstTensor, void* mapPtr) override;
|
2024-05-11 19:17:02 +08:00
|
|
|
|
2023-09-04 10:42:11 +08:00
|
|
|
virtual void onResizeBegin() override;
|
2023-09-20 20:16:25 +08:00
|
|
|
virtual ErrorCode onResizeEnd() override;
|
2021-04-08 15:34:23 +08:00
|
|
|
|
|
|
|
const CoreFunctions* functions() const {
|
|
|
|
return mCoreFunctions;
|
|
|
|
}
|
2021-06-11 17:17:13 +08:00
|
|
|
// Return element size for Tensor, conside pack
|
2023-09-20 20:16:25 +08:00
|
|
|
size_t getTensorSize(const Tensor* tensor, bool multiBytes = false) const;
|
2021-06-11 17:17:13 +08:00
|
|
|
const CoreInt8Functions* int8Functions() const {
|
|
|
|
return mInt8CoreFunctions;
|
|
|
|
}
|
2024-09-12 12:57:57 +08:00
|
|
|
void _resetDynamicMemory() const;
|
2019-04-17 10:49:11 +08:00
|
|
|
public:
|
|
|
|
class Creator {
|
|
|
|
public:
|
|
|
|
virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
|
|
|
|
const MNN::Op* op, Backend* backend) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool addCreator(OpType t, Creator* c);
|
|
|
|
|
2024-07-22 19:51:53 +08:00
|
|
|
inline int threadNumber() const {
|
2024-10-14 19:26:28 +08:00
|
|
|
return mThreadNumber;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
2023-09-04 10:42:11 +08:00
|
|
|
BufferAllocator* getBufferAllocator(bool defer_allocator = true) const {
|
2024-09-12 12:57:57 +08:00
|
|
|
return mDmaInfo->mCurrentDynamicAllocator;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BackendConfig::MemoryMode memoryMode() const {
|
2023-04-18 18:54:46 +08:00
|
|
|
return mMemory;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
2021-04-08 15:34:23 +08:00
|
|
|
BackendConfig::PrecisionMode precisionMode() const {
|
|
|
|
return mPrecisionMode;
|
|
|
|
}
|
2021-11-30 10:10:53 +08:00
|
|
|
CPUResizeCache* getCache() const {
|
|
|
|
return mCache;
|
|
|
|
}
|
2022-09-30 10:02:52 +08:00
|
|
|
|
|
|
|
virtual const Runtime* getRuntime() override;
|
|
|
|
|
2019-07-19 17:09:09 +08:00
|
|
|
#ifdef MNN_USE_THREAD_POOL
|
2020-11-05 16:41:56 +08:00
|
|
|
inline int taskIndex() const {return mRuntime->mTaskIndex;}
|
2025-07-16 13:11:57 +08:00
|
|
|
inline ThreadPool* threadPool() const {return mRuntime->mThreadPool;}
|
2019-07-19 17:09:09 +08:00
|
|
|
#endif
|
2020-11-05 16:41:56 +08:00
|
|
|
static void initCreatorMap();
|
2025-05-30 18:38:23 +08:00
|
|
|
static size_t getBytes(const Backend* backend, const Tensor* output);
|
2021-11-30 10:10:53 +08:00
|
|
|
static DataType getDataType(const Tensor* tensor);
|
2024-07-22 19:51:53 +08:00
|
|
|
friend class CPURuntime;
|
2025-03-12 11:35:16 +08:00
|
|
|
void enqueueTask(std::function<int()>&& task);
|
2022-09-30 10:02:52 +08:00
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
protected:
|
2023-09-20 20:16:25 +08:00
|
|
|
MemObj* allocBuffer(size_t size, Tensor* dest, StorageType storageType);
|
2024-07-22 19:51:53 +08:00
|
|
|
CoreFunctions* mCoreFunctions;
|
|
|
|
CoreInt8Functions* mInt8CoreFunctions;
|
2019-04-17 10:49:11 +08:00
|
|
|
private:
|
2025-03-12 11:35:16 +08:00
|
|
|
mutable std::shared_ptr<WorkerThread> mInitWorkQueue;
|
2024-10-14 19:26:28 +08:00
|
|
|
int mThreadNumber;
|
2025-07-16 13:11:57 +08:00
|
|
|
#ifdef MNN_USE_THREAD_POOL
|
|
|
|
ThreadPool* mThreadPool = nullptr;
|
|
|
|
#endif
|
2024-10-14 19:26:28 +08:00
|
|
|
std::vector<std::pair<float, int>> mGroupWithComputeRate;
|
2024-12-02 10:12:08 +08:00
|
|
|
float mComputeI = 0.f;
|
2024-10-14 19:26:28 +08:00
|
|
|
|
2024-09-12 12:57:57 +08:00
|
|
|
std::shared_ptr<CPURuntime::DynamicAllocator> mDmaInfo;
|
2022-09-30 10:02:52 +08:00
|
|
|
CPURuntime* mRuntime;
|
2021-04-08 15:34:23 +08:00
|
|
|
BackendConfig::PrecisionMode mPrecisionMode;
|
2023-04-18 18:54:46 +08:00
|
|
|
BackendConfig::MemoryMode mMemory;
|
2020-11-05 16:41:56 +08:00
|
|
|
static std::map<OpType, CPUBackend::Creator*>* gCreator;
|
2021-11-30 10:10:53 +08:00
|
|
|
CPUResizeCache* mCache;
|
2024-04-19 11:58:21 +08:00
|
|
|
std::vector<std::shared_ptr<CPUResizeCache>> mCacheGroup;
|
2019-04-17 10:49:11 +08:00
|
|
|
};
|
2022-12-30 15:18:58 +08:00
|
|
|
/** execution cast wrapper. insert tensor cast dynamic. */
|
|
|
|
class CastWrapExecution : public Execution {
|
|
|
|
public:
|
|
|
|
CastWrapExecution(Backend* backend, DataType runT)
|
|
|
|
: Execution(backend), mRunType(runT) {}
|
|
|
|
virtual ErrorCode onExecute(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) override;
|
|
|
|
private:
|
|
|
|
DataType mRunType;
|
|
|
|
};
|
|
|
|
|
2019-05-09 19:39:33 +08:00
|
|
|
|
|
|
|
#define REGISTER_CPU_OP_CREATOR(name, opType) \
|
|
|
|
void ___##name##__##opType##__() { \
|
2020-11-05 16:41:56 +08:00
|
|
|
static name _temp;\
|
|
|
|
CPUBackend::addCreator(opType, &_temp); \
|
2019-05-09 19:39:33 +08:00
|
|
|
}
|
|
|
|
|
2022-07-19 13:52:07 +08:00
|
|
|
#ifdef MNN_SUPPORT_DEPRECATED_OP
|
|
|
|
#define REGISTER_CPU_OP_CREATOR_OLD(name, opType) \
|
|
|
|
void ___##name##__##opType##__() { \
|
|
|
|
static name _temp;\
|
|
|
|
CPUBackend::addCreator(opType, &_temp); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define REGISTER_CPU_OP_CREATOR_OLD(name, opType) \
|
|
|
|
void ___##name##__##opType##__() { \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-12-04 11:12:20 +08:00
|
|
|
#define REGISTER_CPU_OP_CREATOR_RENDER(name, opType) \
|
|
|
|
void ___##name##__##opType##__() { \
|
|
|
|
static name _temp;\
|
|
|
|
CPUBackend::addCreator(opType, &_temp); \
|
|
|
|
}
|
|
|
|
|
2024-05-11 19:17:02 +08:00
|
|
|
#define REGISTER_CPU_OP_CREATOR_TRANSFORMER(name, opType) \
|
|
|
|
void ___##name##__##opType##__() { \
|
|
|
|
static name _temp;\
|
|
|
|
CPUBackend::addCreator(opType, &_temp); \
|
|
|
|
}
|
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
} // namespace MNN
|
|
|
|
|
|
|
|
#endif /* CPUBackend_hpp */
|