diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 75cf442..dde032c 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -32,6 +32,7 @@ constexpr int32_t NUMBER_CACHE_INFO_MEMBERS = 3; constexpr int32_t HEX_UNIT = 16; constexpr char ROOT_DIR_STR = '/'; constexpr char DOUBLE_SLASH_STR[] = "//"; +constexpr int OPVERSION_SUBSTR_NUM = 2; OH_NN_ReturnCode NNCompiledCache::Save(const std::vector& caches, const std::string& cacheDir, @@ -162,7 +163,7 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheFiles(const std::vector cacheInfo = CreateUniquePtr(cacheSize); if (cacheInfo == nullptr) { LOGE("[NNCompiledCache] GenerateCacheFiles failed, fail to create cacheInfo instance."); @@ -232,6 +233,17 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector(modelCheckSum[i]); } + if (!infoCacheFile.read(reinterpret_cast(&(modelCacheInfo.opVersion)), sizeof(uint64_t))) { + LOGW("[NNCompiledCache] opVersion failed."); + } + + infoCacheFile.close(); return OH_NN_SUCCESS; } diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.h b/frameworks/native/neural_network_runtime/nncompiled_cache.h index f720474..45897e9 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.h +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.h @@ -33,6 +33,7 @@ struct NNCompiledCacheInfo { int64_t version{0}; int64_t deviceId{0}; std::vector modelCheckSum; + int64_t opVersion{0}; }; class NNCompiledCache { diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index 190e402..31656e3 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -31,6 +31,7 @@ const int CACHE_INPUT_TENSORDESC_OFFSET = 2; const int CACHE_OUTPUT_TENSORDESC_OFFSET = 1; constexpr int32_t NUMBER_CACHE_INFO_MEMBERS = 3; const std::string EXTENSION_KEY_MODEL_NAME = "ModelName"; +const int OPVERSION_SUBSTR_NUM = 2; struct SerializedTensorDesc { public: @@ -618,6 +619,16 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() if (isUpdatable) { LOGI("isUpdatable is true"); + std::string currentVersion = "0x00000000"; + std::string path = "/data/data/hiai/version"; + std::ifstream inf(path.c_str()); + if (inf.is_open()) { + getline(inf, currentVersion); + } + + int currentOpVersion = std::stoi(currentVersion.substr(OPVERSION_SUBSTR_NUM)); + inf.close(); + NNCompiledCacheInfo modelCacheInfo; std::string cacheInfoPath = m_cachePath + "/" + m_extensionConfig.modelName + "cache_info.nncache"; ret = compiledCache.CheckCacheInfo(modelCacheInfo, cacheInfoPath); @@ -626,31 +637,36 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() return ret; } - LOGI("isUpdatable modelCacheInfo"); + LOGI("isUpdatable currentOpVersion is: %{public}d", currentOpVersion); + LOGI("isUpdatable modelCacheInfo opVersion is %{public}d", static_cast(modelCacheInfo.opVersion)); - const size_t cacheNumber = caches.size(); - uint32_t cacheSize = NUMBER_CACHE_INFO_MEMBERS + cacheNumber; - uint32_t infoCharNumber = cacheSize * sizeof(int64_t); + if (currentOpVersion > modelCacheInfo.opVersion) { + const size_t cacheNumber = caches.size(); + uint32_t cacheSize = NUMBER_CACHE_INFO_MEMBERS + cacheNumber + 1; + uint32_t infoCharNumber = cacheSize * sizeof(int64_t); - std::unique_ptr cacheInfo = CreateUniquePtr(cacheSize); - if (cacheInfo == nullptr) { - LOGE("[NNCompiledCache] isUpdatable is true to create unique failed."); - return OH_NN_MEMORY_ERROR; - } + std::unique_ptr cacheInfo = CreateUniquePtr(cacheSize); + if (cacheInfo == nullptr) { + LOGE("[NNCompiledCache] isUpdatable is true to create unique failed."); + return OH_NN_MEMORY_ERROR; + } - auto cacheInfoPtr = cacheInfo.get(); - *cacheInfoPtr++ = modelCacheInfo.fileNumber; - *cacheInfoPtr++ = modelCacheInfo.version - 1; - *cacheInfoPtr++ = 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) { - *cacheInfoPtr++ = static_cast(modelCacheInfo.modelCheckSum[i]); - } + for (size_t i = 0; i < modelCacheInfo.modelCheckSum.size(); ++i) { + *cacheInfoPtr++ = static_cast(modelCacheInfo.modelCheckSum[i]); + } - ret = compiledCache.WriteCacheInfo(infoCharNumber, cacheInfo, m_cachePath); - if (ret != OH_NN_SUCCESS) { - LOGE("[NNCompiledCache] isUpdatable is true to write cache info failed."); - return ret; + *cacheInfoPtr++ = currentOpVersion; + + ret = compiledCache.WriteCacheInfo(infoCharNumber, cacheInfo, m_cachePath); + if (ret != OH_NN_SUCCESS) { + LOGE("[NNCompiledCache] isUpdatable is true to write cache info failed."); + return ret; + } } }