summaryrefslogtreecommitdiff
path: root/runtime/onert/backend/cpu/ops/OperationUtils.cc
blob: aa4ef352ef5ebd5495c33d76604ac568d331c43e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 "OperationUtils.h"

#include <algorithm>
#include <cassert>
#include <cmath>

namespace onert
{
namespace backend
{
namespace cpu
{
namespace ops
{

uint32_t getNumberOfDimensions(const IPortableTensor *tensor)
{
  assert(tensor);
  return tensor->getShape().rank();
}

uint32_t getNumberOfElements(const IPortableTensor *tensor)
{
  assert(tensor);
  uint32_t count = 1;
  auto shape = tensor->getShape();
  for (int i = 0; i < shape.rank(); i++)
  {
    count *= shape.dim(i);
  }
  return count;
}

uint32_t getSizeOfDimension(const IPortableTensor *tensor, uint32_t dimensionIdx)
{
  assert(tensor);
  auto shape = tensor->getShape();
  if (dimensionIdx >= static_cast<uint32_t>(shape.rank()))
  {
    // TODO, log the error
    return 0;
  }
  return shape.dim(dimensionIdx);
}

void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
{
  if (double_multiplier == 0.)
  {
    *quantized_multiplier = 0;
    *shift = 0;
    return;
  }
  const double q = std::frexp(double_multiplier, shift);
  auto q_fixed = static_cast<int64_t>(std::round(q * (1ll << 31)));

  assert(q_fixed <= (1ll << 31));
  if (q_fixed == (1ll << 31))
  {
    q_fixed /= 2;
    ++*shift;
  }
  assert(q_fixed <= std::numeric_limits<int32_t>::max());
  *quantized_multiplier = static_cast<int32_t>(q_fixed);
}

void GetQuantizedConvolutionMultiplier(const IPortableTensor *input, const IPortableTensor *filter,
                                       const IPortableTensor *bias, const IPortableTensor *output,
                                       double *multiplier)
{
  const double input_product_scale = input->data_scale() * filter->data_scale();
  const double bias_scale = (bias != nullptr) ? bias->data_scale() : input_product_scale;
  const double output_scale = output->data_scale();
  // The following conditions must be guaranteed by the training pipeline.
  UNUSED_RELEASE(bias_scale);
  assert(std::abs(input_product_scale - bias_scale) <=
         1e-6 * std::min(input_product_scale, bias_scale));
  assert(input_product_scale >= 0);
  assert(input_product_scale < output_scale);
  *multiplier = input_product_scale / output_scale;
}

void GetQuantizedConvolutionMultipliersAndShifts(
  float input_scale, float output_scale, const float *filter_scales, size_t filter_scales_size,
  int num_channels, std::vector<int32_t> &per_channel_output_multiplier,
  std::vector<int> &per_channel_output_shift)
{
  // Originates from tflite's PopulateConvolutionQuantizationParams()
  per_channel_output_multiplier.resize(num_channels);
  per_channel_output_shift.resize(num_channels);

  const bool is_per_channel = filter_scales_size > 1;
  auto per_channel_multiplier = per_channel_output_multiplier.data();
  auto per_channel_shift = per_channel_output_shift.data();
  for (int i = 0; i < num_channels; ++i)
  {
    // If per-tensor quantization parameter is specified, broadcast it along the
    // quantization dimension (channels_out).
    const float scale = is_per_channel ? filter_scales[i] : filter_scales[0];
    const double filter_scale = static_cast<double>(scale);
    const double effective_output_scale =
      static_cast<double>(input_scale) * filter_scale / static_cast<double>(output_scale);
    int32_t significand;
    int channel_shift;
    QuantizeMultiplier(effective_output_scale, &significand, &channel_shift);
    per_channel_multiplier[i] = significand;
    per_channel_shift[i] = channel_shift;
  }
}

void QuantizeMultiplierGreaterThanOne(double double_multiplier, int32_t *quantized_multiplier,
                                      int *left_shift)
{
  assert(double_multiplier > 1.);
  const double q = std::frexp(double_multiplier, left_shift);
  int64_t q_fixed = static_cast<int64_t>(std::round(q * (1ll << 31)));
  assert(q_fixed <= (1ll << 31));
  if (q_fixed == (1ll << 31))
  {
    q_fixed /= 2;
    ++*left_shift;
  }
  assert(*left_shift >= 0);
  assert(q_fixed <= std::numeric_limits<int32_t>::max());
  *quantized_multiplier = static_cast<int32_t>(q_fixed);
}

void CalculateActivationRangeQuantized(ir::Activation activation, const IPortableTensor *output,
                                       int32_t *act_min, int32_t *act_max)
{
  int32_t qmin = 0;
  int32_t qmax = 0;

  switch (output->data_type())
  {
    case OperandType::QUANT_UINT8_ASYMM:
      qmin = std::numeric_limits<uint8_t>::min();
      qmax = std::numeric_limits<uint8_t>::max();
      break;
    case OperandType::QUANT_INT8_ASYMM:
    case OperandType::QUANT_INT8_SYMM:
      qmin = std::numeric_limits<int8_t>::min();
      qmax = std::numeric_limits<int8_t>::max();
      break;
    default:
      throw std::runtime_error("CalculateActivationRangeQuantized: Not supported operand type.");
  }

  const auto scale = output->data_scale();
  const auto zero_point = output->data_zero_point();
  auto quantize = [scale, zero_point](float f) {
    return zero_point + static_cast<int32_t>(std::round(f / scale));
  };
  if (activation == ir::Activation::RELU)
  {
    *act_min = std::max(qmin, quantize(0.0));
    *act_max = qmax;
  }
  else if (activation == ir::Activation::RELU6)
  {
    *act_min = std::max(qmin, quantize(0.0));
    *act_max = std::min(qmax, quantize(6.0));
  }
  else if (activation == ir::Activation::RELU1)
  {
    *act_min = std::max(qmin, quantize(-1.0));
    *act_max = std::min(qmax, quantize(1.0));
  }
  else if (activation == ir::Activation::SIGMOID)
  {
    *act_min = std::max(qmin, quantize(0.0));
    *act_max = std::min(qmax, quantize(1.0));
  }
  else if (activation == ir::Activation::NONE)
  {
    *act_min = qmin;
    *act_max = qmax;
  }
  else
  {
    throw std::runtime_error{"Unsupported fused activation function."};
  }
}

bool HaveSameShapes(const IPortableTensor *input1, const IPortableTensor *input2)
{
  if (input1 == input2)
    return true;
  if (input2 == NULL || input2 == NULL)
    return false;

  if (input1 == NULL)
  {
    return (getNumberOfDimensions(input2) == 0);
  }

  if (getNumberOfDimensions(input1) != getNumberOfDimensions(input2))
    return false;

  auto shape1 = input1->getShape();
  auto shape2 = input2->getShape();
  for (uint32_t i = 0; i < getNumberOfDimensions(input1); i++)
    if (shape1.dim(i) != shape2.dim(i))
      return false;

  return true;
}

int32_t CalculateInputRadius(int input_integer_bits, int input_left_shift)
{
  const double max_input_rescaled = 1.0 * ((1 << input_integer_bits) - 1) *
                                    (1ll << (31 - input_integer_bits)) / (1ll << input_left_shift);
  // Tighten bound using floor.  Suppose that we could use the exact value.
  // After scaling the difference, the result would be at the maximum.  Thus we
  // must ensure that our value has lower magnitude.
  return static_cast<int32_t>(std::floor(max_input_rescaled));
}

uint32_t sizeOfData(OperandType type, const std::vector<int32_t> &dimensions)
{
  uint32_t size = 4;

  switch (type)
  {
    case OperandType::FLOAT32:
    case OperandType::INT32:
    case OperandType::UINT32:
      size = 4;
      break;
    case OperandType::BOOL8:
    case OperandType::QUANT_UINT8_ASYMM:
    case OperandType::QUANT_INT8_SYMM:
      size = 1;
      break;
    case OperandType::INT64:
      size = 8;
      break;
    default:
      throw std::runtime_error("Not supported operand type.");
      break;
  }

  for (auto d : dimensions)
  {
    assert(d >= 0);
    size *= static_cast<uint32_t>(d);
  }

  return size;
}

nnfw::cker::PaddingType getPaddingType(ir::PaddingType ir_padding_type)
{
  switch (ir_padding_type)
  {
    case ir::PaddingType::EXPLICIT:
      return nnfw::cker::PaddingType::kNone;
    case ir::PaddingType::SAME:
      return nnfw::cker::PaddingType::kSame;
    case ir::PaddingType::VALID:
      return nnfw::cker::PaddingType::kValid;
    default:
      throw std::runtime_error("Wrong padding type.");
      break;
  }
}

std::vector<int32_t> getReducerAxes(const IPortableTensor *axes)
{
  std::vector<int32_t> ret;

  auto axes_vals = (axes->getShape().rank() == 0) ? 1 : axes->getShape().dim(0);
  assert(axes->layout() == ir::Layout::NHWC);
  assert(static_cast<size_t>(axes_vals) == axes->getShape().num_elements());
  switch (axes->data_type())
  {
    case ir::DataType::INT32:
    {
      for (int i = 0; i < axes_vals; ++i)
        ret.emplace_back(*(getBuffer<int32_t>(axes) + i));
      break;
    }
    case ir::DataType::INT64:
    {
      for (int i = 0; i < axes_vals; ++i)
        ret.emplace_back(*(getBuffer<int64_t>(axes) + i));
      break;
    }
    default:
      throw std::runtime_error("getReducerAxes: Not supported data type");
      break;
  }
  return ret;
}

} // namespace ops
} // namespace cpu
} // namespace backend
} // namespace onert