1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NEURAL_NETWORK_BACKEND_NNCOMPILED_CACHE_H 17 #define NEURAL_NETWORK_BACKEND_NNCOMPILED_CACHE_H 18 19 #include <vector> 20 #include <fstream> 21 #include <memory> 22 #include <unistd.h> 23 #include <sys/mman.h> 24 #include <sys/stat.h> 25 #include <fcntl.h> 26 27 #include "nlohmann/json.hpp" 28 29 #include "device.h" 30 #include "neural_network_runtime/neural_network_runtime.h" 31 #include "tensor_desc.h" 32 33 namespace OHOS { 34 namespace NeuralNetworkRuntime { 35 const uint32_t INVALID_CAHCE_VERSION = UINT32_MAX; // UINT32_MAX is reserved for invalid cache version. 36 constexpr size_t NN_CACHE_FILE_NUMBER_MAX = 100; // 限制cache文件数量最大为100 37 38 struct NNCompiledCacheInfo { 39 int64_t fileNumber{0}; 40 int64_t version{0}; 41 int64_t deviceId{0}; 42 std::vector<unsigned short> modelCheckSum; 43 int64_t opVersion{0}; 44 int64_t isExceedRamLimit{0}; 45 }; 46 47 class NNCompiledCache { 48 public: 49 NNCompiledCache() = default; 50 ~NNCompiledCache() = default; 51 52 OH_NN_ReturnCode Save(const std::vector<Buffer>& caches, 53 const std::string& cacheDir, 54 uint32_t version); 55 OH_NN_ReturnCode Restore(const std::string& cacheDir, 56 uint32_t version, 57 std::vector<Buffer>& caches); 58 59 OH_NN_ReturnCode SetBackend(size_t backendID); 60 void SetModelName(const std::string& modelName); 61 void SetIsExceedRamLimit(const bool isExceedRamLimit); 62 OH_NN_ReturnCode WriteCacheInfo(uint32_t cacheSize, 63 nlohmann::json& cacheInfo, 64 const std::string& cacheDir) const; 65 OH_NN_ReturnCode CheckCacheInfo(NNCompiledCacheInfo& modelCacheInfo, const std::string& cacheInfoPath) const; 66 OH_NN_ReturnCode CheckCacheInfoExtension(NNCompiledCacheInfo& modelCacheInfo, nlohmann::json& j) const; 67 void ReleaseCacheBuffer(std::vector<Buffer>& buffers); 68 unsigned short GetCrc16(char* buffer, size_t length) const; 69 70 private: 71 OH_NN_ReturnCode CheckCache(const std::string& cacheDir, 72 uint32_t version, 73 std::vector<OHOS::NeuralNetworkRuntime::Buffer>& caches); 74 OH_NN_ReturnCode GenerateCacheFiles(const std::vector<Buffer>& caches, 75 const std::string& cacheDir, 76 uint32_t version) const; 77 OH_NN_ReturnCode GenerateCacheModel(const std::vector<Buffer>& caches, 78 nlohmann::json& cacheInfo, 79 const std::string& cacheDir, 80 uint32_t version) const; 81 OH_NN_ReturnCode ReadCacheModelFile(const std::string& file, Buffer& cache); 82 OH_NN_ReturnCode GetCacheFileLength(FILE* pFile, long& fileSize) const; 83 OH_NN_ReturnCode VerifyCachePath(const std::string& cachePath) const; 84 85 private: 86 size_t m_backendID {0}; 87 std::string m_modelName; 88 std::shared_ptr<Device> m_device {nullptr}; 89 bool m_isExceedRamLimit {false}; 90 }; 91 92 } // namespace NeuralNetworkRuntime 93 } // namespace OHOS 94 95 #endif // NEURAL_NETWORK_BACKEND_NNCOMPILED_CACHE_H 96