ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Div.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/Div.h"
18
19#include "kernels/Utils.h"
20
21#include <tensorflow/lite/kernels/internal/reference/div.h>
22#include <tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h>
23
24namespace luci_interpreter
25{
26namespace kernels
27{
28
29Div::Div(const Tensor *input1, const Tensor *input2, Tensor *output, const DivParams &params)
30 : KernelWithParams<DivParams>({input1, input2}, {output}, params)
31{
32}
33
35{
36 LUCI_INTERPRETER_CHECK(input1()->element_type() == input2()->element_type());
37 LUCI_INTERPRETER_CHECK(input1()->element_type() == output()->element_type());
38
39 output()->resize(calculateShapeForBroadcast(input1()->shape(), input2()->shape()));
40}
41
42void Div::execute() const
43{
44 switch (input1()->element_type())
45 {
46 case DataType::FLOAT32:
47 evalFloat();
48 break;
49 case DataType::S64:
50 evalInteger<int64_t>();
51 break;
52 case DataType::S32:
53 evalInteger<int32_t>();
54 break;
55 case DataType::U8:
56 evalQuantized();
57 break;
58 default:
59 throw std::runtime_error("luci-intp Div Unsupported type.");
60 }
61}
62
63void Div::evalFloat() const
64{
65 tflite::ArithmeticParams params{};
66 fillArithmeticActivationRange<float>(params, _params.activation);
67
68 const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
70
71 if (need_broadcast)
72 {
73 tflite::reference_ops::BroadcastDivSlow(
74 params, getTensorShape(input1()), getTensorData<float>(input1()), getTensorShape(input2()),
75 getTensorData<float>(input2()), getTensorShape(output()), getTensorData<float>(output()));
76 }
77 else
78 {
79 tflite::reference_ops::Div(params, getTensorShape(input1()), getTensorData<float>(input1()),
80 getTensorShape(input2()), getTensorData<float>(input2()),
81 getTensorShape(output()), getTensorData<float>(output()));
82 }
83}
84
85template <typename T> void Div::evalInteger() const
86{
87 tflite::ArithmeticParams params{};
88 fillArithmeticActivationRange<T>(params, _params.activation);
89
90 const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
92
93 if (need_broadcast)
94 {
95 tflite::reference_ops::BroadcastDivSlow(
96 params, getTensorShape(input1()), getTensorData<T>(input1()), getTensorShape(input2()),
97 getTensorData<T>(input2()), getTensorShape(output()), getTensorData<T>(output()));
98 }
99 else
100 {
101 tflite::reference_ops::Div(params, getTensorShape(input1()), getTensorData<T>(input1()),
102 getTensorShape(input2()), getTensorData<T>(input2()),
103 getTensorShape(output()), getTensorData<T>(output()));
104 }
105}
106
107void Div::evalQuantized() const
108{
109 const auto input1_scale = static_cast<double>(input1()->scale());
110 const auto input2_scale = static_cast<double>(input2()->scale());
111 const auto output_scale = static_cast<double>(output()->scale());
112
113 const double real_output_multiplier = input1_scale / (input2_scale * output_scale);
114
115 int32_t output_multiplier{};
116 int output_shift{};
117
118 quantizeMultiplier(real_output_multiplier, &output_multiplier, &output_shift);
119
120 int32_t activation_min{};
121 int32_t activation_max{};
122 calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);
123
124 tflite::ArithmeticParams params{};
125
126 params.input1_offset = -input1()->zero_point(); // Note the '-'.
127 params.input2_offset = -input2()->zero_point(); // Note the '-'.
128 params.output_offset = output()->zero_point();
129 params.output_multiplier = output_multiplier;
130 params.output_shift = output_shift;
131 params.quantized_activation_min = activation_min;
132 params.quantized_activation_max = activation_max;
133
134 const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
136
137 if (need_broadcast)
138 {
139 tflite::reference_ops::BroadcastDivSlow(
140 params, getTensorShape(input1()), getTensorData<uint8_t>(input1()), getTensorShape(input2()),
141 getTensorData<uint8_t>(input2()), getTensorShape(output()), getTensorData<uint8_t>(output()));
142 }
143 else
144 {
145 tflite::reference_ops::Div(params, getTensorShape(input1()), getTensorData<uint8_t>(input1()),
146 getTensorShape(input2()), getTensorData<uint8_t>(input2()),
147 getTensorShape(output()), getTensorData<uint8_t>(output()));
148 }
149}
150
151} // namespace kernels
152} // namespace luci_interpreter
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
float scale() const
Definition Tensor.h:109
int32_t zero_point() const
Definition Tensor.h:115
Div(const Tensor *input1, const Tensor *input2, Tensor *output, const DivParams &params)
Definition Div.cpp:29
Tensor * output() const
Definition Div.h:35
const Tensor * input2() const
Definition Div.h:34
void configure() override
Definition Div.cpp:34
void execute() const override
Definition Div.cpp:42
const Tensor * input1() const
Definition Div.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 calculateActivationRangeQuantized(Activation activation, const Tensor *output, int32_t *activation_min, int32_t *activation_max)
Definition Utils.cpp:119
void quantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
Definition Utils.cpp:157