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 <algorithm>
20 #include <cmath>
21 
22 #include "IndexedShapeWrapper.h"
23 #include "OperationResolver.h"
24 #include "OperationsUtils.h"
25 #include "Tracing.h"
26 
27 namespace android {
28 namespace nn {
29 namespace quantize {
30 
31 constexpr uint32_t kNumInputs = 1;
32 constexpr uint32_t kInputTensor = 0;
33 
34 constexpr uint32_t kNumOutputs = 1;
35 constexpr uint32_t kOutputTensor = 0;
36 
37 namespace {
38 
39 template <typename T>
quantizeToQuant8(const T * inputData,uint8_t * outputData,const Shape & outputShape)40 bool quantizeToQuant8(const T* inputData, uint8_t* outputData, const Shape& outputShape) {
41     NNTRACE_COMP("quantizeToQuant8");
42     uint32_t size = getNumberOfElements(outputShape);
43     for (uint32_t i = 0; i < size; ++i) {
44         outputData[i] = static_cast<uint8_t>(std::max<float>(
45                 0.0f, std::min<float>(255.0f, outputShape.offset + std::round(inputData[i] /
46                                                                               outputShape.scale))));
47     }
48     return true;
49 }
50 
51 template <typename T>
quantizeToQuant8Signed(const T * inputData,int8_t * outputData,const Shape & outputShape)52 bool quantizeToQuant8Signed(const T* inputData, int8_t* outputData, const Shape& outputShape) {
53     NNTRACE_COMP("quantizeToQuant8Signed");
54     uint32_t size = getNumberOfElements(outputShape);
55     for (uint32_t i = 0; i < size; ++i) {
56         outputData[i] = static_cast<int8_t>(std::max<float>(
57                 -128.0f,
58                 std::min<float>(127.0f, outputShape.offset +
59                                                 std::round(inputData[i] / outputShape.scale))));
60     }
61     return true;
62 }
63 
64 }  // namespace
65 
validate(const IOperationValidationContext * context)66 Result<Version> validate(const IOperationValidationContext* context) {
67     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
68     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
69 
70     const OperandType inputType = context->getInputType(kInputTensor);
71     const OperandType outputType = context->getOutputType(kOutputTensor);
72 
73     NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
74                  inputType == OperandType::TENSOR_FLOAT32)
75             << "Unsupported input operand type for QUANTIZE op: " << inputType;
76     NN_RET_CHECK(outputType == OperandType::TENSOR_QUANT8_ASYMM ||
77                  outputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
78             << "Unsupported output operand type for QUANTIZE op: " << outputType;
79     if (outputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
80         return Version::ANDROID_R;
81     } else {
82         return Version::ANDROID_Q;
83     }
84 }
85 
prepare(IOperationExecutionContext * context)86 bool prepare(IOperationExecutionContext* context) {
87     const Shape& input = context->getInputShape(kInputTensor);
88     Shape output = context->getOutputShape(kOutputTensor);
89     output.dimensions = input.dimensions;
90     return context->setOutputShape(kOutputTensor, output);
91 }
92 
execute(IOperationExecutionContext * context)93 bool execute(IOperationExecutionContext* context) {
94     // Bypass execution in the case of zero-sized input.
95     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
96 
97     const OperandType inputType = context->getInputType(kInputTensor);
98     const OperandType outputType = context->getOutputType(kOutputTensor);
99     if (inputType == OperandType::TENSOR_FLOAT32) {
100         if (outputType == OperandType::TENSOR_QUANT8_ASYMM) {
101             return quantizeToQuant8<float>(context->getInputBuffer<float>(kInputTensor),
102                                            context->getOutputBuffer<uint8_t>(kOutputTensor),
103                                            context->getOutputShape(kOutputTensor));
104         } else if (outputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
105             return quantizeToQuant8Signed<float>(context->getInputBuffer<float>(kInputTensor),
106                                                  context->getOutputBuffer<int8_t>(kOutputTensor),
107                                                  context->getOutputShape(kOutputTensor));
108         }
109     } else if (inputType == OperandType::TENSOR_FLOAT16) {
110         if (outputType == OperandType::TENSOR_QUANT8_ASYMM) {
111             return quantizeToQuant8<_Float16>(context->getInputBuffer<_Float16>(kInputTensor),
112                                               context->getOutputBuffer<uint8_t>(kOutputTensor),
113                                               context->getOutputShape(kOutputTensor));
114         } else if (outputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
115             return quantizeToQuant8Signed<_Float16>(context->getInputBuffer<_Float16>(kInputTensor),
116                                                     context->getOutputBuffer<int8_t>(kOutputTensor),
117                                                     context->getOutputShape(kOutputTensor));
118         }
119     }
120     NN_RET_CHECK_FAIL() << "Unsupported tensor types combination for QUANTIZE op. (input type: "
121                         << inputType << " output type: " << context->getOutputType(kOutputTensor)
122                         << ")";
123 }
124 
125 }  // namespace quantize
126 
127 NN_REGISTER_OPERATION(QUANTIZE, "QUANTIZE", quantize::validate, quantize::prepare,
128                       quantize::execute, .allowZeroSizedInput = true);
129 
130 }  // namespace nn
131 }  // namespace android
132