ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Less.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 "Builders.h"
18#include "ComparisonCommon.h"
19#include "TISOKernel.h"
20
21namespace luci_interpreter
22{
23
24namespace
25{
26#ifndef DIS_QUANT
27void evalQuantized(const circle::Tensor *x, const circle::Tensor *y, const circle::Tensor *output,
28 BaseRuntimeGraph *runtime_graph)
29{
30 auto x_data = kernels::getTensorData<uint8_t>(runtime_graph->getDataByTensor(x));
31 if (x_data == nullptr)
32 x_data = kernels::getTensorData<uint8_t>(runtime_graph->getConstDataByTensor(x));
33
34 assert(x_data != nullptr);
35
36 auto y_data = kernels::getTensorData<uint8_t>(runtime_graph->getDataByTensor(y));
37 if (y_data == nullptr)
38 y_data = kernels::getTensorData<uint8_t>(runtime_graph->getConstDataByTensor(y));
39
40 assert(y_data != nullptr);
41
42 auto output_data = kernels::getTensorData<bool>(runtime_graph->getDataByTensor(output));
43
44 int32_t x_multiplier;
45 int x_shift;
46
47 int32_t y_multiplier;
48 int y_shift;
49
50 kernels::quantizeMultiplierSmallerThanOneExp(Tensor::scale(x), &x_multiplier, &x_shift);
51 kernels::quantizeMultiplierSmallerThanOneExp(Tensor::scale(y), &y_multiplier, &y_shift);
52
54 op_params.left_shift = 8;
55 op_params.input1_offset = -Tensor::zero_point(x); // Note the '-'
56 op_params.input1_shift = x_shift;
57 op_params.input1_multiplier = x_multiplier;
58 op_params.input2_offset = -Tensor::zero_point(y); // Note the '-'
59 op_params.input2_shift = y_shift;
60 op_params.input2_multiplier = y_multiplier;
61 op_params.is_broadcast = Tensor::num_elements(x) != Tensor::num_elements(y);
62
63 if (op_params.is_broadcast)
64 {
65 luci_interpreter_pal::BroadcastComparison4DSlowWithScaling<uint8_t>(
66 op_params, kernels::getTensorShape(x), x_data, kernels::getTensorShape(y), y_data,
68 }
69 else
70 {
71 const int64_t flat_size = kernels::getTensorShape(x).flatSize();
72 luci_interpreter_pal::ComparisonWithScaling<uint8_t>(op_params, flat_size, x_data, y_data,
73 output_data, luci_interpreter_pal::LessFn);
74 }
75}
76#endif // DIS_QUANT
77
78} // namespace
79
80void configure_kernel_CircleLess(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
81{
82 kernels::TISOKernel kernel(cur_op, runtime_graph);
83
84 LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
85 Tensor::element_type(kernel.input2()));
86 LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.output()) == DataType::BOOL);
87}
88
89void execute_kernel_CircleLess(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
90{
91 kernels::TISOKernel kernel(cur_op, runtime_graph);
92
93 switch (Tensor::element_type(kernel.input1()))
94 {
95 case DataType::S64:
96 kernels::evalComparisonGeneric<int64_t>(kernel.input1(), kernel.input2(), kernel.output(),
97 runtime_graph, luci_interpreter_pal::LessFn);
98 break;
99 case DataType::S32:
100 kernels::evalComparisonGeneric<int32_t>(kernel.input1(), kernel.input2(), kernel.output(),
101 runtime_graph, luci_interpreter_pal::LessFn);
102 break;
103#ifndef DIS_QUANT
104 case DataType::U8:
105 evalQuantized(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
106 break;
107#endif // DIS_QUANT
108#ifndef DIS_FLOAT
109 case DataType::FLOAT32:
110 kernels::evalComparisonGeneric<float>(kernel.input1(), kernel.input2(), kernel.output(),
111 runtime_graph, luci_interpreter_pal::LessFn);
112 break;
113#endif // DIS_FLOAT
114 default:
115 assert(false && "Unsupported type.");
116 }
117}
118
119} // namespace luci_interpreter
const circle::Tensor * output() const
Definition TISOKernel.h:62
const circle::Tensor * input2() const
Definition TISOKernel.h:61
const circle::Tensor * input1() const
Definition TISOKernel.h:60
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
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
bool LessFn(T lhs, T rhs)
void configure_kernel_CircleLess(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition Less.cpp:80
void execute_kernel_CircleLess(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition Less.cpp:89
RuntimeGraph BaseRuntimeGraph