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 "Elu.h"
20 
21 #include <algorithm>
22 #include <cmath>
23 #include <vector>
24 
25 #include "IndexedShapeWrapper.h"
26 #include "OperationResolver.h"
27 #include "OperationsExecutionUtils.h"
28 #include "Tracing.h"
29 
30 namespace android {
31 namespace nn {
32 namespace elu {
33 namespace {
34 
35 template <typename T>
eluFloat(const T * inputData,const Shape & inputShape,const T alpha,T * outputData,const Shape &)36 bool eluFloat(const T* inputData, const Shape& inputShape, const T alpha, T* outputData,
37               const Shape& /*outputShape*/) {
38     NNTRACE_COMP("ELU");
39     int numElements = getNumberOfElements(inputShape);
40     for (int i = 0; i < numElements; ++i) {
41         float x = static_cast<float>(inputData[i]);
42         outputData[i] = static_cast<T>(std::max(0.f, x) + std::min(0.f, alpha * (std::exp(x) - 1)));
43     }
44     return true;
45 }
46 
47 }  // namespace
48 
prepare(IOperationExecutionContext * context)49 bool prepare(IOperationExecutionContext* context) {
50     Shape inputShape = context->getInputShape(kInputTensor);
51     return context->setOutputShape(kOutputTensor, inputShape);
52 }
53 
execute(IOperationExecutionContext * context)54 bool execute(IOperationExecutionContext* context) {
55     // Bypass execution in the case of zero-sized input.
56     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
57     switch (context->getInputType(kInputTensor)) {
58         case OperandType::TENSOR_FLOAT16:
59             return eluFloat(context->getInputBuffer<_Float16>(kInputTensor),
60                             context->getInputShape(kInputTensor),
61                             context->getInputValue<_Float16>(kAlphaScalar),
62                             context->getOutputBuffer<_Float16>(kOutputTensor),
63                             context->getOutputShape(kOutputTensor));
64         case OperandType::TENSOR_FLOAT32:
65             return eluFloat(context->getInputBuffer<float>(kInputTensor),
66                             context->getInputShape(kInputTensor),
67                             context->getInputValue<float>(kAlphaScalar),
68                             context->getOutputBuffer<float>(kOutputTensor),
69                             context->getOutputShape(kOutputTensor));
70         default:
71             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation ELU";
72     }
73 }
74 
75 }  // namespace elu
76 
77 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(ELU, elu::prepare, elu::execute);
78 
79 }  // namespace nn
80 }  // namespace android
81