ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Sign.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 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/Sign.h"
18
19#include "kernels/Utils.h"
20
21#include <cmath>
22
23namespace luci_interpreter
24{
25namespace kernels
26{
27
28namespace
29{
30
31template <typename T>
32inline void CalcSign(const T *input_data, const size_t num_elements, T *output_data)
33{
34 for (size_t i = 0; i < num_elements; ++i)
35 {
36 const T x = input_data[i];
37 if constexpr (std::is_floating_point_v<T>)
38 {
39 if (std::isnan(x))
40 {
41 output_data[i] = x; // NaN -> NaN
42 continue;
43 }
44 }
45 output_data[i] = (T(0) < x) - (x < T(0)); // -1/0/1
46 }
47}
48
49} // namespace
50
51Sign::Sign(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
52
54{
55 auto et = input()->element_type();
56 LUCI_INTERPRETER_CHECK(et == DataType::FLOAT32 || et == DataType::FLOAT64 || et == DataType::S32);
57 LUCI_INTERPRETER_CHECK(et == output()->element_type());
58 output()->resize(input()->shape());
59}
60
61void Sign::execute() const
62{
63 switch (input()->element_type())
64 {
65 case DataType::S32:
66 evalS32();
67 break;
68 case DataType::FLOAT32:
69 evalFloat32();
70 break;
71 case DataType::FLOAT64:
72 evalFloat64();
73 break;
74 default:
75 throw std::runtime_error("luci-intp Sign Unsupported type.");
76 }
77}
78
79template <typename T> void Sign::eval() const
80{
81 const int size = tflite::MatchingFlatSize(getTensorShape(input()), getTensorShape(output()));
82 CalcSign(getTensorData<T>(input()), static_cast<size_t>(size), getTensorData<T>(output()));
83}
84
85void Sign::evalS32() const { eval<int32_t>(); }
86void Sign::evalFloat32() const { eval<float>(); }
87void Sign::evalFloat64() const { eval<double>(); }
88
89} // namespace kernels
90} // namespace luci_interpreter
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
DataType element_type() const
Definition Tensor.h:105
void execute() const override
Definition Sign.cpp:61
void configure() override
Definition Sign.cpp:53
Tensor * output() const
Definition Sign.h:34
const Tensor * input() const
Definition Sign.h:33
Sign(const Tensor *input, Tensor *output)
Definition Sign.cpp:51
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
list input_data
Definition infer.py:29
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
T must_cast(loco::Node *node)
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
int32_t size[5]
Definition Slice.cpp:35