From f40c70e4504624b8a046067d89c16c949abd3685 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sat, 7 Jun 2025 17:16:01 +0800 Subject: [PATCH 1/6] =?UTF-8?q?OH6.0=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../native/neural_network_runtime/nncompiled_cache.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 0fcf114..f43ca40 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -122,7 +122,7 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, cacheInfo.modelCheckSum[i]) { LOGE("[NNCompiledCache] Restore failed, the cache model file %{public}s has been changed.", cacheModelPath.c_str()); - return OH_NN_INVALID_FILE; + close(modelBuffer.fd); return OH_NN_INVALID_FILE; } caches.emplace_back(std::move(modelBuffer)); @@ -424,7 +424,7 @@ OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath off_t fsize = sb.st_size; - void *ptr = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); + void *ptr = mmap(nullptr, fsize, PROT_READ, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { LOGE("[NNCompiledCache] ReadCacheModelFile failed, failed to mmap file."); close(fd); @@ -441,6 +441,10 @@ unsigned short NNCompiledCache::GetCrc16(char* buffer, size_t length) const { unsigned int sum = 0; + if (buffer == nullptr) { + return static_cast(~sum); + } + if (length < MAX_CACHE_SIZE) { while (length > 1) { sum += *(reinterpret_cast(buffer)); -- Gitee From 1ca24b508b84552ca4ea28454d55f41bea604030 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sun, 8 Jun 2025 10:23:59 +0800 Subject: [PATCH 2/6] =?UTF-8?q?OH6.0=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- frameworks/native/neural_network_runtime/nncompiled_cache.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index f43ca40..d7dfa10 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -122,7 +122,8 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, cacheInfo.modelCheckSum[i]) { LOGE("[NNCompiledCache] Restore failed, the cache model file %{public}s has been changed.", cacheModelPath.c_str()); - close(modelBuffer.fd); return OH_NN_INVALID_FILE; + close(modelBuffer.fd); + return OH_NN_INVALID_FILE; } caches.emplace_back(std::move(modelBuffer)); -- Gitee From 37d658dff8144329ecfb6136612406df4ad98d48 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sun, 8 Jun 2025 10:40:43 +0800 Subject: [PATCH 3/6] =?UTF-8?q?OH6.0=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../nncompiled_cache.cpp | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index d7dfa10..c379a9f 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -61,9 +61,10 @@ OH_NN_ReturnCode NNCompiledCache::Save(const std::vector& caches) +namespace { +OH_NN_ReturnCode CheckCache(const std::string& cacheDir, + uint32_t version, + std::vector& caches) { if (cacheDir.empty()) { LOGE("[NNCompiledCache] Restore failed, cacheDir is empty."); @@ -79,6 +80,18 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, LOGE("[NNCompiledCache] Restore failed, m_device is empty."); return OH_NN_INVALID_PARAMETER; } + return OH_NN_SUCCESS; +} +} + +OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, + uint32_t version, + std::vector& caches) +{ + OH_NN_ReturnCode ret = CheckCache(cacheDir,version,caches); + if (ret != OH_NN_SUCCESS) { + return ret; + } std::string cacheInfoPath = cacheDir + "/" + m_modelName + "cache_info.nncache"; char path[PATH_MAX]; @@ -92,7 +105,7 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, } NNCompiledCacheInfo cacheInfo; - OH_NN_ReturnCode ret = CheckCacheInfo(cacheInfo, path); + ret = CheckCacheInfo(cacheInfo, path); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiledCache] Restore failed, error happened when calling CheckCacheInfo."); return ret; -- Gitee From b41336f29fd66df3f41302e08f29eb7cd30ceb57 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sun, 8 Jun 2025 11:08:02 +0800 Subject: [PATCH 4/6] =?UTF-8?q?OH6.0=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../native/neural_network_runtime/nncompiled_cache.cpp | 8 +++----- .../native/neural_network_runtime/nncompiled_cache.h | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index c379a9f..0dd6885 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -61,10 +61,9 @@ OH_NN_ReturnCode NNCompiledCache::Save(const std::vector& caches) +OH_NN_ReturnCode NNCompiledCache::CheckCache(const std::string& cacheDir, + uint32_t version, + std::vector& caches) { if (cacheDir.empty()) { LOGE("[NNCompiledCache] Restore failed, cacheDir is empty."); @@ -82,7 +81,6 @@ OH_NN_ReturnCode CheckCache(const std::string& cacheDir, } return OH_NN_SUCCESS; } -} OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, uint32_t version, diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.h b/frameworks/native/neural_network_runtime/nncompiled_cache.h index 7ee8ce2..dcdce76 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.h +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.h @@ -52,6 +52,9 @@ public: OH_NN_ReturnCode Save(const std::vector& caches, const std::string& cacheDir, uint32_t version); + OH_NN_ReturnCode CheckCache(const std::string& cacheDir, + int32_t version, + std::vector& caches); OH_NN_ReturnCode Restore(const std::string& cacheDir, uint32_t version, std::vector& caches); -- Gitee From 66a4a64ff4f45ca9a4646abe633c162f237636cb Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sun, 8 Jun 2025 11:59:33 +0800 Subject: [PATCH 5/6] =?UTF-8?q?OH6.0=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- frameworks/native/neural_network_runtime/nncompiled_cache.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.h b/frameworks/native/neural_network_runtime/nncompiled_cache.h index dcdce76..c68e605 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.h +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.h @@ -52,9 +52,6 @@ public: OH_NN_ReturnCode Save(const std::vector& caches, const std::string& cacheDir, uint32_t version); - OH_NN_ReturnCode CheckCache(const std::string& cacheDir, - int32_t version, - std::vector& caches); OH_NN_ReturnCode Restore(const std::string& cacheDir, uint32_t version, std::vector& caches); @@ -71,6 +68,9 @@ public: unsigned short GetCrc16(char* buffer, size_t length) const; private: + OH_NN_ReturnCode CheckCache(const std::string& cacheDir, + uint32_t version, + std::vector& caches); OH_NN_ReturnCode GenerateCacheFiles(const std::vector& caches, const std::string& cacheDir, uint32_t version) const; -- Gitee From 3ad9bc503ab184d5c934e33c5435bf7bc40af7c9 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sun, 8 Jun 2025 13:28:45 +0800 Subject: [PATCH 6/6] =?UTF-8?q?OH6.0=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- frameworks/native/neural_network_runtime/nncompiled_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 0dd6885..5053e52 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -86,7 +86,7 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, uint32_t version, std::vector& caches) { - OH_NN_ReturnCode ret = CheckCache(cacheDir,version,caches); + OH_NN_ReturnCode ret = CheckCache(cacheDir, version, caches); if (ret != OH_NN_SUCCESS) { return ret; } -- Gitee