ONE - On-device Neural Engine
Loading...
Searching...
No Matches
GreaterEqual.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
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
30GreaterEqual::GreaterEqual(const Tensor *x, const Tensor *y, Tensor *output)
31 : Kernel({x, y}, {output})
32{
33}
34
36{
37 LUCI_INTERPRETER_CHECK(x()->element_type() == y()->element_type());
38 LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::BOOL);
39
40 if (x()->element_type() == DataType::U8)
41 {
42 quantizeMultiplierSmallerThanOneExp(x()->scale(), &_x_multiplier, &_x_shift);
43 quantizeMultiplierSmallerThanOneExp(y()->scale(), &_y_multiplier, &_y_shift);
44 }
45 output()->resize(calculateShapeForBroadcast(x()->shape(), y()->shape()));
46}
47
49{
50 switch (x()->element_type())
51 {
52 case DataType::FLOAT32:
53 evalFloat();
54 break;
55 case DataType::S64:
56 evalInteger<int64_t>();
57 break;
58 case DataType::S32:
59 evalInteger<int32_t>();
60 break;
61 case DataType::U8:
62 evalQuantized();
63 break;
64 default:
65 throw std::runtime_error("luci-intp GreaterEqual Unsupported type.");
66 }
67}
68
69void GreaterEqual::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::Broadcast4DSlowGreaterEqual(op_params, getTensorShape(x()), x_data,
81 getTensorShape(y()), y_data,
82 getTensorShape(output()), output_data);
83 }
84 else
85 {
86 tflite::reference_ops::GreaterEqual(op_params, getTensorShape(x()), x_data, getTensorShape(y()),
87 y_data, getTensorShape(output()), output_data);
88 }
89}
90
91template <typename T> void GreaterEqual::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::Broadcast4DSlowGreaterEqualNoScaling(
103 op_params, getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
104 output_data);
105 }
106 else
107 {
108 tflite::reference_ops::GreaterEqualNoScaling(op_params, getTensorShape(x()), x_data,
109 getTensorShape(y()), y_data,
110 getTensorShape(output()), output_data);
111 }
112}
113
114void GreaterEqual::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::Broadcast4DSlowGreaterEqualWithScaling(
133 op_params, getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
134 output_data);
135 }
136 else
137 {
138 tflite::reference_ops::GreaterEqualWithScaling(op_params, getTensorShape(x()), x_data,
139 getTensorShape(y()), y_data,
140 getTensorShape(output()), output_data);
141 }
142}
143
144} // namespace kernels
145} // 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
GreaterEqual(const Tensor *x, const Tensor *y, Tensor *output)
#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