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 "Elementwise.h"
20
21 #include <algorithm>
22 #include <cmath>
23 #include <functional>
24 #include <limits>
25
26 #include "OperationResolver.h"
27 #include "OperationsExecutionUtils.h"
28 #include "Tracing.h"
29
30 namespace android {
31 namespace nn {
32 namespace elementwise {
33 namespace {
34
35 template <typename IntermediateType, typename T>
compute(const std::function<IntermediateType (IntermediateType)> & func,const T * input,const Shape & shape,T * output)36 inline bool compute(const std::function<IntermediateType(IntermediateType)>& func, const T* input,
37 const Shape& shape, T* output) {
38 const auto size = getNumberOfElements(shape);
39 for (uint32_t i = 0; i < size; ++i) {
40 output[i] = static_cast<T>(func(static_cast<IntermediateType>(input[i])));
41 }
42 return true;
43 }
44
45 template <typename IntermediateType, typename T>
compute(IntermediateType func (IntermediateType),const T * input,const Shape & shape,T * output)46 inline bool compute(IntermediateType func(IntermediateType), const T* input, const Shape& shape,
47 T* output) {
48 return compute(std::function<IntermediateType(IntermediateType)>(func), input, shape, output);
49 }
50
51 template <typename IntermediateType, typename T>
makeQuantized(const std::function<IntermediateType (IntermediateType)> & func,float inScale,T inZeroPoint,float outScale,T outZeroPoint)52 auto makeQuantized(const std::function<IntermediateType(IntermediateType)>& func, float inScale,
53 T inZeroPoint, float outScale, T outZeroPoint) {
54 return [func, inScale, inZeroPoint, outScale, outZeroPoint](T val) -> T {
55 // For dequantization formula, see Dequantize.cpp.
56 using WideT = int32_t;
57 static_assert(sizeof(T) < sizeof(WideT));
58 IntermediateType dequantizedVal =
59 (static_cast<WideT>(val) - static_cast<WideT>(inZeroPoint)) * inScale;
60
61 IntermediateType res = func(dequantizedVal);
62
63 // For quantization formula, see Quantize.cpp.
64 T quantizedRes = static_cast<T>(std::max<float>(
65 static_cast<IntermediateType>(std::numeric_limits<T>::min()),
66 std::min<float>(static_cast<IntermediateType>(std::numeric_limits<T>::max()),
67 outZeroPoint + std::round(res / outScale))));
68
69 return quantizedRes;
70 };
71 }
72
execute(IOperationExecutionContext * context,float func (float))73 bool execute(IOperationExecutionContext* context, float func(float)) {
74 switch (context->getInputType(kInputTensor)) {
75 case OperandType::TENSOR_FLOAT16:
76 return compute<float, _Float16>(func, context->getInputBuffer<_Float16>(kInputTensor),
77 context->getInputShape(kInputTensor),
78 context->getOutputBuffer<_Float16>(kOutputTensor));
79 case OperandType::TENSOR_FLOAT32:
80 return compute<float, float>(func, context->getInputBuffer<float>(kInputTensor),
81 context->getInputShape(kInputTensor),
82 context->getOutputBuffer<float>(kOutputTensor));
83 default:
84 NN_RET_CHECK_FAIL() << "Unsupported tensor type for elementwise operation";
85 }
86 }
87
88 } // namespace
89
executeAbs(IOperationExecutionContext * context)90 bool executeAbs(IOperationExecutionContext* context) {
91 switch (context->getInputType(kInputTensor)) {
92 case OperandType::TENSOR_FLOAT16:
93 return compute<float, _Float16>(std::abs,
94 context->getInputBuffer<_Float16>(kInputTensor),
95 context->getInputShape(kInputTensor),
96 context->getOutputBuffer<_Float16>(kOutputTensor));
97 case OperandType::TENSOR_FLOAT32:
98 return compute<float, float>(std::abs, context->getInputBuffer<float>(kInputTensor),
99 context->getInputShape(kInputTensor),
100 context->getOutputBuffer<float>(kOutputTensor));
101 case OperandType::TENSOR_INT32:
102 return compute<int32_t, int32_t>(std::abs,
103 context->getInputBuffer<int32_t>(kInputTensor),
104 context->getInputShape(kInputTensor),
105 context->getOutputBuffer<int32_t>(kOutputTensor));
106 default:
107 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation ABS";
108 }
109 }
110
executeRsqrt(IOperationExecutionContext * context)111 bool executeRsqrt(IOperationExecutionContext* context) {
112 const std::function<float(float)> frsqrt = [](float x) { return 1.f / std::sqrt(x); };
113 const auto tensorType = context->getInputType(kInputTensor);
114 switch (tensorType) {
115 case OperandType::TENSOR_FLOAT16:
116 return compute<float, _Float16>(frsqrt, context->getInputBuffer<_Float16>(kInputTensor),
117 context->getInputShape(kInputTensor),
118 context->getOutputBuffer<_Float16>(kOutputTensor));
119 case OperandType::TENSOR_FLOAT32:
120 return compute<float, float>(frsqrt, context->getInputBuffer<float>(kInputTensor),
121 context->getInputShape(kInputTensor),
122 context->getOutputBuffer<float>(kOutputTensor));
123 case OperandType::TENSOR_QUANT8_ASYMM: {
124 const Shape inShape = context->getInputShape(kInputTensor);
125 const Shape outShape = context->getOutputShape(kOutputTensor);
126 return compute<uint8_t, uint8_t>(
127 makeQuantized(frsqrt, inShape.scale, static_cast<uint8_t>(inShape.offset),
128 outShape.scale, static_cast<uint8_t>(outShape.offset)),
129 context->getInputBuffer<uint8_t>(kInputTensor),
130 context->getInputShape(kInputTensor),
131 context->getOutputBuffer<uint8_t>(kOutputTensor));
132 }
133 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
134 const Shape inShape = context->getInputShape(kInputTensor);
135 const Shape outShape = context->getOutputShape(kOutputTensor);
136 return compute<int8_t, int8_t>(
137 makeQuantized(frsqrt, inShape.scale, static_cast<int8_t>(inShape.offset),
138 outShape.scale, static_cast<int8_t>(outShape.offset)),
139 context->getInputBuffer<int8_t>(kInputTensor),
140 context->getInputShape(kInputTensor),
141 context->getOutputBuffer<int8_t>(kOutputTensor));
142 }
143 default:
144 NN_RET_CHECK_FAIL() << "Unsupported tensor type " << tensorType
145 << " for operation RSQRT";
146 }
147 }
148
prepare(IOperationExecutionContext * context)149 bool prepare(IOperationExecutionContext* context) {
150 Shape input = context->getInputShape(kInputTensor);
151 Shape output = context->getOutputShape(kOutputTensor);
152 NN_RET_CHECK(SetShape(input, &output));
153 return context->setOutputShape(kOutputTensor, output);
154 }
155
prepareFloor(IOperationExecutionContext * context)156 bool prepareFloor(IOperationExecutionContext* context) {
157 Shape input = context->getInputShape(kInputTensor);
158 Shape output = context->getOutputShape(kOutputTensor);
159 NN_RET_CHECK_LE(getNumberOfDimensions(input), 4u);
160 NN_RET_CHECK(SetShape(input, &output));
161 return context->setOutputShape(kOutputTensor, output);
162 }
163
executeExp(IOperationExecutionContext * context)164 bool executeExp(IOperationExecutionContext* context) {
165 return execute(context, std::exp);
166 }
167
executeFloor(IOperationExecutionContext * context)168 bool executeFloor(IOperationExecutionContext* context) {
169 return execute(context, std::floor);
170 }
171
executeLog(IOperationExecutionContext * context)172 bool executeLog(IOperationExecutionContext* context) {
173 return execute(context, std::log);
174 }
175
executeSin(IOperationExecutionContext * context)176 bool executeSin(IOperationExecutionContext* context) {
177 return execute(context, std::sin);
178 }
179
executeSqrt(IOperationExecutionContext * context)180 bool executeSqrt(IOperationExecutionContext* context) {
181 return execute(context, std::sqrt);
182 }
183
184 } // namespace elementwise
185
186 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(ABS, elementwise::prepare, elementwise::executeAbs);
187 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(EXP, elementwise::prepare, elementwise::executeExp);
188 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(FLOOR, elementwise::prepareFloor,
189 elementwise::executeFloor);
190 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(LOG, elementwise::prepare, elementwise::executeLog);
191 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(RSQRT, elementwise::prepare, elementwise::executeRsqrt);
192 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(SIN, elementwise::prepare, elementwise::executeSin);
193 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(SQRT, elementwise::prepare, elementwise::executeSqrt);
194
195 } // namespace nn
196 } // namespace android
197