2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/Softmax.h"
19 #include "kernels/Utils.h"
21 #include <tensorflow/lite/kernels/internal/reference/softmax.h>
22 #include "PALSoftmax.h"
26 namespace luci_interpreter
32 Softmax::Softmax(const Tensor *input, Tensor *output, const SoftmaxParams ¶ms)
33 : KernelWithParams<SoftmaxParams>({input}, {output}, params)
37 void Softmax::configure()
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)
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);
50 output()->resize(input()->shape());
53 void Softmax::execute() const
55 switch (input()->element_type())
57 case DataType::FLOAT32:
61 evalQuantized<int8_t>();
64 evalQuantized<uint8_t>();
67 throw std::runtime_error("Unsupported type.");
71 void Softmax::evalFloat() const
73 tflite::SoftmaxParams op_params{};
74 op_params.beta = params().beta;
76 tflite::reference_ops::Softmax(op_params, getTensorShape(input()), getTensorData<float>(input()),
77 getTensorShape(output()), getTensorData<float>(output()));
80 template <typename T> void Softmax::evalQuantized() const
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()));
91 } // namespace kernels
92 } // namespace luci_interpreter