• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RUNTIME_NNCOMPILER_H
17 #define NEURAL_NETWORK_RUNTIME_NNCOMPILER_H
18 
19 #include "compiler.h"
20 
21 #include "mindir.h"
22 #include "device.h"
23 #include "inner_model.h"
24 #include "prepared_model.h"
25 #include "nnexecutor.h"
26 
27 namespace OHOS {
28 namespace NeuralNetworkRuntime {
29 
30 class NNCompiler : public Compiler {
31 public:
32     NNCompiler() = delete;
33     NNCompiler(std::shared_ptr<Device> device, size_t backendID);
34     NNCompiler(const void* model, std::shared_ptr<Device> device, size_t backendID);
35     ~NNCompiler() override;
36 
37     size_t GetBackendID() const override;
38 
39     OH_NN_ReturnCode SetCacheDir(const std::string& cacheModelPath, uint32_t version) override;
40     OH_NN_ReturnCode SetPerformance(OH_NN_PerformanceMode performance) override;
41     OH_NN_ReturnCode SetPriority(OH_NN_Priority priority) override;
42     OH_NN_ReturnCode SetEnableFp16(bool isFp16) override;
43 
44     bool IsBuild() const override;
45     OH_NN_ReturnCode Build() override;
46 
47     OH_NN_ReturnCode SaveToCacheFile() const override;
48     OH_NN_ReturnCode RestoreFromCacheFile() override;
49     OH_NN_ReturnCode SaveToCacheBuffer(const void* buffer, size_t length, size_t* modelSize) const override;
50     OH_NN_ReturnCode RestoreFromCacheBuffer(const void* buffer, size_t length) override;
51 
52     OH_NN_ReturnCode SetExtensionConfig(const std::unordered_map<std::string, std::vector<char>>& configs) override;
53     OH_NN_ReturnCode SetOptions(const std::vector<std::shared_ptr<void>>& options) override;
54     OH_NN_ReturnCode GetModelName(std::string& modelName) override;
55     size_t GetModelSize() override;
56     size_t GetOnlineModelID() override;
57 
58     NNExecutor* CreateExecutor();
59 
60 private:
61     void ReleaseBuffer(std::vector<Buffer>& buffers) const;
62     void ReleaseBufferByDevice(std::vector<Buffer>& buffers) const;
63     OH_NN_ReturnCode SerializeTensorsToBuffer(
64         const std::vector<std::pair<std::shared_ptr<TensorDesc>, OH_NN_TensorType>>& tensorDescs,
65         Buffer& buffer) const;
66     OH_NN_ReturnCode DeserializedTensorsFromBuffer(
67         const Buffer& buffer, std::vector<std::pair<std::shared_ptr<TensorDesc>, OH_NN_TensorType>>& tensorDescs);
68 
69     OH_NN_ReturnCode OnlineBuild();
70     OH_NN_ReturnCode NormalBuild();
71     OH_NN_ReturnCode BuildOfflineModel();
72     OH_NN_ReturnCode CheckModelParameter() const;
73     OH_NN_ReturnCode IsOfflineModel(bool& isOfflineModel) const;
74     OH_NN_ReturnCode IsSupportedModel(const std::shared_ptr<mindspore::lite::LiteGraph>& liteGraph,
75                                       bool& isSupportedModel) const;
76 
77     size_t GetModelSizeFromCache(std::string& path, const std::string& modelName);
78     size_t GetModelSizeFromFile(std::string& path);
79     size_t GetModelSizeFromModel(InnerModel* innerModel);
80     OH_NN_ReturnCode GetNNRtModelIDFromCache(const std::string& path, const std::string& modelName,
81         size_t& nnrtModelID);
82     OH_NN_ReturnCode GetNNRtModelIDFromModel(InnerModel* innerModel, size_t& nnrtModelID);
83     size_t GetOnlineModelID(const std::shared_ptr<mindspore::lite::LiteGraph>& liteGraph);
84     std::vector<mindspore::lite::LiteGraph::Node*> GetNodeIndices(
85         const std::shared_ptr<mindspore::lite::LiteGraph>& liteGraph, size_t layer);
86     size_t DataTypeSize(mindspore::lite::DataType dataType);
87     size_t GetFileSize(const char* fileName);
88 
89 private:
90     bool m_isBuild {false};
91     bool m_enableFp16 {false};
92     std::string m_cachePath;
93     uint32_t m_cacheVersion {0};
94     std::shared_ptr<Device> m_device {nullptr};
95     size_t m_backendID {0};
96     OH_NN_Priority m_priority {OH_NN_PRIORITY_NONE};
97     OH_NN_PerformanceMode m_performance {OH_NN_PERFORMANCE_NONE};
98     std::shared_ptr<PreparedModel> m_preparedModel {nullptr};
99     void* m_metaGraph {nullptr};
100     InnerModel* m_innerModel {nullptr};
101     std::shared_ptr<mindspore::lite::LiteGraph> m_liteGraph {nullptr};
102     std::vector<std::pair<std::shared_ptr<TensorDesc>, OH_NN_TensorType>> m_inputTensorDescs;
103     std::vector<std::pair<std::shared_ptr<TensorDesc>, OH_NN_TensorType>> m_outputTensorDescs;
104     ExtensionConfig m_extensionConfig;
105 };
106 } // NeuralNetworkRuntime
107 } // OHOS
108 
109 #endif // NEURAL_NETWORK_RUNTIME_NNCOMPILER_H