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 #include "OperationsUtils.h"
18 #define LOG_TAG "Operations"
19 
20 #include "HalInterfaces.h"
21 #include "IndexedShapeWrapper.h"
22 #include "OperationResolver.h"
23 
24 namespace android {
25 namespace nn {
26 namespace dequantize {
27 
28 constexpr uint32_t kNumInputs = 1;
29 constexpr uint32_t kInputTensor = 0;
30 
31 constexpr uint32_t kNumOutputs = 1;
32 constexpr uint32_t kOutputTensor = 0;
33 
34 namespace {
35 
36 using namespace hal;
37 
38 template <typename InputType, typename OutputType>
compute(const InputType * inputData,const Shape & inputShape,OutputType * outputData)39 bool compute(const InputType* inputData, const Shape& inputShape, OutputType* outputData) {
40     const int numElements = getNumberOfElements(inputShape);
41     const int32_t zeroPoint = inputShape.offset;
42     const float scale = inputShape.scale;
43     for (int i = 0; i < numElements; ++i) {
44         const int32_t value = inputData[i];
45         outputData[i] = static_cast<OutputType>(scale * (value - zeroPoint));
46     }
47     return true;
48 }
49 
50 template <typename OutputType>
computePerChannel(const int8_t * inputData,const Shape & inputShape,OutputType * outputData)51 bool computePerChannel(const int8_t* inputData, const Shape& inputShape, OutputType* outputData) {
52     // First we calculate a stride which is the number of elements we need to
53     // skip to change an index along a dimension with different quantization
54     // scales.
55     const int channelDim = inputShape.extraParams.channelQuant().channelDim;
56     int stride = 1;
57     for (int i = getNumberOfDimensions(inputShape) - 1; i > channelDim; --i) {
58         stride *= getSizeOfDimension(inputShape, i);
59     }
60 
61     const int numElements = getNumberOfElements(inputShape);
62     const int32_t zeroPoint = inputShape.offset;
63 
64     for (int i = 0; i < numElements; ++i) {
65         // To get current index along the quantized dimension we calculate how
66         // many even |strides| we looped through and take this number modulo the
67         // size of the dimension (so that we don't have an overflow if the
68         // channelDim is not 0).
69         const int scaleIndex = (i / stride) % getSizeOfDimension(inputShape, channelDim);
70         const float scale = inputShape.extraParams.channelQuant().scales[scaleIndex];
71         const int32_t value = inputData[i];
72         outputData[i] = static_cast<OutputType>(scale * (value - zeroPoint));
73     }
74     return true;
75 }
76 
77 }  // namespace
78 
validate(const IOperationValidationContext * context)79 bool validate(const IOperationValidationContext* context) {
80     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
81     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
82 
83     const OperandType inputType = context->getInputType(kInputTensor);
84     const OperandType outputType = context->getOutputType(kOutputTensor);
85 
86     const Shape& input = context->getInputShape(kInputTensor);
87     if (hasKnownRank(input)) {
88         NN_RET_CHECK_LE(getNumberOfDimensions(input), 4);
89     }
90 
91     if (inputType == OperandType::TENSOR_QUANT8_ASYMM &&
92         outputType == OperandType::TENSOR_FLOAT32) {
93         return validateHalVersion(context, HalVersion::V1_0);
94     }
95 
96     NN_RET_CHECK(inputType == OperandType::TENSOR_QUANT8_ASYMM ||
97                  inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
98                  inputType == OperandType::TENSOR_QUANT8_SYMM ||
99                  inputType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)
100             << "Unsupported input operand type for DEQUANTIZE op: " << toString(inputType);
101     NN_RET_CHECK(outputType == OperandType::TENSOR_FLOAT16 ||
102                  outputType == OperandType::TENSOR_FLOAT32)
103             << "Unsupported output operand type for DEQUANTIZE op: " << toString(outputType);
104     return validateHalVersion(context, HalVersion::V1_2);
105 }
106 
prepare(IOperationExecutionContext * context)107 bool prepare(IOperationExecutionContext* context) {
108     const Shape& input = context->getInputShape(kInputTensor);
109     NN_RET_CHECK_LE(getNumberOfDimensions(input), 4);
110     Shape output = context->getOutputShape(kOutputTensor);
111     output.dimensions = input.dimensions;
112     return context->setOutputShape(kOutputTensor, output);
113 }
114 
execute(IOperationExecutionContext * context)115 bool execute(IOperationExecutionContext* context) {
116     // Bypass execution in the case of zero-sized input.
117     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
118 
119     const OperandType inputType = context->getInputType(kInputTensor);
120     const OperandType outputType = context->getOutputType(kOutputTensor);
121 
122     const Shape& inputShape = context->getInputShape(kInputTensor);
123     if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
124         const uint8_t* inputBuffer = context->getInputBuffer<uint8_t>(kInputTensor);
125         if (outputType == OperandType::TENSOR_FLOAT16) {
126             return compute(inputBuffer, inputShape,
127                            context->getOutputBuffer<_Float16>(kOutputTensor));
128         } else if (outputType == OperandType::TENSOR_FLOAT32) {
129             return compute(inputBuffer, inputShape, context->getOutputBuffer<float>(kOutputTensor));
130         }
131     } else if (inputType == OperandType::TENSOR_QUANT8_SYMM) {
132         const int8_t* inputBuffer = context->getInputBuffer<int8_t>(kInputTensor);
133         if (outputType == OperandType::TENSOR_FLOAT16) {
134             return compute(inputBuffer, inputShape,
135                            context->getOutputBuffer<_Float16>(kOutputTensor));
136         } else if (outputType == OperandType::TENSOR_FLOAT32) {
137             return compute(inputBuffer, inputShape, context->getOutputBuffer<float>(kOutputTensor));
138         }
139     } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
140         const int8_t* inputBuffer = context->getInputBuffer<int8_t>(kInputTensor);
141         if (outputType == OperandType::TENSOR_FLOAT16) {
142             return compute(inputBuffer, inputShape,
143                            context->getOutputBuffer<_Float16>(kOutputTensor));
144         } else if (outputType == OperandType::TENSOR_FLOAT32) {
145             return compute(inputBuffer, inputShape, context->getOutputBuffer<float>(kOutputTensor));
146         }
147     } else if (inputType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
148         const int8_t* inputBuffer = context->getInputBuffer<int8_t>(kInputTensor);
149         if (outputType == OperandType::TENSOR_FLOAT16) {
150             return computePerChannel(inputBuffer, inputShape,
151                                      context->getOutputBuffer<_Float16>(kOutputTensor));
152         } else if (outputType == OperandType::TENSOR_FLOAT32) {
153             return computePerChannel(inputBuffer, inputShape,
154                                      context->getOutputBuffer<float>(kOutputTensor));
155         }
156     }
157     NN_RET_CHECK_FAIL() << "Unsupported tensor types combination for dequantize op. (input type: "
158                         << toString(inputType) << " output type: " << toString(outputType) << ")";
159 }
160 
161 }  // namespace dequantize
162 
163 NN_REGISTER_OPERATION(DEQUANTIZE, "DEQUANTIZE", dequantize::validate, dequantize::prepare,
164                       dequantize::execute, .allowZeroSizedInput = true);
165 
166 }  // namespace nn
167 }  // namespace android
168