1 /*
2  * Copyright (C) 2019 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 "Dequantize.h"
20 
21 #include "IndexedShapeWrapper.h"
22 #include "OperationResolver.h"
23 #include "OperationsExecutionUtils.h"
24 
25 namespace android {
26 namespace nn {
27 namespace dequantize {
28 namespace {
29 
30 template <typename InputType, typename OutputType>
compute(const InputType * inputData,const Shape & inputShape,OutputType * outputData)31 bool compute(const InputType* inputData, const Shape& inputShape, OutputType* outputData) {
32     const int numElements = getNumberOfElements(inputShape);
33     const int32_t zeroPoint = inputShape.offset;
34     const float scale = inputShape.scale;
35     for (int i = 0; i < numElements; ++i) {
36         const int32_t value = inputData[i];
37         // This dequantization formula also appears in Elementwise.cpp.
38         outputData[i] = static_cast<OutputType>(scale * (value - zeroPoint));
39     }
40     return true;
41 }
42 
43 template <typename OutputType>
computePerChannel(const int8_t * inputData,const Shape & inputShape,OutputType * outputData)44 bool computePerChannel(const int8_t* inputData, const Shape& inputShape, OutputType* outputData) {
45     // First we calculate a stride which is the number of elements we need to
46     // skip to change an index along a dimension with different quantization
47     // scales.
48     const int channelDim =
49             std::get<Operand::SymmPerChannelQuantParams>(inputShape.extraParams).channelDim;
50     int stride = 1;
51     for (int i = getNumberOfDimensions(inputShape) - 1; i > channelDim; --i) {
52         stride *= getSizeOfDimension(inputShape, i);
53     }
54 
55     const int numElements = getNumberOfElements(inputShape);
56     const int32_t zeroPoint = inputShape.offset;
57 
58     for (int i = 0; i < numElements; ++i) {
59         // To get current index along the quantized dimension we calculate how
60         // many even |strides| we looped through and take this number modulo the
61         // size of the dimension (so that we don't have an overflow if the
62         // channelDim is not 0).
63         const int scaleIndex = (i / stride) % getSizeOfDimension(inputShape, channelDim);
64         const float scale = std::get<Operand::SymmPerChannelQuantParams>(inputShape.extraParams)
65                                     .scales[scaleIndex];
66         const int32_t value = inputData[i];
67         outputData[i] = static_cast<OutputType>(scale * (value - zeroPoint));
68     }
69     return true;
70 }
71 
72 }  // namespace
73 
prepare(IOperationExecutionContext * context)74 bool prepare(IOperationExecutionContext* context) {
75     const Shape& input = context->getInputShape(kInputTensor);
76     NN_RET_CHECK_LE(getNumberOfDimensions(input), 4u);
77     Shape output = context->getOutputShape(kOutputTensor);
78     output.dimensions = input.dimensions;
79     return context->setOutputShape(kOutputTensor, output);
80 }
81 
execute(IOperationExecutionContext * context)82 bool execute(IOperationExecutionContext* context) {
83     // Bypass execution in the case of zero-sized input.
84     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
85 
86     const OperandType inputType = context->getInputType(kInputTensor);
87     const OperandType outputType = context->getOutputType(kOutputTensor);
88 
89     const Shape& inputShape = context->getInputShape(kInputTensor);
90     if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
91         const uint8_t* inputBuffer = context->getInputBuffer<uint8_t>(kInputTensor);
92         if (outputType == OperandType::TENSOR_FLOAT16) {
93             return compute(inputBuffer, inputShape,
94                            context->getOutputBuffer<_Float16>(kOutputTensor));
95         } else if (outputType == OperandType::TENSOR_FLOAT32) {
96             return compute(inputBuffer, inputShape, context->getOutputBuffer<float>(kOutputTensor));
97         }
98     } else if (inputType == OperandType::TENSOR_QUANT8_SYMM) {
99         const int8_t* inputBuffer = context->getInputBuffer<int8_t>(kInputTensor);
100         if (outputType == OperandType::TENSOR_FLOAT16) {
101             return compute(inputBuffer, inputShape,
102                            context->getOutputBuffer<_Float16>(kOutputTensor));
103         } else if (outputType == OperandType::TENSOR_FLOAT32) {
104             return compute(inputBuffer, inputShape, context->getOutputBuffer<float>(kOutputTensor));
105         }
106     } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
107         const int8_t* inputBuffer = context->getInputBuffer<int8_t>(kInputTensor);
108         if (outputType == OperandType::TENSOR_FLOAT16) {
109             return compute(inputBuffer, inputShape,
110                            context->getOutputBuffer<_Float16>(kOutputTensor));
111         } else if (outputType == OperandType::TENSOR_FLOAT32) {
112             return compute(inputBuffer, inputShape, context->getOutputBuffer<float>(kOutputTensor));
113         }
114     } else if (inputType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
115         const int8_t* inputBuffer = context->getInputBuffer<int8_t>(kInputTensor);
116         if (outputType == OperandType::TENSOR_FLOAT16) {
117             return computePerChannel(inputBuffer, inputShape,
118                                      context->getOutputBuffer<_Float16>(kOutputTensor));
119         } else if (outputType == OperandType::TENSOR_FLOAT32) {
120             return computePerChannel(inputBuffer, inputShape,
121                                      context->getOutputBuffer<float>(kOutputTensor));
122         }
123     }
124     NN_RET_CHECK_FAIL() << "Unsupported tensor types combination for dequantize op. (input type: "
125                         << inputType << " output type: " << outputType << ")";
126 }
127 
128 }  // namespace dequantize
129 
130 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(DEQUANTIZE, dequantize::prepare, dequantize::execute,
131                                          .allowZeroSizedInput = true);
132 
133 }  // namespace nn
134 }  // namespace android
135