CPUBackend.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // CPUBackend.hpp
  3. // MNN
  4. //
  5. // Created by MNN on 2018/07/06.
  6. // Copyright © 2018, Alibaba Group Holding Limited
  7. //
  8. #ifndef CPUBackend_hpp
  9. #define CPUBackend_hpp
  10. #include <map>
  11. #include <memory>
  12. #include "core/Backend.hpp"
  13. #include "core/Execution.hpp"
  14. #include "core/BufferAllocator.hpp"
  15. #include "MNN_generated.h"
  16. namespace MNN {
  17. class CPURuntime : public Runtime {
  18. public:
  19. friend class CPUBackend;
  20. CPURuntime(const Backend::Info& info);
  21. virtual ~ CPURuntime();
  22. int onGetRuntimeStatus(RuntimeStatus statusEnum) const override;
  23. virtual Backend* onCreate(const BackendConfig* config) const override;
  24. virtual void onGabageCollect(int level) override;
  25. virtual float onGetMemoryInMB() override;
  26. virtual CompilerType onGetCompilerType() const override {
  27. return Compiler_Loop;
  28. }
  29. void onConcurrencyBegin() const;
  30. void onConcurrencyEnd() const;
  31. virtual bool onCheckInfo(Backend::Info& info) const override;
  32. private:
  33. std::shared_ptr<EagerBufferAllocator> mStaticAllocator;
  34. int mThreadNumber;
  35. mutable int mTaskIndex;
  36. BackendConfig::MemoryMode mMemory;
  37. BackendConfig::PowerMode mPower;
  38. BackendConfig::PrecisionMode mPrecision;
  39. // Backend features
  40. // CPU features
  41. float mFlops = 0.0f;
  42. static Backend*(*gExtraCreate)(const Runtime* runtime);
  43. size_t mFlags = 0;
  44. int mAllocator = 0;
  45. };
  46. struct CoreFunctions;
  47. struct CoreInt8Functions;
  48. class CPUResizeCache;
  49. class CPUMemObj : public Backend::MemObj {
  50. public:
  51. CPUMemObj(BufferAllocator* allocator, MemChunk chunk, int size) : mAllocator(allocator), mChunk(chunk), mSize(size) {}
  52. virtual ~ CPUMemObj() {
  53. if (mAllocator) {
  54. mAllocator->free(mChunk);
  55. }
  56. }
  57. virtual MemChunk chunk() {
  58. return mChunk;
  59. }
  60. inline int getSize() const {
  61. return mSize;
  62. }
  63. private:
  64. BufferAllocator* mAllocator;
  65. MemChunk mChunk;
  66. int mSize;
  67. };
  68. class CPUBackend : public Backend {
  69. public:
  70. CPUBackend(const CPURuntime* runtime, BackendConfig::PrecisionMode precision, BackendConfig::MemoryMode memory, MNNForwardType type = MNN_FORWARD_CPU, size_t flags = 0);
  71. virtual ~CPUBackend();
  72. // Return sizeDivide, scheduleNumber aligned memory
  73. std::pair<int, int> multiThreadDivide(int size) const;
  74. public:
  75. virtual MemObj* onAcquire(const Tensor* nativeTensor, StorageType storageType) override;
  76. virtual bool onClearBuffer() override;
  77. virtual void onCopyBuffer(const Tensor* srcTensor, const Tensor* dstTensor) const override;
  78. virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
  79. const MNN::Op* op) override;
  80. virtual void onExecuteBegin() const override;
  81. virtual void onExecuteEnd() const override;
  82. virtual void onResizeBegin() override;
  83. virtual ErrorCode onResizeEnd() override;
  84. const CoreFunctions* functions() const {
  85. return mCoreFunctions;
  86. }
  87. // Return element size for Tensor, conside pack
  88. size_t getTensorSize(const Tensor* tensor, bool multiBytes = false) const;
  89. const CoreInt8Functions* int8Functions() const {
  90. return mInt8CoreFunctions;
  91. }
  92. public:
  93. class Creator {
  94. public:
  95. virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
  96. const MNN::Op* op, Backend* backend) const = 0;
  97. };
  98. static bool addCreator(OpType t, Creator* c);
  99. int threadNumber() const {
  100. return mRuntime->mThreadNumber;
  101. }
  102. BufferAllocator* getBufferAllocator(bool defer_allocator = true) const {
  103. return mDynamicAllocator.get();
  104. }
  105. BackendConfig::MemoryMode memoryMode() const {
  106. return mMemory;
  107. }
  108. BackendConfig::PrecisionMode precisionMode() const {
  109. return mPrecisionMode;
  110. }
  111. CPUResizeCache* getCache() const {
  112. return mCache;
  113. }
  114. virtual const Runtime* getRuntime() override;
  115. #ifdef MNN_USE_THREAD_POOL
  116. inline int taskIndex() const {return mRuntime->mTaskIndex;}
  117. #endif
  118. static void initCreatorMap();
  119. static int getBytes(const Backend* backend, const Tensor* output);
  120. static DataType getDataType(const Tensor* tensor);
  121. protected:
  122. MemObj* allocBuffer(size_t size, Tensor* dest, StorageType storageType);
  123. const CoreFunctions* mCoreFunctions;
  124. const CoreInt8Functions* mInt8CoreFunctions;
  125. private:
  126. std::shared_ptr<EagerBufferAllocator> mStaticAllocator;
  127. std::shared_ptr<BufferAllocator> mDynamicAllocator;
  128. CPURuntime* mRuntime;
  129. BackendConfig::PrecisionMode mPrecisionMode;
  130. BackendConfig::MemoryMode mMemory;
  131. static std::map<OpType, CPUBackend::Creator*>* gCreator;
  132. CPUResizeCache* mCache;
  133. };
  134. /** execution cast wrapper. insert tensor cast dynamic. */
  135. class CastWrapExecution : public Execution {
  136. public:
  137. CastWrapExecution(Backend* backend, DataType runT)
  138. : Execution(backend), mRunType(runT) {}
  139. virtual ErrorCode onExecute(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) override;
  140. private:
  141. DataType mRunType;
  142. };
  143. #define REGISTER_CPU_OP_CREATOR(name, opType) \
  144. void ___##name##__##opType##__() { \
  145. static name _temp;\
  146. CPUBackend::addCreator(opType, &_temp); \
  147. }
  148. #ifdef MNN_SUPPORT_DEPRECATED_OP
  149. #define REGISTER_CPU_OP_CREATOR_OLD(name, opType) \
  150. void ___##name##__##opType##__() { \
  151. static name _temp;\
  152. CPUBackend::addCreator(opType, &_temp); \
  153. }
  154. #else
  155. #define REGISTER_CPU_OP_CREATOR_OLD(name, opType) \
  156. void ___##name##__##opType##__() { \
  157. }
  158. #endif
  159. } // namespace MNN
  160. #endif /* CPUBackend_hpp */