ONE - On-device Neural Engine
Loading...
Searching...
No Matches
LessEqual.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/LessEqual.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
30LessEqual::LessEqual(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 default:
62 throw std::runtime_error("luci-intp LessEqual Unsupported type.");
63 }
64}
65
66void LessEqual::evalFloat() const
67{
68 const auto x_data = getTensorData<float>(x());
69 const auto y_data = getTensorData<float>(y());
70 auto output_data = getTensorData<bool>(output());
71
72 tflite::ComparisonParams op_params;
73 op_params.is_broadcast = x()->shape() != y()->shape();
74
75 if (op_params.is_broadcast)
76 {
77 tflite::reference_ops::Broadcast4DSlowLessEqual(op_params, getTensorShape(x()), x_data,
78 getTensorShape(y()), y_data,
79 getTensorShape(output()), output_data);
80 }
81 else
82 {
83 tflite::reference_ops::LessEqual(op_params, getTensorShape(x()), x_data, getTensorShape(y()),
84 y_data, getTensorShape(output()), output_data);
85 }
86}
87
88template <typename T> void LessEqual::evalInteger() const
89{
90 const auto x_data = getTensorData<T>(x());
91 const auto y_data = getTensorData<T>(y());
92 auto output_data = getTensorData<bool>(output());
93
94 tflite::ComparisonParams op_params;
95 op_params.is_broadcast = x()->shape() != y()->shape();
96
97 if (op_params.is_broadcast)
98 {
99 tflite::reference_ops::Broadcast4DSlowLessEqualNoScaling(op_params, getTensorShape(x()), x_data,
100 getTensorShape(y()), y_data,
101 getTensorShape(output()), output_data);
102 }
103 else
104 {
105 tflite::reference_ops::LessEqualNoScaling(op_params, getTensorShape(x()), x_data,
106 getTensorShape(y()), y_data, getTensorShape(output()),
107 output_data);
108 }
109}
110
111void LessEqual::evalQuantized() const
112{
113 const auto x_data = getTensorData<uint8_t>(x());
114 const auto y_data = getTensorData<uint8_t>(y());
115 auto output_data = getTensorData<bool>(output());
116
117 tflite::ComparisonParams op_params;
118 op_params.left_shift = 8;
119 op_params.input1_offset = -x()->zero_point(); // Note the '-'
120 op_params.input1_shift = _x_shift;
121 op_params.input1_multiplier = _x_multiplier;
122 op_params.input2_offset = -y()->zero_point(); // Note the '-'
123 op_params.input2_shift = _y_shift;
124 op_params.input2_multiplier = _y_multiplier;
125 op_params.is_broadcast = x()->shape() != y()->shape();
126
127 if (op_params.is_broadcast)
128 {
129 tflite::reference_ops::Broadcast4DSlowLessEqualWithScaling(
130 op_params, getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
131 output_data);
132 }
133 else
134 {
135 tflite::reference_ops::LessEqualWithScaling(op_params, getTensorShape(x()), x_data,
136 getTensorShape(y()), y_data,
137 getTensorShape(output()), output_data);
138 }
139}
140
141} // namespace kernels
142} // 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
LessEqual(const Tensor *x, const Tensor *y, Tensor *output)
Definition LessEqual.cpp:30
#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