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 // index has been prevented from taking value out of modelIDToGraphID, no need to check.
32 std::transform(m_inputsIndex.begin(), m_inputsIndex.end(), std::back_inserter(inputsIndex),
33 [modelIDToGraphID](uint32_t index) {
34 return modelIDToGraphID.at(index);
35 });
36 }
37
GetOutputIndex(std::vector<uint32_t> & outputsIndex,const std::unordered_map<uint32_t,uint32_t> & modelIDToGraphID) const38 void OpsBuilder::GetOutputIndex(std::vector<uint32_t>& outputsIndex,
39 const std::unordered_map<uint32_t, uint32_t>& modelIDToGraphID) const
40 {
41 // index has been prevented from taking value out of modelIDToGraphID, no need to check.
42 std::transform(m_outputsIndex.begin(), m_outputsIndex.end(), std::back_inserter(outputsIndex),
43 [modelIDToGraphID](uint32_t index) {
44 return modelIDToGraphID.at(index);
45 });
46 }
47
GetName() const48 std::string OpsBuilder::GetName() const
49 {
50 return m_name;
51 }
52
GetQuantType() const53 OpsQuantType OpsBuilder::GetQuantType() const
54 {
55 return m_quantType;
56 }
57
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) const58 OH_NN_ReturnCode OpsBuilder::CheckIOIndex(const std::vector<uint32_t>& inputsIndex,
59 const std::vector<uint32_t>& outputsIndex,
60 const std::vector<std::shared_ptr<NNTensor>>& allTensors,
61 const size_t inputNum,
62 const size_t outputNum) const
63 {
64 size_t inputsIndexSize = inputsIndex.size();
65 size_t outputIndexSize = outputsIndex.size();
66 if (inputsIndexSize != inputNum) {
67 LOGE("The number of index of inputs is %{public}zu don't equal to %{public}zu.", inputsIndexSize, inputNum);
68 return OH_NN_INVALID_PARAMETER;
69 }
70 if (outputIndexSize != outputNum) {
71 LOGE("The number of index of outputs is %{public}zu don't equal to %zu.", outputIndexSize, outputNum);
72 return OH_NN_INVALID_PARAMETER;
73 }
74
75 size_t allTensorsSize = allTensors.size();
76 bool isOverTensorSize = std::any_of(inputsIndex.begin(), inputsIndex.end(), [allTensorsSize](uint32_t index) {
77 return index >= allTensorsSize;
78 });
79 if (isOverTensorSize) {
80 LOGE("The index of inputs is out of range.");
81 return OH_NN_INVALID_PARAMETER;
82 }
83
84 isOverTensorSize = std::any_of(outputsIndex.begin(), outputsIndex.end(), [allTensorsSize](uint32_t index) {
85 return index >= allTensorsSize;
86 });
87 if (isOverTensorSize) {
88 LOGE("The index of outputs is out of range.");
89 return OH_NN_INVALID_PARAMETER;
90 }
91
92 return OH_NN_SUCCESS;
93 }
94
CheckParamIndex(const std::vector<uint32_t> & paramsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors,const size_t paramNum) const95 OH_NN_ReturnCode OpsBuilder::CheckParamIndex(const std::vector<uint32_t>& paramsIndex,
96 const std::vector<std::shared_ptr<NNTensor>>& allTensors,
97 const size_t paramNum) const
98 {
99 size_t paramsIndexSize = paramsIndex.size();
100 if (paramsIndexSize > paramNum) {
101 LOGE("The number of index of params is %{public}zu larger than %{public}zu.", paramsIndexSize, paramNum);
102 return OH_NN_INVALID_PARAMETER;
103 }
104
105 size_t allTensorsSize = allTensors.size();
106 bool isParamsOutOfRange = std::any_of(paramsIndex.begin(), paramsIndex.end(), [allTensorsSize](uint32_t index) {
107 return index >= allTensorsSize;
108 });
109 if (isParamsOutOfRange) {
110 LOGE("The index of params is out of range.");
111 return OH_NN_INVALID_PARAMETER;
112 }
113
114 return OH_NN_SUCCESS;
115 }
116
SetQuantType(const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)117 void OpsBuilder::SetQuantType(const std::vector<uint32_t>& outputsIndex,
118 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
119 {
120 if (allTensors[outputsIndex.front()]->IsQuantTensor()) {
121 m_quantType = OpsQuantType::QUANT_ALL;
122 }
123 }
124 } // namespace Ops
125 } // namespace NeuralNetworkRuntime
126 } // namespace OHOS