1 /* 2 * Copyright (c) 2022 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_INNER_MODEL_H 17 #define NEURAL_NETWORK_RUNTIME_INNER_MODEL_H 18 19 #include <memory> 20 #include <unordered_map> 21 22 #include "mindir.h" 23 #include "ops_builder.h" 24 #include "interfaces/kits/c/neural_network_runtime.h" 25 26 namespace OHOS { 27 namespace NeuralNetworkRuntime { 28 class InnerModel { 29 public: 30 InnerModel(); 31 32 bool IsBuild() const; 33 OH_NN_ReturnCode BuildFromLiteGraph(const mindspore::lite::LiteGraph* liteGraph); 34 OH_NN_ReturnCode AddTensor(const OH_NN_Tensor& nnTensor); 35 OH_NN_ReturnCode SetTensorValue(uint32_t index, const void* buffer, size_t length); 36 OH_NN_ReturnCode AddOperation(OH_NN_OperationType opType, 37 const OH_NN_UInt32Array& paramIndices, 38 const OH_NN_UInt32Array& inputIndices, 39 const OH_NN_UInt32Array& outputIndices); 40 OH_NN_ReturnCode GetSupportedOperations(size_t deviceID, const bool** isSupported, uint32_t& opCount); 41 OH_NN_ReturnCode SpecifyInputsAndOutputs( 42 const OH_NN_UInt32Array& inputIndices, const OH_NN_UInt32Array& outputIndices); 43 OH_NN_ReturnCode Build(); 44 std::vector<std::shared_ptr<NNTensor>> GetInputTensors() const; 45 std::vector<std::shared_ptr<NNTensor>> GetOutputTensors() const; 46 std::shared_ptr<mindspore::lite::LiteGraph> GetLiteGraphs() const; 47 48 private: 49 void AddTensorsToLiteGraph(std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID); 50 OH_NN_ReturnCode AddNodesToLiteGraph(const std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID); 51 OH_NN_ReturnCode ValidateInputAndOutput( 52 const OH_NN_UInt32Array& inputIndices, const OH_NN_UInt32Array& outputIndices) const; 53 OH_NN_ReturnCode ValidateTensorArray(const OH_NN_UInt32Array& indices) const; 54 55 private: 56 std::vector<char> m_supportedOperations; // std::vector<bool> not support data(), use std::vector<char> instead. 57 std::vector<uint32_t> m_inputIndices; 58 std::vector<uint32_t> m_outputIndices; 59 std::vector<std::unique_ptr<Ops::OpsBuilder>> m_ops; 60 std::vector<std::shared_ptr<NNTensor>> m_allTensors; 61 std::vector<std::shared_ptr<NNTensor>> m_inputTensors; // Used to pass input tensors to compilation. 62 std::vector<std::shared_ptr<NNTensor>> m_outputTensors; // Used to pass output tensors to compilation. 63 std::shared_ptr<mindspore::lite::LiteGraph> m_liteGraph {nullptr}; 64 }; 65 } // namespace NeuralNetworkRuntime 66 } // namespace OHOS 67 #endif // NEURAL_NETWORK_RUNTIME_INNER_MODEL_H 68