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_OPS_BUILDER_H 17 #define NEURAL_NETWORK_RUNTIME_OPS_BUILDER_H 18 19 #include <memory> 20 #include <unordered_map> 21 22 #include "nn_tensor.h" 23 #include "common/log.h" 24 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime.h" 25 26 namespace OHOS { 27 namespace NeuralNetworkRuntime { 28 namespace Ops { 29 using LiteGraphPrimitvePtr = std::unique_ptr<void, void(*)(void*)>; 30 void DestroyLiteGraphPrimitive(void* primitive); 31 32 // QuantType Enum 33 enum class OpsQuantType: int { 34 QUANT_NONE = 0, 35 QUANT_ALL = 1 36 }; 37 38 class OpsBuilder { 39 public: 40 OpsBuilder() = default; 41 virtual ~OpsBuilder() = default; 42 43 // Other operation builders inherit from OpsBuilder, delete these special construction and assignment functions. 44 OpsBuilder(const OpsBuilder& opsBuilder) = delete; 45 OpsBuilder& operator=(const OpsBuilder& opsBuilder) = delete; 46 OpsBuilder(OpsBuilder&& opsBuilder) = delete; 47 OpsBuilder& operator=(OpsBuilder&& opsBuilder) = delete; 48 49 virtual OH_NN_ReturnCode Build(const std::vector<uint32_t>& paramsIndex, 50 const std::vector<uint32_t>& inputsIndex, 51 const std::vector<uint32_t>& outputsIndex, 52 const std::vector<std::shared_ptr<NNTensor>>& allTensors) = 0; 53 virtual LiteGraphPrimitvePtr GetPrimitive() = 0; 54 55 virtual void GetInputIndex(std::vector<uint32_t>& inputsIndex, 56 const std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID) const; 57 virtual void GetOutputIndex(std::vector<uint32_t>& outputsIndex, 58 const std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID) const; 59 virtual std::string GetName() const; 60 virtual OpsQuantType GetQuantType() const; 61 62 protected: 63 OH_NN_ReturnCode CheckIOIndex(const std::vector<uint32_t>& inputsIndex, 64 const std::vector<uint32_t>& outputsIndex, 65 const std::vector<std::shared_ptr<NNTensor>>& allTensors, 66 const size_t inputNum, 67 const size_t outputNum) const; 68 OH_NN_ReturnCode CheckParamIndex(const std::vector<uint32_t>& paramsIndex, 69 const std::vector<std::shared_ptr<NNTensor>>& allTensors, 70 const size_t paramNum) const; 71 void SetQuantType(const std::vector<uint32_t>& outputsIndex, 72 const std::vector<std::shared_ptr<NNTensor>>& allTensors); 73 74 protected: 75 std::string m_name; 76 std::vector<uint32_t> m_inputsIndex; 77 std::vector<uint32_t> m_outputsIndex; 78 OpsQuantType m_quantType {OpsQuantType::QUANT_NONE}; 79 bool m_isBuild {false}; 80 }; 81 } // namespace Ops 82 } // namespace NeuralNetworkRuntime 83 } // namespace OHOS 84 #endif // NEURAL_NETWORK_RUNTIME_OPS_BUILDER_H