1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "Operations"
18
19 #include <tensorflow/lite/kernels/internal/common.h>
20
21 #include <algorithm>
22 #include <cfloat>
23 #include <cmath>
24 #include <memory>
25 #include <vector>
26
27 #include "CpuOperationUtils.h"
28 #include "HalInterfaces.h"
29 #include "OperationResolver.h"
30 #include "Tracing.h"
31
32 namespace android {
33 namespace nn {
34 namespace transpose_conv_2d {
35
36 constexpr char kOperationName[] = "TRANSPOSE_CONV_2D";
37
38 constexpr uint32_t kInputTensor = 0;
39 constexpr uint32_t kFilterTensor = 1;
40 constexpr uint32_t kBiasTensor = 2;
41
42 constexpr uint32_t kNumInputs1 = 9;
43 constexpr uint32_t kNumInputs2 = 11;
44 constexpr uint32_t kNumOutputs = 1;
45 constexpr uint32_t kOutputTensor = 0;
46
47 namespace {
48
49 using namespace hal;
50
51 // If possible we will use this static buffer for the tensor.
52 constexpr size_t kStaticBufferSize = 1605632;
53 char static_scratch_buffer[kStaticBufferSize];
54
55 // executionMutex is used to protect concurrent access of the static_scratch_buffer.
56 // std::mutex is safe for pthreads on Android.
57 std::mutex executionMutex;
58
59 struct TransposeConv2dParam {
60 int32_t paddingLeft, paddingRight;
61 int32_t paddingTop, paddingBottom;
62 int32_t strideWidth, strideHeight;
63 int32_t activation;
64 bool useNchw = false;
65
initializeandroid::nn::transpose_conv_2d::__anond5f20d490111::TransposeConv2dParam66 bool initialize(const IOperationExecutionContext* context) {
67 uint32_t inCount = context->getNumInputs();
68 int32_t paddingImplicit = 0;
69 if (inCount == 9) {
70 paddingImplicit = context->getInputValue<int32_t>(4);
71 strideWidth = context->getInputValue<int32_t>(5);
72 strideHeight = context->getInputValue<int32_t>(6);
73 activation = context->getInputValue<int32_t>(7);
74 useNchw = context->getInputValue<bool>(8);
75 Shape filterShape = context->getInputShape(kFilterTensor);
76 int32_t filterWidth = getSizeOfDimension(filterShape, 2);
77 int32_t filterHeight = getSizeOfDimension(filterShape, 1);
78 NN_RET_CHECK_EQ(getNumberOfDimensions(context->getInputShape(3)), 1);
79 NN_RET_CHECK_EQ(getSizeOfDimension(context->getInputShape(3), 0), 4);
80 const int32_t* outputShapeData = context->getInputBuffer<int32_t>(3);
81 int32_t outputWidth = useNchw ? outputShapeData[3] : outputShapeData[2];
82 int32_t outputHeight = useNchw ? outputShapeData[2] : outputShapeData[1];
83 calculateExplicitPaddingTransposeConv(outputWidth, strideWidth, filterWidth,
84 paddingImplicit, &paddingLeft, &paddingRight);
85 calculateExplicitPaddingTransposeConv(outputHeight, strideHeight, filterHeight,
86 paddingImplicit, &paddingTop, &paddingBottom);
87 } else if (inCount == 11) {
88 paddingLeft = context->getInputValue<int32_t>(3);
89 paddingRight = context->getInputValue<int32_t>(4);
90 paddingTop = context->getInputValue<int32_t>(5);
91 paddingBottom = context->getInputValue<int32_t>(6);
92 strideWidth = context->getInputValue<int32_t>(7);
93 strideHeight = context->getInputValue<int32_t>(8);
94 activation = context->getInputValue<int32_t>(9);
95 useNchw = context->getInputValue<bool>(10);
96 } else {
97 NN_RET_CHECK_FAIL() << "Unsupported input spec for operation " << kOperationName;
98 }
99 // paddingRight and paddingBottom in transpose conv may be less than 0 to resolve the
100 // ambiguous output shape issue in the case of stride > 1.
101 NN_RET_CHECK_GE(paddingLeft, 0);
102 NN_RET_CHECK_GE(paddingTop, 0);
103 NN_RET_CHECK_GT(strideWidth, 0);
104 NN_RET_CHECK_GT(strideHeight, 0);
105 NN_RET_CHECK_GE(activation, 0);
106 return true;
107 }
108 };
109
110 #define ANDROID_NN_TRANSPOSE_CONV_PARAMETERS \
111 uint32_t numBatches = getSizeOfDimension(inputShape, 0); \
112 uint32_t inputHeight = getSizeOfDimension(inputShape, 1); \
113 uint32_t inputWidth = getSizeOfDimension(inputShape, 2); \
114 uint32_t inputDepth = getSizeOfDimension(inputShape, 3); \
115 uint32_t filterHeight = getSizeOfDimension(filterShape, 1); \
116 uint32_t filterWidth = getSizeOfDimension(filterShape, 2); \
117 uint32_t outputHeight = getSizeOfDimension(outputShape, 1); \
118 uint32_t outputWidth = getSizeOfDimension(outputShape, 2); \
119 uint32_t outputDepth = getSizeOfDimension(outputShape, 3); \
120 int32_t paddingLeft = param.paddingLeft, paddingRight = param.paddingRight; \
121 int32_t paddingTop = param.paddingTop, paddingBottom = param.paddingBottom; \
122 int32_t strideWidth = param.strideWidth, strideHeight = param.strideHeight; \
123 int32_t activation = param.activation;
124
transposeConvNhwc(const float * inputData,const Shape & inputShape,const float * filterData,const Shape & filterShape,const float * biasData,const Shape & biasShape,const TransposeConv2dParam & param,float * outputData,const Shape & outputShape)125 bool transposeConvNhwc(const float* inputData, const Shape& inputShape, const float* filterData,
126 const Shape& filterShape, const float* biasData, const Shape& biasShape,
127 const TransposeConv2dParam& param, float* outputData,
128 const Shape& outputShape) {
129 NNTRACE_TRANS("transposeConvFloat32");
130 ANDROID_NN_TRANSPOSE_CONV_PARAMETERS
131
132 float outputActivationMin = 0.0f, outputActivationMax = 0.0f;
133 CalculateActivationRangeFloat(activation, &outputActivationMin, &outputActivationMax);
134
135 memset(outputData, 0, getNumberOfElements(outputShape) * sizeof(float));
136
137 const float* inputBase = inputData;
138 float* outputBase = outputData;
139 for (uint32_t b = 0; b < numBatches; b++) {
140 for (uint32_t h = 0; h < inputHeight; h++) {
141 for (uint32_t w = 0; w < inputWidth; w++) {
142 int32_t wOutputOrigin = static_cast<int32_t>(w) * strideWidth - paddingLeft;
143 int32_t hOutputOrigin = static_cast<int32_t>(h) * strideHeight - paddingTop;
144
145 const float* filterBase = filterData;
146 for (uint32_t k = 0; k < outputDepth; k++) {
147 for (uint32_t i = 0; i < filterHeight; i++) {
148 for (uint32_t j = 0; j < filterWidth; j++, filterBase += inputDepth) {
149 int32_t hOutput = hOutputOrigin + static_cast<int32_t>(i);
150 int32_t wOutput = wOutputOrigin + static_cast<int32_t>(j);
151 if (hOutput >= 0 && hOutput < static_cast<int32_t>(outputHeight) &&
152 wOutput >= 0 && wOutput < static_cast<int32_t>(outputWidth)) {
153 for (uint32_t d = 0; d < inputDepth; d++) {
154 uint32_t outputIndex = hOutput * outputWidth * outputDepth +
155 wOutput * outputDepth + k;
156 outputBase[outputIndex] += inputBase[d] * filterBase[d];
157 }
158 }
159 }
160 }
161 }
162
163 inputBase += inputDepth;
164 }
165 }
166 outputBase += outputHeight * outputWidth * outputDepth;
167 }
168
169 const uint32_t outerSize = numBatches * outputHeight * outputWidth;
170 float* outPtr = outputData;
171 for (uint32_t i = 0; i < outerSize; i++) {
172 for (uint32_t d = 0; d < outputDepth; d++, outPtr++) {
173 *outPtr += biasData[d];
174 *outPtr = std::max(std::min(*outPtr, outputActivationMax), outputActivationMin);
175 }
176 }
177
178 return true;
179 }
180
181 template <typename T>
transposeConvNhwc(const T * inputData,const Shape & inputShape,const T * filterData,const Shape & filterShape,const int32_t * biasData,const Shape & biasShape,const TransposeConv2dParam & param,T * outputData,const Shape & outputShape)182 bool transposeConvNhwc(const T* inputData, const Shape& inputShape, const T* filterData,
183 const Shape& filterShape, const int32_t* biasData, const Shape& biasShape,
184 const TransposeConv2dParam& param, T* outputData, const Shape& outputShape) {
185 NNTRACE_TRANS("transposeConvQuant8");
186 ANDROID_NN_TRANSPOSE_CONV_PARAMETERS
187
188 int32_t* tempBuffer = nullptr;
189 std::unique_ptr<int32_t[]> bufferGuard;
190 uint32_t tempBufferByteSize = getNumberOfElements(outputShape) * sizeof(int32_t);
191 if (tempBufferByteSize <= kStaticBufferSize) {
192 tempBuffer = reinterpret_cast<int32_t*>(static_scratch_buffer);
193 } else {
194 tempBuffer = new (std::nothrow) int32_t[tempBufferByteSize / sizeof(int32_t)];
195 if (tempBuffer == nullptr) {
196 LOG(ERROR) << "ConvTranspose size is too large, not enough memory";
197 return false;
198 }
199 bufferGuard.reset(tempBuffer);
200 }
201
202 int32_t inputOffset = -inputShape.offset;
203 int32_t filterOffset = -filterShape.offset;
204 int32_t outputOffset = outputShape.offset;
205
206 double realMultiplier = 0.0;
207 int32_t outputMultiplier = 0;
208 int32_t outputShift = 0;
209 NN_RET_CHECK(GetQuantizedConvolutionMultipler(inputShape, filterShape, biasShape, outputShape,
210 &realMultiplier));
211 int exponent;
212 NN_RET_CHECK(QuantizeMultiplier(realMultiplier, &outputMultiplier, &exponent));
213 outputShift = -exponent;
214
215 int32_t outputActivationMin = 0, outputActivationMax = 0;
216 CalculateActivationRange<T>(activation, outputShape, &outputActivationMin,
217 &outputActivationMax);
218
219 // Prevent concurrent executions that may access the scratch buffer
220 std::unique_lock<std::mutex> lock(executionMutex);
221 memset(tempBuffer, 0, tempBufferByteSize);
222
223 const T* inputPtr = inputData;
224 int32_t* outputBase = tempBuffer;
225 for (uint32_t b = 0; b < numBatches; b++) {
226 for (uint32_t h = 0; h < inputHeight; h++) {
227 for (uint32_t w = 0; w < inputWidth; w++) {
228 for (uint32_t d = 0; d < inputDepth; d++) {
229 int32_t wOutputOrigin = static_cast<int32_t>(w) * strideWidth - paddingLeft;
230 int32_t hOutputOrigin = static_cast<int32_t>(h) * strideHeight - paddingTop;
231
232 for (uint32_t i = 0; i < filterHeight; i++) {
233 for (uint32_t j = 0; j < filterWidth; j++) {
234 for (uint32_t k = 0; k < outputDepth; k++) {
235 int32_t hOutput = hOutputOrigin + static_cast<int32_t>(i);
236 int32_t wOutput = wOutputOrigin + static_cast<int32_t>(j);
237 if (hOutput >= 0 && hOutput < static_cast<int32_t>(outputHeight) &&
238 wOutput >= 0 && wOutput < static_cast<int32_t>(outputWidth)) {
239 uint32_t filterIndex =
240 k * filterHeight * filterWidth * inputDepth +
241 i * filterWidth * inputDepth + j * inputDepth + d;
242 uint32_t outputIndex = hOutput * outputWidth * outputDepth +
243 wOutput * outputDepth + k;
244 outputBase[outputIndex] +=
245 (static_cast<int32_t>(*inputPtr) + inputOffset) *
246 (static_cast<int32_t>(filterData[filterIndex]) +
247 filterOffset);
248 }
249 }
250 }
251 }
252
253 inputPtr++;
254 }
255 }
256 }
257 outputBase += outputHeight * outputWidth * outputDepth;
258 }
259
260 const uint32_t outerSize = numBatches * outputHeight * outputWidth;
261 int32_t* bufferPtr = tempBuffer;
262 T* outPtr = outputData;
263 for (uint32_t i = 0; i < outerSize; i++) {
264 for (uint32_t d = 0; d < outputDepth; d++, bufferPtr++, outPtr++) {
265 int32_t outVal = *bufferPtr + biasData[d];
266 outVal = tflite::MultiplyByQuantizedMultiplier(outVal, outputMultiplier, -outputShift);
267 outVal += outputOffset;
268 outVal = std::max(std::min(outVal, outputActivationMax), outputActivationMin);
269 *outPtr = static_cast<T>(outVal);
270 }
271 }
272
273 return true;
274 }
275
transposeConvNhwc(const _Float16 * inputData,const Shape & inputShape,const _Float16 * filterData,const Shape & filterShape,const _Float16 * biasData,const Shape & biasShape,const TransposeConv2dParam & param,_Float16 * outputData,const Shape & outputShape)276 bool transposeConvNhwc(const _Float16* inputData, const Shape& inputShape,
277 const _Float16* filterData, const Shape& filterShape,
278 const _Float16* biasData, const Shape& biasShape,
279 const TransposeConv2dParam& param, _Float16* outputData,
280 const Shape& outputShape) {
281 NNTRACE_TRANS("transposeConvFloat16");
282 std::vector<float> inputData_float32(getNumberOfElements(inputShape));
283 std::vector<float> filterData_float32(getNumberOfElements(filterShape));
284 std::vector<float> biasData_float32(getNumberOfElements(biasShape));
285 std::vector<float> outputData_float32(getNumberOfElements(outputShape));
286
287 convertFloat16ToFloat32(inputData, &inputData_float32);
288 convertFloat16ToFloat32(filterData, &filterData_float32);
289 convertFloat16ToFloat32(biasData, &biasData_float32);
290
291 transposeConvNhwc(inputData_float32.data(), inputShape, filterData_float32.data(), filterShape,
292 biasData_float32.data(), biasShape, param, outputData_float32.data(),
293 outputShape);
294 convertFloat32ToFloat16(outputData_float32, outputData);
295
296 return true;
297 }
298
299 template <typename T_Input, typename T_Filter, typename T_Bias>
transposeConv(const T_Input * inputData,const Shape & inputShape,const T_Filter * filterData,const Shape & filterShape,const T_Bias * biasData,const Shape & biasShape,const TransposeConv2dParam & param,T_Input * outputData,const Shape & outputShape)300 bool transposeConv(const T_Input* inputData, const Shape& inputShape, const T_Filter* filterData,
301 const Shape& filterShape, const T_Bias* biasData, const Shape& biasShape,
302 const TransposeConv2dParam& param, T_Input* outputData,
303 const Shape& outputShape) {
304 InputWithLayout<T_Input> input(param.useNchw);
305 OutputWithLayout<T_Input> output(param.useNchw);
306 NN_RET_CHECK(input.initialize(inputData, inputShape));
307 NN_RET_CHECK(output.initialize(outputData, outputShape));
308 NN_RET_CHECK(transposeConvNhwc(input.getNhwcBuffer(), input.getNhwcShape(), filterData,
309 filterShape, biasData, biasShape, param, output.getNhwcBuffer(),
310 output.getNhwcShape()));
311 NN_RET_CHECK(output.commit());
312 return true;
313 }
314
315 template <typename T>
transposeConvQuant8PerChannelNhwc(const T * inputData,const Shape & inputShape,const int8_t * filterData,const Shape & filterShape,const float * filterScales,const int32_t * biasData,const Shape & biasShape,const TransposeConv2dParam & param,T * outputData,const Shape & outputShape)316 bool transposeConvQuant8PerChannelNhwc(const T* inputData, const Shape& inputShape,
317 const int8_t* filterData, const Shape& filterShape,
318 const float* filterScales, const int32_t* biasData,
319 const Shape& biasShape, const TransposeConv2dParam& param,
320 T* outputData, const Shape& outputShape) {
321 NNTRACE_TRANS("transposeConvQuant8PerChannel");
322 ANDROID_NN_TRANSPOSE_CONV_PARAMETERS
323
324 int32_t* tempBuffer = nullptr;
325 std::unique_ptr<int32_t[]> bufferGuard;
326 uint32_t tempBufferByteSize = getNumberOfElements(outputShape) * sizeof(int32_t);
327 if (tempBufferByteSize <= kStaticBufferSize) {
328 tempBuffer = reinterpret_cast<int32_t*>(static_scratch_buffer);
329 } else {
330 tempBuffer = new (std::nothrow) int32_t[tempBufferByteSize / sizeof(int32_t)];
331 if (tempBuffer == nullptr) {
332 LOG(ERROR) << "ConvTranspose size is too large, not enough memory";
333 return false;
334 }
335 bufferGuard.reset(tempBuffer);
336 }
337
338 int32_t inputOffset = -inputShape.offset;
339 int32_t outputOffset = outputShape.offset;
340
341 std::vector<double> realMultiplier(outputDepth, 0.0);
342 std::vector<int32_t> outputMultiplier(outputDepth, 0);
343 std::vector<int32_t> outputShift(outputDepth, 0);
344 for (int i = 0; i < outputDepth; ++i) {
345 Shape filterChannelShape = filterShape;
346 filterChannelShape.scale = filterScales[i];
347 Shape biasChannelShape = biasShape;
348 biasChannelShape.scale = filterScales[i] * inputShape.scale;
349
350 NN_RET_CHECK(GetQuantizedConvolutionMultipler(
351 inputShape, filterChannelShape, biasChannelShape, outputShape, &realMultiplier[i]));
352 int exponent;
353 NN_RET_CHECK(QuantizeMultiplier(realMultiplier[i], &outputMultiplier[i], &exponent));
354 outputShift[i] = -exponent;
355 }
356
357 int32_t outputActivationMin = 0, outputActivationMax = 0;
358 CalculateActivationRange<T>(activation, outputShape, &outputActivationMin,
359 &outputActivationMax);
360
361 // Prevent concurrent executions that may access the scratch buffer
362 std::unique_lock<std::mutex> lock(executionMutex);
363 memset(tempBuffer, 0, tempBufferByteSize);
364
365 const T* inputPtr = inputData;
366 int32_t* outputBase = tempBuffer;
367 for (uint32_t b = 0; b < numBatches; b++) {
368 for (uint32_t h = 0; h < inputHeight; h++) {
369 for (uint32_t w = 0; w < inputWidth; w++) {
370 for (uint32_t d = 0; d < inputDepth; d++) {
371 int32_t wOutputOrigin = static_cast<int32_t>(w) * strideWidth - paddingLeft;
372 int32_t hOutputOrigin = static_cast<int32_t>(h) * strideHeight - paddingTop;
373
374 for (uint32_t i = 0; i < filterHeight; i++) {
375 for (uint32_t j = 0; j < filterWidth; j++) {
376 for (uint32_t k = 0; k < outputDepth; k++) {
377 int32_t hOutput = hOutputOrigin + static_cast<int32_t>(i);
378 int32_t wOutput = wOutputOrigin + static_cast<int32_t>(j);
379 if (hOutput >= 0 && hOutput < static_cast<int32_t>(outputHeight) &&
380 wOutput >= 0 && wOutput < static_cast<int32_t>(outputWidth)) {
381 uint32_t filterIndex =
382 k * filterHeight * filterWidth * inputDepth +
383 i * filterWidth * inputDepth + j * inputDepth + d;
384 uint32_t outputIndex = hOutput * outputWidth * outputDepth +
385 wOutput * outputDepth + k;
386 outputBase[outputIndex] +=
387 (static_cast<int32_t>(*inputPtr) + inputOffset) *
388 static_cast<int32_t>(filterData[filterIndex]);
389 }
390 }
391 }
392 }
393
394 inputPtr++;
395 }
396 }
397 }
398 outputBase += outputHeight * outputWidth * outputDepth;
399 }
400
401 const uint32_t outerSize = numBatches * outputHeight * outputWidth;
402 int32_t* bufferPtr = tempBuffer;
403 T* outPtr = outputData;
404 for (uint32_t i = 0; i < outerSize; i++) {
405 for (uint32_t d = 0; d < outputDepth; d++, bufferPtr++, outPtr++) {
406 int32_t outVal = *bufferPtr + biasData[d];
407 outVal = tflite::MultiplyByQuantizedMultiplier(outVal, outputMultiplier[d],
408 -outputShift[d]);
409 outVal += outputOffset;
410 outVal = std::max(std::min(outVal, outputActivationMax), outputActivationMin);
411 *outPtr = static_cast<T>(outVal);
412 }
413 }
414
415 return true;
416 }
417
418 template <typename T>
transposeConvQuant8PerChannel(const T * inputData,const Shape & inputShape,const int8_t * filterData,const Shape & filterShape,const float * filterScales,const int32_t * biasData,const Shape & biasShape,const TransposeConv2dParam & param,T * outputData,const Shape & outputShape)419 bool transposeConvQuant8PerChannel(const T* inputData, const Shape& inputShape,
420 const int8_t* filterData, const Shape& filterShape,
421 const float* filterScales, const int32_t* biasData,
422 const Shape& biasShape, const TransposeConv2dParam& param,
423 T* outputData, const Shape& outputShape) {
424 InputWithLayout<T> input(param.useNchw);
425 OutputWithLayout<T> output(param.useNchw);
426 NN_RET_CHECK(input.initialize(inputData, inputShape));
427 NN_RET_CHECK(output.initialize(outputData, outputShape));
428 NN_RET_CHECK(transposeConvQuant8PerChannelNhwc(
429 input.getNhwcBuffer(), input.getNhwcShape(), filterData, filterShape, filterScales,
430 biasData, biasShape, param, output.getNhwcBuffer(), output.getNhwcShape()));
431 NN_RET_CHECK(output.commit());
432 return true;
433 }
434
435 #undef ANDROID_NN_TRANSPOSE_CONV_PARAMETERS
436
437 } // namespace
438
validate(const IOperationValidationContext * context)439 bool validate(const IOperationValidationContext* context) {
440 const uint32_t inputCount = context->getNumInputs();
441 NN_RET_CHECK(inputCount == kNumInputs1 || inputCount == kNumInputs2);
442 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
443 const auto inputType = context->getInputType(kInputTensor);
444 const auto filterType = context->getInputType(kFilterTensor);
445 std::vector<OperandType> inExpectedTypes;
446 HalVersion minSupportedHalVersion = HalVersion::V1_2;
447 if (inputType == OperandType::TENSOR_FLOAT32 || inputType == OperandType::TENSOR_FLOAT16) {
448 inExpectedTypes = {inputType, inputType, inputType};
449 } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM ||
450 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
451 NN_RET_CHECK(filterType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
452 filterType == inputType)
453 << "Unsupported filter tensor type for operation " << kOperationName;
454 if (filterType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
455 NN_RET_CHECK_EQ(context->getInputExtraParams(kFilterTensor).channelQuant().channelDim,
456 0)
457 << "Unsupported filter tensor channel dimension for operation "
458 << kOperationName;
459 }
460 inExpectedTypes = {inputType, filterType, OperandType::TENSOR_INT32};
461 if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
462 minSupportedHalVersion = HalVersion::V1_3;
463 }
464 } else {
465 NN_RET_CHECK_FAIL() << "Unsupported input tensor type for operation " << kOperationName;
466 }
467
468 std::vector<OperandType> argExpectedTypes;
469 if (inputCount == 11) {
470 argExpectedTypes = {OperandType::INT32, OperandType::INT32, OperandType::INT32,
471 OperandType::INT32, OperandType::INT32, OperandType::INT32,
472 OperandType::INT32, OperandType::BOOL};
473 } else {
474 argExpectedTypes = {OperandType::TENSOR_INT32, OperandType::INT32, OperandType::INT32,
475 OperandType::INT32, OperandType::INT32, OperandType::BOOL};
476 }
477 inExpectedTypes.insert(inExpectedTypes.end(), argExpectedTypes.begin(), argExpectedTypes.end());
478 NN_RET_CHECK(validateHalVersion(context, minSupportedHalVersion));
479 return validateInputTypes(context, inExpectedTypes) &&
480 validateOutputTypes(context, {inputType});
481 }
482
prepare(IOperationExecutionContext * context)483 bool prepare(IOperationExecutionContext* context) {
484 Shape input = context->getInputShape(kInputTensor);
485 Shape filter = context->getInputShape(kFilterTensor);
486 Shape bias = context->getInputShape(kBiasTensor);
487
488 if (filter.type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
489 NN_RET_CHECK(input.type == OperandType::TENSOR_QUANT8_ASYMM ||
490 input.type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED);
491 } else {
492 NN_RET_CHECK(input.type == filter.type);
493 }
494 if (input.type == OperandType::TENSOR_QUANT8_ASYMM ||
495 input.type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
496 NN_RET_CHECK(bias.type == OperandType::TENSOR_INT32);
497 } else {
498 NN_RET_CHECK(input.type == bias.type);
499 }
500 NN_RET_CHECK_EQ(getNumberOfDimensions(input), 4);
501 NN_RET_CHECK_EQ(getNumberOfDimensions(filter), 4);
502 NN_RET_CHECK_EQ(getNumberOfDimensions(bias), 1);
503
504 TransposeConv2dParam param;
505 NN_RET_CHECK(param.initialize(context));
506
507 uint32_t batches = getSizeOfDimension(input, 0);
508 uint32_t height = getSizeOfDimension(input, param.useNchw ? 2 : 1);
509 uint32_t width = getSizeOfDimension(input, param.useNchw ? 3 : 2);
510 uint32_t channels_in = getSizeOfDimension(input, param.useNchw ? 1 : 3);
511 uint32_t channels_out = getSizeOfDimension(filter, 0);
512 uint32_t filterHeight = getSizeOfDimension(filter, 1);
513 uint32_t filterWidth = getSizeOfDimension(filter, 2);
514 // Only batches can be zero.
515 NN_RET_CHECK_EQ(channels_in, getSizeOfDimension(filter, 3));
516 NN_RET_CHECK_EQ(channels_out, getSizeOfDimension(bias, 0));
517 NN_RET_CHECK_GT(height, 0);
518 NN_RET_CHECK_GT(width, 0);
519 NN_RET_CHECK_GT(channels_in, 0);
520 NN_RET_CHECK_GT(channels_out, 0);
521 NN_RET_CHECK_GT(filterWidth, 0);
522 NN_RET_CHECK_GT(filterHeight, 0);
523
524 uint32_t outWidth = computeOutSizeTransposeConv(width, filterWidth, param.strideWidth,
525 param.paddingLeft, param.paddingRight);
526 uint32_t outHeight = computeOutSizeTransposeConv(height, filterHeight, param.strideHeight,
527 param.paddingTop, param.paddingBottom);
528 NN_RET_CHECK_GT(outWidth, 0);
529 NN_RET_CHECK_GT(outHeight, 0);
530
531 Shape output = context->getOutputShape(kOutputTensor);
532 output.type = input.type;
533 if (param.useNchw) {
534 output.dimensions = {batches, channels_out, outHeight, outWidth};
535 } else {
536 output.dimensions = {batches, outHeight, outWidth, channels_out};
537 }
538 return context->setOutputShape(kOutputTensor, output);
539 }
540
execute(IOperationExecutionContext * context)541 bool execute(IOperationExecutionContext* context) {
542 // Bypass execution in the case of zero-sized input.
543 if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
544 TransposeConv2dParam param;
545 NN_RET_CHECK(param.initialize(context));
546 switch (context->getInputType(kInputTensor)) {
547 case OperandType::TENSOR_FLOAT32:
548 return transposeConv(context->getInputBuffer<float>(kInputTensor),
549 context->getInputShape(kInputTensor),
550 context->getInputBuffer<float>(kFilterTensor),
551 context->getInputShape(kFilterTensor),
552 context->getInputBuffer<float>(kBiasTensor),
553 context->getInputShape(kBiasTensor), param,
554 context->getOutputBuffer<float>(kOutputTensor),
555 context->getOutputShape(kOutputTensor));
556 case OperandType::TENSOR_FLOAT16:
557 return transposeConv(context->getInputBuffer<_Float16>(kInputTensor),
558 context->getInputShape(kInputTensor),
559 context->getInputBuffer<_Float16>(kFilterTensor),
560 context->getInputShape(kFilterTensor),
561 context->getInputBuffer<_Float16>(kBiasTensor),
562 context->getInputShape(kBiasTensor), param,
563 context->getOutputBuffer<_Float16>(kOutputTensor),
564 context->getOutputShape(kOutputTensor));
565 case OperandType::TENSOR_QUANT8_ASYMM:
566 if (context->getInputType(kFilterTensor) ==
567 OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
568 return transposeConvQuant8PerChannel(
569 context->getInputBuffer<uint8_t>(kInputTensor),
570 context->getInputShape(kInputTensor),
571 context->getInputBuffer<int8_t>(kFilterTensor),
572 context->getInputShape(kFilterTensor),
573 context->getInputExtraParams(kFilterTensor).channelQuant().scales.data(),
574 context->getInputBuffer<int32_t>(kBiasTensor),
575 context->getInputShape(kBiasTensor), param,
576 context->getOutputBuffer<uint8_t>(kOutputTensor),
577 context->getOutputShape(kOutputTensor));
578 } else if (context->getInputType(kFilterTensor) == OperandType::TENSOR_QUANT8_ASYMM) {
579 return transposeConv(context->getInputBuffer<uint8_t>(kInputTensor),
580 context->getInputShape(kInputTensor),
581 context->getInputBuffer<uint8_t>(kFilterTensor),
582 context->getInputShape(kFilterTensor),
583 context->getInputBuffer<int32_t>(kBiasTensor),
584 context->getInputShape(kBiasTensor), param,
585 context->getOutputBuffer<uint8_t>(kOutputTensor),
586 context->getOutputShape(kOutputTensor));
587 } else {
588 NN_RET_CHECK_FAIL() << "Unsupported filter type for operation " << kOperationName;
589 }
590 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
591 if (context->getInputType(kFilterTensor) ==
592 OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
593 return transposeConvQuant8PerChannel(
594 context->getInputBuffer<int8_t>(kInputTensor),
595 context->getInputShape(kInputTensor),
596 context->getInputBuffer<int8_t>(kFilterTensor),
597 context->getInputShape(kFilterTensor),
598 context->getInputExtraParams(kFilterTensor).channelQuant().scales.data(),
599 context->getInputBuffer<int32_t>(kBiasTensor),
600 context->getInputShape(kBiasTensor), param,
601 context->getOutputBuffer<int8_t>(kOutputTensor),
602 context->getOutputShape(kOutputTensor));
603 } else if (context->getInputType(kFilterTensor) ==
604 OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
605 return transposeConv(context->getInputBuffer<int8_t>(kInputTensor),
606 context->getInputShape(kInputTensor),
607 context->getInputBuffer<int8_t>(kFilterTensor),
608 context->getInputShape(kFilterTensor),
609 context->getInputBuffer<int32_t>(kBiasTensor),
610 context->getInputShape(kBiasTensor), param,
611 context->getOutputBuffer<int8_t>(kOutputTensor),
612 context->getOutputShape(kOutputTensor));
613 } else {
614 NN_RET_CHECK_FAIL() << "Unsupported filter type for operation " << kOperationName;
615 }
616 default:
617 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
618 }
619 }
620
621 } // namespace transpose_conv_2d
622
623 NN_REGISTER_OPERATION(TRANSPOSE_CONV_2D, transpose_conv_2d::kOperationName,
624 transpose_conv_2d::validate, transpose_conv_2d::prepare,
625 transpose_conv_2d::execute, .allowZeroSizedInput = true);
626
627 } // namespace nn
628 } // namespace android
629