From 7a7fc16b7fca8192b592e86023f7c008f5a40f1e Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Sat, 24 May 2025 18:51:27 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../neural_network_core.cpp | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/frameworks/native/neural_network_core/neural_network_core.cpp b/frameworks/native/neural_network_core/neural_network_core.cpp index 63fed5c..c4249b7 100644 --- a/frameworks/native/neural_network_core/neural_network_core.cpp +++ b/frameworks/native/neural_network_core/neural_network_core.cpp @@ -648,6 +648,33 @@ OH_NN_ReturnCode GetNnrtModelId(Compilation* compilationImpl, NNRtServiceApi& nn return OH_NN_SUCCESS; } + +OH_NN_ReturnCode IsCompilationAvaliable(Compilation* compilationImpl) +{ + if (compilationImpl == nullptr) { + LOGE("IsCompilationAvaliable failed, compilation implementation is nullptr."); + return OH_NN_INVALID_PARAMETER; + } + + if (((compilationImpl->nnModel != nullptr) && (compilationImpl->offlineModelPath != nullptr)) || + ((compilationImpl->nnModel != nullptr) && + ((compilationImpl->offlineModelBuffer.first != nullptr) || + (compilationImpl->offlineModelBuffer.second != static_cast(0)))) || + ((compilationImpl->offlineModelPath != nullptr) && + ((compilationImpl->offlineModelBuffer.first != nullptr) || + (compilationImpl->offlineModelBuffer.second != static_cast(0))))) { + LOGE("IsCompilationAvaliable failed, find multi model to build compilation."); + return OH_NN_INVALID_PARAMETER; + } + + if (compilationImpl->compiler != nullptr) { + LOGE("IsCompilationAvaliable failed, the compiler in compilation is not nullptr, " + "please input a new compilation."); + return OH_NN_INVALID_PARAMETER; + } + + return OH_NN_SUCCESS; +} } OH_NN_ReturnCode GetModelId(Compilation** compilation) @@ -700,24 +727,12 @@ NNRT_API OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation) return OH_NN_INVALID_PARAMETER; } - Compilation* compilationImpl = reinterpret_cast(compilation); - - if (((compilationImpl->nnModel != nullptr) && (compilationImpl->offlineModelPath != nullptr)) || - ((compilationImpl->nnModel != nullptr) && - ((compilationImpl->offlineModelBuffer.first != nullptr) || - (compilationImpl->offlineModelBuffer.second != static_cast(0)))) || - ((compilationImpl->offlineModelPath != nullptr) && - ((compilationImpl->offlineModelBuffer.first != nullptr) || - (compilationImpl->offlineModelBuffer.second != static_cast(0))))) { - LOGE("OH_NNCompilation_Build failed, find multi model to build compilation."); - return OH_NN_INVALID_PARAMETER; - } - OH_NN_ReturnCode ret = OH_NN_SUCCESS; - if (compilationImpl->compiler != nullptr) { - LOGE("OH_NNCompilation_Build failed, the compiler in compilation is not nullptr, " - "please input a new compilation."); - return OH_NN_INVALID_PARAMETER; + Compilation* compilationImpl = reinterpret_cast(compilation); + ret = IsCompilationAvaliable(compilationImpl); + if (ret != OH_NN_SUCCESS) { + LOGE("OH_NNCompilation_Build failed, fail to compiler parameter."); + return ret; } Compiler* compiler = nullptr; @@ -755,7 +770,7 @@ NNRT_API OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation) if (nnrtService.IsServiceAvaliable()) { bool retCode = nnrtService.PullUpDlliteService(); if (!retCode) { - LOGI("OH_NNCompilation_Build failed, PullUpDlliteService failed."); + LOGW("OH_NNCompilation_Build failed, PullUpDlliteService failed."); } } @@ -1126,7 +1141,6 @@ NNRT_API NN_Tensor* OH_NNTensor_CreateWithFd(size_t deviceID, NNRT_API OH_NN_ReturnCode OH_NNTensor_Destroy(NN_Tensor **tensor) { - LOGI("start OH_NNTensor_Destroy"); if (tensor == nullptr) { LOGE("OH_NNTensor_Destroy failed, tensor is nullptr."); return OH_NN_INVALID_PARAMETER; -- Gitee 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 2/7] =?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 3/7] =?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 4/7] =?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 5/7] =?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 6/7] =?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 7/7] =?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