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 "add_builder.h"
17
18 #include "transform.h"
19 #include "validation.h"
20
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Ops {
24 static const int INPUT_NUM = 2;
25 static const int OUTPUT_NUM = 1;
26 static const int PARAM_MAX_NUM = 1;
27 static const std::string OP_NAME = "Add";
28
AddBuilder()29 AddBuilder::AddBuilder() {}
30
~AddBuilder()31 AddBuilder::~AddBuilder() {}
32
SetActivation(const std::shared_ptr<NNTensor> & tensor)33 OH_NN_ReturnCode AddBuilder::SetActivation(const std::shared_ptr<NNTensor>& tensor)
34 {
35 tensor->IdentifyOpParameter();
36
37 if (tensor->GetDataType() != OH_NN_INT8) {
38 LOGE("[Add] SetActivation failed, the activationType should be type OH_NN_INT8.");
39 return OH_NN_INVALID_PARAMETER;
40 }
41
42 void* buffer = tensor->GetBuffer();
43 if (buffer == nullptr) {
44 LOGE("[Add] SetActivation GetBuffer return nullptr.");
45 return OH_NN_INVALID_PARAMETER;
46 }
47 int8_t* fuseData = static_cast<int8_t*>(buffer);
48 if (!Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*fuseData))) {
49 LOGE("[Add] SetActivation failed, fuse activation type is invalid.");
50 return OH_NN_INVALID_PARAMETER;
51 }
52 m_activationType = NNToMS::TransfromFusionType(static_cast<OH_NN_FuseType>(*fuseData));
53
54 return OH_NN_SUCCESS;
55 }
56
Build(const std::vector<uint32_t> & paramsIndex,const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)57 OH_NN_ReturnCode AddBuilder::Build(const std::vector<uint32_t>& paramsIndex,
58 const std::vector<uint32_t>& inputsIndex,
59 const std::vector<uint32_t>& outputsIndex,
60 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
61 {
62 if (m_isBuild) {
63 LOGE("[Add] Build failed, operation has been build, cannot build again.");
64 return OH_NN_OPERATION_FORBIDDEN;
65 }
66
67 auto ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
68 if (ret != OH_NN_SUCCESS) {
69 LOGE("[Add] Build failed, the input or output index of Add operation is invalid.");
70 return ret;
71 }
72
73 m_inputsIndex = inputsIndex;
74 m_outputsIndex = outputsIndex;
75
76 ret = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
77 if (ret != OH_NN_SUCCESS) {
78 LOGE("[Add] Build failed, the param index of Add operation is invalid.");
79 return ret;
80 }
81
82 for (uint32_t i : paramsIndex) {
83 std::shared_ptr<NNTensor> tensor = allTensors[i];
84 if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
85 ret = (this->*(m_paramMap[tensor->GetType()]))(tensor);
86 } else {
87 LOGE("[Add] Build failed, param invalid, type=%d", tensor->GetType());
88 return OH_NN_INVALID_PARAMETER;
89 }
90
91 if (ret != OH_NN_SUCCESS) {
92 LOGE("[Add] Build failed, passed invalid param.");
93 return ret;
94 }
95 }
96
97 // The quantization type of the first output determinies that of the operator.
98 SetQuantType(outputsIndex, allTensors);
99
100 m_name = OP_NAME;
101 m_isBuild = true;
102 return OH_NN_SUCCESS;
103 }
104
GetPrimitive()105 LiteGraphPrimitvePtr AddBuilder::GetPrimitive()
106 {
107 if (!m_isBuild) {
108 LOGE("[Add] GetPrimitive failed, cannot get primitive before call build.");
109 return {nullptr, DestroyLiteGraphPrimitive};
110 }
111
112 void* primitive = mindspore::lite::MindIR_AddFusion_CreatePrimitive(m_activationType);
113 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive) ;
114 return graphPrimitivePtr;
115 }
116
117 REGISTER_OPS(AddBuilder, OH_NN_OPS_ADD);
118 } // namespace Ops
119 } // namespace NeuralNetworkRuntime
120 } // namespace OHOS
121