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 #include <vector>
22
23 #include "IndexedShapeWrapper.h"
24 #include "OperationResolver.h"
25 #include "OperationsUtils.h"
26 #include "Tracing.h"
27
28 namespace android {
29 namespace nn {
30 namespace elu {
31
32 constexpr uint32_t kNumInputs = 2;
33 constexpr uint32_t kInputTensor = 0;
34 constexpr uint32_t kAlphaScalar = 1;
35
36 constexpr uint32_t kNumOutputs = 1;
37 constexpr uint32_t kOutputTensor = 0;
38
39 namespace {
40
41 template <typename T>
eluFloat(const T * inputData,const Shape & inputShape,const T alpha,T * outputData,const Shape & outputShape)42 bool eluFloat(const T* inputData, const Shape& inputShape, const T alpha, T* outputData,
43 const Shape& outputShape) {
44 NNTRACE_COMP("ELU");
45 int numElements = getNumberOfElements(inputShape);
46 for (int i = 0; i < numElements; ++i) {
47 float x = static_cast<float>(inputData[i]);
48 outputData[i] = static_cast<T>(std::max(0.f, x) + std::min(0.f, alpha * (std::exp(x) - 1)));
49 }
50 return true;
51 }
52
53 } // namespace
54
validate(const IOperationValidationContext * context)55 Result<Version> validate(const IOperationValidationContext* context) {
56 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
57 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
58 auto inputType = context->getInputType(kInputTensor);
59 auto minSupportedVersion = Version::ANDROID_OC_MR1;
60 if (inputType == OperandType::TENSOR_FLOAT16 || inputType == OperandType::TENSOR_FLOAT32) {
61 minSupportedVersion = Version::ANDROID_R;
62 } else {
63 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation ELU";
64 }
65 auto scalarType =
66 inputType == OperandType::TENSOR_FLOAT16 ? OperandType::FLOAT16 : OperandType::FLOAT32;
67 NN_RET_CHECK(validateInputTypes(context, {inputType, scalarType}));
68 NN_RET_CHECK(validateOutputTypes(context, {inputType}));
69 return minSupportedVersion;
70 }
71
prepare(IOperationExecutionContext * context)72 bool prepare(IOperationExecutionContext* context) {
73 Shape inputShape = context->getInputShape(kInputTensor);
74 return context->setOutputShape(kOutputTensor, inputShape);
75 }
76
execute(IOperationExecutionContext * context)77 bool execute(IOperationExecutionContext* context) {
78 // Bypass execution in the case of zero-sized input.
79 if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
80 switch (context->getInputType(kInputTensor)) {
81 case OperandType::TENSOR_FLOAT16:
82 return eluFloat(context->getInputBuffer<_Float16>(kInputTensor),
83 context->getInputShape(kInputTensor),
84 context->getInputValue<_Float16>(kAlphaScalar),
85 context->getOutputBuffer<_Float16>(kOutputTensor),
86 context->getOutputShape(kOutputTensor));
87 case OperandType::TENSOR_FLOAT32:
88 return eluFloat(context->getInputBuffer<float>(kInputTensor),
89 context->getInputShape(kInputTensor),
90 context->getInputValue<float>(kAlphaScalar),
91 context->getOutputBuffer<float>(kOutputTensor),
92 context->getOutputShape(kOutputTensor));
93 default:
94 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation ELU";
95 }
96 }
97
98 } // namespace elu
99
100 NN_REGISTER_OPERATION(ELU, "ELU", elu::validate, elu::prepare, elu::execute);
101
102 } // namespace nn
103 } // namespace android
104