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