blob: 532d08de924ac795985784171c96ff671e58c29b [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 Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/utils/StringUtils.h"
SiCong Li91295492023-07-21 18:16:13 +010029#include "arm_compute/function_info/ActivationLayerInfo.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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (binary)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 {
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 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 catch (const std::ifstream::failure &e)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 {
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{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 static std::map<Channel, const std::string> channels_map = {{Channel::UNKNOWN, "UNKNOWN"},
81 {Channel::R, "R"},
82 {Channel::G, "G"},
83 {Channel::B, "B"},
84 {Channel::A, "A"},
85 {Channel::Y, "Y"},
86 {Channel::U, "U"},
87 {Channel::V, "V"},
88 {Channel::C0, "C0"},
89 {Channel::C1, "C1"},
90 {Channel::C2, "C2"},
91 {Channel::C3, "C3"}};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
93 return channels_map[channel];
94}
95
SiCong Lie357a252020-08-09 20:05:52 +010096const std::string &string_from_border_mode(BorderMode border_mode)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 static std::map<BorderMode, const std::string> border_mode_map = {
99 {BorderMode::UNDEFINED, "UNDEFINED"},
100 {BorderMode::CONSTANT, "CONSTANT"},
101 {BorderMode::REPLICATE, "REPLICATE"},
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 };
103
104 return border_mode_map[border_mode];
105}
106
SiCong Lie357a252020-08-09 20:05:52 +0100107const std::string &string_from_norm_type(NormType type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 static std::map<NormType, const std::string> norm_type_map = {
110 {NormType::IN_MAP_1D, "IN_MAP_1D"},
111 {NormType::IN_MAP_2D, "IN_MAP_2D"},
112 {NormType::CROSS_MAP, "CROSS_MAP"},
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 };
114
115 return norm_type_map[type];
116}
117
SiCong Lie357a252020-08-09 20:05:52 +0100118const std::string &string_from_pooling_type(PoolingType type)
Georgios Pinitascdf51452017-08-31 14:21:36 +0100119{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 static std::map<PoolingType, const std::string> pool_type_map = {
121 {PoolingType::MAX, "MAX"},
122 {PoolingType::AVG, "AVG"},
123 {PoolingType::L2, "L2"},
Georgios Pinitascdf51452017-08-31 14:21:36 +0100124 };
125
126 return pool_type_map[type];
127}
128
SiCongLic4270cf2021-12-22 11:22:40 +0000129bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info)
130{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 if (info.is_global_pooling || info.exclude_padding || info.pool_size.x() == 0 || info.pool_size.y() == 0)
SiCongLic4270cf2021-12-22 11:22:40 +0000132 {
133 return false;
134 }
135 const auto ps = info.pad_stride_info;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 const auto pool_le_padding_x = info.pool_size.x() <= std::max({ps.pad_left(), ps.pad_right()});
137 const auto pool_le_padding_y = info.pool_size.y() <= std::max({ps.pad_top(), ps.pad_bottom()});
SiCongLic4270cf2021-12-22 11:22:40 +0000138 return pool_le_padding_x || pool_le_padding_y;
139}
140
ramelg0137515692022-02-26 22:06:20 +0000141bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info)
142{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 if (info.is_global_pooling || info.pool_size.x() == 0 || info.pool_size.y() == 0 || info.pool_size.z() == 0)
ramelg0137515692022-02-26 22:06:20 +0000144 {
145 return false;
146 }
147 const auto ps = info.padding;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 const auto pool_le_padding_x = info.pool_size.x() <= std::max({ps.left, ps.right});
149 const auto pool_le_padding_y = info.pool_size.y() <= std::max({ps.top, ps.bottom});
150 const auto pool_le_padding_z = info.pool_size.z() <= std::max({ps.front, ps.back});
ramelg0137515692022-02-26 22:06:20 +0000151 return pool_le_padding_x || pool_le_padding_y || pool_le_padding_z;
152}
153
SiCong Lie357a252020-08-09 20:05:52 +0100154const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100155{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map = {
157 {GEMMLowpOutputStageType::NONE, ""},
158 {GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down"},
159 {GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint"},
160 {GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float"}};
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100161
162 return output_stage_map[output_stage];
163}
164
SiCong Lie357a252020-08-09 20:05:52 +0100165std::string string_from_pixel_value(const PixelValue &value, const DataType data_type)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100166{
167 std::stringstream ss;
168 std::string converted_string;
169
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 switch (data_type)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100171 {
172 case DataType::U8:
173 case DataType::QASYMM8:
174 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
175 ss << uint32_t(value.get<uint8_t>());
176 converted_string = ss.str();
177 break;
178 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100179 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100180 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100181 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
182 ss << int32_t(value.get<int8_t>());
183 converted_string = ss.str();
184 break;
185 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100186 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100187 ss << value.get<uint16_t>();
188 converted_string = ss.str();
189 break;
190 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100191 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100192 ss << value.get<int16_t>();
193 converted_string = ss.str();
194 break;
195 case DataType::U32:
196 ss << value.get<uint32_t>();
197 converted_string = ss.str();
198 break;
199 case DataType::S32:
200 ss << value.get<int32_t>();
201 converted_string = ss.str();
202 break;
203 case DataType::F32:
204 converted_string = float_to_string_with_full_precision(value.get<float>());
205 break;
206 case DataType::F16:
207 static_assert(sizeof(half) == 2, "Half must be 16 bit");
208 ss << value.get<half>();
209 converted_string = ss.str();
210 break;
211 default:
212 ARM_COMPUTE_ERROR("Not handled");
213 }
214
215 return converted_string;
216}
217
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100218PadStrideInfo calculate_same_pad(TensorShape input_shape,
219 TensorShape weights_shape,
220 PadStrideInfo conv_info,
221 DataLayout data_layout,
222 const Size2D &dilation,
SiCong Lie357a252020-08-09 20:05:52 +0100223 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000224{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000225 const auto &strides = conv_info.stride();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1),
227 "Stride values should be greater than or equal to 1.");
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000228
SiCong Lie357a252020-08-09 20:05:52 +0100229 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
230 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena17203582019-08-02 16:00:41 +0100231 const unsigned int in_width = input_shape[width_idx];
232 const unsigned int in_height = input_shape[height_idx];
233 const unsigned int kernel_width = weights_shape[width_idx];
234 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100235
236 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100237 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
238 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
239 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100240
241 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100242 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
243 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100244
245 // Calculate total pad
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100246 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
247 const int pad_height =
248 std::max(0, static_cast<int>((out_height - 1) * strides.second + real_weight_height - in_height));
Georgios Pinitas4c758512019-07-10 19:49:11 +0100249
250 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100251 const unsigned int pad_left = pad_width / 2;
252 const unsigned int pad_top = pad_height / 2;
253 const unsigned int pad_right = pad_width - pad_left;
254 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000255
Giorgio Arena17203582019-08-02 16:00:41 +0100256 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
257
258 // Check for correctness of predicted output shape against the one calculated using the generated info
259 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
260 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
261 ARM_COMPUTE_UNUSED(out_dims);
262
263 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000264}
265
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100266std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width,
267 unsigned int in_height,
268 unsigned int kernel_width,
269 unsigned int kernel_height,
SiCong Lie357a252020-08-09 20:05:52 +0100270 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100288std::pair<unsigned int, unsigned int> scaled_dimensions(int width,
289 int height,
290 int kernel_width,
291 int kernel_height,
SiCong Lie357a252020-08-09 20:05:52 +0100292 const PadStrideInfo &pad_stride_info,
293 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294{
Georgios Pinitas80838f12019-12-12 18:23:13 +0000295 const int dilation_x = dilation.x();
296 const int dilation_y = dilation.y();
297 const int pad_left = pad_stride_info.pad_left();
298 const int pad_top = pad_stride_info.pad_top();
299 const int pad_right = pad_stride_info.pad_right();
300 const int pad_bottom = pad_stride_info.pad_bottom();
301 const int stride_x = pad_stride_info.stride().first;
302 const int stride_y = pad_stride_info.stride().second;
303 int w = 0;
304 int h = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100305 switch (pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 {
307 case DimensionRoundingType::FLOOR:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100308 w = static_cast<int>(std::floor(
309 (static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) +
310 1));
311 h = static_cast<int>(
312 std::floor((static_cast<float>(height + pad_top + pad_bottom - (dilation_y * (kernel_height - 1) + 1)) /
313 stride_y) +
314 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315 break;
316 case DimensionRoundingType::CEIL:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100317 w = static_cast<int>(std::ceil(
318 (static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) +
319 1));
320 h = static_cast<int>(
321 std::ceil((static_cast<float>(height + pad_top + pad_bottom - (dilation_y * (kernel_height - 1) + 1)) /
322 stride_y) +
323 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 break;
325 default:
326 ARM_COMPUTE_ERROR("Unsupported rounding type");
327 }
328
Georgios Pinitas80838f12019-12-12 18:23:13 +0000329 w = std::max(1, w);
330 h = std::max(1, h);
331 return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332}
333
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100334std::pair<int, int> scaled_dimensions_signed(
335 int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info)
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100336{
337 const int pad_left = pad_stride_info.pad_left();
338 const int pad_top = pad_stride_info.pad_top();
339 const int pad_right = pad_stride_info.pad_right();
340 const int pad_bottom = pad_stride_info.pad_bottom();
341 const int stride_x = pad_stride_info.stride().first;
342 const int stride_y = pad_stride_info.stride().second;
343 int w = 0;
344 int h = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100345 switch (pad_stride_info.round())
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100346 {
347 case DimensionRoundingType::FLOOR:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100348 w = static_cast<int>(
349 std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
350 h = static_cast<int>(
351 std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100352 break;
353 case DimensionRoundingType::CEIL:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100354 w = static_cast<int>(
355 std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
356 h = static_cast<int>(
357 std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100358 break;
359 default:
360 ARM_COMPUTE_ERROR("Unsupported rounding type");
361 }
362
363 return std::make_pair(static_cast<int>(w), static_cast<int>(h));
364}
365
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100366std::tuple<int, int, int> scaled_3d_dimensions_signed(int width,
367 int height,
368 int depth,
369 int kernel_width,
370 int kernel_height,
371 int kernel_depth,
ramelg0137515692022-02-26 22:06:20 +0000372 const Pooling3dLayerInfo &pool3d_info)
373{
374 const int pad_left = pool3d_info.padding.left;
375 const int pad_top = pool3d_info.padding.top;
376 const int pad_right = pool3d_info.padding.right;
377 const int pad_bottom = pool3d_info.padding.bottom;
378 const int pad_front = pool3d_info.padding.front;
379 const int pad_back = pool3d_info.padding.back;
380 const int stride_x = pool3d_info.stride.x();
381 const int stride_y = pool3d_info.stride.y();
382 const int stride_z = pool3d_info.stride.z();
383 int w = 0;
384 int h = 0;
385 int d = 0;
386
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100387 switch (pool3d_info.round_type)
ramelg0137515692022-02-26 22:06:20 +0000388 {
389 case DimensionRoundingType::FLOOR:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100390 w = static_cast<int>(
391 std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
392 h = static_cast<int>(
393 std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
394 d = static_cast<int>(
395 std::floor((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1));
ramelg0137515692022-02-26 22:06:20 +0000396 break;
397 case DimensionRoundingType::CEIL:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100398 w = static_cast<int>(
399 std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
400 h = static_cast<int>(
401 std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
402 d = static_cast<int>(
403 std::ceil((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1));
ramelg0137515692022-02-26 22:06:20 +0000404 break;
405 default:
406 ARM_COMPUTE_ERROR("Unsupported rounding type");
407 }
408
409 return std::make_tuple(static_cast<int>(w), static_cast<int>(h), static_cast<int>(d));
410}
411
SiCong Lie357a252020-08-09 20:05:52 +0100412bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100413{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100414 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
415 const bool is_quantized_type = is_data_type_quantized(dt);
416 const bool is_first_dim = (axis == 0);
417
Gunes Bayir338ef462023-07-18 15:57:23 +0100418 return !is_first_dim || (is_quantized_type && !is_min_max);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100419}
420
SiCong Lie357a252020-08-09 20:05:52 +0100421QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000422{
423 // Note: Output quantization info for softmax should always have
424 // * Softmax with QASYMM8: scale = 1/256, offset = 0
425 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
426 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
427 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100428 if (is_data_type_quantized_asymmetric_signed(input_type))
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000429 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100430 if (is_log)
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000431 {
432 return QuantizationInfo(16.f / 256, 127);
433 }
434 else
435 {
436 return QuantizationInfo(1.f / 256, -128);
437 }
438 }
439 return QuantizationInfo(1.f / 256, 0);
440}
441
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100442std::pair<int32_t, int32_t> get_quantized_activation_min_max(const ActivationLayerInfo &act_info,
443 DataType data_type,
444 UniformQuantizationInfo oq_info)
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000445{
446 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
447 const auto a = act_info.a();
448 const auto b = act_info.b();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100449 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
450 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
451 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000452
Gunes Bayir06601722023-12-08 17:11:48 +0000453 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
454 ? std::min(oq_info.offset, type_max_value)
455 : b_int;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100456 const int32_t max_activation =
457 act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000458
459 return std::make_pair(min_activation, max_activation);
460}
461
Giorgio Arena4112eed2020-10-23 14:24:26 +0100462std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensor *> tensors)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100463{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100464 std::unordered_map<const ITensorInfo *, PaddingSize> res;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100465
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100466 for (const ITensor *tensor : tensors)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100467 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100468 if (tensor)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100469 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100470 res.insert({tensor->info(), tensor->info()->padding()});
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100471 }
472 }
473
474 return res;
475}
476
Giorgio Arena4112eed2020-10-23 14:24:26 +0100477std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100478{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100479 std::unordered_map<const ITensorInfo *, PaddingSize> res;
480
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100481 for (const ITensorInfo *info : infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100482 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100483 if (info)
Giorgio Arena4112eed2020-10-23 14:24:26 +0100484 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100485 res.insert({info, info->padding()});
Giorgio Arena4112eed2020-10-23 14:24:26 +0100486 }
487 }
488
489 return res;
490}
491
492bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map)
493{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100494 return std::find_if(padding_map.begin(), padding_map.end(),
495 [](const std::pair<const ITensorInfo *, PaddingSize> &padding_info)
496 { return (padding_info.first->padding() != padding_info.second); }) != padding_map.end();
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100497}
498
giuros01edc21e42018-11-16 14:45:31 +0000499#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100500void print_consecutive_elements(std::ostream &s,
501 DataType dt,
502 const uint8_t *ptr,
503 unsigned int n,
504 int stream_width,
505 const std::string &element_delim)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100506{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100507 switch (dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100508 {
509 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100510 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100511 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
512 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100513 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100514 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100515 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100516 case DataType::QSYMM8_PER_CHANNEL:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100517 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width,
518 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100519 break;
520 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100521 case DataType::QASYMM16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100522 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width,
523 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100524 break;
525 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100526 case DataType::QSYMM16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100527 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width,
528 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100529 break;
530 case DataType::U32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100531 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width,
532 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533 break;
534 case DataType::S32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100535 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width,
536 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100537 break;
Pablo Marquez Tello4a1c9172023-07-18 14:51:24 +0100538 case DataType::U64:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100539 print_consecutive_elements_impl<uint64_t>(s, reinterpret_cast<const uint64_t *>(ptr), n, stream_width,
540 element_delim);
Pablo Marquez Tello4a1c9172023-07-18 14:51:24 +0100541 break;
542 case DataType::S64:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100543 print_consecutive_elements_impl<int64_t>(s, reinterpret_cast<const int64_t *>(ptr), n, stream_width,
544 element_delim);
Pablo Marquez Tello4a1c9172023-07-18 14:51:24 +0100545 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000546 case DataType::BFLOAT16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100547 print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width,
548 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100549 break;
550 case DataType::F16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100551 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width,
552 element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100553 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000554 case DataType::F32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100555 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width,
556 element_delim);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000557 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100558 default:
559 ARM_COMPUTE_ERROR("Undefined element size for given data type");
560 }
561}
562
SiCong Lie357a252020-08-09 20:05:52 +0100563int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100564{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100565 switch (dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100566 {
567 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100568 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100569 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100570 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100571 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100572 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100573 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100574 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
575 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100576 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100577 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100578 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100579 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100580 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
581 case DataType::U32:
582 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
583 case DataType::S32:
584 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
Pablo Marquez Tello4a1c9172023-07-18 14:51:24 +0100585 case DataType::U64:
586 return max_consecutive_elements_display_width_impl<uint64_t>(s, reinterpret_cast<const uint64_t *>(ptr), n);
587 case DataType::S64:
588 return max_consecutive_elements_display_width_impl<int64_t>(s, reinterpret_cast<const int64_t *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000589 case DataType::BFLOAT16:
590 return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100591 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100592 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000593 case DataType::F32:
594 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100595 default:
596 ARM_COMPUTE_ERROR("Undefined element size for given data type");
597 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000598 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100599}
giuros01edc21e42018-11-16 14:45:31 +0000600#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
SiCong Lie357a252020-08-09 20:05:52 +0100601
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000602} // namespace arm_compute