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 "pooling_builder.h"
17
18 #include "frameworks/native/transform.h"
19 #include "frameworks/native/validation.h"
20
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Ops {
24 static const int INPUT_NUM = 1;
25 static const int OUTPUT_NUM = 1;
26 static const int NUM_ELEMENT_PAD_MODE = 1;
27 static const int NUM_ELEMENT_PAD_LIST = 4;
28 static const int ACTIVATION_LENGTH = 1;
29
PoolingBuild(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)30 OH_NN_ReturnCode PoolingBuilder::PoolingBuild(const std::vector<uint32_t>& paramsIndex,
31 const std::vector<uint32_t>& inputsIndex,
32 const std::vector<uint32_t>& outputsIndex,
33 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
34 {
35 if (m_isBuild) {
36 LOGE("[PoolingBuilder] PoolingBuild failed, operation has been build, cannot build again.");
37 return OH_NN_OPERATION_FORBIDDEN;
38 }
39
40 // Set input and output
41 OH_NN_ReturnCode returnCode = SetInputAndOutput(inputsIndex, outputsIndex, allTensors);
42 if (returnCode != OH_NN_SUCCESS) {
43 LOGE("[PoolingBuilder] PoolingBuild failed, the SetInputAndOutput failed.");
44 return returnCode;
45 }
46
47 for (int i : paramsIndex) {
48 std::shared_ptr<NNTensor> tensor = allTensors[i];
49 switch (tensor->GetType()) {
50 case OH_NN_AVG_POOL_KERNEL_SIZE:
51 case OH_NN_MAX_POOL_KERNEL_SIZE:
52 returnCode = SetKernel(tensor);
53 break;
54 case OH_NN_AVG_POOL_STRIDE:
55 case OH_NN_MAX_POOL_STRIDE:
56 returnCode = SetStrides(tensor);
57 break;
58 case OH_NN_AVG_POOL_PAD_MODE:
59 case OH_NN_MAX_POOL_PAD_MODE:
60 case OH_NN_MAX_POOL_PAD:
61 case OH_NN_AVG_POOL_PAD:
62 returnCode = SetPadModeOrPaddings(tensor);
63 break;
64 case OH_NN_AVG_POOL_ACTIVATION_TYPE:
65 case OH_NN_MAX_POOL_ACTIVATION_TYPE:
66 returnCode = SetActivation(tensor);
67 break;
68 default:
69 LOGE("[PoolingBuilder] Build failed, param invalid, type = %d.", tensor->GetType());
70 return OH_NN_INVALID_PARAMETER;
71 }
72 if (returnCode != OH_NN_SUCCESS) {
73 LOGE("[PoolingBuilder] PoolingBuild failed, passed invalid param.");
74 return returnCode;
75 }
76 }
77
78 // The quantization type of the first output determinies that of the operator.
79 SetQuantType(outputsIndex, allTensors);
80
81 return OH_NN_SUCCESS;
82 }
83
SetInputAndOutput(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)84 OH_NN_ReturnCode PoolingBuilder::SetInputAndOutput(const std::vector<uint32_t>& inputsIndex,
85 const std::vector<uint32_t>& outputsIndex,
86 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
87 {
88 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
89 if (returnCode != OH_NN_SUCCESS) {
90 LOGE("[PoolingBuilder] SetInputAndOutput failed, passed invalid input or output index.");
91 return returnCode;
92 }
93
94 m_inputsIndex = inputsIndex;
95 m_outputsIndex = outputsIndex;
96
97 return OH_NN_SUCCESS;
98 }
99
SetKernel(std::shared_ptr<NNTensor> tensor)100 OH_NN_ReturnCode PoolingBuilder::SetKernel(std::shared_ptr<NNTensor> tensor)
101 {
102 tensor->IdentifyOpParameter();
103 // Set kernelSize
104 if (tensor->GetDataType() != OH_NN_INT64) {
105 LOGE("[PoolingBuilder] SetKernel failed, the KernelSize should be type OH_NN_INT64.");
106 return OH_NN_INVALID_PARAMETER;
107 }
108
109 void* buffer = tensor->GetBuffer();
110 if (buffer == nullptr) {
111 LOGE("[PoolingBuilder] SetKernel GetBuffer return nullptr");
112 return OH_NN_INVALID_PARAMETER;
113 }
114
115 const int64_t* pKernelSize = reinterpret_cast<const int64_t*>(buffer);
116 int kernelSize = tensor->GetElementCount();
117 m_kernelSize.assign(pKernelSize, pKernelSize + kernelSize);
118
119 return OH_NN_SUCCESS;
120 }
121
SetStrides(std::shared_ptr<NNTensor> tensor)122 OH_NN_ReturnCode PoolingBuilder::SetStrides(std::shared_ptr<NNTensor> tensor)
123 {
124 tensor->IdentifyOpParameter();
125 // Set Strides
126 if (tensor->GetDataType() != OH_NN_INT64) {
127 LOGE("[PoolingBuilder] SetStrides failed, the Strides should be type OH_NN_INT64.");
128 return OH_NN_INVALID_PARAMETER;
129 }
130
131 void* buffer = tensor->GetBuffer();
132 if (buffer == nullptr) {
133 LOGE("[PoolingBuilder] SetStrides GetBuffer return nullptr");
134 return OH_NN_INVALID_PARAMETER;
135 }
136
137 const int64_t* pStrides = reinterpret_cast<const int64_t*>(buffer);
138 int strideslSize = tensor->GetElementCount();
139 m_strides.assign(pStrides, pStrides + strideslSize);
140
141 return OH_NN_SUCCESS;
142 }
143
SetPadModeOrPaddings(std::shared_ptr<NNTensor> tensor)144 OH_NN_ReturnCode PoolingBuilder::SetPadModeOrPaddings(std::shared_ptr<NNTensor> tensor)
145 {
146 tensor->IdentifyOpParameter();
147
148 void* buffer = tensor->GetBuffer();
149 if (buffer == nullptr) {
150 LOGE("[PoolingBuilder] SetPadModeOrPaddings GetBuffer return nullptr");
151 return OH_NN_INVALID_PARAMETER;
152 }
153 size_t tensorElementCount = tensor->GetElementCount();
154 // Set PadMode or PadList
155 if (tensorElementCount == NUM_ELEMENT_PAD_MODE) {
156 // PadMode
157 if (tensor->GetDataType() != OH_NN_INT8) {
158 LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, the type of padMode should be OH_NN_INT8.");
159 return OH_NN_INVALID_PARAMETER;
160 }
161
162 int8_t* pPadMode = static_cast<int8_t*>(buffer);
163 if (!OHOS::NeuralNetworkRuntime::Validation::ValidatePadMode(*pPadMode)) {
164 LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, invalid pad mode.");
165 return OH_NN_INVALID_PARAMETER;
166 }
167 m_padMode = NNToMS::TransformPadModeValue(*pPadMode);
168 } else if (tensorElementCount == NUM_ELEMENT_PAD_LIST) {
169 if (tensor->GetDataType() != OH_NN_INT64) {
170 LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, the type of padList should be OH_NN_INT64.");
171 return OH_NN_INVALID_PARAMETER;
172 }
173
174 int64_t* pPad = static_cast<int64_t*>(buffer);
175 // PadList
176 m_pad.clear();
177 for (int i = 0; i < NUM_ELEMENT_PAD_LIST; i++) {
178 m_pad.emplace_back(static_cast<int64_t>(pPad[i]));
179 }
180 } else {
181 LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, invalid element size of padMode or padList,"
182 "padMode should be single value, and padList should be 4.");
183 return OH_NN_INVALID_PARAMETER;
184 }
185 return OH_NN_SUCCESS;
186 }
187
SetActivation(std::shared_ptr<NNTensor> tensor)188 OH_NN_ReturnCode PoolingBuilder::SetActivation(std::shared_ptr<NNTensor> tensor)
189 {
190 tensor->IdentifyOpParameter();
191 // Set ActivationType
192 if (tensor->GetElementCount() != ACTIVATION_LENGTH) {
193 LOGE("[PoolingBuilder] SetActivation failed, the Activation shoule be a scalar");
194 return OH_NN_INVALID_PARAMETER;
195 }
196
197 if (tensor->GetDataType() != OH_NN_INT8) {
198 LOGE("[PoolingBuilder] SetActivation failed, the ActivationType should be type OH_NN_INT8.");
199 return OH_NN_INVALID_PARAMETER;
200 }
201
202 void* buffer = tensor->GetBuffer();
203 if (buffer == nullptr) {
204 LOGE("[PoolingBuilder] SetActivation GetBuffer return nullptr");
205 return OH_NN_INVALID_PARAMETER;
206 }
207
208 int8_t* pFuseData = static_cast<int8_t*>(buffer);
209 if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*pFuseData))) {
210 LOGE("[PoolingBuilder] SetActivation failed, activation input is invalid.");
211 return OH_NN_INVALID_PARAMETER;
212 }
213 auto fuseType = (OH_NN_FuseType)(*pFuseData);
214 m_activationType = NNToMS::TransfromFusionType(fuseType);
215
216 return OH_NN_SUCCESS;
217 }
218 } // namespace Ops
219 } // namespace NeuralNetworkRuntime
220 } // namespace OHOS
221