• 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 "conv2d_transpose_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 constexpr int INPUT_NUM = 3;
26 static constexpr int OUTPUT_NUM = 1;
27 static constexpr int INPUT_WEIGHT = 1;
28 static constexpr int WEIGHT_SIZE = 4;
29 static constexpr int OUT_CHANNEL_INDEX = 0;
30 static constexpr int IN_CHANNEL_INDEX = 3;
31 static constexpr int KERNEL_HEIGHT_INDEX = 1;
32 static constexpr int KERNEL_WEIGHT_INDEX = 2;
33 static constexpr int PAD_MODE_PARAM_NUM = 1;
34 static constexpr int PAD_LIST_PARAM_NUM = 4;
35 static constexpr int SCALAR_LENGTH = 1;
36 static const std::string OP_NAME = "Conv2DTranspose";
37 
Conv2DTransposeBuilder()38 Conv2DTransposeBuilder::Conv2DTransposeBuilder() {}
39 
~Conv2DTransposeBuilder()40 Conv2DTransposeBuilder::~Conv2DTransposeBuilder() {}
41 
SetInput(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)42 OH_NN_ReturnCode Conv2DTransposeBuilder::SetInput(const std::vector<uint32_t>& inputsIndex,
43                                                   const std::vector<uint32_t>& outputsIndex,
44                                                   const std::vector<std::shared_ptr<NNTensor>>& allTensors)
45 {
46     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
47     if (returnCode != OH_NN_SUCCESS) {
48         LOGE("[Conv2dTranspose] SetInput failed, Passed invalid input or output index.");
49         return returnCode;
50     }
51 
52     m_inputsIndex = inputsIndex;
53     m_outputsIndex = outputsIndex;
54 
55     // set inChannel, outChannel, kernelSize
56     auto weightShape = allTensors[inputsIndex[INPUT_WEIGHT]]->GetDimensions();
57     if (weightShape.size() != WEIGHT_SIZE) {
58         LOGE("[Conv2dTranspose] SetInput failed, the dimension of weight should be %d", WEIGHT_SIZE);
59         return OH_NN_INVALID_PARAMETER;
60     }
61 
62     m_inChannel = weightShape[IN_CHANNEL_INDEX];
63     m_outChannel = weightShape[OUT_CHANNEL_INDEX];
64 
65     return OH_NN_SUCCESS;
66 }
67 
SetKernelSize(const std::vector<uint32_t> & inputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)68 void Conv2DTransposeBuilder::SetKernelSize(const std::vector<uint32_t>& inputsIndex,
69                                            const std::vector<std::shared_ptr<NNTensor>>& allTensors)
70 {
71     auto weightShape = allTensors[inputsIndex[INPUT_WEIGHT]]->GetDimensions();
72 
73     m_kernelSize.clear();
74     m_kernelSize.emplace_back(weightShape[KERNEL_HEIGHT_INDEX]);
75     m_kernelSize.emplace_back(weightShape[KERNEL_WEIGHT_INDEX]);
76 }
77 
SetStrides(std::shared_ptr<NNTensor> tensor)78 OH_NN_ReturnCode Conv2DTransposeBuilder::SetStrides(std::shared_ptr<NNTensor> tensor)
79 {
80     tensor->IdentifyOpParameter();
81     // Set Strides
82     if (tensor->GetDataType() != OH_NN_INT64) {
83         LOGE("[Conv2DTranspose] SetStrides failed, the Strides should be type OH_NN_INT64.");
84         return OH_NN_INVALID_PARAMETER;
85     }
86 
87     void* buffer = tensor->GetBuffer();
88     if (buffer == nullptr) {
89         LOGE("[Conv2DTranspose] SetStrides GetBuffer return nullptr");
90         return OH_NN_INVALID_PARAMETER;
91     }
92     const int64_t* pStrides = reinterpret_cast<const int64_t*>(buffer);
93     uint32_t elementSize = tensor->GetElementCount();
94     m_strides.assign(pStrides, pStrides + elementSize);
95 
96     return OH_NN_SUCCESS;
97 }
98 
SetDilation(std::shared_ptr<NNTensor> tensor)99 OH_NN_ReturnCode Conv2DTransposeBuilder::SetDilation(std::shared_ptr<NNTensor> tensor)
100 {
101     tensor->IdentifyOpParameter();
102     // Set Dilation
103     if (tensor->GetDataType() != OH_NN_INT64) {
104         LOGE("[Conv2DTranspose] SetDilation failed, the Dilation should be type OH_NN_INT64");
105         return OH_NN_INVALID_PARAMETER;
106     }
107 
108     void* buffer = tensor->GetBuffer();
109     if (buffer == nullptr) {
110         LOGE("[Conv2DTranspose] SetDilation GetBuffer return nullptr");
111         return OH_NN_INVALID_PARAMETER;
112     }
113     const int64_t* pDilation = reinterpret_cast<const int64_t*>(buffer);
114     uint32_t dilationSize = tensor->GetElementCount();
115     m_dilation.assign(pDilation, pDilation + dilationSize);
116 
117     return OH_NN_SUCCESS;
118 }
119 
SetPad(std::shared_ptr<NNTensor> tensor)120 OH_NN_ReturnCode Conv2DTransposeBuilder::SetPad(std::shared_ptr<NNTensor> tensor)
121 {
122     tensor->IdentifyOpParameter();
123 
124     bool isPadMode = false;
125     if (tensor->GetElementCount() == PAD_MODE_PARAM_NUM) {
126         isPadMode = true;
127     } else if (tensor->GetElementCount() != PAD_LIST_PARAM_NUM) {
128         LOGE("[Conv2DTranspose] SetPad failed, the inputs should be 1 if using padMode or 4 if using padList.");
129         return OH_NN_INVALID_PARAMETER;
130     }
131 
132     void* buffer = tensor->GetBuffer();
133     if (buffer == nullptr) {
134         LOGE("[Conv2DTranspose] SetPadMode GetBuffer return nullptr");
135         return OH_NN_INVALID_PARAMETER;
136     }
137 
138     // Set PadMode or PadList
139     if (isPadMode) {
140         if (tensor->GetDataType() != OH_NN_INT8) {
141             LOGE("[Conv2DTranspose] SetPad failed, the PadMode should have type OH_NN_INT8.");
142             return OH_NN_INVALID_PARAMETER;
143         }
144 
145         int8_t* pPad = static_cast<int8_t*>(buffer);
146         if (!OHOS::NeuralNetworkRuntime::Validation::ValidatePadMode(*pPad)) {
147             LOGE("[Conv2DTranspose] SetPad failed, invalid pad mode.");
148             return OH_NN_INVALID_PARAMETER;
149         }
150         m_padMode = NNToMS::TransformPadModeValue(*pPad);
151     } else {
152         if (tensor->GetDataType() != OH_NN_INT64) {
153             LOGE("[Conv2DTranspose] SetPad failed, the PadList should have type OH_NN_INT64.");
154             return OH_NN_INVALID_PARAMETER;
155         }
156 
157         const int64_t* pPadList = reinterpret_cast<const int64_t*>(buffer);
158         uint32_t padListPadSize = tensor->GetElementCount();
159         m_padList.assign(pPadList, pPadList + padListPadSize);
160     }
161 
162     return OH_NN_SUCCESS;
163 }
164 
SetGroup(std::shared_ptr<NNTensor> tensor)165 OH_NN_ReturnCode Conv2DTransposeBuilder::SetGroup(std::shared_ptr<NNTensor> tensor)
166 {
167     tensor->IdentifyOpParameter();
168     // Set Group
169     if (tensor->GetElementCount() != SCALAR_LENGTH) {
170         LOGE("[Conv2dTranspose] SetGroup failed, the Group shoule be a scalar");
171         return OH_NN_INVALID_PARAMETER;
172     }
173 
174     if (tensor->GetDataType() != OH_NN_INT64) {
175         LOGE("[Conv2dTranspose] SetGroup failed, the Group should have type OH_NN_INT64.");
176         return OH_NN_INVALID_PARAMETER;
177     }
178 
179     void* buffer = tensor->GetBuffer();
180     if (buffer == nullptr) {
181         LOGE("[Conv2DTranspose] SetGroup GetBuffer return nullptr");
182         return OH_NN_INVALID_PARAMETER;
183     }
184     m_group = *reinterpret_cast<const int64_t*>(buffer);
185 
186     return OH_NN_SUCCESS;
187 }
188 
SetOutPadding(std::shared_ptr<NNTensor> tensor)189 OH_NN_ReturnCode Conv2DTransposeBuilder::SetOutPadding(std::shared_ptr<NNTensor> tensor)
190 {
191     tensor->IdentifyOpParameter();
192     // Set outputPadding
193     if (tensor->GetDataType() != OH_NN_INT64) {
194         LOGE("[Conv2DTranspose] SetOutPadding failed, the outputPadding should be type OH_NN_INT64.");
195         return OH_NN_INVALID_PARAMETER;
196     }
197 
198     void* buffer = tensor->GetBuffer();
199     if (buffer == nullptr) {
200         LOGE("[Conv2DTranspose] SetOutPadding GetBuffer return nullptr");
201         return OH_NN_INVALID_PARAMETER;
202     }
203     const int64_t* pOutputPadding = reinterpret_cast<const int64_t*>(buffer);
204     uint32_t outputPadSize = tensor->GetElementCount();
205     m_outputPaddings.assign(pOutputPadding, pOutputPadding + outputPadSize);
206 
207     return OH_NN_SUCCESS;
208 }
209 
SetActivation(std::shared_ptr<NNTensor> tensor)210 OH_NN_ReturnCode Conv2DTransposeBuilder::SetActivation(std::shared_ptr<NNTensor> tensor)
211 {
212     tensor->IdentifyOpParameter();
213 
214     if (tensor->GetElementCount() != SCALAR_LENGTH) {
215         LOGE("[Conv2DTranspose] SetActivation failed, the ActivationType shoule be a scalar");
216         return OH_NN_INVALID_PARAMETER;
217     }
218 
219     if (tensor->GetDataType() != OH_NN_INT8) {
220         LOGE("[Conv2DTranspose] SetActivation failed, the ActivationType should have type OH_NN_INT8.");
221         return OH_NN_INVALID_PARAMETER;
222     }
223 
224     void* buffer = tensor->GetBuffer();
225     if (buffer == nullptr) {
226         LOGE("[Conv2DTranspose] SetOutPadding GetBuffer return nullptr");
227         return OH_NN_INVALID_PARAMETER;
228     }
229     int8_t* pFuseData = static_cast<int8_t*>(buffer);
230     if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*pFuseData))) {
231         LOGE("[Conv2DTranspose] SetActivation failed, activation input is invalid.");
232         return OH_NN_INVALID_PARAMETER;
233     }
234     m_activationType = NNToMS::TransfromFusionType((OH_NN_FuseType)(*pFuseData));
235 
236     return OH_NN_SUCCESS;
237 }
238 
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)239 OH_NN_ReturnCode Conv2DTransposeBuilder::Build(const std::vector<uint32_t>& paramsIndex,
240                                                const std::vector<uint32_t>& inputsIndex,
241                                                const std::vector<uint32_t>& outputsIndex,
242                                                const std::vector<std::shared_ptr<NNTensor>>& allTensors)
243 {
244     if (m_isBuild) {
245         LOGE("[Conv2DTranspose] Build failed, conv2DTranspose operation has been build, cannot build again.");
246         return OH_NN_OPERATION_FORBIDDEN;
247     }
248 
249     OH_NN_ReturnCode returnCode = SetInput(inputsIndex, outputsIndex, allTensors);
250     if (returnCode != OH_NN_SUCCESS) {
251         return returnCode;
252     }
253 
254     SetKernelSize(inputsIndex, allTensors);
255 
256     for (int i : paramsIndex) {
257         std::shared_ptr<NNTensor> tensor =  allTensors[i]; // 参数 tensor
258         switch (tensor->GetType()) {
259             case OH_NN_CONV2D_TRANSPOSE_STRIDES:
260                 returnCode = SetStrides(tensor);
261                 break;
262             case OH_NN_CONV2D_TRANSPOSE_DILATION:
263                 returnCode = SetDilation(tensor);
264                 break;
265             case OH_NN_CONV2D_TRANSPOSE_PAD_MODE:
266             case OH_NN_CONV2D_TRANSPOSE_PAD:
267                 returnCode = SetPad(tensor);
268                 break;
269             case OH_NN_CONV2D_TRANSPOSE_GROUP:
270                 returnCode = SetGroup(tensor);
271                 break;
272             case OH_NN_CONV2D_TRANSPOSE_OUTPUT_PADDINGS:
273                 returnCode = SetOutPadding(tensor);
274                 break;
275             case OH_NN_CONV2D_TRANSPOSE_ACTIVATION_TYPE:
276                 returnCode = SetActivation(tensor);
277                 break;
278             default:
279                 LOGE("[Conv2DTranspose] Build failed, param invalid, type = %d.", tensor->GetType());
280                 return OH_NN_INVALID_PARAMETER;
281         }
282 
283         if (returnCode != OH_NN_SUCCESS) {
284             LOGE("[Conv2DTranspose] Build failed, passed invalid param.");
285             return returnCode;
286         }
287     }
288 
289     // The quantization type of the first output determinies that of the operator.
290     SetQuantType(outputsIndex, allTensors);
291 
292     m_isBuild = true;
293     m_name = OP_NAME;
294     return OH_NN_SUCCESS;
295 }
296 
GetPrimitive()297 LiteGraphPrimitvePtr Conv2DTransposeBuilder::GetPrimitive()
298 {
299     if (!m_isBuild) {
300         LOGE("[Conv2DTranspose] GetPrimitive failed, cannot get primitive before call build.");
301         return {nullptr, DestroyLiteGraphPrimitive};
302     }
303 
304     void* primitive = MindIR_Conv2dTransposeFusion_CreatePrimitive(m_kernelSize,
305         m_strides, m_dilation, m_padMode, m_padList, m_group, m_inChannel, m_outChannel,
306         m_activationType, m_outputPaddings);
307     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
308     return graphPrimitivePtr;
309 }
310 
311 REGISTER_OPS(Conv2DTransposeBuilder, OH_NN_OPS_CONV2D_TRANSPOSE);
312 } // namespace Ops
313 } // namespace NeuralNetworkRuntime
314 } // namespace OHOS
315