blob: ef7186aad14b982d42085ffa816eca4280992810 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2016-2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +000024
Matthew Bentham314d3e22023-06-23 10:53:52 +000025#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Matthew Benthamf1aeab92023-05-30 13:35:34 +000027#include "arm_compute/core/ActivationLayerInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000028#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/utils/StringUtils.h"
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +000030
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <algorithm>
32#include <cmath>
33#include <cstdint>
34#include <fstream>
35#include <map>
36#include <string>
37
SiCong Lie357a252020-08-09 20:05:52 +010038namespace arm_compute
39{
40std::string read_file(const std::string &filename, bool binary)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42 std::string out;
43 std::ifstream fs;
44
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000045#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 try
47 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000048#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
50 std::ios_base::openmode mode = std::ios::in;
51
52 if(binary)
53 {
54 mode |= std::ios::binary;
55 }
56
57 fs.open(filename, mode);
58
59 // Go to the end of the file
60 fs.seekg(0, std::ios::end);
61 // Reserve the memory required to store the file's content
62 out.reserve(fs.tellg());
63 // Go back to the beginning of the file
64 fs.seekg(0, std::ios::beg);
65 // Copy the content of the file
66 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000067#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 }
69 catch(const std::ifstream::failure &e)
70 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010071 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000073#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
75 return out;
76}
77
SiCong Lie357a252020-08-09 20:05:52 +010078const std::string &string_from_channel(Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079{
80 static std::map<Channel, const std::string> channels_map =
81 {
82 { Channel::UNKNOWN, "UNKNOWN" },
83 { Channel::R, "R" },
84 { Channel::G, "G" },
85 { Channel::B, "B" },
86 { Channel::A, "A" },
87 { Channel::Y, "Y" },
88 { Channel::U, "U" },
89 { Channel::V, "V" },
90 { Channel::C0, "C0" },
91 { Channel::C1, "C1" },
92 { Channel::C2, "C2" },
93 { Channel::C3, "C3" }
94 };
95
96 return channels_map[channel];
97}
98
SiCong Lie357a252020-08-09 20:05:52 +010099const std::string &string_from_border_mode(BorderMode border_mode)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100{
101 static std::map<BorderMode, const std::string> border_mode_map =
102 {
103 { BorderMode::UNDEFINED, "UNDEFINED" },
104 { BorderMode::CONSTANT, "CONSTANT" },
105 { BorderMode::REPLICATE, "REPLICATE" },
106 };
107
108 return border_mode_map[border_mode];
109}
110
SiCong Lie357a252020-08-09 20:05:52 +0100111const std::string &string_from_norm_type(NormType type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112{
113 static std::map<NormType, const std::string> norm_type_map =
114 {
115 { NormType::IN_MAP_1D, "IN_MAP_1D" },
116 { NormType::IN_MAP_2D, "IN_MAP_2D" },
117 { NormType::CROSS_MAP, "CROSS_MAP" },
118 };
119
120 return norm_type_map[type];
121}
122
SiCong Lie357a252020-08-09 20:05:52 +0100123const std::string &string_from_pooling_type(PoolingType type)
Georgios Pinitascdf51452017-08-31 14:21:36 +0100124{
125 static std::map<PoolingType, const std::string> pool_type_map =
126 {
127 { PoolingType::MAX, "MAX" },
128 { PoolingType::AVG, "AVG" },
129 { PoolingType::L2, "L2" },
130 };
131
132 return pool_type_map[type];
133}
134
SiCongLic4270cf2021-12-22 11:22:40 +0000135bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info)
136{
137 if(info.is_global_pooling || info.exclude_padding || info.pool_size.x() == 0 || info.pool_size.y() == 0)
138 {
139 return false;
140 }
141 const auto ps = info.pad_stride_info;
142 const auto pool_le_padding_x = info.pool_size.x() <= std::max({ ps.pad_left(), ps.pad_right() });
143 const auto pool_le_padding_y = info.pool_size.y() <= std::max({ ps.pad_top(), ps.pad_bottom() });
144 return pool_le_padding_x || pool_le_padding_y;
145}
146
ramelg0137515692022-02-26 22:06:20 +0000147bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info)
148{
149 if(info.is_global_pooling || info.pool_size.x() == 0 || info.pool_size.y() == 0 || info.pool_size.z() == 0)
150 {
151 return false;
152 }
153 const auto ps = info.padding;
154 const auto pool_le_padding_x = info.pool_size.x() <= std::max({ ps.left, ps.right });
155 const auto pool_le_padding_y = info.pool_size.y() <= std::max({ ps.top, ps.bottom });
156 const auto pool_le_padding_z = info.pool_size.z() <= std::max({ ps.front, ps.back });
157 return pool_le_padding_x || pool_le_padding_y || pool_le_padding_z;
158}
159
SiCong Lie357a252020-08-09 20:05:52 +0100160const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100161{
162 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
163 {
164 { GEMMLowpOutputStageType::NONE, "" },
165 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
166 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
167 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
168 };
169
170 return output_stage_map[output_stage];
171}
172
SiCong Lie357a252020-08-09 20:05:52 +0100173std::string string_from_pixel_value(const PixelValue &value, const DataType data_type)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100174{
175 std::stringstream ss;
176 std::string converted_string;
177
178 switch(data_type)
179 {
180 case DataType::U8:
181 case DataType::QASYMM8:
182 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
183 ss << uint32_t(value.get<uint8_t>());
184 converted_string = ss.str();
185 break;
186 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100187 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100188 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100189 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
190 ss << int32_t(value.get<int8_t>());
191 converted_string = ss.str();
192 break;
193 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100194 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100195 ss << value.get<uint16_t>();
196 converted_string = ss.str();
197 break;
198 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100199 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100200 ss << value.get<int16_t>();
201 converted_string = ss.str();
202 break;
203 case DataType::U32:
204 ss << value.get<uint32_t>();
205 converted_string = ss.str();
206 break;
207 case DataType::S32:
208 ss << value.get<int32_t>();
209 converted_string = ss.str();
210 break;
211 case DataType::F32:
212 converted_string = float_to_string_with_full_precision(value.get<float>());
213 break;
214 case DataType::F16:
215 static_assert(sizeof(half) == 2, "Half must be 16 bit");
216 ss << value.get<half>();
217 converted_string = ss.str();
218 break;
219 default:
220 ARM_COMPUTE_ERROR("Not handled");
221 }
222
223 return converted_string;
224}
225
SiCong Lie357a252020-08-09 20:05:52 +0100226PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation,
227 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000228{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000229 const auto &strides = conv_info.stride();
230 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1.");
231
SiCong Lie357a252020-08-09 20:05:52 +0100232 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
233 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena17203582019-08-02 16:00:41 +0100234 const unsigned int in_width = input_shape[width_idx];
235 const unsigned int in_height = input_shape[height_idx];
236 const unsigned int kernel_width = weights_shape[width_idx];
237 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100238
239 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100240 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
241 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
242 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100243
244 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100245 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
246 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100247
248 // Calculate total pad
Giorgio Arena17203582019-08-02 16:00:41 +0100249 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
250 const int pad_height = std::max(0, static_cast<int>((out_height - 1) * strides.second + real_weight_height - in_height));
Georgios Pinitas4c758512019-07-10 19:49:11 +0100251
252 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100253 const unsigned int pad_left = pad_width / 2;
254 const unsigned int pad_top = pad_height / 2;
255 const unsigned int pad_right = pad_width - pad_left;
256 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000257
Giorgio Arena17203582019-08-02 16:00:41 +0100258 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
259
260 // Check for correctness of predicted output shape against the one calculated using the generated info
261 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
262 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
263 ARM_COMPUTE_UNUSED(out_dims);
264
265 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000266}
267
SiCong Lie357a252020-08-09 20:05:52 +0100268std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
269 unsigned int kernel_width, unsigned int kernel_height,
270 const PadStrideInfo &pad_stride_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100271{
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100272 const unsigned int pad_left = pad_stride_info.pad_left();
273 const unsigned int pad_top = pad_stride_info.pad_top();
274 const unsigned int pad_right = pad_stride_info.pad_right();
275 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
276 const unsigned int stride_x = pad_stride_info.stride().first;
277 const unsigned int stride_y = pad_stride_info.stride().second;
278
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100279 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100280 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right));
281 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom));
282 const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right);
283 const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100284
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100285 return std::make_pair<unsigned int, unsigned int>(w, h);
286}
287
SiCong Lie357a252020-08-09 20:05:52 +0100288std::pair<unsigned int, unsigned int> scaled_dimensions(int width, int height,
289 int kernel_width, int kernel_height,
290 const PadStrideInfo &pad_stride_info,
291 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292{
Georgios Pinitas80838f12019-12-12 18:23:13 +0000293 const int dilation_x = dilation.x();
294 const int dilation_y = dilation.y();
295 const int pad_left = pad_stride_info.pad_left();
296 const int pad_top = pad_stride_info.pad_top();
297 const int pad_right = pad_stride_info.pad_right();
298 const int pad_bottom = pad_stride_info.pad_bottom();
299 const int stride_x = pad_stride_info.stride().first;
300 const int stride_y = pad_stride_info.stride().second;
301 int w = 0;
302 int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100303 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304 {
305 case DimensionRoundingType::FLOOR:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000306 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
307 h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - (dilation_y * (kernel_height - 1) + 1)) / stride_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 break;
309 case DimensionRoundingType::CEIL:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000310 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
311 h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - (dilation_y * (kernel_height - 1) + 1)) / stride_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312 break;
313 default:
314 ARM_COMPUTE_ERROR("Unsupported rounding type");
315 }
316
Georgios Pinitas80838f12019-12-12 18:23:13 +0000317 w = std::max(1, w);
318 h = std::max(1, h);
319 return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320}
321
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100322std::pair<int, int> scaled_dimensions_signed(int width, int height,
323 int kernel_width, int kernel_height,
324 const PadStrideInfo &pad_stride_info)
325{
326 const int pad_left = pad_stride_info.pad_left();
327 const int pad_top = pad_stride_info.pad_top();
328 const int pad_right = pad_stride_info.pad_right();
329 const int pad_bottom = pad_stride_info.pad_bottom();
330 const int stride_x = pad_stride_info.stride().first;
331 const int stride_y = pad_stride_info.stride().second;
332 int w = 0;
333 int h = 0;
334 switch(pad_stride_info.round())
335 {
336 case DimensionRoundingType::FLOOR:
337 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
338 h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
339 break;
340 case DimensionRoundingType::CEIL:
341 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
342 h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
343 break;
344 default:
345 ARM_COMPUTE_ERROR("Unsupported rounding type");
346 }
347
348 return std::make_pair(static_cast<int>(w), static_cast<int>(h));
349}
350
ramelg0137515692022-02-26 22:06:20 +0000351std::tuple<int, int, int> scaled_3d_dimensions_signed(int width, int height, int depth,
352 int kernel_width, int kernel_height, int kernel_depth,
353 const Pooling3dLayerInfo &pool3d_info)
354{
355 const int pad_left = pool3d_info.padding.left;
356 const int pad_top = pool3d_info.padding.top;
357 const int pad_right = pool3d_info.padding.right;
358 const int pad_bottom = pool3d_info.padding.bottom;
359 const int pad_front = pool3d_info.padding.front;
360 const int pad_back = pool3d_info.padding.back;
361 const int stride_x = pool3d_info.stride.x();
362 const int stride_y = pool3d_info.stride.y();
363 const int stride_z = pool3d_info.stride.z();
364 int w = 0;
365 int h = 0;
366 int d = 0;
367
368 switch(pool3d_info.round_type)
369 {
370 case DimensionRoundingType::FLOOR:
371 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
372 h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
373 d = static_cast<int>(std::floor((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1));
374 break;
375 case DimensionRoundingType::CEIL:
376 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
377 h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
378 d = static_cast<int>(std::ceil((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1));
379 break;
380 default:
381 ARM_COMPUTE_ERROR("Unsupported rounding type");
382 }
383
384 return std::make_tuple(static_cast<int>(w), static_cast<int>(h), static_cast<int>(d));
385}
386
SiCong Lie357a252020-08-09 20:05:52 +0100387bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100388{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100389 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
390 const bool is_quantized_type = is_data_type_quantized(dt);
391 const bool is_first_dim = (axis == 0);
392
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100393 return !is_first_dim || is_min_max || is_quantized_type;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100394}
395
SiCong Lie357a252020-08-09 20:05:52 +0100396QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000397{
398 // Note: Output quantization info for softmax should always have
399 // * Softmax with QASYMM8: scale = 1/256, offset = 0
400 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
401 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
402 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
403 if(is_data_type_quantized_asymmetric_signed(input_type))
404 {
405 if(is_log)
406 {
407 return QuantizationInfo(16.f / 256, 127);
408 }
409 else
410 {
411 return QuantizationInfo(1.f / 256, -128);
412 }
413 }
414 return QuantizationInfo(1.f / 256, 0);
415}
416
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000417std::pair<int32_t, int32_t> get_quantized_activation_min_max(const ActivationLayerInfo& act_info, DataType data_type, UniformQuantizationInfo oq_info)
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000418{
419 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
420 const auto a = act_info.a();
421 const auto b = act_info.b();
422 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
423 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
424 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
425
426 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int;
427 const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
428
429 return std::make_pair(min_activation, max_activation);
430}
431
Giorgio Arena4112eed2020-10-23 14:24:26 +0100432std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensor *> tensors)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100433{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100434 std::unordered_map<const ITensorInfo *, PaddingSize> res;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100435
436 for(const ITensor *tensor : tensors)
437 {
438 if(tensor)
439 {
Giorgio Arena4112eed2020-10-23 14:24:26 +0100440 res.insert({ tensor->info(), tensor->info()->padding() });
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100441 }
442 }
443
444 return res;
445}
446
Giorgio Arena4112eed2020-10-23 14:24:26 +0100447std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100448{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100449 std::unordered_map<const ITensorInfo *, PaddingSize> res;
450
451 for(const ITensorInfo *info : infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100452 {
Giorgio Arena4112eed2020-10-23 14:24:26 +0100453 if(info)
454 {
455 res.insert({ info, info->padding() });
456 }
457 }
458
459 return res;
460}
461
462bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map)
463{
464 return std::find_if(padding_map.begin(), padding_map.end(), [](const std::pair<const ITensorInfo *, PaddingSize> &padding_info)
465 {
466 return (padding_info.first->padding() != padding_info.second);
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100467 })
468 != padding_map.end();
469}
470
giuros01edc21e42018-11-16 14:45:31 +0000471#ifdef ARM_COMPUTE_ASSERTS_ENABLED
SiCong Lie357a252020-08-09 20:05:52 +0100472void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100473{
474 switch(dt)
475 {
476 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100477 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100478 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
479 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100480 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100481 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100482 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100483 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
485 break;
486 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100487 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100488 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
489 break;
490 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100491 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100492 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
493 break;
494 case DataType::U32:
495 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
496 break;
497 case DataType::S32:
498 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
499 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000500 case DataType::BFLOAT16:
501 print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100502 break;
503 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100504 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000506 case DataType::F32:
507 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
508 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100509 default:
510 ARM_COMPUTE_ERROR("Undefined element size for given data type");
511 }
512}
513
SiCong Lie357a252020-08-09 20:05:52 +0100514int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100515{
516 switch(dt)
517 {
518 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100519 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100520 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100521 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100522 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100523 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100524 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100525 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
526 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100527 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100528 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100529 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100530 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100531 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
532 case DataType::U32:
533 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
534 case DataType::S32:
535 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000536 case DataType::BFLOAT16:
537 return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100538 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100539 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000540 case DataType::F32:
541 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100542 default:
543 ARM_COMPUTE_ERROR("Undefined element size for given data type");
544 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000545 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100546}
giuros01edc21e42018-11-16 14:45:31 +0000547#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
SiCong Lie357a252020-08-09 20:05:52 +0100548
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000549} // namespace arm_compute