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 for (auto index : inputsIndex) {
74 if (index >= allTensors.size()) {
75 LOGE("The index of inputs is out of range.");
76 return OH_NN_INVALID_PARAMETER;
77 }
78 }
79
80 for (auto index : outputsIndex) {
81 if (index >= allTensors.size()) {
82 LOGE("The index of outputs is out of range.");
83 return OH_NN_INVALID_PARAMETER;
84 }
85 }
86
87 return OH_NN_SUCCESS;
88 }
89
SetQuantType(const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)90 void OpsBuilder::SetQuantType(const std::vector<uint32_t>& outputsIndex,
91 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
92 {
93 if (allTensors[outputsIndex.front()]->IsQuantTensor()) {
94 m_quantType = OpsQuantType::QUANT_ALL;
95 }
96 }
97 } // namespace Ops
98 } // namespace NeuralNetworkRuntime
99 } // namespace OHOS