ONE - On-device Neural Engine
Loading...
Searching...
No Matches
NotEqual.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/NotEqual.h"
18#include "kernels/Utils.h"
19
20#include <tensorflow/lite/kernels/internal/reference/comparisons.h>
21
22#include <stdexcept>
23
24namespace luci_interpreter
25{
26
27namespace kernels
28{
29
30NotEqual::NotEqual(const Tensor *x, const Tensor *y, Tensor *output) : Kernel({x, y}, {output}) {}
31
33{
34 LUCI_INTERPRETER_CHECK(x()->element_type() == y()->element_type());
35 LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::BOOL);
36
37 if (x()->element_type() == DataType::U8)
38 {
39 quantizeMultiplierSmallerThanOneExp(x()->scale(), &_x_multiplier, &_x_shift);
40 quantizeMultiplierSmallerThanOneExp(y()->scale(), &_y_multiplier, &_y_shift);
41 }
42 output()->resize(calculateShapeForBroadcast(x()->shape(), y()->shape()));
43}
44
46{
47 switch (x()->element_type())
48 {
49 case DataType::FLOAT32:
50 evalFloat();
51 break;
52 case DataType::S64:
53 evalInteger<int64_t>();
54 break;
55 case DataType::S32:
56 evalInteger<int32_t>();
57 break;
58 case DataType::U8:
59 evalQuantized();
60 break;
61 case DataType::BOOL:
62 evalBool();
63 break;
64 default:
65 throw std::runtime_error("luci-intp NotEqual Unsupported type.");
66 }
67}
68
69void NotEqual::evalFloat() const
70{
71 const auto x_data = getTensorData<float>(x());
72 const auto y_data = getTensorData<float>(y());
73 auto output_data = getTensorData<bool>(output());
74
75 tflite::ComparisonParams op_params;
76 op_params.is_broadcast = x()->shape() != y()->shape();
77
78 if (op_params.is_broadcast)
79 {
80 tflite::reference_ops::Broadcast4DSlowNotEqual(op_params, getTensorShape(x()), x_data,
81 getTensorShape(y()), y_data,
82 getTensorShape(output()), output_data);
83 }
84 else
85 {
86 tflite::reference_ops::NotEqual(op_params, getTensorShape(x()), x_data, getTensorShape(y()),
87 y_data, getTensorShape(output()), output_data);
88 }
89}
90
91template <typename T> void NotEqual::evalInteger() const
92{
93 const auto x_data = getTensorData<T>(x());
94 const auto y_data = getTensorData<T>(y());
95 auto output_data = getTensorData<bool>(output());
96
97 tflite::ComparisonParams op_params;
98 op_params.is_broadcast = x()->shape() != y()->shape();
99
100 if (op_params.is_broadcast)
101 {
102 tflite::reference_ops::Broadcast4DSlowNotEqualNoScaling(op_params, getTensorShape(x()), x_data,
103 getTensorShape(y()), y_data,
104 getTensorShape(output()), output_data);
105 }
106 else
107 {
108 tflite::reference_ops::NotEqualNoScaling(op_params, getTensorShape(x()), x_data,
109 getTensorShape(y()), y_data, getTensorShape(output()),
110 output_data);
111 }
112}
113
114void NotEqual::evalQuantized() const
115{
116 const auto x_data = getTensorData<uint8_t>(x());
117 const auto y_data = getTensorData<uint8_t>(y());
118 auto output_data = getTensorData<bool>(output());
119
120 tflite::ComparisonParams op_params;
121 op_params.left_shift = 8;
122 op_params.input1_offset = -x()->zero_point(); // Note the '-'
123 op_params.input1_shift = _x_shift;
124 op_params.input1_multiplier = _x_multiplier;
125 op_params.input2_offset = -y()->zero_point(); // Note the '-'
126 op_params.input2_shift = _y_shift;
127 op_params.input2_multiplier = _y_multiplier;
128 op_params.is_broadcast = x()->shape() != y()->shape();
129
130 if (op_params.is_broadcast)
131 {
132 tflite::reference_ops::Broadcast4DSlowNotEqualWithScaling(
133 op_params, getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
134 output_data);
135 }
136 else
137 {
138 tflite::reference_ops::NotEqualWithScaling(op_params, getTensorShape(x()), x_data,
139 getTensorShape(y()), y_data,
140 getTensorShape(output()), output_data);
141 }
142}
143
144void NotEqual::evalBool() const
145{
146 const auto x_data = getTensorData<bool>(x());
147 const auto y_data = getTensorData<bool>(y());
148 auto output_data = getTensorData<bool>(output());
149
150 tflite::ComparisonParams op_params;
151 op_params.is_broadcast = x()->shape() != y()->shape();
152
153 if (op_params.is_broadcast)
154 {
155 tflite::reference_ops::Broadcast4DSlowNotEqualNoScaling(op_params, getTensorShape(x()), x_data,
156 getTensorShape(y()), y_data,
157 getTensorShape(output()), output_data);
158 }
159 else
160 {
161 tflite::reference_ops::NotEqualNoScaling(op_params, getTensorShape(x()), x_data,
162 getTensorShape(y()), y_data, getTensorShape(output()),
163 output_data);
164 }
165}
166
167} // namespace kernels
168} // namespace luci_interpreter
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
int32_t zero_point() const
Definition Tensor.h:115
NotEqual(const Tensor *x, const Tensor *y, Tensor *output)
Definition NotEqual.cpp:30
const Tensor * x() const
Definition NotEqual.h:32
void execute() const override
Definition NotEqual.cpp:45
const Tensor * y() const
Definition NotEqual.h:33
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
Shape calculateShapeForBroadcast(const Shape &input1_shape, const Shape &input2_shape)
Definition Utils.cpp:204
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
void quantizeMultiplierSmallerThanOneExp(double double_multiplier, int32_t *quantized_multiplier, int *left_shift)
Definition Utils.cpp:193