d5f99178a880550697b910eac849fa45c631e447
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / LogSoftmax.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/LogSoftmax.h"
19
20 #include "kernels/Utils.h"
21
22 #include <tensorflow/lite/kernels/internal/reference/log_softmax.h>
23
24 #include "PALLogSoftmax.h"
25
26 namespace luci_interpreter
27 {
28 namespace kernels
29 {
30
31 LogSoftmax::LogSoftmax(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
32
33 void LogSoftmax::configure()
34 {
35   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
36   if (input()->element_type() == DataType::U8)
37   {
38     LUCI_INTERPRETER_CHECK(output()->scale() == 16. / 256);
39     LUCI_INTERPRETER_CHECK(output()->zero_point() == 255);
40
41     tflite::SoftmaxParams params{};
42
43     params.table = _table;
44     params.beta = 1.0;
45     luci_interpreter_pal::PopulateSoftmaxLookupTable(&params, input()->scale(), params.beta);
46   }
47   // TODO: enable it only if kernel with dynamic shapes
48   output()->resize(input()->shape());
49 }
50
51 void LogSoftmax::execute() const
52 {
53   switch (input()->element_type())
54   {
55     case DataType::FLOAT32:
56       evalFloat();
57       break;
58     case DataType::U8:
59       evalQuantized();
60       break;
61     default:
62       assert(false && "Unsupported type.");
63   }
64 }
65
66 void LogSoftmax::evalFloat() const
67 {
68   tflite::SoftmaxParams params{};
69   tflite::reference_ops::LogSoftmax(params, getTensorShape(input()), getTensorData<float>(input()),
70                                     getTensorShape(output()), getTensorData<float>(output()));
71 }
72
73 void LogSoftmax::evalQuantized() const
74 {
75   const auto input_shape = getTensorShape(input());
76   const auto output_shape = getTensorShape(output());
77   const auto input_scale = input()->scale();
78   uint8_t *output_data = getTensorData<uint8_t>(output());
79   const uint8_t *input_data = getTensorData<uint8_t>(input());
80   const float beta = 1.0;
81
82   tflite::SoftmaxParams params{};
83
84   params.table = const_cast<float *>(_table);
85   params.zero_point = output()->zero_point();
86   params.scale = output()->scale();
87
88   luci_interpreter_pal::InitializeParams(&params, input_scale, beta);
89   luci_interpreter_pal::LogSoftmax(params, input_scale, input_shape, input_data, output_shape,
90                                    output_data);
91 }
92
93 } // namespace kernels
94 } // namespace luci_interpreter