Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / LogSoftMaxLayer.cc
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 #include "LogSoftMaxLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/LogSoftMax.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 LogSoftMaxLayer::LogSoftMaxLayer() : _input(nullptr), _output(nullptr), _beta(0.0), _axis(0)
33 {
34   // DO NOTHING
35 }
36
37 void LogSoftMaxLayer::PopulateLookupTable(const float kBeta)
38 {
39   const float scale = -_input->data_scale() * kBeta;
40   const int32_t max_uint8 = std::numeric_limits<uint8_t>::max();
41   for (int32_t val = 0; val <= max_uint8; ++val)
42   {
43     _table[max_uint8 - val] = expf(scale * val);
44   }
45 }
46
47 void LogSoftMaxLayer::logsoftmaxFloat32()
48 {
49   nnfw::cker::SoftmaxParams op_params;
50   op_params.beta = _beta;
51   op_params.axis = _axis;
52   nnfw::cker::LogSoftmax(op_params, getTensorShape(_input),
53                          reinterpret_cast<const float *>(_input->buffer()), getTensorShape(_output),
54                          reinterpret_cast<float *>(_output->buffer()));
55 }
56
57 void LogSoftMaxLayer::logsoftmaxQuant8()
58 {
59   nnfw::cker::SoftmaxParams op_params;
60   op_params.beta = _beta;
61   op_params.axis = _axis;
62   op_params.table = _table;
63   op_params.zero_point = _output->data_offset();
64   op_params.scale = _output->data_scale();
65   nnfw::cker::LogSoftmax(op_params, _input->data_scale(), getTensorShape(_input),
66                          reinterpret_cast<const uint8_t *>(_input->buffer()),
67                          getTensorShape(_output), reinterpret_cast<uint8_t *>(_output->buffer()));
68 }
69
70 void LogSoftMaxLayer::configure(const IPortableTensor *input, const float beta, const int axis,
71                                 IPortableTensor *output)
72 {
73   _input = input;
74   _output = output;
75   _beta = beta;
76   _axis = axis;
77   if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
78   {
79     PopulateLookupTable(_beta);
80   }
81 }
82
83 void LogSoftMaxLayer::run()
84 {
85   if (_input->data_type() == OperandType::FLOAT32)
86   {
87     logsoftmaxFloat32();
88   }
89   else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
90   {
91     logsoftmaxQuant8();
92   }
93   else
94   {
95     throw std::runtime_error{"LogSoftmax : unsupported data type"};
96   }
97 }
98
99 } // namespace ops
100 } // namespace cpu
101 } // namespace backend
102 } // namespace onert