From 60ed86ae6df028b890bd7c7cfbd26cffda2cc19e Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 11:45:01 +0800 Subject: [PATCH 1/8] modify cache Signed-off-by: w30052974 --- .../neural_network_core.cpp | 10 +++---- .../neural_network_runtime.cpp | 16 ++++++++++-- .../nncompiled_cache.cpp | 14 +++++----- .../neural_network_runtime/nncompiler.cpp | 26 +++++++++++++++++++ .../c/neural_network_runtime_inner.h | 2 +- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/frameworks/native/neural_network_core/neural_network_core.cpp b/frameworks/native/neural_network_core/neural_network_core.cpp index 5766705..f546cc1 100644 --- a/frameworks/native/neural_network_core/neural_network_core.cpp +++ b/frameworks/native/neural_network_core/neural_network_core.cpp @@ -81,7 +81,7 @@ NNRT_API OH_NN_ReturnCode OH_NNDevice_GetName(size_t deviceID, const char **name BackendManager& backendManager = BackendManager::GetInstance(); const std::string& backendName = backendManager.GetBackendName(deviceID); if (backendName.empty()) { - LOGE("OH_NNDevice_GetName failed, error happened when getting name of deviceID %{public}zu.", deviceID); + LOGE("OH_NNDevice_GetName failed, error happened when getting name of deviceID."); *name = nullptr; return OH_NN_FAILED; } @@ -106,7 +106,7 @@ NNRT_API OH_NN_ReturnCode OH_NNDevice_GetType(size_t deviceID, OH_NN_DeviceType* OH_NN_ReturnCode ret = backend->GetBackendType(*deviceType); if (ret != OH_NN_SUCCESS) { - LOGE("OH_NNDevice_GetType failed, device id: %{public}zu.", deviceID); + LOGE("OH_NNDevice_GetType failed."); return ret; } return OH_NN_SUCCESS; @@ -978,7 +978,7 @@ NNRT_API NN_Tensor* OH_NNTensor_Create(size_t deviceID, NN_TensorDesc *tensorDes BackendManager& backendManager = BackendManager::GetInstance(); std::shared_ptr backend = backendManager.GetBackend(deviceID); if (backend == nullptr) { - LOGE("OH_NNTensor_Create failed, passed invalid backend name %{public}zu.", deviceID); + LOGE("OH_NNTensor_Create failed, passed invalid backend name."); return nullptr; } @@ -1010,7 +1010,7 @@ NNRT_API NN_Tensor* OH_NNTensor_CreateWithSize(size_t deviceID, NN_TensorDesc *t BackendManager& backendManager = BackendManager::GetInstance(); std::shared_ptr backend = backendManager.GetBackend(deviceID); if (backend == nullptr) { - LOGE("OH_NNTensor_CreateWithSize failed, passed invalid backend name %{public}zu.", deviceID); + LOGE("OH_NNTensor_CreateWithSize failed, passed invalid backend name."); return nullptr; } @@ -1068,7 +1068,7 @@ NNRT_API NN_Tensor* OH_NNTensor_CreateWithFd(size_t deviceID, BackendManager& backendManager = BackendManager::GetInstance(); std::shared_ptr backend = backendManager.GetBackend(deviceID); if (backend == nullptr) { - LOGE("OH_NNTensor_CreateWithFd failed, passed invalid backend name %{public}zu.", deviceID); + LOGE("OH_NNTensor_CreateWithFd failed, passed invalid backend name."); return nullptr; } diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 25d71c3..110f3e4 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -503,7 +503,7 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const return innerModel->BuildFromLiteGraph(pLiteGraph, extensionConfig); } -NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName) +NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, uint32_t version) { if (cacheDir == nullptr) { LOGI("OH_NNModel_HasCache get empty cache directory."); @@ -543,8 +543,15 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName) } int64_t fileNumber{0}; + int64_t cacheVersion{0}; if (!ifs.read(reinterpret_cast(&(fileNumber)), sizeof(fileNumber))) { - LOGI("OH_NNModel_HasCache read cache info file failed."); + LOGI("OH_NNModel_HasCache read fileNumber cache info file failed."); + ifs.close(); + return false; + } + + if (!ifs.read(reinterpret_cast(&(cacheVersion)), sizeof(cacheVersion))) { + LOGI("OH_NNModel_HasCache read cacheVersion cache info file failed."); ifs.close(); return false; } @@ -557,6 +564,11 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName) exist = (exist && (stat(cacheModelPath.c_str(), &buffer) == 0)); } + if (cacheVersion != version) { + LOGW("OH_NNModel_HasCache version is not match."); + exist = false; + } + return exist; } diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index dde032c..05aeeda 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -213,8 +213,9 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector(modelCacheInfo.deviceId); if (deviceId != m_backendID) { - LOGE("[NNCompiledCache] CheckCacheInfo failed. The deviceId=%{public}zu in the cache files " - "is different from current deviceId=%{public}zu," - "please change the cache directory or current deviceId.", - 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; } diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index 31656e3..7bfb51c 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -450,6 +450,32 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() { // cache存在,从cache直接复原prepareModel、input/output TensorDesc OH_NN_ReturnCode ret = RestoreFromCacheFile(); + if (ret != OH_NN_SUCCESS) { + LOGE("[NNCompiler] cache file is failed, to delete cache."); + char path[PATH_MAX]; + if (realpath(m_cachePath.c_str(), path) == nullptr) { + LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cache Dir."); + return OH_NN_INVALID_PARAMETER; + } + + std::string cachePath = path; + std::string firstCache = cachePath + "/" + m_extensionConfig.modelName + "0.nncache"; + std::string secondCache = cachePath + "/" + m_extensionConfig.modelName + "1.nncache"; + std::string thirdCache = cachePath + "/" + m_extensionConfig.modelName + "2.nncache"; + std::string cacheInfo = cachePath + "/" + m_extensionConfig.modelName + "cache_info.nncache"; + if (std::filesystem::exists(firstCache)) { + std::filesystem::remove_all(firstCache); + } + if (std::filesystem::exists(secondCache)) { + std::filesystem::remove_all(secondCache); + } + if (std::filesystem::exists(thirdCache)) { + std::filesystem::remove_all(thirdCache); + } + if (std::filesystem::exists(cacheInfo)) { + std::filesystem::remove_all(cacheInfo); + } + } if (ret == OH_NN_OPERATION_FORBIDDEN) { LOGE("[NNCompiler] Build failed, operation is forbidden."); return ret; diff --git a/interfaces/innerkits/c/neural_network_runtime_inner.h b/interfaces/innerkits/c/neural_network_runtime_inner.h index f6eabda..c35dff3 100644 --- a/interfaces/innerkits/c/neural_network_runtime_inner.h +++ b/interfaces/innerkits/c/neural_network_runtime_inner.h @@ -139,7 +139,7 @@ OH_NN_ReturnCode OH_NNModel_BuildFromMetaGraph(OH_NNModel *model, const void *me * @since 11 * @version 1.0 */ -bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName); +bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, uint32_t version); #ifdef __cplusplus } -- Gitee From dd534d0ed0233f72b6886ce6f1e3d9dba9598ccd Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 14:56:10 +0800 Subject: [PATCH 2/8] modify cache Signed-off-by: w30052974 --- .../neural_network_core.cpp | 6 ++-- .../neural_network_runtime.cpp | 32 ++++++++++++------- .../neural_network_runtime/nncompiler.cpp | 3 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/frameworks/native/neural_network_core/neural_network_core.cpp b/frameworks/native/neural_network_core/neural_network_core.cpp index f546cc1..5299230 100644 --- a/frameworks/native/neural_network_core/neural_network_core.cpp +++ b/frameworks/native/neural_network_core/neural_network_core.cpp @@ -638,17 +638,17 @@ OH_NN_ReturnCode GetModelId(Compilation** compilation) } if (nnrtService.GetNNRtModelIDFromPath == nullptr) { - LOGE("GetModelId failed, nnrtService GetNNRtModelIDFromPath func is nullptr"); + LOGE("GetModelId failed, nnrtService GetNNRtModelIDFromPath func is nullptr."); return OH_NN_INVALID_PARAMETER; } if (nnrtService.GetNNRtModelIDFromBuffer == nullptr) { - LOGE("GetModelId failed, nnrtService GetNNRtModelIDFromBuffer func is nullptr"); + LOGE("GetModelId failed, nnrtService GetNNRtModelIDFromBuffer func is nullptr."); return OH_NN_INVALID_PARAMETER; } if (nnrtService.GetNNRtModelIDFromModel == nullptr) { - LOGE("GetModelId failed, nnrtService GetNNRtModelIDFromModel func is nullptr"); + LOGE("GetModelId failed, nnrtService GetNNRtModelIDFromModel func is nullptr."); return OH_NN_INVALID_PARAMETER; } diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 110f3e4..dcd237f 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -503,6 +503,24 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const return innerModel->BuildFromLiteGraph(pLiteGraph, extensionConfig); } +bool CheckCacheFile(int64_t fileNumber, int64_t cacheVersion) +{ + if (!ifs.read(reinterpret_cast(&(fileNumber)), sizeof(fileNumber))) { + LOGI("CheckCacheFile read fileNumber cache info file failed."); + ifs.close(); + return false; + } + + if (!ifs.read(reinterpret_cast(&(cacheVersion)), sizeof(cacheVersion))) { + LOGI("CheckCacheFile read cacheVersion cache info file failed."); + ifs.close(); + return false; + } + + ifs.close(); + return true; +} + NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, uint32_t version) { if (cacheDir == nullptr) { @@ -544,19 +562,11 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u int64_t fileNumber{0}; int64_t cacheVersion{0}; - if (!ifs.read(reinterpret_cast(&(fileNumber)), sizeof(fileNumber))) { - LOGI("OH_NNModel_HasCache read fileNumber cache info file failed."); - ifs.close(); + if (!CheckCacheFile(fileNumber, cacheVersion)) { + LOGE("OH_NNModel_HasCache read fileNumber or cacheVersion filed."); return false; } - if (!ifs.read(reinterpret_cast(&(cacheVersion)), sizeof(cacheVersion))) { - LOGI("OH_NNModel_HasCache read cacheVersion cache info file failed."); - ifs.close(); - return false; - } - ifs.close(); - // determine whether cache model files exist for (int64_t i = 0; i < fileNumber; ++i) { std::string cacheModelPath = @@ -568,7 +578,7 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u LOGW("OH_NNModel_HasCache version is not match."); exist = false; } - + return exist; } diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index 7bfb51c..5d2c6b4 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -454,7 +454,7 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() LOGE("[NNCompiler] cache file is failed, to delete cache."); char path[PATH_MAX]; if (realpath(m_cachePath.c_str(), path) == nullptr) { - LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cache Dir."); + LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); return OH_NN_INVALID_PARAMETER; } @@ -476,6 +476,7 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() std::filesystem::remove_all(cacheInfo); } } + if (ret == OH_NN_OPERATION_FORBIDDEN) { LOGE("[NNCompiler] Build failed, operation is forbidden."); return ret; -- Gitee From 84a59711ed1b5dc5612bf47e8f65ee440e7ff83a Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 14:57:44 +0800 Subject: [PATCH 3/8] modify cache Signed-off-by: w30052974 --- .../native/neural_network_runtime/neural_network_runtime.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index dcd237f..a007e9c 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -563,7 +563,7 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u int64_t fileNumber{0}; int64_t cacheVersion{0}; if (!CheckCacheFile(fileNumber, cacheVersion)) { - LOGE("OH_NNModel_HasCache read fileNumber or cacheVersion filed."); + LOGI("OH_NNModel_HasCache read fileNumber or cacheVersion filed."); return false; } -- Gitee From 64a1897083cbe11d4b54b4ae0b3e2ec490b108e0 Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 15:26:28 +0800 Subject: [PATCH 4/8] modify cache Signed-off-by: w30052974 --- frameworks/native/neural_network_runtime/nncompiler.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index 5d2c6b4..a65f5dd 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -454,8 +454,7 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() LOGE("[NNCompiler] cache file is failed, to delete cache."); char path[PATH_MAX]; if (realpath(m_cachePath.c_str(), path) == nullptr) { - LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); - return OH_NN_INVALID_PARAMETER; + LOGW("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); } std::string cachePath = path; -- Gitee From 8b30d61ac8b5e3bef47a267e76cc3995297a6b10 Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 15:45:39 +0800 Subject: [PATCH 5/8] modify cache Signed-off-by: w30052974 --- frameworks/native/neural_network_runtime/nncompiler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index a65f5dd..a453d09 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -452,7 +452,7 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() OH_NN_ReturnCode ret = RestoreFromCacheFile(); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] cache file is failed, to delete cache."); - char path[PATH_MAX]; + char* path = new char[PATH_MAX]; if (realpath(m_cachePath.c_str(), path) == nullptr) { LOGW("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); } @@ -474,6 +474,7 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() if (std::filesystem::exists(cacheInfo)) { std::filesystem::remove_all(cacheInfo); } + delete []path; } if (ret == OH_NN_OPERATION_FORBIDDEN) { -- Gitee From fbe922de44ac9a37dda657a69c15e1e3b0623531 Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 15:56:25 +0800 Subject: [PATCH 6/8] modify cache Signed-off-by: w30052974 --- frameworks/native/neural_network_runtime/nncompiler.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index a453d09..1585331 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -452,8 +452,8 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() OH_NN_ReturnCode ret = RestoreFromCacheFile(); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] cache file is failed, to delete cache."); - char* path = new char[PATH_MAX]; - if (realpath(m_cachePath.c_str(), path) == nullptr) { + char path[PATH_MAX + 1]; + if (realpath(m_cachePath.c_str(), path) == nullptr || strlen(path) >= PATH_MAX) { LOGW("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); } @@ -474,7 +474,6 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() if (std::filesystem::exists(cacheInfo)) { std::filesystem::remove_all(cacheInfo); } - delete []path; } if (ret == OH_NN_OPERATION_FORBIDDEN) { -- Gitee From 8e8fef6e823101ba9be8f90edca5d68a71e04db5 Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 16:01:42 +0800 Subject: [PATCH 7/8] modify cache Signed-off-by: w30052974 --- frameworks/native/neural_network_runtime/nncompiler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index 1585331..a65f5dd 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -452,8 +452,8 @@ OH_NN_ReturnCode NNCompiler::OnlineBuild() OH_NN_ReturnCode ret = RestoreFromCacheFile(); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] cache file is failed, to delete cache."); - char path[PATH_MAX + 1]; - if (realpath(m_cachePath.c_str(), path) == nullptr || strlen(path) >= PATH_MAX) { + char path[PATH_MAX]; + if (realpath(m_cachePath.c_str(), path) == nullptr) { LOGW("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); } -- Gitee From 6825108ba388d111dd312892a091c6237b7eee88 Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 19 Jul 2024 16:14:41 +0800 Subject: [PATCH 8/8] modify cache Signed-off-by: w30052974 --- .../neural_network_runtime.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index a007e9c..a4212d7 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -503,8 +503,14 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const return innerModel->BuildFromLiteGraph(pLiteGraph, extensionConfig); } -bool CheckCacheFile(int64_t fileNumber, int64_t cacheVersion) +bool CheckCacheFile(char* path, int64_t fileNumber, int64_t cacheVersion) { + std::ifstream ifs(path, std::ios::in | std::ios::binary); + if (!ifs) { + LOGI("OH_NNModel_HasCache open cache info file failed."); + return false; + } + if (!ifs.read(reinterpret_cast(&(fileNumber)), sizeof(fileNumber))) { LOGI("CheckCacheFile read fileNumber cache info file failed."); ifs.close(); @@ -554,15 +560,9 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u return false; } - std::ifstream ifs(path, std::ios::in | std::ios::binary); - if (!ifs) { - LOGI("OH_NNModel_HasCache open cache info file failed."); - return false; - } - int64_t fileNumber{0}; int64_t cacheVersion{0}; - if (!CheckCacheFile(fileNumber, cacheVersion)) { + if (!CheckCacheFile(path, fileNumber, cacheVersion)) { LOGI("OH_NNModel_HasCache read fileNumber or cacheVersion filed."); return false; } -- Gitee