blob: 392603d5e6aae42d3384452d6a31e48ab2eabc45 [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
Isabella Gottardi6a914402019-01-30 15:45:42 +000025#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Matthew Benthamf1aeab92023-05-30 13:35:34 +000027#include "arm_compute/core/ActivationLayerInfo.h"
Isabella Gottardi6a914402019-01-30 15:45:42 +000028#include "arm_compute/core/Utils.h"
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +000029
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include <algorithm>
31#include <cmath>
32#include <cstdint>
33#include <fstream>
34#include <map>
35#include <string>
36
SiCong Lie357a252020-08-09 20:05:52 +010037namespace arm_compute
38{
39std::string read_file(const std::string &filename, bool binary)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41 std::string out;
42 std::ifstream fs;
43
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000044#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 try
46 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000047#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
49 std::ios_base::openmode mode = std::ios::in;
50
51 if(binary)
52 {
53 mode |= std::ios::binary;
54 }
55
56 fs.open(filename, mode);
57
58 // Go to the end of the file
59 fs.seekg(0, std::ios::end);
60 // Reserve the memory required to store the file's content
61 out.reserve(fs.tellg());
62 // Go back to the beginning of the file
63 fs.seekg(0, std::ios::beg);
64 // Copy the content of the file
65 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000066#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 }
68 catch(const std::ifstream::failure &e)
69 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010070 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000072#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
74 return out;
75}
76
SiCong Lie357a252020-08-09 20:05:52 +010077const std::string &string_from_format(Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078{
79 static std::map<Format, const std::string> formats_map =
80 {
81 { Format::UNKNOWN, "UNKNOWN" },
82 { Format::U8, "U8" },
83 { Format::S16, "S16" },
84 { Format::U16, "U16" },
85 { Format::S32, "S32" },
86 { Format::U32, "U32" },
87 { Format::F16, "F16" },
88 { Format::F32, "F32" },
89 { Format::UV88, "UV88" },
90 { Format::RGB888, "RGB888" },
91 { Format::RGBA8888, "RGBA8888" },
92 { Format::YUV444, "YUV444" },
93 { Format::YUYV422, "YUYV422" },
94 { Format::NV12, "NV12" },
95 { Format::NV21, "NV21" },
96 { Format::IYUV, "IYUV" },
97 { Format::UYVY422, "UYVY422" }
98 };
99
100 return formats_map[format];
101}
102
SiCong Lie357a252020-08-09 20:05:52 +0100103const std::string &string_from_channel(Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104{
105 static std::map<Channel, const std::string> channels_map =
106 {
107 { Channel::UNKNOWN, "UNKNOWN" },
108 { Channel::R, "R" },
109 { Channel::G, "G" },
110 { Channel::B, "B" },
111 { Channel::A, "A" },
112 { Channel::Y, "Y" },
113 { Channel::U, "U" },
114 { Channel::V, "V" },
115 { Channel::C0, "C0" },
116 { Channel::C1, "C1" },
117 { Channel::C2, "C2" },
118 { Channel::C3, "C3" }
119 };
120
121 return channels_map[channel];
122}
123
SiCong Lie357a252020-08-09 20:05:52 +0100124const std::string &string_from_data_layout(DataLayout dl)
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000125{
126 static std::map<DataLayout, const std::string> dl_map =
127 {
128 { DataLayout::UNKNOWN, "UNKNOWN" },
129 { DataLayout::NCHW, "NCHW" },
130 { DataLayout::NHWC, "NHWC" },
131 };
132
133 return dl_map[dl];
134}
135
SiCong Lie357a252020-08-09 20:05:52 +0100136const std::string &string_from_data_type(DataType dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137{
138 static std::map<DataType, const std::string> dt_map =
139 {
140 { DataType::UNKNOWN, "UNKNOWN" },
141 { DataType::S8, "S8" },
142 { DataType::U8, "U8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 { DataType::S16, "S16" },
144 { DataType::U16, "U16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 { DataType::S32, "S32" },
146 { DataType::U32, "U32" },
147 { DataType::S64, "S64" },
148 { DataType::U64, "U64" },
149 { DataType::F16, "F16" },
150 { DataType::F32, "F32" },
151 { DataType::F64, "F64" },
152 { DataType::SIZET, "SIZET" },
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100153 { DataType::QSYMM8, "QSYMM8" },
154 { DataType::QSYMM8_PER_CHANNEL, "QSYMM8_PER_CHANNEL" },
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100155 { DataType::QASYMM8, "QASYMM8" },
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100156 { DataType::QASYMM8_SIGNED, "QASYMM8_SIGNED" },
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100157 { DataType::QSYMM16, "QSYMM16" },
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100158 { DataType::QASYMM16, "QASYMM16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 };
160
161 return dt_map[dt];
162}
163
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000164const std::string &string_from_activation_func(const ActivationLayerInfo::ActivationFunction& act)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165{
166 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
167 {
168 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
169 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
170 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
171 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
172 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100173 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100174 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100176 { ActivationLayerInfo::ActivationFunction::ELU, "ELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
178 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
179 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
Usama Arif6a98a6e2019-05-10 17:07:27 +0100180 { ActivationLayerInfo::ActivationFunction::IDENTITY, "IDENTITY" },
Murray Kornelsen926f5022022-07-13 21:22:39 -0400181 { ActivationLayerInfo::ActivationFunction::HARD_SWISH, "HARD_SWISH" },
Jonathan Deakind6b8a712022-08-23 11:44:18 +0100182 { ActivationLayerInfo::ActivationFunction::SWISH, "SWISH" },
Murray Kornelsen926f5022022-07-13 21:22:39 -0400183 { ActivationLayerInfo::ActivationFunction::GELU, "GELU" }
morgolock6b3865a2020-03-04 14:57:46 +0000184
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 };
186
187 return act_map[act];
188}
189
SiCong Lie357a252020-08-09 20:05:52 +0100190const std::string &string_from_interpolation_policy(InterpolationPolicy policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191{
192 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
193 {
194 { InterpolationPolicy::AREA, "AREA" },
195 { InterpolationPolicy::BILINEAR, "BILINEAR" },
196 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
197 };
198
199 return interpolation_policy_map[policy];
200}
201
SiCong Lie357a252020-08-09 20:05:52 +0100202const std::string &string_from_border_mode(BorderMode border_mode)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203{
204 static std::map<BorderMode, const std::string> border_mode_map =
205 {
206 { BorderMode::UNDEFINED, "UNDEFINED" },
207 { BorderMode::CONSTANT, "CONSTANT" },
208 { BorderMode::REPLICATE, "REPLICATE" },
209 };
210
211 return border_mode_map[border_mode];
212}
213
SiCong Lie357a252020-08-09 20:05:52 +0100214const std::string &string_from_norm_type(NormType type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215{
216 static std::map<NormType, const std::string> norm_type_map =
217 {
218 { NormType::IN_MAP_1D, "IN_MAP_1D" },
219 { NormType::IN_MAP_2D, "IN_MAP_2D" },
220 { NormType::CROSS_MAP, "CROSS_MAP" },
221 };
222
223 return norm_type_map[type];
224}
225
SiCong Lie357a252020-08-09 20:05:52 +0100226const std::string &string_from_pooling_type(PoolingType type)
Georgios Pinitascdf51452017-08-31 14:21:36 +0100227{
228 static std::map<PoolingType, const std::string> pool_type_map =
229 {
230 { PoolingType::MAX, "MAX" },
231 { PoolingType::AVG, "AVG" },
232 { PoolingType::L2, "L2" },
233 };
234
235 return pool_type_map[type];
236}
237
SiCongLic4270cf2021-12-22 11:22:40 +0000238bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info)
239{
240 if(info.is_global_pooling || info.exclude_padding || info.pool_size.x() == 0 || info.pool_size.y() == 0)
241 {
242 return false;
243 }
244 const auto ps = info.pad_stride_info;
245 const auto pool_le_padding_x = info.pool_size.x() <= std::max({ ps.pad_left(), ps.pad_right() });
246 const auto pool_le_padding_y = info.pool_size.y() <= std::max({ ps.pad_top(), ps.pad_bottom() });
247 return pool_le_padding_x || pool_le_padding_y;
248}
249
ramelg0137515692022-02-26 22:06:20 +0000250bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info)
251{
252 if(info.is_global_pooling || info.pool_size.x() == 0 || info.pool_size.y() == 0 || info.pool_size.z() == 0)
253 {
254 return false;
255 }
256 const auto ps = info.padding;
257 const auto pool_le_padding_x = info.pool_size.x() <= std::max({ ps.left, ps.right });
258 const auto pool_le_padding_y = info.pool_size.y() <= std::max({ ps.top, ps.bottom });
259 const auto pool_le_padding_z = info.pool_size.z() <= std::max({ ps.front, ps.back });
260 return pool_le_padding_x || pool_le_padding_y || pool_le_padding_z;
261}
262
SiCong Lie357a252020-08-09 20:05:52 +0100263const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100264{
265 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
266 {
267 { GEMMLowpOutputStageType::NONE, "" },
268 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
269 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
270 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
271 };
272
273 return output_stage_map[output_stage];
274}
275
SiCong Lie357a252020-08-09 20:05:52 +0100276std::string string_from_pixel_value(const PixelValue &value, const DataType data_type)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100277{
278 std::stringstream ss;
279 std::string converted_string;
280
281 switch(data_type)
282 {
283 case DataType::U8:
284 case DataType::QASYMM8:
285 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
286 ss << uint32_t(value.get<uint8_t>());
287 converted_string = ss.str();
288 break;
289 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100290 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100291 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100292 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
293 ss << int32_t(value.get<int8_t>());
294 converted_string = ss.str();
295 break;
296 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100297 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100298 ss << value.get<uint16_t>();
299 converted_string = ss.str();
300 break;
301 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100302 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100303 ss << value.get<int16_t>();
304 converted_string = ss.str();
305 break;
306 case DataType::U32:
307 ss << value.get<uint32_t>();
308 converted_string = ss.str();
309 break;
310 case DataType::S32:
311 ss << value.get<int32_t>();
312 converted_string = ss.str();
313 break;
314 case DataType::F32:
315 converted_string = float_to_string_with_full_precision(value.get<float>());
316 break;
317 case DataType::F16:
318 static_assert(sizeof(half) == 2, "Half must be 16 bit");
319 ss << value.get<half>();
320 converted_string = ss.str();
321 break;
322 default:
323 ARM_COMPUTE_ERROR("Not handled");
324 }
325
326 return converted_string;
327}
328
SiCong Lie357a252020-08-09 20:05:52 +0100329DataType data_type_from_name(const std::string &name)
330{
331 static const std::map<std::string, DataType> data_types =
332 {
333 { "f16", DataType::F16 },
334 { "f32", DataType::F32 },
335 { "qasymm8", DataType::QASYMM8 },
SiCongLif466d752021-03-01 15:26:18 +0000336 { "qasymm8_signed", DataType::QASYMM8_SIGNED },
SiCong Lie357a252020-08-09 20:05:52 +0100337 };
338
339#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
340 try
341 {
342#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
343 return data_types.at(utility::tolower(name));
344
345#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
346 }
347 catch(const std::out_of_range &)
348 {
349 ARM_COMPUTE_ERROR_VAR("Invalid data type name: %s", name.c_str());
350 }
351#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
352}
353
354std::string lower_string(const std::string &val)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355{
356 std::string res = val;
357 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
358 return res;
359}
360
ramelg019cca5922021-11-11 10:05:00 +0000361std::string upper_string(const std::string &val)
362{
363 std::string res = val;
364 std::transform(res.begin(), res.end(), res.begin(), ::toupper);
365 return res;
366}
367
SiCong Lie357a252020-08-09 20:05:52 +0100368PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation,
369 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000370{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000371 const auto &strides = conv_info.stride();
372 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1.");
373
SiCong Lie357a252020-08-09 20:05:52 +0100374 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
375 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena17203582019-08-02 16:00:41 +0100376 const unsigned int in_width = input_shape[width_idx];
377 const unsigned int in_height = input_shape[height_idx];
378 const unsigned int kernel_width = weights_shape[width_idx];
379 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100380
381 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100382 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
383 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
384 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100385
386 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100387 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
388 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100389
390 // Calculate total pad
Giorgio Arena17203582019-08-02 16:00:41 +0100391 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
392 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 +0100393
394 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100395 const unsigned int pad_left = pad_width / 2;
396 const unsigned int pad_top = pad_height / 2;
397 const unsigned int pad_right = pad_width - pad_left;
398 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000399
Giorgio Arena17203582019-08-02 16:00:41 +0100400 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
401
402 // Check for correctness of predicted output shape against the one calculated using the generated info
403 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
404 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
405 ARM_COMPUTE_UNUSED(out_dims);
406
407 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000408}
409
SiCong Lie357a252020-08-09 20:05:52 +0100410std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
411 unsigned int kernel_width, unsigned int kernel_height,
412 const PadStrideInfo &pad_stride_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100413{
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100414 const unsigned int pad_left = pad_stride_info.pad_left();
415 const unsigned int pad_top = pad_stride_info.pad_top();
416 const unsigned int pad_right = pad_stride_info.pad_right();
417 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
418 const unsigned int stride_x = pad_stride_info.stride().first;
419 const unsigned int stride_y = pad_stride_info.stride().second;
420
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100421 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100422 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right));
423 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom));
424 const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right);
425 const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100426
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100427 return std::make_pair<unsigned int, unsigned int>(w, h);
428}
429
SiCong Lie357a252020-08-09 20:05:52 +0100430std::pair<unsigned int, unsigned int> scaled_dimensions(int width, int height,
431 int kernel_width, int kernel_height,
432 const PadStrideInfo &pad_stride_info,
433 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100434{
Georgios Pinitas80838f12019-12-12 18:23:13 +0000435 const int dilation_x = dilation.x();
436 const int dilation_y = dilation.y();
437 const int pad_left = pad_stride_info.pad_left();
438 const int pad_top = pad_stride_info.pad_top();
439 const int pad_right = pad_stride_info.pad_right();
440 const int pad_bottom = pad_stride_info.pad_bottom();
441 const int stride_x = pad_stride_info.stride().first;
442 const int stride_y = pad_stride_info.stride().second;
443 int w = 0;
444 int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100445 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100446 {
447 case DimensionRoundingType::FLOOR:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000448 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
449 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 +0100450 break;
451 case DimensionRoundingType::CEIL:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000452 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
453 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 +0100454 break;
455 default:
456 ARM_COMPUTE_ERROR("Unsupported rounding type");
457 }
458
Georgios Pinitas80838f12019-12-12 18:23:13 +0000459 w = std::max(1, w);
460 h = std::max(1, h);
461 return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100462}
463
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100464std::pair<int, int> scaled_dimensions_signed(int width, int height,
465 int kernel_width, int kernel_height,
466 const PadStrideInfo &pad_stride_info)
467{
468 const int pad_left = pad_stride_info.pad_left();
469 const int pad_top = pad_stride_info.pad_top();
470 const int pad_right = pad_stride_info.pad_right();
471 const int pad_bottom = pad_stride_info.pad_bottom();
472 const int stride_x = pad_stride_info.stride().first;
473 const int stride_y = pad_stride_info.stride().second;
474 int w = 0;
475 int h = 0;
476 switch(pad_stride_info.round())
477 {
478 case DimensionRoundingType::FLOOR:
479 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
480 h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
481 break;
482 case DimensionRoundingType::CEIL:
483 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
484 h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
485 break;
486 default:
487 ARM_COMPUTE_ERROR("Unsupported rounding type");
488 }
489
490 return std::make_pair(static_cast<int>(w), static_cast<int>(h));
491}
492
ramelg0137515692022-02-26 22:06:20 +0000493std::tuple<int, int, int> scaled_3d_dimensions_signed(int width, int height, int depth,
494 int kernel_width, int kernel_height, int kernel_depth,
495 const Pooling3dLayerInfo &pool3d_info)
496{
497 const int pad_left = pool3d_info.padding.left;
498 const int pad_top = pool3d_info.padding.top;
499 const int pad_right = pool3d_info.padding.right;
500 const int pad_bottom = pool3d_info.padding.bottom;
501 const int pad_front = pool3d_info.padding.front;
502 const int pad_back = pool3d_info.padding.back;
503 const int stride_x = pool3d_info.stride.x();
504 const int stride_y = pool3d_info.stride.y();
505 const int stride_z = pool3d_info.stride.z();
506 int w = 0;
507 int h = 0;
508 int d = 0;
509
510 switch(pool3d_info.round_type)
511 {
512 case DimensionRoundingType::FLOOR:
513 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
514 h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
515 d = static_cast<int>(std::floor((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1));
516 break;
517 case DimensionRoundingType::CEIL:
518 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
519 h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
520 d = static_cast<int>(std::ceil((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1));
521 break;
522 default:
523 ARM_COMPUTE_ERROR("Unsupported rounding type");
524 }
525
526 return std::make_tuple(static_cast<int>(w), static_cast<int>(h), static_cast<int>(d));
527}
528
SiCong Lie357a252020-08-09 20:05:52 +0100529bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100530{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100531 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
532 const bool is_quantized_type = is_data_type_quantized(dt);
533 const bool is_first_dim = (axis == 0);
534
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100535 return !is_first_dim || is_min_max || is_quantized_type;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100536}
537
SiCong Lie357a252020-08-09 20:05:52 +0100538QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000539{
540 // Note: Output quantization info for softmax should always have
541 // * Softmax with QASYMM8: scale = 1/256, offset = 0
542 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
543 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
544 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
545 if(is_data_type_quantized_asymmetric_signed(input_type))
546 {
547 if(is_log)
548 {
549 return QuantizationInfo(16.f / 256, 127);
550 }
551 else
552 {
553 return QuantizationInfo(1.f / 256, -128);
554 }
555 }
556 return QuantizationInfo(1.f / 256, 0);
557}
558
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000559std::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 +0000560{
561 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
562 const auto a = act_info.a();
563 const auto b = act_info.b();
564 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
565 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
566 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
567
568 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int;
569 const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
570
571 return std::make_pair(min_activation, max_activation);
572}
573
Giorgio Arena4112eed2020-10-23 14:24:26 +0100574std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensor *> tensors)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100575{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100576 std::unordered_map<const ITensorInfo *, PaddingSize> res;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100577
578 for(const ITensor *tensor : tensors)
579 {
580 if(tensor)
581 {
Giorgio Arena4112eed2020-10-23 14:24:26 +0100582 res.insert({ tensor->info(), tensor->info()->padding() });
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100583 }
584 }
585
586 return res;
587}
588
Giorgio Arena4112eed2020-10-23 14:24:26 +0100589std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100590{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100591 std::unordered_map<const ITensorInfo *, PaddingSize> res;
592
593 for(const ITensorInfo *info : infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100594 {
Giorgio Arena4112eed2020-10-23 14:24:26 +0100595 if(info)
596 {
597 res.insert({ info, info->padding() });
598 }
599 }
600
601 return res;
602}
603
604bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map)
605{
606 return std::find_if(padding_map.begin(), padding_map.end(), [](const std::pair<const ITensorInfo *, PaddingSize> &padding_info)
607 {
608 return (padding_info.first->padding() != padding_info.second);
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100609 })
610 != padding_map.end();
611}
612
giuros01edc21e42018-11-16 14:45:31 +0000613#ifdef ARM_COMPUTE_ASSERTS_ENABLED
SiCong Lie357a252020-08-09 20:05:52 +0100614void 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 +0100615{
616 switch(dt)
617 {
618 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100619 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100620 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
621 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100622 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100623 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100624 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100625 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100626 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
627 break;
628 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100629 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100630 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
631 break;
632 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100633 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100634 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
635 break;
636 case DataType::U32:
637 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
638 break;
639 case DataType::S32:
640 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
641 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000642 case DataType::BFLOAT16:
643 print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100644 break;
645 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100646 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100647 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000648 case DataType::F32:
649 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
650 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100651 default:
652 ARM_COMPUTE_ERROR("Undefined element size for given data type");
653 }
654}
655
SiCong Lie357a252020-08-09 20:05:52 +0100656int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100657{
658 switch(dt)
659 {
660 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100661 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100662 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100663 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100664 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100665 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100666 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100667 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
668 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100669 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100670 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100671 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100672 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100673 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
674 case DataType::U32:
675 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
676 case DataType::S32:
677 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000678 case DataType::BFLOAT16:
679 return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100680 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100681 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000682 case DataType::F32:
683 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100684 default:
685 ARM_COMPUTE_ERROR("Undefined element size for given data type");
686 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000687 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100688}
giuros01edc21e42018-11-16 14:45:31 +0000689#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
SiCong Lie357a252020-08-09 20:05:52 +0100690
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000691} // namespace arm_compute