diff --git a/bundle.json b/bundle.json index 89327cdb8977be8bec2702454e684bf220bf04c0..bb80c796ee7b3a869e7bd69f3ea8169f87dfa027 100644 --- a/bundle.json +++ b/bundle.json @@ -30,9 +30,7 @@ "hitrace", "ipc", "mindspore", - "init", - "json", - "jsoncpp" + "init" ], "third_party": [] }, diff --git a/frameworks/native/neural_network_core/cpp_type.h b/frameworks/native/neural_network_core/cpp_type.h index 690f417f407e042053c0df840d0796816934a25e..3122297a815ec22734cedce52e913f56bb3b1b33 100644 --- a/frameworks/native/neural_network_core/cpp_type.h +++ b/frameworks/native/neural_network_core/cpp_type.h @@ -44,7 +44,6 @@ enum class TuningStrategy { struct Buffer { void* data = nullptr; size_t length = 0; - int fd = -1; }; struct ExtensionConfig { diff --git a/frameworks/native/neural_network_core/neural_network_core.cpp b/frameworks/native/neural_network_core/neural_network_core.cpp index fb1c31393fda59776b55b17453fdadd9071ccfdd..ee8aa28c99e24401cf8993219d001fc86bba928c 100644 --- a/frameworks/native/neural_network_core/neural_network_core.cpp +++ b/frameworks/native/neural_network_core/neural_network_core.cpp @@ -31,7 +31,6 @@ using namespace OHOS::NeuralNetworkRuntime; #define NNRT_API __attribute__((visibility("default"))) -const size_t INPUT_OUTPUT_MAX_INDICES = 200; NNRT_API OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount) { @@ -751,12 +750,6 @@ NNRT_API OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation) configContents.push_back('0'); } - NNRtServiceApi& nnrtService = NNRtServiceApi::GetInstance(); - bool retCode = nnrtService.PullUpDlliteService(); - if (!retCode) { - LOGI("OH_NNCompilation_Build failed, PullUpDlliteService failed."); - } - configs["isExceedRamLimit"] = configContents; compilationImpl->compiler->SetExtensionConfig(configs); @@ -1706,17 +1699,16 @@ NNRT_API OH_NN_ReturnCode OH_NNExecutor_RunSync(OH_NNExecutor *executor, LOGE("OH_NNExecutor_RunSync failed, inputTensor is nullptr."); return OH_NN_INVALID_PARAMETER; } - - if ((inputCount == 0) || (inputCount > INPUT_OUTPUT_MAX_INDICES)) { - LOGE("OH_NNExecutor_RunSync failed, inputCount is 0 or more than 200."); + if (inputCount == 0) { + LOGE("OH_NNExecutor_RunSync failed, inputCount is 0."); return OH_NN_INVALID_PARAMETER; } if (outputTensor == nullptr) { LOGE("OH_NNExecutor_RunSync failed, outputTensor is nullptr."); return OH_NN_INVALID_PARAMETER; } - if ((outputCount == 0) || (outputCount > INPUT_OUTPUT_MAX_INDICES)) { - LOGE("OH_NNExecutor_RunSync failed, outputCount is 0 or more than 200."); + if (outputCount == 0) { + LOGE("OH_NNExecutor_RunSync failed, outputCount is 0."); return OH_NN_INVALID_PARAMETER; } @@ -1740,16 +1732,16 @@ NNRT_API OH_NN_ReturnCode OH_NNExecutor_RunAsync(OH_NNExecutor *executor, LOGE("OH_NNExecutor_RunAsync failed, inputTensor is nullptr."); return OH_NN_INVALID_PARAMETER; } - if ((inputCount == 0) || (inputCount > INPUT_OUTPUT_MAX_INDICES)) { - LOGE("OH_NNExecutor_RunAsync failed, inputCount is 0 or more than 200."); + if (inputCount == 0) { + LOGE("OH_NNExecutor_RunAsync failed, inputCount is 0."); return OH_NN_INVALID_PARAMETER; } if (outputTensor == nullptr) { LOGE("OH_NNExecutor_RunAsync failed, outputTensor is nullptr."); return OH_NN_INVALID_PARAMETER; } - if ((outputCount == 0) || (outputCount > INPUT_OUTPUT_MAX_INDICES)) { - LOGE("OH_NNExecutor_RunAsync failed, outputCount is 0 or more than 200."); + if (outputCount == 0) { + LOGE("OH_NNExecutor_RunAsync failed, outputCount is 0."); return OH_NN_INVALID_PARAMETER; } if (userData == nullptr) { diff --git a/frameworks/native/neural_network_core/nnrt_client.cpp b/frameworks/native/neural_network_core/nnrt_client.cpp index 1dcf43f737202955022de4dc7a54ad1a2c34417d..4921ac11c69a5be9776956ee7fa78875b0907136 100644 --- a/frameworks/native/neural_network_core/nnrt_client.cpp +++ b/frameworks/native/neural_network_core/nnrt_client.cpp @@ -72,7 +72,6 @@ NNRtServiceApi& NNRtServiceApi::GetInstance() LoadFunction(libNNRtService, "Scheduling", &nnrtService.Scheduling); LoadFunction(libNNRtService, "UpdateModelLatency", &nnrtService.UpdateModelLatency); LoadFunction(libNNRtService, "Unload", &nnrtService.Unload); - LoadFunction(libNNRtService, "PullUpDlliteService", &nnrtService.PullUpDlliteService); nnrtService.m_serviceAvailable = true; return nnrtService; diff --git a/frameworks/native/neural_network_core/nnrt_client.h b/frameworks/native/neural_network_core/nnrt_client.h index 03fb8b03041f657b2ef7ee102553039270597236..8b87c900b4d4410b2df1609183630b038d55d3b1 100644 --- a/frameworks/native/neural_network_core/nnrt_client.h +++ b/frameworks/native/neural_network_core/nnrt_client.h @@ -41,7 +41,6 @@ public: int (*Scheduling)(uint32_t hiaiModelId, bool* needModelLatency, const char* cachePath) = nullptr; int (*UpdateModelLatency)(uint32_t hiaiModelId, int modelLatency) = nullptr; int (*Unload)(uint32_t hiaiModelId) = nullptr; - bool (*PullUpDlliteService)() = nullptr; private: bool m_serviceAvailable = false; diff --git a/frameworks/native/neural_network_core/tensor_desc.cpp b/frameworks/native/neural_network_core/tensor_desc.cpp index b7ecf7499f1553e45fc382ae38ebe8f3d08e5177..2f1a8bf2903aa1eea726591755f705a625067bbe 100644 --- a/frameworks/native/neural_network_core/tensor_desc.cpp +++ b/frameworks/native/neural_network_core/tensor_desc.cpp @@ -23,7 +23,6 @@ const uint32_t BIT8_TO_BYTE = 1; const uint32_t BIT16_TO_BYTE = 2; const uint32_t BIT32_TO_BYTE = 4; const uint32_t BIT64_TO_BYTE = 8; -const size_t SHAPE_MAX_NUM = 10; uint32_t GetTypeSize(OH_NN_DataType type) { @@ -115,11 +114,10 @@ OH_NN_ReturnCode TensorDesc::SetShape(const int32_t* shape, size_t shapeNum) LOGE("SetShape failed, shape is nullptr."); return OH_NN_INVALID_PARAMETER; } - if (shapeNum == 0 || shapeNum > SHAPE_MAX_NUM) { - LOGE("SetShape failed, shapeNum is 0 or greater than 10."); + if (shapeNum == 0) { + LOGE("SetShape failed, shapeNum is 0."); return OH_NN_INVALID_PARAMETER; } - m_shape.clear(); for (size_t i = 0; i < shapeNum; ++i) { m_shape.emplace_back(shape[i]); diff --git a/frameworks/native/neural_network_runtime/BUILD.gn b/frameworks/native/neural_network_runtime/BUILD.gn index 5ef450f5a81f243770636c8a0a9dd305c958fa17..31959c7a1963dc2c2b9cee7b2efe8fc6d5efe1be 100644 --- a/frameworks/native/neural_network_runtime/BUILD.gn +++ b/frameworks/native/neural_network_runtime/BUILD.gn @@ -195,7 +195,6 @@ ohos_shared_library("libneural_network_runtime") { "hitrace:libhitracechain", "init:libbegetutil", "ipc:ipc_core", - "json:nlohmann_json_static", "mindspore:mindir_lib", ] diff --git a/frameworks/native/neural_network_runtime/device.h b/frameworks/native/neural_network_runtime/device.h index 78968993d57732eafa7fa9dbcee533fbb61f19a4..17017ba740c58376be9f3d4aa2bd67d6ee66d39c 100644 --- a/frameworks/native/neural_network_runtime/device.h +++ b/frameworks/native/neural_network_runtime/device.h @@ -69,7 +69,6 @@ public: virtual OH_NN_ReturnCode AllocateBuffer(size_t length, int& fd) = 0; virtual OH_NN_ReturnCode ReleaseBuffer(int fd, size_t length) = 0; - virtual OH_NN_ReturnCode ReadOpVersion(int& currentOpVersion) = 0; }; } // namespace NeuralNetworkRuntime } // namespace OHOS diff --git a/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp b/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp index 67a2a3b2d2d2c5f34eb54572717220359caf7ba2..a8fac98e81a68119b4a6979a8f63cdaf3c0e58e8 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp +++ b/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp @@ -302,9 +302,17 @@ OH_NN_ReturnCode HDIDeviceV1_0::PrepareModelFromModelCache(const std::vector& preparedModel, bool& isUpdatable) { std::vector iBuffers; + auto memManager = MemoryManager::GetInstance(); + Memory memory; + OH_NN_ReturnCode ret; size_t modelCacheSize = modelCache.size(); for (size_t i = 0; i < modelCacheSize; i++) { - iBuffers.emplace_back(V1_0::SharedBuffer {modelCache[i].fd, modelCache[i].length, 0, modelCache[i].length}); + ret = memManager->GetMemory(modelCache[i].data, memory); + if (ret != OH_NN_SUCCESS) { + LOGE("The %zuth model cache is invalid. Please put valid model cache.", i + 1); + return ret; + } + iBuffers.emplace_back(V1_0::SharedBuffer {memory.fd, memory.length, 0, memory.length}); } V1_0::ModelConfig iModelConfig; @@ -443,11 +451,5 @@ OH_NN_ReturnCode HDIDeviceV1_0::PrepareOfflineModel(std::shared_ptr& preparedModel, bool& isUpdatable) { std::vector iBuffers; + auto memManager = MemoryManager::GetInstance(); + Memory memory; + OH_NN_ReturnCode ret; size_t modelCacheSize = modelCache.size(); for (size_t i = 0; i < modelCacheSize; i++) { - iBuffers.emplace_back(V2_0::SharedBuffer {modelCache[i].fd, modelCache[i].length, 0, modelCache[i].length}); + ret = memManager->GetMemory(modelCache[i].data, memory); + if (ret != OH_NN_SUCCESS) { + LOGE("The %{public}zuth model cache is invalid. Please put valid model cache.", i + 1); + return ret; + } + iBuffers.emplace_back(V2_0::SharedBuffer {memory.fd, memory.length, 0, memory.length}); } V2_0::ModelConfig iModelConfig; @@ -676,11 +684,5 @@ OH_NN_ReturnCode HDIDeviceV2_0::PrepareOfflineModel(std::shared_ptr& preparedModel, bool& isUpdatable) { std::vector iBuffers; + auto memManager = MemoryManager::GetInstance(); + Memory memory; + OH_NN_ReturnCode ret; size_t modelCacheSize = modelCache.size(); for (size_t i = 0; i < modelCacheSize; i++) { - iBuffers.emplace_back(V2_1::SharedBuffer {modelCache[i].fd, modelCache[i].length, 0, modelCache[i].length}); + ret = memManager->GetMemory(modelCache[i].data, memory); + if (ret != OH_NN_SUCCESS) { + LOGE("The %{public}zuth model cache is invalid. Please put valid model cache.", i + 1); + return ret; + } + iBuffers.emplace_back(V2_1::SharedBuffer {memory.fd, memory.length, 0, memory.length}); } V2_1::ModelConfig iModelConfig; @@ -671,11 +679,5 @@ OH_NN_ReturnCode HDIDeviceV2_1::PrepareOfflineModel(std::shared_ptr& indices, std::vector>& nnTensors) { - if ((indices.empty()) || (indices.size() > INPUT_OUTPUT_MAX_INDICES)) { + if (indices.empty()) { LOGE("ConstructNNTensorsFromLiteGraph failed, passed empty indices list."); return OH_NN_INVALID_PARAMETER; } @@ -325,16 +324,6 @@ OH_NN_ReturnCode InnerModel::SetTensorValue(uint32_t index, const void* buffer, OH_NN_ReturnCode InnerModel::ValidateInputAndOutput( const OH_NN_UInt32Array& inputIndices, const OH_NN_UInt32Array& outputIndices) const { - if (inputIndices.size == 0 || inputIndices.size > INPUT_OUTPUT_MAX_INDICES) { - LOGE("ValidateInputAndOutput failed, passed empty input indices."); - return OH_NN_INVALID_PARAMETER; - } - - if (outputIndices.size == 0 || outputIndices.size > INPUT_OUTPUT_MAX_INDICES) { - LOGE("ValidateInputAndOutput failed, passed empty output indices."); - return OH_NN_INVALID_PARAMETER; - } - OH_NN_ReturnCode ret = ValidateTensorArray(inputIndices); if (ret != OH_NN_SUCCESS) { LOGE("ValidateInputAndOutput failed, please check input indices."); @@ -347,6 +336,16 @@ OH_NN_ReturnCode InnerModel::ValidateInputAndOutput( return ret; } + if (inputIndices.size == 0) { + LOGE("ValidateInputAndOutput failed, passed empty input indices."); + return OH_NN_INVALID_PARAMETER; + } + + if (outputIndices.size == 0) { + LOGE("ValidateInputAndOutput failed, passed empty output indices."); + return OH_NN_INVALID_PARAMETER; + } + std::shared_ptr tensor{nullptr}; for (uint32_t i = 0; i < inputIndices.size; i++) { tensor = m_allTensors[inputIndices.data[i]]; @@ -720,11 +719,6 @@ std::vector, OH_NN_TensorType>> InnerModel { std::vector, OH_NN_TensorType>> inputTensorDescs; std::pair, OH_NN_TensorType> tensorDescPair; - if (m_inputTensors.size() > INPUT_OUTPUT_MAX_INDICES) { - LOGE("Input tensor descs more than 200."); - return inputTensorDescs; - } - for (auto inputTensor : m_inputTensors) { tensorDescPair.first = OHOS::NeuralNetworkRuntime::CreateSharedPtr(); inputTensor->ConvertToTensorDesc(*(tensorDescPair.first.get())); @@ -739,11 +733,6 @@ std::vector, OH_NN_TensorType>> InnerModel { std::vector, OH_NN_TensorType>> outputTensorDescs; std::pair, OH_NN_TensorType> tensorDescPair; - if (m_outputTensors.size() > INPUT_OUTPUT_MAX_INDICES) { - LOGE("Output tensor descs more than 200."); - return outputTensorDescs; - } - for (auto outputTensor : m_outputTensors) { tensorDescPair.first = OHOS::NeuralNetworkRuntime::CreateSharedPtr(); outputTensor->ConvertToTensorDesc(*(tensorDescPair.first.get())); diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 905582d3d4152ba0d18ae624b193d91ca2dee68f..1228e741f526f910a76fba74261d5ffe5eaf8574 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -23,14 +23,13 @@ #include "quant_param.h" #include "validation.h" #include "syspara/parameter.h" +#include "securec.h" #include #include #include #include #include -#include "nlohmann/json.hpp" -#include "securec.h" using namespace OHOS::NeuralNetworkRuntime; @@ -50,29 +49,6 @@ const std::string HARDWARE_NAME = "const.ai.nnrt_deivce"; const std::string HARDWARE_VERSION = "v5_0"; constexpr size_t HARDWARE_NAME_MAX_LENGTH = 128; constexpr size_t FILE_NUMBER_MAX = 100; // 限制cache文件数量最大为100 -constexpr size_t EXTENSION_MAX_SIZE = 200; // 限制MS传过来的参数最多为200 -constexpr size_t INPUT_MAX_COUNT = 200; // 限制模型最大输入个数为200 -constexpr int32_t HEX_UNIT = 16; - -unsigned short CacheInfoGetCrc16(char* buffer, size_t length) -{ - unsigned int sum = 0; - while (length > 1) { - sum += *(reinterpret_cast(buffer)); - length -= sizeof(unsigned short); - buffer += sizeof(unsigned short); - } - - if (length > 0) { - sum += *(reinterpret_cast(buffer)); - } - - while (sum >> HEX_UNIT) { - sum = (sum >> HEX_UNIT) + (sum & 0xffff); - } - - return static_cast(~sum); -} NNRT_API NN_QuantParam *OH_NNQuantParam_Create() { @@ -350,11 +326,6 @@ OH_NN_ReturnCode ParseInputDimsFromExtensions(char* data, size_t dataSize, const size_t inputCount = liteGraph->input_indices_.size(); // LiteGraph输入个数 size_t allTensorSize = liteGraph->all_tensors_.size(); // LiteGraph所有tensor个数 - if (inputCount > INPUT_MAX_COUNT) { - LOGE("ParseInputDimsFromExtensions failed, inputCount more than 200."); - return OH_NN_INVALID_PARAMETER; - } - std::vector inputDim; size_t dataIndex = 0; for (size_t i = 0; i < inputCount; ++i) { @@ -534,11 +505,6 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const return OH_NN_INVALID_PARAMETER; } - if (extensionSize > EXTENSION_MAX_SIZE) { - LOGE("OH_NNModel_BuildFromLiteGraph failed, extensionSize more than 200."); - return OH_NN_INVALID_PARAMETER; - } - auto *pLiteGraph = reinterpret_cast(liteGraph); ExtensionConfig extensionConfig; std::unordered_map>> extensionMap; @@ -565,76 +531,39 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const } namespace { -OH_NN_ReturnCode CheckCacheFileExtension(const std::string& content, int64_t& fileNumber, int64_t& cacheVersion); - OH_NN_ReturnCode CheckCacheFile(const std::string& cacheInfoPath, int64_t& fileNumber, int64_t& cacheVersion) { + // read number of cache models char path[PATH_MAX]; if (realpath(cacheInfoPath.c_str(), path) == nullptr) { LOGE("OH_NNModel_HasCache get real path of cache info failed."); - return OH_NN_INVALID_FILE; + return OH_NN_INVALID_PARAMETER; } if (access(path, F_OK) != 0) { LOGE("OH_NNModel_HasCache access cache info file failed."); - return OH_NN_INVALID_FILE; + return OH_NN_INVALID_PARAMETER; } std::ifstream ifs(path, std::ios::in | std::ios::binary); if (!ifs) { LOGE("OH_NNModel_HasCache open cache info file failed."); - return OH_NN_INVALID_FILE; - } - - // Read the entire file into a string - std::string content((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - ifs.close(); - - return CheckCacheFileExtension(content, fileNumber, cacheVersion); -} - -OH_NN_ReturnCode CheckCacheFileExtension(const std::string& content, int64_t& fileNumber, int64_t& cacheVersion) -{ - if (!nlohmann::json::accept(content)) { - LOGE("OH_NNModel_HasCache CheckCacheFile JSON parse error"); - return OH_NN_INVALID_FILE; - } - - nlohmann::json j = nlohmann::json::parse(content); - if (j.find("data") == j.end()) { - LOGE("OH_NNModel_HasCache read data from cache info file failed."); - return OH_NN_INVALID_FILE; - } - - if (j["data"].find("fileNumber") == j["data"].end()) { - LOGE("OH_NNModel_HasCache read fileNumber from cache info file failed."); - return OH_NN_INVALID_FILE; - } - fileNumber = j["data"]["fileNumber"].get(); - - if (j["data"].find("version") == j["data"].end()) { - LOGE("OH_NNModel_HasCache read version from cache info file failed."); - return OH_NN_INVALID_FILE; - } - cacheVersion = j["data"]["version"].get(); - - if (j.find("CheckSum") == j.end()) { - LOGE("OH_NNModel_HasCache read CheckSum from cache info file failed."); - return OH_NN_INVALID_FILE; + return OH_NN_INVALID_PARAMETER; } - const size_t dataLength = j["data"].dump().length(); - char jData[dataLength + 1]; - if (strncpy_s(jData, dataLength+1, j["data"].dump().c_str(), dataLength) != 0) { - LOGE("OH_NNModel_HasCache ParseStr failed due to strncpy_s error."); - return OH_NN_INVALID_FILE; + if (!ifs.read(reinterpret_cast(&(fileNumber)), sizeof(fileNumber))) { + LOGI("OH_NNModel_HasCache read cache info file failed."); + ifs.close(); + return OH_NN_INVALID_PARAMETER; } - if (static_cast(CacheInfoGetCrc16(jData, dataLength)) != j["CheckSum"].get()) { - LOGE("OH_NNModel_HasCache cache_info CheckSum is not correct."); - return OH_NN_INVALID_FILE; + if (!ifs.read(reinterpret_cast(&(cacheVersion)), sizeof(cacheVersion))) { + LOGI("OH_NNModel_HasCache read cache info file failed."); + ifs.close(); + return OH_NN_INVALID_PARAMETER; } + ifs.close(); return OH_NN_SUCCESS; } } @@ -648,6 +577,7 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u if (modelName == nullptr) { LOGI("OH_NNModel_HasCache get empty model name."); + return false; } std::string cacheInfoPath = std::string(cacheDir) + "/" + std::string(modelName) + "cache_info.nncache"; @@ -664,7 +594,6 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u OH_NN_ReturnCode returnCode = CheckCacheFile(cacheInfoPath, fileNumber, cacheVersion); if (returnCode != OH_NN_SUCCESS) { LOGE("OH_NNModel_HasCache get fileNumber or cacheVersion fail."); - std::filesystem::remove_all(cacheInfoPath); return false; } diff --git a/frameworks/native/neural_network_runtime/nn_tensor.cpp b/frameworks/native/neural_network_runtime/nn_tensor.cpp index e6d002f09e43e57249969c301591d45c33947908..df42f2fdaf1a781874c109ead9e6b44d5a8b9861 100644 --- a/frameworks/native/neural_network_runtime/nn_tensor.cpp +++ b/frameworks/native/neural_network_runtime/nn_tensor.cpp @@ -28,7 +28,6 @@ namespace OHOS { namespace NeuralNetworkRuntime { const uint32_t SUPPORT_NUM_BIT = 8; // Currently support 8-bit quantization only -constexpr size_t DIM_MAX_NUM = 200; void DestroyLiteGraphTensor(void* tensor) { @@ -235,11 +234,6 @@ OH_NN_ReturnCode NNTensor::ValidateDimensions(const std::vector& dimens uint64_t elementCount {1}; uint64_t dataLength {static_cast(GetTypeSize(m_dataType))}; m_isDynamicShape = false; - if (dimensions.size() > DIM_MAX_NUM) { - LOGE("ParseDimension failed, dimensions more than 200."); - return OH_NN_INVALID_PARAMETER; - } - for (int32_t dim : dimensions) { if (dim < -1 || dim == 0) { LOGE("ParseDimension failed, dimension of OH_NN_Tensor cannot be 0 or less than -1, receive %d.", dim); diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 0fcf1144a34979fb9747888d5581b7b2696c685e..5f5ab3c70cdb7b688aa522bee7fef22cd3bd80b7 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include "utils.h" #include "backend_manager.h" @@ -32,9 +31,11 @@ constexpr int32_t NULL_PTR_LENGTH = 0; constexpr int32_t NUMBER_CACHE_INFO_MEMBERS = 3; constexpr int32_t NUMBER_CACHE_INFO_EXTENSION_MEMBERS = 2; constexpr int32_t HEX_UNIT = 16; -constexpr size_t MAX_CACHE_SIZE = 2 * 1024 * 1024; // 限制最大校验内存为2MB constexpr char ROOT_DIR_STR = '/'; constexpr char DOUBLE_SLASH_STR[] = "//"; +constexpr int OPVERSION_SUBSTR_NUM = 2; +const std::string CURRENT_VERSION = "0x00000000"; +const std::string HIAI_VERSION_PATH = "/data/data/hiai/version"; OH_NN_ReturnCode NNCompiledCache::Save(const std::vector& caches, const std::string& cacheDir, @@ -167,7 +168,11 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheFiles(const std::vector cacheInfo = CreateUniquePtr(cacheSize); + if (cacheInfo == nullptr) { + LOGE("[NNCompiledCache] GenerateCacheFiles failed, fail to create cacheInfo instance."); + return OH_NN_MEMORY_ERROR; + } OH_NN_ReturnCode ret = GenerateCacheModel(caches, cacheInfo, cacheDir, version); if (ret != OH_NN_SUCCESS) { @@ -186,7 +191,7 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheFiles(const std::vector& caches, - nlohmann::json& cacheInfo, + std::unique_ptr& cacheInfo, const std::string& cacheDir, uint32_t version) const { @@ -196,9 +201,10 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector(cacheNumber); - cacheInfo["data"]["version"] = static_cast(version); - cacheInfo["data"]["deviceId"] = static_cast(m_backendID); // Should call SetBackend first. + auto cacheInfoPtr = cacheInfo.get(); + *cacheInfoPtr++ = static_cast(cacheNumber); + *cacheInfoPtr++ = static_cast(version); + *cacheInfoPtr++ = static_cast(m_backendID); // Should call SetBackend first. // standardize the input dir OH_NN_ReturnCode ret = OH_NN_SUCCESS; @@ -226,7 +232,7 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector(GetCrc16(static_cast(caches[i].data), caches[i].length)); - cacheInfo["data"]["modelCheckSum"][i] = checkSum; + *cacheInfoPtr++ = checkSum; if (!cacheModelStream.write(static_cast(caches[i].data), caches[i].length)) { LOGE("[NNCompiledCache] GenerateCacheModel failed, fail to write cache model."); cacheModelStream.close(); @@ -236,31 +242,31 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vectorReadOpVersion(currentOpVersion); - if (ret != OH_NN_SUCCESS) { - LOGE("[NNCompiledCache] GenerateCacheModel failed, fail to read op version."); - return ret; + std::string currentVersion = CURRENT_VERSION; + char versionPath[PATH_MAX]; + if (realpath(HIAI_VERSION_PATH.c_str(), versionPath) != nullptr) { + std::ifstream inf(versionPath); + if (inf.is_open()) { + getline(inf, currentVersion); + } + inf.close(); } - cacheInfo["data"]["opVersion"] = currentOpVersion; - LOGI("[NNCompiledCache] GenerateCacheModel m_isExceedRamLimit: %{public}d", static_cast(m_isExceedRamLimit)); - cacheInfo["data"]["isExceedRamLimit"] = m_isExceedRamLimit ? 1 : 0; + int currentOpVersion = std::stoi(currentVersion.substr(OPVERSION_SUBSTR_NUM)); + *cacheInfoPtr++ = currentOpVersion; - const size_t dataLength = cacheInfo["data"].dump().length(); - char cacheInfoData[dataLength + 1]; - if (strncpy_s(cacheInfoData, dataLength+1, cacheInfo["data"].dump().c_str(), dataLength) != 0) { - LOGE("ParseStr failed due to strncpy_s error"); - return OH_NN_INVALID_PARAMETER; + LOGI("[NNCompiledCache::GenerateCacheModel] m_isExceedRamLimit: %{public}d", static_cast(m_isExceedRamLimit)); + if (m_isExceedRamLimit) { + *cacheInfoPtr++ = 1; + } else { + *cacheInfoPtr++ = 0; } - cacheInfo["CheckSum"] = static_cast(CacheInfoGetCrc16(cacheInfoData, dataLength)); - return OH_NN_SUCCESS; } OH_NN_ReturnCode NNCompiledCache::WriteCacheInfo(uint32_t cacheSize, - nlohmann::json& cacheInfo, + std::unique_ptr& cacheInfo, const std::string& cacheDir) const { // standardize the input dir @@ -285,7 +291,11 @@ OH_NN_ReturnCode NNCompiledCache::WriteCacheInfo(uint32_t cacheSize, return OH_NN_INVALID_FILE; } - cacheInfoStream << cacheInfo << std::endl; + if (!cacheInfoStream.write(reinterpret_cast(cacheInfo.get()), cacheSize)) { + LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to write cache info."); + cacheInfoStream.close(); + return OH_NN_SAVE_CACHE_EXCEPTION; + } cacheInfoStream.close(); return OH_NN_SUCCESS; @@ -301,103 +311,52 @@ OH_NN_ReturnCode NNCompiledCache::CheckCacheInfo(NNCompiledCacheInfo& modelCache return OH_NN_INVALID_FILE; } - std::string content((std::istreambuf_iterator(infoCacheFile)), std::istreambuf_iterator()); - infoCacheFile.close(); - if (!nlohmann::json::accept(content)) { - LOGE("[NNCompiledCache] CheckCacheInfo JSON parse error"); + int charNumber = NUMBER_CACHE_INFO_MEMBERS * sizeof(uint64_t); + if (!infoCacheFile.read(reinterpret_cast(&(modelCacheInfo)), charNumber)) { + LOGE("[NNCompiledCache] CheckCacheInfo failed, error happened when reading cache info file."); + infoCacheFile.close(); return OH_NN_INVALID_FILE; } - // Parse the JSON string - nlohmann::json j = nlohmann::json::parse(content); // modelCacheInfo.deviceId type is int64_t, // it is transformed from size_t value, so the transform here will not truncate value. - if (j.find("data") == j.end()) { - LOGE("[NNCompiledCache] CheckCacheInfo read cache info file failed."); - return OH_NN_INVALID_FILE; - } - - if (j["data"].find("deviceId") == j["data"].end()) { - LOGE("[NNCompiledCache] CheckCacheInfo read deviceId from cache info file failed."); - return OH_NN_INVALID_FILE; - } - modelCacheInfo.deviceId = j["data"]["deviceId"].get(); - - if (j["data"].find("version") == j["data"].end()) { - LOGE("[NNCompiledCache] CheckCacheInfo read version from cache info file failed."); - return OH_NN_INVALID_FILE; - } - modelCacheInfo.version = j["data"]["version"].get(); - size_t deviceId = static_cast(modelCacheInfo.deviceId); if (deviceId != m_backendID) { LOGE("[NNCompiledCache] CheckCacheInfo failed. The deviceId in the cache files " "is different from current deviceId," "please change the cache directory or current deviceId."); + infoCacheFile.close(); return OH_NN_INVALID_PARAMETER; } - if (j["data"].find("fileNumber") == j["data"].end()) { - LOGE("[NNCompiledCache] CheckCacheInfo read fileNumber from cache info file failed."); - return OH_NN_INVALID_FILE; - } - modelCacheInfo.fileNumber = j["data"]["fileNumber"].get(); - - return CheckCacheInfoExtension(modelCacheInfo, j); -} - -OH_NN_ReturnCode NNCompiledCache::CheckCacheInfoExtension(NNCompiledCacheInfo& modelCacheInfo, - nlohmann::json& j) const -{ - const size_t dataLength = j["data"].dump().length(); - char jData[dataLength + 1]; - if (strncpy_s(jData, dataLength+1, j["data"].dump().c_str(), dataLength) != 0) { - LOGE("[NNCompiledCache] ParseStr failed due to strncpy_s error."); - return OH_NN_INVALID_FILE; - } - - if (j.find("CheckSum") == j.end()) { - LOGE("[NNCompiledCache] read CheckSum from cache info file failed."); - return OH_NN_INVALID_FILE; - } - - if (static_cast(CacheInfoGetCrc16(jData, dataLength)) != j["CheckSum"].get()) { - LOGE("[NNCompiledCache] cache_info CheckSum is not correct."); - return OH_NN_INVALID_FILE; - } std::vector modelCheckSum; modelCheckSum.resize(modelCacheInfo.fileNumber); modelCacheInfo.modelCheckSum.resize(modelCacheInfo.fileNumber); - if (j["data"].find("modelCheckSum") == j["data"].end()) { - LOGE("[NNCompiledCache] CheckCacheInfo read modelCheckSum from cache file failed."); + if (!infoCacheFile.read(reinterpret_cast(&modelCheckSum[0]), + modelCacheInfo.fileNumber * sizeof(uint64_t))) { + LOGE("[NNCompiledCache] CheckCacheInfo failed. The info cache file has been changed."); + infoCacheFile.close(); return OH_NN_INVALID_FILE; } - for (uint32_t i = 0; i < modelCacheInfo.fileNumber; ++i) { - modelCheckSum[i] = static_cast(j["data"]["modelCheckSum"][i]); - } for (uint32_t i = 0; i < modelCacheInfo.fileNumber; ++i) { modelCacheInfo.modelCheckSum[i] = static_cast(modelCheckSum[i]); } - if (j["data"].find("opVersion") == j["data"].end()) { - LOGW("[NNCompiledCache] CheckCacheInfo read opVersion from cache info file failed."); - } else { - modelCacheInfo.opVersion = j["data"]["opVersion"].get(); + if (!infoCacheFile.read(reinterpret_cast(&(modelCacheInfo.opVersion)), sizeof(uint64_t))) { + LOGW("[NNCompiledCache] opVersion failed."); } - if (j["data"].find("isExceedRamLimit") == j["data"].end()) { - LOGE("[NNCompiledCache] CheckCacheInfo read isExceedRamLimit from cache info file failed."); - return OH_NN_INVALID_FILE; - } else { - modelCacheInfo.isExceedRamLimit = j["data"]["isExceedRamLimit"].get(); + if (!infoCacheFile.read(reinterpret_cast(&(modelCacheInfo.isExceedRamLimit)), sizeof(uint64_t))) { + LOGW("[NNCompiledCache] isExceedRamLimit failed."); } + infoCacheFile.close(); return OH_NN_SUCCESS; } OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath, - OHOS::NeuralNetworkRuntime::Buffer& cache) + OHOS::NeuralNetworkRuntime::Buffer& cache) const { char path[PATH_MAX]; if (realpath(filePath.c_str(), path) == nullptr) { @@ -409,55 +368,56 @@ OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath return OH_NN_INVALID_PARAMETER; } - int fd = open(path, O_RDONLY); - if (fd == -1) { + FILE* pFile = fopen(path, "rb"); + if (pFile == NULL) { LOGE("[NNCompiledCache] ReadCacheModelFile failed, file fopen failed."); return OH_NN_INVALID_FILE; } - struct stat sb; - if (fstat(fd, &sb) == -1) { - close(fd); - LOGE("[NNCompiledCache] ReadCacheModelFile failed, get file %{public}s state failed.", filePath.c_str()); - return OH_NN_MEMORY_ERROR; + long fsize{-1}; + OH_NN_ReturnCode ret = GetCacheFileLength(pFile, fsize); + if (ret != OH_NN_SUCCESS) { + fclose(pFile); + LOGE("[NNCompiledCache] ReadCacheModelFile failed, get file %{public}s length fialed.", filePath.c_str()); + return ret; } - off_t fsize = sb.st_size; + rewind(pFile); - void *ptr = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); - if (ptr == MAP_FAILED) { - LOGE("[NNCompiledCache] ReadCacheModelFile failed, failed to mmap file."); - close(fd); + char* ptr = static_cast(m_device->AllocateBuffer(fsize)); + if (ptr == nullptr) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, failed to allocate memory."); + fclose(pFile); + return OH_NN_MEMORY_ERROR; + } + + LOGI("ReadCacheModelFile read start."); + size_t result = fread(ptr, 1, fsize, pFile); // size of each object in bytes is 1 + LOGI("ReadCacheModelFile read end."); + if (result != static_cast(fsize)) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, failed to read file."); + fclose(pFile); + m_device->ReleaseBuffer(ptr); + ptr = nullptr; return OH_NN_INVALID_FILE; } + fclose(pFile); cache.data = ptr; cache.length = static_cast(fsize); // fsize should be non-negative, safe to cast. - cache.fd = fd; return OH_NN_SUCCESS; } unsigned short NNCompiledCache::GetCrc16(char* buffer, size_t length) const { unsigned int sum = 0; - - if (length < MAX_CACHE_SIZE) { - while (length > 1) { - sum += *(reinterpret_cast(buffer)); - length -= sizeof(unsigned short); - buffer += sizeof(unsigned short); - } - } else { - size_t step = length / MAX_CACHE_SIZE; - while (length > sizeof(unsigned short) * step + 1) { - sum += *(reinterpret_cast(buffer)); - length -= step * sizeof(unsigned short); - buffer += step * sizeof(unsigned short); - } + while (length > 1) { + sum += *(reinterpret_cast(buffer)); + length -= sizeof(unsigned short); + buffer += sizeof(unsigned short); } if (length > 0) { - buffer += length - 1; sum += *(reinterpret_cast(buffer)); } @@ -514,14 +474,5 @@ OH_NN_ReturnCode NNCompiledCache::VerifyCachePath(const std::string& cachePath) return OH_NN_SUCCESS; } - -void NNCompiledCache::ReleaseCacheBuffer(std::vector& buffers) -{ - for (auto buffer : buffers) { - munmap(buffer.data, buffer.length); - close(buffer.fd); - } - buffers.clear(); -} } // namespace NeuralNetworkRuntime } // namespace OHOS diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.h b/frameworks/native/neural_network_runtime/nncompiled_cache.h index 7ee8ce21ee6d1bcfbe7d85b7f7020c351a5cfc87..f182ac4ea5bc948c9dd80d4eaed89c04aabef9b4 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.h +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.h @@ -19,12 +19,6 @@ #include #include #include -#include -#include -#include -#include - -#include "nlohmann/json.hpp" #include "device.h" #include "neural_network_runtime/neural_network_runtime.h" @@ -60,22 +54,20 @@ public: void SetModelName(const std::string& modelName); void SetIsExceedRamLimit(const bool isExceedRamLimit); OH_NN_ReturnCode WriteCacheInfo(uint32_t cacheSize, - nlohmann::json& cacheInfo, + std::unique_ptr& cacheInfo, const std::string& cacheDir) const; OH_NN_ReturnCode CheckCacheInfo(NNCompiledCacheInfo& modelCacheInfo, const std::string& cacheInfoPath) const; - OH_NN_ReturnCode CheckCacheInfoExtension(NNCompiledCacheInfo& modelCacheInfo, nlohmann::json& j) const; - void ReleaseCacheBuffer(std::vector& buffers); - unsigned short GetCrc16(char* buffer, size_t length) const; private: OH_NN_ReturnCode GenerateCacheFiles(const std::vector& caches, const std::string& cacheDir, uint32_t version) const; OH_NN_ReturnCode GenerateCacheModel(const std::vector& caches, - nlohmann::json& cacheInfo, + std::unique_ptr& cacheInfo, const std::string& cacheDir, uint32_t version) const; - OH_NN_ReturnCode ReadCacheModelFile(const std::string& file, Buffer& cache); + OH_NN_ReturnCode ReadCacheModelFile(const std::string& file, Buffer& cache) const; + unsigned short GetCrc16(char* buffer, size_t length) const; OH_NN_ReturnCode GetCacheFileLength(FILE* pFile, long& fileSize) const; OH_NN_ReturnCode VerifyCachePath(const std::string& cachePath) const; diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index ec40f00e2d037745cd27b02905dce5f6e5a50aa5..a55825e9c8e74dee0cb57af03b7191672cb437b9 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -14,7 +14,6 @@ */ #include "nncompiler.h" -#include "neural_network_runtime/neural_network_runtime.h" #include #include @@ -24,7 +23,6 @@ #include "validation.h" #include "nncompiled_cache.h" #include "utils.h" -#include "nlohmann/json.hpp" namespace OHOS { namespace NeuralNetworkRuntime { @@ -36,7 +34,9 @@ constexpr int32_t NUMBER_CACHE_INFO_EXTENSION_MEMBERS = 2; const std::string EXTENSION_KEY_MODEL_NAME = "ModelName"; const std::string EXTENSION_KEY_FM_SHARED = "NPU_FM_SHARED"; const std::string EXTENSION_KEY_IS_EXCEED_RAMLIMIT = "isExceedRamLimit"; -constexpr size_t INPUT_OUTPUT_MAX_NUM = 200; +const int OPVERSION_SUBSTR_NUM = 2; +const std::string CURRENT_VERSION = "0x00000000"; +const std::string HIAI_VERSION_PATH = "/data/data/hiai/version"; struct SerializedTensorDesc { public: @@ -499,6 +499,15 @@ void NNCompiler::ReleaseBuffer(std::vector& buffers) const buffers.clear(); } +void NNCompiler::ReleaseBufferByDevice(std::vector& buffers) const +{ + for (size_t i = 0; i < buffers.size(); ++i) { + // release cache buffer which is allocated by idevice. + m_device->ReleaseBuffer(buffers[i].data); + } + buffers.clear(); +} + OH_NN_ReturnCode NNCompiler::SaveToCacheFile() const { if (m_cachePath.empty()) { @@ -537,11 +546,6 @@ OH_NN_ReturnCode NNCompiler::SaveToCacheFile() const return ret; } - if ((m_inputTensorDescs.size() > INPUT_OUTPUT_MAX_NUM) || (m_outputTensorDescs.size() > INPUT_OUTPUT_MAX_NUM)) { - LOGE("[NNCompiler] SaveToCacheFile failed, m_inputTensorDescs or m_outputTensorDescs is more than 200."); - return OH_NN_INVALID_PARAMETER; - } - Buffer inputTensorDescBuffer; ret = SerializeTensorsToBuffer(m_inputTensorDescs, inputTensorDescBuffer); if (ret != OH_NN_SUCCESS) { @@ -562,8 +566,8 @@ OH_NN_ReturnCode NNCompiler::SaveToCacheFile() const tensorBuffers.emplace_back(outputTensorDescBuffer); compiledCache.SetModelName(m_extensionConfig.modelName); - compiledCache.SetIsExceedRamLimit(m_extensionConfig.isExceedRamLimit); ret = compiledCache.Save(caches, m_cachePath, m_cacheVersion); + compiledCache.SetIsExceedRamLimit(m_extensionConfig.isExceedRamLimit); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] SaveToCacheFile failed, error happened when saving model cache."); ReleaseBuffer(tensorBuffers); @@ -610,7 +614,7 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() ret = compiledCache.Restore(m_cachePath, m_cacheVersion, caches); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] RestoreFromCacheFile failed, error happened when restoring model cache."); - compiledCache.ReleaseCacheBuffer(caches); + ReleaseBufferByDevice(caches); return ret; } @@ -619,7 +623,7 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() ret = DeserializedTensorsFromBuffer(caches[cacheNum - CACHE_INPUT_TENSORDESC_OFFSET], inputTensorDescs); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] RestoreFromCacheFile failed, error happened when deserializing input tensor desc."); - compiledCache.ReleaseCacheBuffer(caches); + ReleaseBufferByDevice(caches); return ret; } @@ -627,7 +631,7 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() ret = DeserializedTensorsFromBuffer(caches[cacheNum - CACHE_OUTPUT_TENSORDESC_OFFSET], outputTensorDescs); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] RestoreFromCacheFile failed, error happened when deserializing output tensor desc."); - compiledCache.ReleaseCacheBuffer(caches); + ReleaseBufferByDevice(caches); return ret; } @@ -641,20 +645,25 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() ret = m_device->PrepareModelFromModelCache(modelOnlyCaches, config, m_preparedModel, isUpdatable); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] RestoreFromCacheFile failed, error happened when preparing model from cache."); - compiledCache.ReleaseCacheBuffer(caches); + ReleaseBufferByDevice(caches); return ret; } if (isUpdatable) { LOGI("isUpdatable is true"); - int currentOpVersion = 0; - ret = m_device->ReadOpVersion(currentOpVersion); - if (ret != OH_NN_SUCCESS) { - LOGE("[NNCompiledCache] GenerateCacheModel failed, fail to read op version."); - return ret; + std::string currentVersion = CURRENT_VERSION; + char versionPath[PATH_MAX]; + if (realpath(HIAI_VERSION_PATH.c_str(), versionPath) != nullptr) { + std::ifstream inf(versionPath); + if (inf.is_open()) { + getline(inf, currentVersion); + } + inf.close(); } + int currentOpVersion = std::stoi(currentVersion.substr(OPVERSION_SUBSTR_NUM)); + NNCompiledCacheInfo modelCacheInfo; std::string cacheInfoPath = m_cachePath + "/" + m_extensionConfig.modelName + "cache_info.nncache"; ret = compiledCache.CheckCacheInfo(modelCacheInfo, cacheInfoPath); @@ -671,28 +680,29 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() uint32_t cacheSize = NUMBER_CACHE_INFO_MEMBERS + cacheNumber + NUMBER_CACHE_INFO_EXTENSION_MEMBERS; uint32_t infoCharNumber = cacheSize * sizeof(int64_t); - nlohmann::json cacheInfo; + std::unique_ptr cacheInfo = CreateUniquePtr(cacheSize); + if (cacheInfo == nullptr) { + LOGE("[NNCompiledCache] isUpdatable is true to create unique failed."); + return OH_NN_MEMORY_ERROR; + } - cacheInfo["data"]["fileNumber"] = modelCacheInfo.fileNumber; - cacheInfo["data"]["version"] = modelCacheInfo.version - 1; - cacheInfo["data"]["deviceId"] = modelCacheInfo.deviceId; + auto cacheInfoPtr = cacheInfo.get(); + *cacheInfoPtr++ = modelCacheInfo.fileNumber; + *cacheInfoPtr++ = modelCacheInfo.version - 1; + *cacheInfoPtr++ = modelCacheInfo.deviceId; for (size_t i = 0; i < modelCacheInfo.modelCheckSum.size(); ++i) { - cacheInfo["data"]["modelCheckSum"][i] = modelCacheInfo.modelCheckSum[i]; + *cacheInfoPtr++ = static_cast(modelCacheInfo.modelCheckSum[i]); } - cacheInfo["data"]["opVersion"] = currentOpVersion; - cacheInfo["data"]["isExceedRamLimit"] = modelCacheInfo.isExceedRamLimit ? 1 : 0; + *cacheInfoPtr++ = currentOpVersion; - const size_t dataLength = cacheInfo["data"].dump().length(); - char cacheInfoData[dataLength + 1]; - if (strncpy_s(cacheInfoData, dataLength+1, cacheInfo["data"].dump().c_str(), dataLength) != 0) { - LOGE("ParseStr failed due to strncpy_s error"); - return OH_NN_INVALID_PARAMETER; + if (modelCacheInfo.isExceedRamLimit) { + *cacheInfoPtr++ = 1; + } else { + *cacheInfoPtr++ = 0; } - cacheInfo["CheckSum"] = static_cast(CacheInfoGetCrc16(cacheInfoData, dataLength)); - ret = compiledCache.WriteCacheInfo(infoCharNumber, cacheInfo, m_cachePath); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiledCache] isUpdatable is true to write cache info failed."); @@ -701,7 +711,7 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() } } - compiledCache.ReleaseCacheBuffer(caches); + ReleaseBufferByDevice(caches); m_inputTensorDescs = inputTensorDescs; m_outputTensorDescs = outputTensorDescs; @@ -986,5 +996,6 @@ OH_NN_ReturnCode NNCompiler::DeserializedTensorsFromBuffer( ReleaseDescShape(immediateTensorDescs); return ret; } + } // NeuralNetworkRuntime } // OHOS diff --git a/frameworks/native/neural_network_runtime/nnexecutor.cpp b/frameworks/native/neural_network_runtime/nnexecutor.cpp index d8f1e6df9bed244b380c8f8e62d1054d8e190c76..dcf4e9c68714f674eb502e887b9eed7701c5c50e 100644 --- a/frameworks/native/neural_network_runtime/nnexecutor.cpp +++ b/frameworks/native/neural_network_runtime/nnexecutor.cpp @@ -25,8 +25,6 @@ #include "transform.h" namespace OHOS { -constexpr size_t EXTENSION_MAX_SIZE = 200; - namespace NeuralNetworkRuntime { NNExecutor::NNExecutor(size_t backendID, std::shared_ptr device, std::shared_ptr preparedModel, const std::vector, OH_NN_TensorType>>& inputTensorDescs, @@ -199,11 +197,6 @@ OH_NN_ReturnCode NNExecutor::SetExtensionConfig(const std::unordered_map EXTENSION_MAX_SIZE) { - LOGE("[NNExecutor] SetExtensionConfig, configs size more than 200."); - return OH_NN_FAILED; - } - for (auto config : configs) { char* configData = reinterpret_cast(config.second.data()); if (configData == nullptr) { diff --git a/interfaces/innerkits/c/neural_network_runtime_inner.h b/interfaces/innerkits/c/neural_network_runtime_inner.h index b6d97e717970e8ebaf5db157b138d06c53572d9e..22de228e3a587afdfcb14e62cbca656ec6d76a39 100644 --- a/interfaces/innerkits/c/neural_network_runtime_inner.h +++ b/interfaces/innerkits/c/neural_network_runtime_inner.h @@ -153,18 +153,6 @@ bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, uint32_t v */ OH_NN_ReturnCode OH_NN_GetDeviceID(char *nnrtDevice, size_t len); -/** - * @brief 对cache进行crc校验和检验 - * - * 本接口不作为Neural Network Runtime接口对外开放。\n - * - * @param buffer 传入buffer参数。 - * @param length 传入length参数。 - * @return unsigned short值,函数的crc校验和。 - * @since 11 - * @version 1.0 - */ -unsigned short CacheInfoGetCrc16(char* buffer, size_t length); #ifdef __cplusplus } #endif // __cpluscplus diff --git a/test/unittest/components/BUILD.gn b/test/unittest/components/BUILD.gn index f9eb95f007820f79256f041a7a9fa7f13a4dcee5..601e0bf58c687a6eb829275e3d4879c1f8ed6c0e 100644 --- a/test/unittest/components/BUILD.gn +++ b/test/unittest/components/BUILD.gn @@ -230,7 +230,6 @@ ohos_unittest("NNCompiledCacheTest") { "googletest:gtest_main", "hilog:libhilog", "hitrace:libhitracechain", - "json:nlohmann_json_static", "mindspore:mindir_lib", "neural_network_runtime:libneural_network_core", "neural_network_runtime:libneural_network_runtime", diff --git a/test/unittest/components/nn_backend/nn_backend_test.cpp b/test/unittest/components/nn_backend/nn_backend_test.cpp index 175afc6e5eb599ad40d9692d1e1558aa1469c383..71822d948009068e7347147725c7f5946f38b421 100644 --- a/test/unittest/components/nn_backend/nn_backend_test.cpp +++ b/test/unittest/components/nn_backend/nn_backend_test.cpp @@ -67,7 +67,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; /** diff --git a/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp b/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp index 9321af5f4b12cad92421d0b83acd6c95d8a84a06..e43bca551002d7843ede5634a4e95211fac3fba0 100644 --- a/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp +++ b/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp @@ -69,7 +69,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; /** @@ -478,7 +477,7 @@ HWTEST_F(NNCompiledCacheTest, nncompiledcachetest_writecacheinfo_001, TestSize.L NNCompiledCache nncompiledCache; uint32_t cacheSize = 1; - nlohmann::json cacheInfo; + std::unique_ptr cacheInfo = std::make_unique(cacheSize); std::string cacheDir = "mock"; OH_NN_ReturnCode ret = nncompiledCache.WriteCacheInfo(cacheSize, cacheInfo, cacheDir); @@ -496,7 +495,7 @@ HWTEST_F(NNCompiledCacheTest, nncompiledcachetest_writecacheinfo_002, TestSize.L NNCompiledCache nncompiledCache; uint32_t cacheSize = 1; - nlohmann::json cacheInfo; + std::unique_ptr cacheInfo = std::make_unique(cacheSize); std::string cacheDir = "/data/data"; OH_NN_ReturnCode ret = nncompiledCache.WriteCacheInfo(cacheSize, cacheInfo, cacheDir); diff --git a/test/unittest/components/nn_compiler/nn_compiler_test.cpp b/test/unittest/components/nn_compiler/nn_compiler_test.cpp index ee0ba2b7af564814cb3acd3d37495eb718aac657..37d938fd512f0df4015d45c94bf73cd57b7af265 100644 --- a/test/unittest/components/nn_compiler/nn_compiler_test.cpp +++ b/test/unittest/components/nn_compiler/nn_compiler_test.cpp @@ -70,7 +70,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; class MockIPreparedModel : public PreparedModel { diff --git a/test/unittest/components/nn_executor/nn_executor_test.cpp b/test/unittest/components/nn_executor/nn_executor_test.cpp index f5380a660eccef8fe1f6e64f4fbb22794cbd8a57..c57486dfa88ec871e7654756b4ded95de7866ad7 100644 --- a/test/unittest/components/nn_executor/nn_executor_test.cpp +++ b/test/unittest/components/nn_executor/nn_executor_test.cpp @@ -79,7 +79,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; class MockIPreparedModel : public PreparedModel { diff --git a/test/unittest/components/nn_tensor/nn_tensor_test.cpp b/test/unittest/components/nn_tensor/nn_tensor_test.cpp index c89fd8c47ba6ea4f5eaebbb5b260070cb3890a69..9a5a9d9b0dd8653c12571ab8869144bebb28dc96 100644 --- a/test/unittest/components/nn_tensor/nn_tensor_test.cpp +++ b/test/unittest/components/nn_tensor/nn_tensor_test.cpp @@ -75,7 +75,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; class MockTensorDesc : public TensorDesc { diff --git a/test/unittest/components/v1_0/neural_network_core_test/neural_network_core_test.cpp b/test/unittest/components/v1_0/neural_network_core_test/neural_network_core_test.cpp index ee68a83d7003a1524b115cc6209c00f913c24002..aa4b6c38fac60d02dc369e783ee630601f12c01a 100644 --- a/test/unittest/components/v1_0/neural_network_core_test/neural_network_core_test.cpp +++ b/test/unittest/components/v1_0/neural_network_core_test/neural_network_core_test.cpp @@ -133,7 +133,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; class MockBackend : public Backend { diff --git a/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp b/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp index a58ace084458acd3d1f5b656c53d277f6a136747..457908d768132b1b2a79b60d481c0dbe811eeb5e 100644 --- a/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp +++ b/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp @@ -296,7 +296,6 @@ public: MOCK_METHOD1(ReleaseBuffer, OH_NN_ReturnCode(const void*)); MOCK_METHOD2(AllocateBuffer, OH_NN_ReturnCode(size_t, int&)); MOCK_METHOD2(ReleaseBuffer, OH_NN_ReturnCode(int, size_t)); - MOCK_METHOD1(ReadOpVersion, OH_NN_ReturnCode(int&)); }; /*