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 <algorithm>
20 #include <vector>
21 
22 #include "IndexedShapeWrapper.h"
23 #include "OperationResolver.h"
24 #include "OperationsUtils.h"
25 #include "Tracing.h"
26 
27 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
28 #include <tensorflow/lite/kernels/internal/optimized/legacy_optimized_ops.h>
29 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
30 
31 namespace android {
32 namespace nn {
33 namespace prelu {
34 
35 constexpr char kOperationName[] = "PRELU";
36 
37 constexpr uint32_t kNumInputs = 2;
38 constexpr uint32_t kInputTensor = 0;
39 constexpr uint32_t kAlphaTensor = 1;
40 
41 constexpr uint32_t kNumOutputs = 1;
42 constexpr uint32_t kOutputTensor = 0;
43 
44 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
45 template <typename T>
eval(const std::function<T (const T &,const T &)> & func,const T * aData,const Shape & aShape,const T * bData,const Shape & bShape,T * outputData,const Shape & outputShape)46 inline bool eval(const std::function<T(const T&, const T&)>& func, const T* aData,
47                  const Shape& aShape, const T* bData, const Shape& bShape, T* outputData,
48                  const Shape& outputShape) {
49     IndexedShapeWrapper aShapeIndexed(aShape);
50     IndexedShapeWrapper bShapeIndexed(bShape);
51     IndexedShapeWrapper outputShapeIndexed(outputShape);
52     std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
53     bool lastIndex = false;
54     do {
55         uint32_t outputFlatIndex;
56         NN_RET_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
57         uint32_t aFlatIndex;
58         NN_RET_CHECK(aShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &aFlatIndex));
59         uint32_t bFlatIndex;
60         NN_RET_CHECK(bShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &bFlatIndex));
61 
62         outputData[outputFlatIndex] = func(aData[aFlatIndex], bData[bFlatIndex]);
63 
64         NN_RET_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
65     } while (!lastIndex);
66     return true;
67 }
68 
69 template <typename T>
evalQuant8(const T * aData,const Shape & aShape,const T * bData,const Shape & bShape,T * outputData,const Shape & outputShape)70 bool evalQuant8(const T* aData, const Shape& aShape, const T* bData, const Shape& bShape,
71                 T* outputData, const Shape& outputShape) {
72     const int32_t input_offset = -aShape.offset;
73     const int32_t alpha_offset = -bShape.offset;
74     const int32_t output_offset = outputShape.offset;
75     const double input_product_scale = aShape.scale * bShape.scale;
76     const double real_multiplier_pos = aShape.scale / outputShape.scale;
77     const double real_multiplier_neg = input_product_scale / outputShape.scale;
78     int32_t output_multiplier_pos, output_shift_pos;
79     int32_t output_multiplier_neg, output_shift_neg;
80     tflite::QuantizeMultiplier(real_multiplier_pos, &output_multiplier_pos, &output_shift_pos);
81     tflite::QuantizeMultiplier(real_multiplier_neg, &output_multiplier_neg, &output_shift_neg);
82     return eval<T>(
83             [&](const T& val1, const T& val2) -> uint8_t {
84                 const int32_t input = input_offset + static_cast<int32_t>(val1);
85                 int32_t output_val;
86                 if (input >= 0) {
87                     output_val =
88                             output_offset + tflite::MultiplyByQuantizedMultiplier(
89                                                     input, output_multiplier_pos, output_shift_pos);
90                 } else {
91                     const int32_t alpha = alpha_offset + static_cast<int32_t>(val2);
92                     output_val = output_offset +
93                                  tflite::MultiplyByQuantizedMultiplier(
94                                          input * alpha, output_multiplier_neg, output_shift_neg);
95                 }
96                 return saturateCast<T>(output_val);
97             },
98             aData, aShape, bData, bShape, outputData, outputShape);
99 }
100 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
101 
validate(const IOperationValidationContext * context)102 Result<Version> validate(const IOperationValidationContext* context) {
103     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
104     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
105     auto inputType = context->getInputType(kInputTensor);
106     NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
107                  inputType == OperandType::TENSOR_FLOAT32 ||
108                  inputType == OperandType::TENSOR_QUANT8_ASYMM ||
109                  inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
110             << "Unsupported tensor type for operation " << kOperationName;
111     NN_RET_CHECK(validateInputTypes(context, {inputType, inputType}));
112     NN_RET_CHECK(validateOutputTypes(context, {inputType}));
113     if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
114         return Version::ANDROID_R;
115     } else {
116         return Version::ANDROID_Q;
117     }
118 }
119 
120 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
prepare(IOperationExecutionContext * context)121 bool prepare(IOperationExecutionContext* context) {
122     Shape input = context->getInputShape(kInputTensor);
123     Shape alpha = context->getInputShape(kAlphaTensor);
124     NN_RET_CHECK(input.type == alpha.type);
125     Shape output = context->getOutputShape(kOutputTensor);
126     NN_RET_CHECK(calculateBroadcastedShape(input, alpha, &output));
127     return context->setOutputShape(kOutputTensor, output);
128 }
129 
execute(IOperationExecutionContext * context)130 bool execute(IOperationExecutionContext* context) {
131     switch (context->getInputType(kInputTensor)) {
132         case OperandType::TENSOR_FLOAT16:
133             return eval<_Float16>(
134                     [](const _Float16& val1, const _Float16& val2) -> _Float16 {
135                         return val1 >= 0.0f ? val1 : val1 * val2;
136                     },
137                     context->getInputBuffer<_Float16>(kInputTensor),
138                     context->getInputShape(kInputTensor),
139                     context->getInputBuffer<_Float16>(kAlphaTensor),
140                     context->getInputShape(kAlphaTensor),
141                     context->getOutputBuffer<_Float16>(kOutputTensor),
142                     context->getOutputShape(kOutputTensor));
143         case OperandType::TENSOR_FLOAT32:
144             return eval<float>(
145                     [](const float& val1, const float& val2) -> float {
146                         return val1 >= 0.0f ? val1 : val1 * val2;
147                     },
148                     context->getInputBuffer<float>(kInputTensor),
149                     context->getInputShape(kInputTensor),
150                     context->getInputBuffer<float>(kAlphaTensor),
151                     context->getInputShape(kAlphaTensor),
152                     context->getOutputBuffer<float>(kOutputTensor),
153                     context->getOutputShape(kOutputTensor));
154         case OperandType::TENSOR_QUANT8_ASYMM: {
155             return evalQuant8(context->getInputBuffer<uint8_t>(kInputTensor),
156                               context->getInputShape(kInputTensor),
157                               context->getInputBuffer<uint8_t>(kAlphaTensor),
158                               context->getInputShape(kAlphaTensor),
159                               context->getOutputBuffer<uint8_t>(kOutputTensor),
160                               context->getOutputShape(kOutputTensor));
161         }
162         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
163             return evalQuant8(context->getInputBuffer<int8_t>(kInputTensor),
164                               context->getInputShape(kInputTensor),
165                               context->getInputBuffer<int8_t>(kAlphaTensor),
166                               context->getInputShape(kAlphaTensor),
167                               context->getOutputBuffer<int8_t>(kOutputTensor),
168                               context->getOutputShape(kOutputTensor));
169         }
170         default:
171             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
172     }
173 }
174 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
175 
176 }  // namespace prelu
177 
178 NN_REGISTER_OPERATION(PRELU, prelu::kOperationName, prelu::validate, prelu::prepare,
179                       prelu::execute);
180 
181 }  // namespace nn
182 }  // namespace android
183