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 "HalInterfaces.h"
23 #include "IndexedShapeWrapper.h"
24 #include "OperationResolver.h"
25 #include "OperationsUtils.h"
26 #include "Tracing.h"
27 
28 #include <tensorflow/lite/kernels/internal/optimized/legacy_optimized_ops.h>
29 
30 namespace android {
31 namespace nn {
32 namespace prelu {
33 
34 using namespace hal;
35 
36 constexpr char kOperationName[] = "PRELU";
37 
38 constexpr uint32_t kNumInputs = 2;
39 constexpr uint32_t kInputTensor = 0;
40 constexpr uint32_t kAlphaTensor = 1;
41 
42 constexpr uint32_t kNumOutputs = 1;
43 constexpr uint32_t kOutputTensor = 0;
44 
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 
validate(const IOperationValidationContext * context)101 bool validate(const IOperationValidationContext* context) {
102     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
103     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
104     auto inputType = context->getInputType(kInputTensor);
105     NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
106                  inputType == OperandType::TENSOR_FLOAT32 ||
107                  inputType == OperandType::TENSOR_QUANT8_ASYMM ||
108                  inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
109             << "Unsupported tensor type for operation " << kOperationName;
110     NN_RET_CHECK(validateInputTypes(context, {inputType, inputType}));
111     NN_RET_CHECK(validateOutputTypes(context, {inputType}));
112     if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
113         return validateHalVersion(context, HalVersion::V1_3);
114     } else {
115         return validateHalVersion(context, HalVersion::V1_2);
116     }
117 }
118 
prepare(IOperationExecutionContext * context)119 bool prepare(IOperationExecutionContext* context) {
120     Shape input = context->getInputShape(kInputTensor);
121     Shape alpha = context->getInputShape(kAlphaTensor);
122     NN_RET_CHECK(input.type == alpha.type);
123     Shape output = context->getOutputShape(kOutputTensor);
124     NN_RET_CHECK(calculateBroadcastedShape(input, alpha, &output));
125     return context->setOutputShape(kOutputTensor, output);
126 }
127 
execute(IOperationExecutionContext * context)128 bool execute(IOperationExecutionContext* context) {
129     switch (context->getInputType(kInputTensor)) {
130         case OperandType::TENSOR_FLOAT16:
131             return eval<_Float16>(
132                     [](const _Float16& val1, const _Float16& val2) -> _Float16 {
133                         return val1 >= 0.0f ? val1 : val1 * val2;
134                     },
135                     context->getInputBuffer<_Float16>(kInputTensor),
136                     context->getInputShape(kInputTensor),
137                     context->getInputBuffer<_Float16>(kAlphaTensor),
138                     context->getInputShape(kAlphaTensor),
139                     context->getOutputBuffer<_Float16>(kOutputTensor),
140                     context->getOutputShape(kOutputTensor));
141         case OperandType::TENSOR_FLOAT32:
142             return eval<float>(
143                     [](const float& val1, const float& val2) -> float {
144                         return val1 >= 0.0f ? val1 : val1 * val2;
145                     },
146                     context->getInputBuffer<float>(kInputTensor),
147                     context->getInputShape(kInputTensor),
148                     context->getInputBuffer<float>(kAlphaTensor),
149                     context->getInputShape(kAlphaTensor),
150                     context->getOutputBuffer<float>(kOutputTensor),
151                     context->getOutputShape(kOutputTensor));
152         case OperandType::TENSOR_QUANT8_ASYMM: {
153             return evalQuant8(context->getInputBuffer<uint8_t>(kInputTensor),
154                               context->getInputShape(kInputTensor),
155                               context->getInputBuffer<uint8_t>(kAlphaTensor),
156                               context->getInputShape(kAlphaTensor),
157                               context->getOutputBuffer<uint8_t>(kOutputTensor),
158                               context->getOutputShape(kOutputTensor));
159         }
160         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
161             return evalQuant8(context->getInputBuffer<int8_t>(kInputTensor),
162                               context->getInputShape(kInputTensor),
163                               context->getInputBuffer<int8_t>(kAlphaTensor),
164                               context->getInputShape(kAlphaTensor),
165                               context->getOutputBuffer<int8_t>(kOutputTensor),
166                               context->getOutputShape(kOutputTensor));
167         }
168         default:
169             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
170     }
171 }
172 
173 }  // namespace prelu
174 
175 NN_REGISTER_OPERATION(PRELU, prelu::kOperationName, prelu::validate, prelu::prepare,
176                       prelu::execute);
177 
178 }  // namespace nn
179 }  // namespace android
180