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 "HalInterfaces.h"
20 #include "OperationResolver.h"
21 #include "OperationsUtils.h"
22 #include "Tracing.h"
23 
24 #include <cmath>
25 
26 namespace android {
27 namespace nn {
28 namespace log_softmax {
29 
30 constexpr char kOperationName[] = "LOG_SOFTMAX";
31 
32 constexpr uint32_t kNumInputs = 3;
33 constexpr uint32_t kInputTensor = 0;
34 constexpr uint32_t kInputBeta = 1;
35 constexpr uint32_t kInputAxis = 2;
36 
37 constexpr uint32_t kNumOutputs = 1;
38 constexpr uint32_t kOutputTensor = 0;
39 
40 template <typename T>
compute(const T * input,const Shape & shape,T beta,uint32_t axis,T * output)41 inline bool compute(const T* input, const Shape& shape, T beta, uint32_t axis, T* output) {
42     const uint32_t outerSize = getNumberOfElements(shape, 0, axis);
43     const uint32_t axisSize = getSizeOfDimension(shape, axis);
44     const uint32_t innerSize = getNumberOfElements(shape, axis + 1, getNumberOfDimensions(shape));
45     for (uint32_t outer = 0; outer < outerSize; ++outer) {
46         for (uint32_t inner = 0; inner < innerSize; ++inner) {
47             // We subtract the maximum value from each element to ensure
48             // numerical stability, taking advantage of the following equality:
49             // exp(x[i])/sum(exp(x[i])) == exp(x[i]+C)/sum(exp(x[i]+C))
50             T maxValue = input[outer * axisSize * innerSize + inner];
51             for (uint32_t i = 1; i < axisSize; ++i) {
52                 maxValue = std::max(maxValue, input[(outer * axisSize + i) * innerSize + inner]);
53             }
54 
55             T sum = 0;
56             for (uint32_t i = 0; i < axisSize; ++i) {
57                 sum += std::exp(static_cast<double>(
58                         (input[(outer * axisSize + i) * innerSize + inner] - maxValue) * beta));
59             }
60 
61             const T logSum = std::log(static_cast<double>(sum));
62             for (uint32_t i = 0; i < axisSize; ++i) {
63                 output[(outer * axisSize + i) * innerSize + inner] =
64                         (input[(outer * axisSize + i) * innerSize + inner] - maxValue) * beta -
65                         logSum;
66             }
67         }
68     }
69     return true;
70 }
71 
validate(const IOperationValidationContext * context)72 bool validate(const IOperationValidationContext* context) {
73     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
74     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
75     OperandType inputType = context->getInputType(kInputTensor);
76     std::vector<OperandType> inExpectedTypes;
77     std::vector<OperandType> outExpectedTypes;
78     if (inputType == OperandType::TENSOR_FLOAT32) {
79         inExpectedTypes = {OperandType::TENSOR_FLOAT32, OperandType::FLOAT32, OperandType::INT32};
80         outExpectedTypes = {OperandType::TENSOR_FLOAT32};
81     } else if (inputType == OperandType::TENSOR_FLOAT16) {
82         inExpectedTypes = {OperandType::TENSOR_FLOAT16, OperandType::FLOAT16, OperandType::INT32};
83         outExpectedTypes = {OperandType::TENSOR_FLOAT16};
84     } else {
85         LOG(ERROR) << "Unsupported input tensor type for operation " << kOperationName;
86         return false;
87     }
88     NN_RET_CHECK(validateInputTypes(context, inExpectedTypes));
89     NN_RET_CHECK(validateOutputTypes(context, outExpectedTypes));
90     return validateHalVersion(context, HalVersion::V1_2);
91 }
92 
prepare(IOperationExecutionContext * context)93 bool prepare(IOperationExecutionContext* context) {
94     return context->setOutputShape(kOutputTensor, context->getInputShape(kInputTensor));
95 }
96 
execute(IOperationExecutionContext * context)97 bool execute(IOperationExecutionContext* context) {
98     int32_t axis = context->getInputValue<int32_t>(kInputAxis);
99     NN_RET_CHECK(handleNegativeAxis(context->getInputShape(kInputTensor), &axis));
100     switch (context->getInputType(kInputTensor)) {
101         case OperandType::TENSOR_FLOAT16:
102             return compute(context->getInputBuffer<_Float16>(kInputTensor),
103                            context->getInputShape(kInputTensor),
104                            context->getInputValue<_Float16>(kInputBeta), axis,
105                            context->getOutputBuffer<_Float16>(kOutputTensor));
106         case OperandType::TENSOR_FLOAT32:
107             return compute(context->getInputBuffer<float>(kInputTensor),
108                            context->getInputShape(kInputTensor),
109                            context->getInputValue<float>(kInputBeta), axis,
110                            context->getOutputBuffer<float>(kOutputTensor));
111         default:
112             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
113     }
114 }
115 
116 }  // namespace log_softmax
117 
118 NN_REGISTER_OPERATION(LOG_SOFTMAX, log_softmax::kOperationName, log_softmax::validate,
119                       log_softmax::prepare, log_softmax::execute);
120 
121 }  // namespace nn
122 }  // namespace android
123