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