/** * Copyright (c) Hisilicon Technologies Co., Ltd.. 2020-2020. All rights reserved. * */ #ifndef __AI_MODEL_MANAGER_IMPL_H__ #define __AI_MODEL_MANAGER_IMPL_H__ #include #include #include #include #include #include #include #include #include #include "AiConfig.h" #include "AiMMListenerProxy.h" static const unsigned MAX_INSTANCE_NUM = 10; static const int PROCESS_NAME_MAX_LENGTH = 256; std::string ConcatModelProcessName(std::string modelName); // ??׮????λ #define PILING 1 #define DEBUG0 1 #define REMOVE30 1 #define SUPPORT_AIPP 1 namespace ai { using namespace android; using ::vendor::huanglong::hardware::hwhlai::V1_0::IAiEngineService; using ::vendor::huanglong::hardware::hwhlai::V1_0::IAiModelMngr; using ::vendor::huanglong::hardware::hwhlai::V1_0::IAiMMListenerProxy; using ::android::hardware::Return; using ::android::hardware::hidl_death_recipient; using ::android::hardware::IPCThreadState; using ::android::hidl::base::V1_0::IBase; using ::vendor::huanglong::hardware::hwhlai::V1_0::ModelInfo; using ::vendor::huanglong::hardware::hwhlai::V1_0::ModelDescription; using ::vendor::huanglong::hardware::hwhlai::V1_0::ModelBuffer; using ::vendor::huanglong::hardware::hwhlai::V1_0::AippPosition; using ::vendor::huanglong::hardware::hwhlai::V1_0::DynamicConfig; using ::vendor::huanglong::hardware::hwhlai::V1_0::TensorDescriptionV2; using ::vendor::huanglong::hardware::hwhlai::V1_0::DataInfo; // instaceID是任务标记,如模型兼容性检查,加载运行模型都是独立的instance // modelID是模型标记,由ge加载模型成功后返回 class AiModelManagerImpl : virtual public RefBase, public Singleton { public: typedef enum { NO_INIT = 0, INITED, } AiStatus; class InstanceInfo { public: InstanceInfo() = default; InstanceInfo(int32_t instanceID, bool isSync) : mInstanceID(instanceID), mIsSync(isSync) { } ~InstanceInfo() {}; int32_t getInstanceID() { return mInstanceID; } bool isSync() { return mIsSync; } private: int32_t mInstanceID {0}; bool mIsSync {true}; }; struct TensorAippInfo { uint32_t batchCount = 0; uint32_t aippNodesCount = 0; }; public: AiModelManagerImpl(); ~AiModelManagerImpl(); public: bool _register(void* instance); bool _register(void* instance, const sp& listener); void _unregister(void* instance); public: /* QUERY FUNCS */ int32_t checkModelNameValid(const string& modelName, bool emtpy = false); int32_t checkModelDescValid(ModelDescription& modelPara); int32_t checkModelDescVecValid(vector& modelParaVec); int32_t checkModelBufferValid(ModelBuffer& modelBuffer); int32_t checkModelBufferVecValid(vector& modelBufferVec); AiModelManager::ModelTensorInfo getModelTensor(void* instance, const string& modelName); AiModelManager::ModelTensorName getModelTensorName(void* instance, const string& modelName); AiModelManager::ModelDynamicBatch getDynamicBatch(void* instance, const string& modelName); AiModelManager::ModelDynamicHW getDynamicHW(void* instance, const string& modelName); TensorAippInfo GetTensorAippInfo(void* instance, const string& modelName, uint32_t index); int32_t GetTensorAippParas(void* instance, const string& modelName, uint32_t index, vector& buffers); #if REMOVE30 #else std::pair, vector> getModelTensorV2(void* instance, const string& modelName); #endif int32_t getMaxUsedMemory(void* instance); bool isServiceDied(); public: /* OPERATION FUNCS */ int32_t startModel(void* instance, int32_t* taskStamp, ModelDescription& modelPara); int32_t startModel(void* instance, int32_t* taskStamp, vector& modelParaVec); int32_t startModel(void* instance, int32_t* taskStamp, ModelBuffer& modelBuffer); int32_t startModel(void* instance, int32_t* taskStamp, vector& modelBufferVec); int32_t runModel(void* instance, int32_t* taskStamp, const vector srcDataVec, const vector srcTensorVec, vector destDataVec, vector destTensorVec, uint32_t ulTimeout, string modelName = ""); int32_t RunAippModel(void* instance, int32_t* taskStamp, const vector& srcDataVec, const vector& srcTensorVec, const vector& aippDataVec, const vector& aippPostionVec, const vector& destDataVec, const vector &destTensorVec, uint32_t ulTimeout, string modelName = ""); int32_t stopModel(void* instance, int32_t* taskStamp); int32_t runDynamicModel(void* instance, int32_t* taskStamp, const vector srcDataVec, const vector srcTensorVec, vector destDataVec, vector destTensorVec, uint32_t ulTimeout, DynamicConfig dynamicPara, string modelName = ""); int32_t RunAippDynamicModel(void* instance, int32_t* taskStamp, const vector& srcDataVec, const vector& srcTensorVec, const vector& aippDataVec, const vector& aippPostionVec, const vector& destDataVec, const vector &destTensorVec, uint32_t ulTimeout, DynamicConfig dynamicPara, string modelName = ""); public: /* FOR MICROSOFT */ int32_t setInputsAndOutputs(void* instance, string modelname, vector& nn_inputs, vector& nn_outputs); int32_t startCompute(void* instance, string modelname); int32_t buildModel(uint8_t frameworkType, vector inputModelBuffers, vector inputModelBufferSizes, void* outputModelBuffer, uint32_t outputModelBufferSize, uint32_t* outModelSize); int32_t checkModelValid(void* instance); int32_t checkModelCompatibility(void* instance, ModelBuffer& modelBuffer); int32_t allocMemory(const void* instance, string config, int size, native_handle_t* &handle); int32_t freeMemory(const void* instance, string config, native_handle_t* handle); int32_t createByFd(const void* instance, size_t offset, size_t size, native_handle_t* handle); int createAshmemFd(const char* name, size_t size); private: void init(); void deinit(); int32_t createInstanceID(); void freeInstanceID(int32_t id); void resetInstanceIDPool(); bool isTensorDescValid(const TensorDescription& tensor); int32_t checkDataVecValid(const vector& dataVec, bool inFlag); int32_t checkTensorDescVecValid(const vector& tensorVec, bool inFlag); int32_t checkNNBufferValid(AINeuralNetworkBuffer& nn_buffer, bool inFlag); int32_t checkNNBufferVecValid(vector& nn_bufferVec, bool inFlag); int32_t CheckRunModelVecInfoValid(void* instance, int32_t* taskStamp, const vector srcDataVec, const vector srcTensorVec, vector destDataVec, vector destTensorVec, uint32_t ulTimeout, string modelName); private: sp connectAiEngineService(); void setServiceDiedStatus(bool status); private: android::Mutex mStatusLock; android::Mutex mLock; bool mIsBinderDied{false}; class MMRecipientClient : public hidl_death_recipient { public: MMRecipientClient(AiModelManagerImpl* client) : mClient(client) {} ~MMRecipientClient() {} virtual void serviceDied(uint64_t cookie, const wp& who); private: wp mClient; }; sp mMMRecipientClient; sp mIAiModelMngrService; sp mAiEngineService; sp mMMListenerProxy; uint32_t mMaxSizeCheck{0}; // to limit the number of inputs and outputs std::atomic mTaskCounter; std::map mInstanceMap; int32_t mInstanceIDPool[MAX_INSTANCE_NUM * 2]; }; }; #endif // __AI_MODEL_MANAGER_IMPL_H__