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 #include "ops_builder.h"
17 #include "mindir.h"
18 #include "mindir_types.h"
19
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
DestroyLiteGraphPrimitive(void * primitive)23 void DestroyLiteGraphPrimitive(void* primitive)
24 {
25 mindspore::lite::MindIR_Primitive_Destroy(&primitive);
26 }
27
GetInputIndex(std::vector<uint32_t> & inputsIndex,const std::unordered_map<uint32_t,uint32_t> & modelIDToGraphID) const28 void OpsBuilder::GetInputIndex(std::vector<uint32_t>& inputsIndex,
29 const std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID) const
30 {
31 for (auto index : m_inputsIndex) {
32 // index has been prevented from taking value out of modelIDToGraphID, no need to check.
33 inputsIndex.emplace_back(modelIDToGraphID.at(index));
34 }
35 }
36
GetOutputIndex(std::vector<uint32_t> & outputsIndex,const std::unordered_map<uint32_t,uint32_t> & modelIDToGraphID) const37 void OpsBuilder::GetOutputIndex(std::vector<uint32_t>& outputsIndex,
38 const std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID) const
39 {
40 for (auto index : m_outputsIndex) {
41 // index has been prevented from taking value out of modelIDToGraphID, no need to check.
42 outputsIndex.emplace_back(modelIDToGraphID.at(index));
43 }
44 }
45
GetName() const46 std::string OpsBuilder::GetName() const
47 {
48 return m_name;
49 }
50
GetQuantType() const51 OpsQuantType OpsBuilder::GetQuantType() const
52 {
53 return m_quantType;
54 }
55
CheckIOIndex(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors,const size_t inputNum,const size_t outputNum) const56 OH_NN_ReturnCode OpsBuilder::CheckIOIndex(const std::vector<uint32_t>& inputsIndex,
57 const std::vector<uint32_t>& outputsIndex,
58 const std::vector<std::shared_ptr<NNTensor>>& allTensors,
59 const size_t inputNum,
60 const size_t outputNum) const
61 {
62 size_t inputsIndexSize = inputsIndex.size();
63 size_t outputIndexSize = outputsIndex.size();
64 if (inputsIndexSize != inputNum) {
65 LOGE("The number of index of inputs is %zu don't equal to %zu.", inputsIndexSize, inputNum);
66 return OH_NN_INVALID_PARAMETER;
67 }
68 if (outputIndexSize != outputNum) {
69 LOGE("The number of index of outputs is %zu don't equal to %zu.", outputIndexSize, outputNum);
70 return OH_NN_INVALID_PARAMETER;
71 }
72
73 size_t allTensorsSize = allTensors.size();
74 for (auto index : inputsIndex) {
75 if (index >= allTensorsSize) {
76 LOGE("The index of inputs is out of range.");
77 return OH_NN_INVALID_PARAMETER;
78 }
79 }
80
81 for (auto index : outputsIndex) {
82 if (index >= allTensorsSize) {
83 LOGE("The index of outputs is out of range.");
84 return OH_NN_INVALID_PARAMETER;
85 }
86 }
87
88 return OH_NN_SUCCESS;
89 }
90
SetQuantType(const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)91 void OpsBuilder::SetQuantType(const std::vector<uint32_t>& outputsIndex,
92 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
93 {
94 if (allTensors[outputsIndex.front()]->IsQuantTensor()) {
95 m_quantType = OpsQuantType::QUANT_ALL;
96 }
97 }
98 } // namespace Ops
99 } // namespace NeuralNetworkRuntime
100 } // namespace OHOS