ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Softmax.cpp
Go to the documentation of this file.
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 "kernels/Softmax.h"
18
19#include "kernels/Utils.h"
20
21#include <tensorflow/lite/kernels/internal/reference/softmax.h>
22#include "PALSoftmax.h"
23
24#include <stdexcept>
25
26namespace luci_interpreter
27{
28
29namespace kernels
30{
31
32Softmax::Softmax(const Tensor *input, Tensor *output, const SoftmaxParams &params)
33 : KernelWithParams<SoftmaxParams>({input}, {output}, params)
34{
35}
36
38{
39 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40 LUCI_INTERPRETER_CHECK(input()->shape().num_dims() >= 1);
41 if (input()->element_type() == DataType::U8 || input()->element_type() == DataType::S8)
42 {
43 LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::S8 || output()->zero_point() == 0);
44 LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::U8 ||
45 output()->zero_point() == std::numeric_limits<int8_t>::min());
46 tflite::SoftmaxParams op_params{};
47 op_params.table = _table;
48 luci_interpreter_pal::PopulateSoftmaxLookupTable(&op_params, input()->scale(), params().beta);
49 }
50 output()->resize(input()->shape());
51}
52
53void Softmax::execute() const
54{
55 switch (input()->element_type())
56 {
57 case DataType::FLOAT32:
58 evalFloat();
59 break;
60 case DataType::S8:
61 evalQuantized<int8_t>();
62 break;
63 case DataType::U8:
64 evalQuantized<uint8_t>();
65 break;
66 default:
67 throw std::runtime_error("luci-intp Softmax Unsupported type.");
68 }
69}
70
71void Softmax::evalFloat() const
72{
73 tflite::SoftmaxParams op_params{};
74 op_params.beta = params().beta;
75
76 tflite::reference_ops::Softmax(op_params, getTensorShape(input()), getTensorData<float>(input()),
77 getTensorShape(output()), getTensorData<float>(output()));
78}
79
80template <typename T> void Softmax::evalQuantized() const
81{
82 tflite::SoftmaxParams op_params{};
83 op_params.table = const_cast<float *>(_table);
84 op_params.zero_point = output()->zero_point();
85 op_params.scale = output()->scale();
86 luci_interpreter_pal::InitializeParams(&op_params, input()->scale(), params().beta);
87 luci_interpreter_pal::Softmax(op_params, getTensorShape(input()), getTensorData<T>(input()),
88 getTensorShape(output()), getTensorData<T>(output()));
89}
90
91} // namespace kernels
92} // namespace luci_interpreter
const SoftmaxParams & params() const
Definition Kernel.h:67
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
float scale() const
Definition Tensor.h:109
int32_t zero_point() const
Definition Tensor.h:115
const Tensor * input() const
Definition Softmax.h:33
Softmax(const Tensor *input, Tensor *output, const SoftmaxParams &params)
Definition Softmax.cpp:32
void execute() const override
Definition Softmax.cpp:53
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194