326a44f0c50d4d5705317764b93b7de97b972060
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / LogSoftMax.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 #ifndef __NNFW_CKER_LOGSOFTMAX_H__
18 #define __NNFW_CKER_LOGSOFTMAX_H__
19
20 #include "cker/Shape.h"
21 #include "cker/Utils.h"
22 #include "cker/Types.h"
23 #include "cker/eigen/Utils.h"
24
25 #include <Eigen/Core>
26 #include <fixedpoint/fixedpoint.h>
27 #include <cmath>
28
29 namespace nnfw
30 {
31 namespace cker
32 {
33
34 inline void LogSoftmax(const SoftmaxParams &params, const Shape &input_shape,
35                        const float *input_data, const Shape &output_shape, float *output_data)
36 {
37   const int rank = input_shape.DimensionsCount();
38   const int axis = (params.axis < 0) ? params.axis + rank : params.axis;
39   const double beta = params.beta;
40   const int depth = MatchingDim(input_shape, axis, output_shape, axis);
41
42   int outer_size = 1;
43   for (int i = 0; i < axis; ++i)
44   {
45     outer_size *= input_shape.Dims(i);
46   }
47
48   int inner_size = 1;
49   for (int i = axis + 1; i < rank; ++i)
50   {
51     inner_size *= input_shape.Dims(i);
52   }
53
54   for (int i = 0; i < outer_size; ++i)
55   {
56     for (int j = 0; j < inner_size; ++j)
57     {
58       float max = std::numeric_limits<float>::lowest();
59       for (int c = 0; c < depth; ++c)
60       {
61         max = std::max(max, input_data[(i * depth + c) * inner_size]);
62       }
63
64       float sum = 0.f;
65       for (int c = 0; c < depth; ++c)
66       {
67         sum += std::exp((input_data[(i * depth + c) * inner_size + j] - max) * beta);
68       }
69
70       const float log_sum = std::log(sum);
71       for (int c = 0; c < depth; ++c)
72       {
73         output_data[(i * depth + c) * inner_size + j] =
74             (input_data[(i * depth + c) * inner_size + j] - max) * beta - log_sum;
75       }
76     }
77   }
78 }
79
80 inline void LogSoftmax(const SoftmaxParams &params, float input_scale, const Shape &input_shape,
81                        const uint8_t *input_data, const Shape &output_shape, uint8_t *output_data)
82 {
83   const int rank = input_shape.DimensionsCount();
84   const int axis = (params.axis < 0) ? params.axis + rank : params.axis;
85   const double beta = params.beta;
86   const int depth = MatchingDim(input_shape, axis, output_shape, axis);
87
88   const int32_t clamp_max = std::numeric_limits<uint8_t>::max();
89   const int32_t clamp_min = std::numeric_limits<uint8_t>::min();
90
91   int outer_size = 1;
92   for (int i = 0; i < axis; ++i)
93   {
94     outer_size *= input_shape.Dims(i);
95   }
96
97   int inner_size = 1;
98   for (int i = axis + 1; i < rank; ++i)
99   {
100     inner_size *= input_shape.Dims(i);
101   }
102
103   for (int i = 0; i < outer_size; ++i)
104   {
105     for (int j = 0; j < inner_size; ++j)
106     {
107       uint8_t max_val = std::numeric_limits<uint8_t>::min();
108       for (int c = 0; c < depth; ++c)
109       {
110         max_val = std::max(max_val, input_data[(i * depth + c) * inner_size]);
111       }
112
113       float sum_exp = 0.0f;
114       const int32_t max_uint8 = std::numeric_limits<uint8_t>::max();
115       const float *table_offset = &params.table[max_uint8 - max_val];
116       for (int c = 0; c < depth; ++c)
117       {
118         sum_exp += table_offset[input_data[(i * depth + c) * inner_size]];
119       }
120       const float log_sum_exp = std::log(sum_exp);
121
122       const float scale = input_scale / params.scale;
123       const float precomputed = (input_scale * max_val * beta + log_sum_exp) / params.scale;
124       for (int c = 0; c < depth; ++c)
125       {
126         const float log_prob =
127             scale * input_data[(i * depth + c) * inner_size] * beta - precomputed;
128         const int32_t prob_quantized = std::rint(log_prob) + params.zero_point;
129         output_data[(i * depth + c) * inner_size] =
130             static_cast<uint8_t>(std::max(std::min(clamp_max, prob_quantized), clamp_min));
131       }
132     }
133   }
134 }
135
136 } // namespace cker
137 } // namespace nnfw
138
139 #endif // __NNFW_CKER_LOGSOFTMAX_H__