• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "transform.h"
19 #include "validation.h"
20 #include "ops_validation.h"
21 
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24 namespace Ops {
25 static const int INPUT_NUM = 1;
26 static const int OUTPUT_NUM = 1;
27 static const int PARAM_MAX_NUM = 7;
28 static const int SCALAR_LENGTH = 1;
29 static const int NUM_ELEMENT_PAD_MODE = 1;
30 static const int NUM_ELEMENT_PAD_LIST = 4;
31 static const int ACTIVATION_LENGTH = 1;
32 static const std::unordered_map<int, mindspore::lite::RoundMode> roundList = {{0, mindspore::lite::ROUND_MODE_FLOOR},
33                                                                               {1, mindspore::lite::ROUND_MODE_CEIL}};
34 
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)35 OH_NN_ReturnCode PoolingBuilder::PoolingBuild(const std::vector<uint32_t>& paramsIndex,
36                                               const std::vector<uint32_t>& inputsIndex,
37                                               const std::vector<uint32_t>& outputsIndex,
38                                               const std::vector<std::shared_ptr<NNTensor>>& allTensors)
39 {
40     if (m_isBuild) {
41         LOGE("[PoolingBuilder] PoolingBuild failed, operation has been build, cannot build again.");
42         return OH_NN_OPERATION_FORBIDDEN;
43     }
44 
45     // Set input and output
46     OH_NN_ReturnCode returnCode = SetInputAndOutput(inputsIndex, outputsIndex, allTensors);
47     if (returnCode != OH_NN_SUCCESS) {
48         LOGE("[PoolingBuilder] PoolingBuild failed, the SetInputAndOutput failed.");
49         return returnCode;
50     }
51 
52     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
53     if (returnCode != OH_NN_SUCCESS) {
54         LOGE("[PoolingBuilder] PoolingBuild failed, passed invalid param index of Onehot operation index.");
55         return returnCode;
56     }
57 
58     for (int i : paramsIndex) {
59         std::shared_ptr<NNTensor> tensor = allTensors[i];
60         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
61             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
62         } else {
63             LOGE("[PoolingBuilder] Build failed, param invalid, type=%d", tensor->GetType());
64             return OH_NN_INVALID_PARAMETER;
65         }
66 
67         if (returnCode != OH_NN_SUCCESS) {
68             LOGE("[PoolingBuilder] PoolingBuild failed, passed invalid param.");
69             return returnCode;
70         }
71     }
72 
73     // The quantization type of the first output determinies that of the operator.
74     SetQuantType(outputsIndex, allTensors);
75 
76     return OH_NN_SUCCESS;
77 }
78 
SetInputAndOutput(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)79 OH_NN_ReturnCode PoolingBuilder::SetInputAndOutput(const std::vector<uint32_t>& inputsIndex,
80                                                    const std::vector<uint32_t>& outputsIndex,
81                                                    const std::vector<std::shared_ptr<NNTensor>>& allTensors)
82 {
83     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
84     if (returnCode != OH_NN_SUCCESS) {
85         LOGE("[PoolingBuilder] SetInputAndOutput failed, passed invalid input or output index.");
86         return returnCode;
87     }
88 
89     m_inputsIndex = inputsIndex;
90     m_outputsIndex = outputsIndex;
91 
92     return OH_NN_SUCCESS;
93 }
94 
SetKernel(const std::shared_ptr<NNTensor> & tensor)95 OH_NN_ReturnCode PoolingBuilder::SetKernel(const std::shared_ptr<NNTensor>& tensor)
96 {
97     tensor->IdentifyOpParameter();
98     // Set kernelSize
99     if (tensor->GetDataType() != OH_NN_INT64) {
100         LOGE("[PoolingBuilder] SetKernel failed, the KernelSize should be type OH_NN_INT64.");
101         return OH_NN_INVALID_PARAMETER;
102     }
103 
104     void* buffer = tensor->GetBuffer();
105     if (buffer == nullptr) {
106         LOGE("[PoolingBuilder] SetKernel GetBuffer return nullptr");
107         return OH_NN_INVALID_PARAMETER;
108     }
109 
110     const int64_t* pKernelSize = reinterpret_cast<const int64_t*>(buffer);
111     uint32_t kernelSize = tensor->GetElementCount();
112     m_kernelSize.assign(pKernelSize, pKernelSize + kernelSize);
113 
114     return OH_NN_SUCCESS;
115 }
116 
SetStrides(const std::shared_ptr<NNTensor> & tensor)117 OH_NN_ReturnCode PoolingBuilder::SetStrides(const std::shared_ptr<NNTensor>& tensor)
118 {
119     tensor->IdentifyOpParameter();
120     // Set Strides
121     if (tensor->GetDataType() != OH_NN_INT64) {
122         LOGE("[PoolingBuilder] SetStrides failed, the Strides should be type OH_NN_INT64.");
123         return OH_NN_INVALID_PARAMETER;
124     }
125 
126     void* buffer = tensor->GetBuffer();
127     if (buffer == nullptr) {
128         LOGE("[PoolingBuilder] SetStrides GetBuffer return nullptr");
129         return OH_NN_INVALID_PARAMETER;
130     }
131 
132     const int64_t* pStrides = reinterpret_cast<const int64_t*>(buffer);
133     uint32_t strideslSize = tensor->GetElementCount();
134     m_strides.assign(pStrides, pStrides + strideslSize);
135 
136     return OH_NN_SUCCESS;
137 }
138 
SetPadModeOrPaddings(const std::shared_ptr<NNTensor> & tensor)139 OH_NN_ReturnCode PoolingBuilder::SetPadModeOrPaddings(const std::shared_ptr<NNTensor>& tensor)
140 {
141     tensor->IdentifyOpParameter();
142 
143     void* buffer = tensor->GetBuffer();
144     if (buffer == nullptr) {
145         LOGE("[PoolingBuilder] SetPadModeOrPaddings GetBuffer return nullptr");
146         return OH_NN_INVALID_PARAMETER;
147     }
148     size_t tensorElementCount = tensor->GetElementCount();
149     // Set PadMode or PadList
150     if (tensorElementCount == NUM_ELEMENT_PAD_MODE) {
151         // PadMode
152         if (tensor->GetDataType() != OH_NN_INT8) {
153             LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, the type of padMode should be OH_NN_INT8.");
154             return OH_NN_INVALID_PARAMETER;
155         }
156 
157         int8_t* pPadMode = static_cast<int8_t*>(buffer);
158         if (!OHOS::NeuralNetworkRuntime::Validation::ValidatePadMode(*pPadMode)) {
159             LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, invalid pad mode.");
160             return OH_NN_INVALID_PARAMETER;
161         }
162         m_padMode = NNToMS::TransformPadModeValue(*pPadMode);
163     } else if (tensorElementCount == NUM_ELEMENT_PAD_LIST) {
164         if (tensor->GetDataType() != OH_NN_INT64) {
165             LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, the type of padList should be OH_NN_INT64.");
166             return OH_NN_INVALID_PARAMETER;
167         }
168 
169         int64_t* pPad = static_cast<int64_t*>(buffer);
170         // PadList
171         m_pad.clear();
172         for (int i = 0; i < NUM_ELEMENT_PAD_LIST; i++) {
173             m_pad.emplace_back(static_cast<int64_t>(pPad[i]));
174         }
175     } else {
176         LOGE("[PoolingBuilder] SetPadModeOrPaddings failed, invalid element size of padMode or padList,"
177             "padMode should be single value, and padList should be 4.");
178         return OH_NN_INVALID_PARAMETER;
179     }
180     return OH_NN_SUCCESS;
181 }
182 
SetRoundMode(const std::shared_ptr<NNTensor> & tensor)183 OH_NN_ReturnCode PoolingBuilder::SetRoundMode(const std::shared_ptr<NNTensor>& tensor)
184 {
185     tensor->IdentifyOpParameter();
186 
187     if (tensor->GetElementCount() != ACTIVATION_LENGTH) {
188         LOGE("[PoolingBuilder] SetRoundMode failed, the roundMode shoule be a scalar");
189         return OH_NN_INVALID_PARAMETER;
190     }
191 
192     if (tensor->GetDataType() != OH_NN_INT32) {
193         LOGE("[PoolingBuilder] SetRoundMode failed, the roundMode should be type OH_NN_INT32.");
194         return OH_NN_INVALID_PARAMETER;
195     }
196 
197     void* buffer = tensor->GetBuffer();
198     if (buffer == nullptr) {
199         LOGE("[PoolingBuilder] SetRoundMode GetBuffer return nullptr");
200         return OH_NN_INVALID_PARAMETER;
201     }
202 
203     int roundModeKey = *(static_cast<int*>(buffer));
204     auto it = roundList.find(roundModeKey);
205     if (it != roundList.end()) {
206         m_roundMode = it->second;
207     } else {
208         LOGE("[PoolingBuilder] The roundMode value should between [0, 1], but get %d.", roundModeKey);
209         LOGE("[PoolingBuilder] roundMode: 0-OH_NN_ROUND_FLOOR, 1-OH_NN_ROUND_CEIL");
210         return OH_NN_INVALID_PARAMETER;
211     }
212 
213     return OH_NN_SUCCESS;
214 }
215 
SetActivation(const std::shared_ptr<NNTensor> & tensor)216 OH_NN_ReturnCode PoolingBuilder::SetActivation(const std::shared_ptr<NNTensor>& tensor)
217 {
218     tensor->IdentifyOpParameter();
219 
220     if (tensor->GetElementCount() != ACTIVATION_LENGTH) {
221         LOGE("[PoolingBuilder] SetActivation failed, the Activation shoule be a scalar");
222         return OH_NN_INVALID_PARAMETER;
223     }
224 
225     if (tensor->GetDataType() != OH_NN_INT8) {
226         LOGE("[PoolingBuilder] SetActivation failed, the ActivationType should be type OH_NN_INT8.");
227         return OH_NN_INVALID_PARAMETER;
228     }
229 
230     void* buffer = tensor->GetBuffer();
231     if (buffer == nullptr) {
232         LOGE("[PoolingBuilder] SetActivation GetBuffer return nullptr");
233         return OH_NN_INVALID_PARAMETER;
234     }
235 
236     int8_t* pFuseData = static_cast<int8_t*>(buffer);
237     if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*pFuseData))) {
238         LOGE("[PoolingBuilder] SetActivation failed, activation input is invalid.");
239         return OH_NN_INVALID_PARAMETER;
240     }
241     auto fuseType = (OH_NN_FuseType)(*pFuseData);
242     m_activationType = NNToMS::TransfromFusionType(fuseType);
243 
244     return OH_NN_SUCCESS;
245 }
246 
SetGlobal(const std::shared_ptr<NNTensor> & tensor)247 OH_NN_ReturnCode PoolingBuilder::SetGlobal(const std::shared_ptr<NNTensor>& tensor)
248 {
249     if (tensor->GetDataType() != OH_NN_BOOL) {
250         LOGE("[PoolingBuilder] The global should be type OH_NN_BOOL.");
251         return OH_NN_INVALID_PARAMETER;
252     }
253 
254     if (tensor->GetElementCount() != SCALAR_LENGTH) {
255         LOGE("[PoolingBuilder] The global should be scalar.");
256         return OH_NN_INVALID_PARAMETER;
257     }
258 
259     void* buffer = tensor->GetBuffer();
260     if (buffer == nullptr) {
261         LOGE("[PoolingBuilder] Tensor buffer is nullptr.");
262         return OH_NN_INVALID_PARAMETER;
263     }
264     m_global = *(static_cast<bool*>(buffer));
265 
266     return OH_NN_SUCCESS;
267 }
268 } // namespace Ops
269 } // namespace NeuralNetworkRuntime
270 } // namespace OHOS
271