summaryrefslogtreecommitdiff
path: root/compiler/luci-interpreter/src/kernels/Sub.cpp
blob: 1fd583c62c78b5dd51c600b991aaab344858aa8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "kernels/Sub.h"
#include "kernels/Utils.h"

#include "PALSub.h"

#include <tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h>

#include <stdexcept>

namespace luci_interpreter
{
namespace kernels
{

Sub::Sub(const Tensor *input1, const Tensor *input2, Tensor *output, const SubParams &params)
  : KernelWithParams<SubParams>({input1, input2}, {output}, params)
{
}

void Sub::configure()
{
  LUCI_INTERPRETER_CHECK(!(input1()->element_type() != input2()->element_type()))
  LUCI_INTERPRETER_CHECK(!(input1()->element_type() != output()->element_type()))
  output()->resize(calculateShapeForBroadcast(input1()->shape(), input2()->shape()));
}

void Sub::execute() const
{
  switch (input1()->element_type())
  {
    case DataType::FLOAT32:
      evalFloat();
      break;
    case DataType::S64:
      evalInteger<int64_t>();
      break;
    case DataType::S32:
      evalInteger<int32_t>();
      break;
    case DataType::U8:
      evalQuantized();
      break;
    default:
      throw std::runtime_error("Unsupported type.");
  }
}

void Sub::evalFloat() const
{
  tflite::ArithmeticParams params{};
  fillArithmeticActivationRange<float>(params, _params.activation);

  const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
    getTensorShape(input1()), getTensorShape(input2()), &params);

  if (need_broadcast)
  {
    tflite::reference_ops::BroadcastSubSlow(
      params, getTensorShape(input1()), getTensorData<float>(input1()), getTensorShape(input2()),
      getTensorData<float>(input2()), getTensorShape(output()), getTensorData<float>(output()));
  }
  else
  {
    luci_interpreter_pal::Sub(params, getTensorShape(input1()), getTensorData<float>(input1()),
                              getTensorShape(input2()), getTensorData<float>(input2()),
                              getTensorShape(output()), getTensorData<float>(output()));
  }
}

template <typename T> void Sub::evalInteger() const
{
  tflite::ArithmeticParams params{};
  fillArithmeticActivationRange<T>(params, _params.activation);

  const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
    getTensorShape(input1()), getTensorShape(input2()), &params);

  if (need_broadcast)
  {
    tflite::reference_ops::BroadcastSubSlow(
      params, getTensorShape(input1()), getTensorData<T>(input1()), getTensorShape(input2()),
      getTensorData<T>(input2()), getTensorShape(output()), getTensorData<T>(output()));
  }
  else
  {
    tflite::reference_ops::Sub(params, getTensorShape(input1()), getTensorData<T>(input1()),
                               getTensorShape(input2()), getTensorData<T>(input2()),
                               getTensorShape(output()), getTensorData<T>(output()));
  }
}

void Sub::evalQuantized() const
{
  const auto input1_scale = static_cast<double>(input1()->scale());
  const auto input2_scale = static_cast<double>(input2()->scale());
  const auto output_scale = static_cast<double>(output()->scale());

  const int left_shift = 20;
  const double twice_max_input_scale = 2 * std::max(input1_scale, input2_scale);
  const double real_input1_multiplier = input1_scale / twice_max_input_scale;
  const double real_input2_multiplier = input2_scale / twice_max_input_scale;
  const double real_output_multiplier = twice_max_input_scale / ((1 << left_shift) * output_scale);

  int32_t input1_multiplier{}, input2_multiplier{}, output_multiplier{};
  int input1_shift{}, input2_shift{}, output_shift{};
  quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, &input1_multiplier, &input1_shift);
  quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, &input2_multiplier, &input2_shift);
  quantizeMultiplierSmallerThanOneExp(real_output_multiplier, &output_multiplier, &output_shift);

  int32_t activation_min{};
  int32_t activation_max{};
  calculateActivationRangeQuantized(_params.activation, output(), &activation_min, &activation_max);

  tflite::ArithmeticParams params{};
  params.left_shift = left_shift;
  // The kernel expects inputs' zero points to be negated.
  params.input1_offset = -input1()->zero_point(); // Note the '-'.
  params.input1_multiplier = input1_multiplier;
  params.input1_shift = input1_shift;
  params.input2_offset = -input2()->zero_point(); // Note the '-'.
  params.input2_multiplier = input2_multiplier;
  params.input2_shift = input2_shift;
  params.output_offset = output()->zero_point();
  params.output_multiplier = output_multiplier;
  params.output_shift = output_shift;
  params.quantized_activation_min = activation_min;
  params.quantized_activation_max = activation_max;

  const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
    getTensorShape(input1()), getTensorShape(input2()), &params);

  if (need_broadcast)
  {
    tflite::reference_ops::BroadcastQuantSubSlow(
      params, getTensorShape(input1()), getTensorData<uint8_t>(input1()), getTensorShape(input2()),
      getTensorData<uint8_t>(input2()), getTensorShape(output()), getTensorData<uint8_t>(output()));
  }
  else
  {
    tflite::reference_ops::Sub(params, getTensorShape(input1()), getTensorData<uint8_t>(input1()),
                               getTensorShape(input2()), getTensorData<uint8_t>(input2()),
                               getTensorShape(output()), getTensorData<uint8_t>(output()));
  }
}

} // namespace kernels
} // namespace luci_interpreter