blob: b81b498ae5e799cbd977a42395d78ebd11f40270 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCongLif466d752021-03-01 15:26:18 +00002 * Copyright (c) 2016-2021 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
Isabella Gottardi6a914402019-01-30 15:45:42 +000027#include "arm_compute/core/Utils.h"
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +000028
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include <algorithm>
30#include <cmath>
31#include <cstdint>
32#include <fstream>
33#include <map>
34#include <string>
35
SiCong Lie357a252020-08-09 20:05:52 +010036namespace arm_compute
37{
38std::string read_file(const std::string &filename, bool binary)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
40 std::string out;
41 std::ifstream fs;
42
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000043#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 try
45 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000046#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
48 std::ios_base::openmode mode = std::ios::in;
49
50 if(binary)
51 {
52 mode |= std::ios::binary;
53 }
54
55 fs.open(filename, mode);
56
57 // Go to the end of the file
58 fs.seekg(0, std::ios::end);
59 // Reserve the memory required to store the file's content
60 out.reserve(fs.tellg());
61 // Go back to the beginning of the file
62 fs.seekg(0, std::ios::beg);
63 // Copy the content of the file
64 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000065#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 }
67 catch(const std::ifstream::failure &e)
68 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010069 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000071#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
73 return out;
74}
75
SiCong Lie357a252020-08-09 20:05:52 +010076const std::string &string_from_format(Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077{
78 static std::map<Format, const std::string> formats_map =
79 {
80 { Format::UNKNOWN, "UNKNOWN" },
81 { Format::U8, "U8" },
82 { Format::S16, "S16" },
83 { Format::U16, "U16" },
84 { Format::S32, "S32" },
85 { Format::U32, "U32" },
86 { Format::F16, "F16" },
87 { Format::F32, "F32" },
88 { Format::UV88, "UV88" },
89 { Format::RGB888, "RGB888" },
90 { Format::RGBA8888, "RGBA8888" },
91 { Format::YUV444, "YUV444" },
92 { Format::YUYV422, "YUYV422" },
93 { Format::NV12, "NV12" },
94 { Format::NV21, "NV21" },
95 { Format::IYUV, "IYUV" },
96 { Format::UYVY422, "UYVY422" }
97 };
98
99 return formats_map[format];
100}
101
SiCong Lie357a252020-08-09 20:05:52 +0100102const std::string &string_from_channel(Channel channel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103{
104 static std::map<Channel, const std::string> channels_map =
105 {
106 { Channel::UNKNOWN, "UNKNOWN" },
107 { Channel::R, "R" },
108 { Channel::G, "G" },
109 { Channel::B, "B" },
110 { Channel::A, "A" },
111 { Channel::Y, "Y" },
112 { Channel::U, "U" },
113 { Channel::V, "V" },
114 { Channel::C0, "C0" },
115 { Channel::C1, "C1" },
116 { Channel::C2, "C2" },
117 { Channel::C3, "C3" }
118 };
119
120 return channels_map[channel];
121}
122
SiCong Lie357a252020-08-09 20:05:52 +0100123const std::string &string_from_data_layout(DataLayout dl)
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000124{
125 static std::map<DataLayout, const std::string> dl_map =
126 {
127 { DataLayout::UNKNOWN, "UNKNOWN" },
128 { DataLayout::NCHW, "NCHW" },
129 { DataLayout::NHWC, "NHWC" },
130 };
131
132 return dl_map[dl];
133}
134
SiCong Lie357a252020-08-09 20:05:52 +0100135const std::string &string_from_data_type(DataType dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136{
137 static std::map<DataType, const std::string> dt_map =
138 {
139 { DataType::UNKNOWN, "UNKNOWN" },
140 { DataType::S8, "S8" },
141 { DataType::U8, "U8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 { DataType::S16, "S16" },
143 { DataType::U16, "U16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 { DataType::S32, "S32" },
145 { DataType::U32, "U32" },
146 { DataType::S64, "S64" },
147 { DataType::U64, "U64" },
148 { DataType::F16, "F16" },
149 { DataType::F32, "F32" },
150 { DataType::F64, "F64" },
151 { DataType::SIZET, "SIZET" },
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100152 { DataType::QSYMM8, "QSYMM8" },
153 { DataType::QSYMM8_PER_CHANNEL, "QSYMM8_PER_CHANNEL" },
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100154 { DataType::QASYMM8, "QASYMM8" },
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100155 { DataType::QASYMM8_SIGNED, "QASYMM8_SIGNED" },
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100156 { DataType::QSYMM16, "QSYMM16" },
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100157 { DataType::QASYMM16, "QASYMM16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 };
159
160 return dt_map[dt];
161}
162
SiCong Lie357a252020-08-09 20:05:52 +0100163const std::string &string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164{
165 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
166 {
167 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
168 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
169 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
170 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
171 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100172 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100173 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100175 { ActivationLayerInfo::ActivationFunction::ELU, "ELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
177 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
178 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
Usama Arif6a98a6e2019-05-10 17:07:27 +0100179 { ActivationLayerInfo::ActivationFunction::IDENTITY, "IDENTITY" },
morgolock6b3865a2020-03-04 14:57:46 +0000180 { ActivationLayerInfo::ActivationFunction::HARD_SWISH, "HARD_SWISH" }
181
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 };
183
184 return act_map[act];
185}
186
SiCong Lie357a252020-08-09 20:05:52 +0100187const std::string &string_from_interpolation_policy(InterpolationPolicy policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188{
189 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
190 {
191 { InterpolationPolicy::AREA, "AREA" },
192 { InterpolationPolicy::BILINEAR, "BILINEAR" },
193 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
194 };
195
196 return interpolation_policy_map[policy];
197}
198
SiCong Lie357a252020-08-09 20:05:52 +0100199const std::string &string_from_border_mode(BorderMode border_mode)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200{
201 static std::map<BorderMode, const std::string> border_mode_map =
202 {
203 { BorderMode::UNDEFINED, "UNDEFINED" },
204 { BorderMode::CONSTANT, "CONSTANT" },
205 { BorderMode::REPLICATE, "REPLICATE" },
206 };
207
208 return border_mode_map[border_mode];
209}
210
SiCong Lie357a252020-08-09 20:05:52 +0100211const std::string &string_from_norm_type(NormType type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212{
213 static std::map<NormType, const std::string> norm_type_map =
214 {
215 { NormType::IN_MAP_1D, "IN_MAP_1D" },
216 { NormType::IN_MAP_2D, "IN_MAP_2D" },
217 { NormType::CROSS_MAP, "CROSS_MAP" },
218 };
219
220 return norm_type_map[type];
221}
222
SiCong Lie357a252020-08-09 20:05:52 +0100223const std::string &string_from_pooling_type(PoolingType type)
Georgios Pinitascdf51452017-08-31 14:21:36 +0100224{
225 static std::map<PoolingType, const std::string> pool_type_map =
226 {
227 { PoolingType::MAX, "MAX" },
228 { PoolingType::AVG, "AVG" },
229 { PoolingType::L2, "L2" },
230 };
231
232 return pool_type_map[type];
233}
234
SiCong Lie357a252020-08-09 20:05:52 +0100235const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100236{
237 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
238 {
239 { GEMMLowpOutputStageType::NONE, "" },
240 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
241 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
242 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
243 };
244
245 return output_stage_map[output_stage];
246}
247
SiCong Lie357a252020-08-09 20:05:52 +0100248std::string string_from_pixel_value(const PixelValue &value, const DataType data_type)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100249{
250 std::stringstream ss;
251 std::string converted_string;
252
253 switch(data_type)
254 {
255 case DataType::U8:
256 case DataType::QASYMM8:
257 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
258 ss << uint32_t(value.get<uint8_t>());
259 converted_string = ss.str();
260 break;
261 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100262 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100263 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100264 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
265 ss << int32_t(value.get<int8_t>());
266 converted_string = ss.str();
267 break;
268 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100269 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100270 ss << value.get<uint16_t>();
271 converted_string = ss.str();
272 break;
273 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100274 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100275 ss << value.get<int16_t>();
276 converted_string = ss.str();
277 break;
278 case DataType::U32:
279 ss << value.get<uint32_t>();
280 converted_string = ss.str();
281 break;
282 case DataType::S32:
283 ss << value.get<int32_t>();
284 converted_string = ss.str();
285 break;
286 case DataType::F32:
287 converted_string = float_to_string_with_full_precision(value.get<float>());
288 break;
289 case DataType::F16:
290 static_assert(sizeof(half) == 2, "Half must be 16 bit");
291 ss << value.get<half>();
292 converted_string = ss.str();
293 break;
294 default:
295 ARM_COMPUTE_ERROR("Not handled");
296 }
297
298 return converted_string;
299}
300
SiCong Lie357a252020-08-09 20:05:52 +0100301DataType data_type_from_name(const std::string &name)
302{
303 static const std::map<std::string, DataType> data_types =
304 {
305 { "f16", DataType::F16 },
306 { "f32", DataType::F32 },
307 { "qasymm8", DataType::QASYMM8 },
SiCongLif466d752021-03-01 15:26:18 +0000308 { "qasymm8_signed", DataType::QASYMM8_SIGNED },
SiCong Lie357a252020-08-09 20:05:52 +0100309 };
310
311#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
312 try
313 {
314#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
315 return data_types.at(utility::tolower(name));
316
317#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
318 }
319 catch(const std::out_of_range &)
320 {
321 ARM_COMPUTE_ERROR_VAR("Invalid data type name: %s", name.c_str());
322 }
323#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
324}
325
326std::string lower_string(const std::string &val)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327{
328 std::string res = val;
329 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
330 return res;
331}
332
SiCong Lie357a252020-08-09 20:05:52 +0100333PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation,
334 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000335{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000336 const auto &strides = conv_info.stride();
337 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1.");
338
SiCong Lie357a252020-08-09 20:05:52 +0100339 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
340 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena17203582019-08-02 16:00:41 +0100341 const unsigned int in_width = input_shape[width_idx];
342 const unsigned int in_height = input_shape[height_idx];
343 const unsigned int kernel_width = weights_shape[width_idx];
344 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100345
346 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100347 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
348 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
349 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100350
351 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100352 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
353 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100354
355 // Calculate total pad
Giorgio Arena17203582019-08-02 16:00:41 +0100356 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
357 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 +0100358
359 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100360 const unsigned int pad_left = pad_width / 2;
361 const unsigned int pad_top = pad_height / 2;
362 const unsigned int pad_right = pad_width - pad_left;
363 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000364
Giorgio Arena17203582019-08-02 16:00:41 +0100365 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
366
367 // Check for correctness of predicted output shape against the one calculated using the generated info
368 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
369 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
370 ARM_COMPUTE_UNUSED(out_dims);
371
372 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000373}
374
SiCong Lie357a252020-08-09 20:05:52 +0100375std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
376 unsigned int kernel_width, unsigned int kernel_height,
377 const PadStrideInfo &pad_stride_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100378{
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100379 const unsigned int pad_left = pad_stride_info.pad_left();
380 const unsigned int pad_top = pad_stride_info.pad_top();
381 const unsigned int pad_right = pad_stride_info.pad_right();
382 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
383 const unsigned int stride_x = pad_stride_info.stride().first;
384 const unsigned int stride_y = pad_stride_info.stride().second;
385
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100386 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100387 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right));
388 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom));
389 const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right);
390 const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100391
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100392 return std::make_pair<unsigned int, unsigned int>(w, h);
393}
394
SiCong Lie357a252020-08-09 20:05:52 +0100395std::pair<unsigned int, unsigned int> scaled_dimensions(int width, int height,
396 int kernel_width, int kernel_height,
397 const PadStrideInfo &pad_stride_info,
398 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100399{
Georgios Pinitas80838f12019-12-12 18:23:13 +0000400 const int dilation_x = dilation.x();
401 const int dilation_y = dilation.y();
402 const int pad_left = pad_stride_info.pad_left();
403 const int pad_top = pad_stride_info.pad_top();
404 const int pad_right = pad_stride_info.pad_right();
405 const int pad_bottom = pad_stride_info.pad_bottom();
406 const int stride_x = pad_stride_info.stride().first;
407 const int stride_y = pad_stride_info.stride().second;
408 int w = 0;
409 int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100410 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411 {
412 case DimensionRoundingType::FLOOR:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000413 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
414 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 +0100415 break;
416 case DimensionRoundingType::CEIL:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000417 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
418 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 +0100419 break;
420 default:
421 ARM_COMPUTE_ERROR("Unsupported rounding type");
422 }
423
Georgios Pinitas80838f12019-12-12 18:23:13 +0000424 w = std::max(1, w);
425 h = std::max(1, h);
426 return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100427}
428
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100429std::pair<int, int> scaled_dimensions_signed(int width, int height,
430 int kernel_width, int kernel_height,
431 const PadStrideInfo &pad_stride_info)
432{
433 const int pad_left = pad_stride_info.pad_left();
434 const int pad_top = pad_stride_info.pad_top();
435 const int pad_right = pad_stride_info.pad_right();
436 const int pad_bottom = pad_stride_info.pad_bottom();
437 const int stride_x = pad_stride_info.stride().first;
438 const int stride_y = pad_stride_info.stride().second;
439 int w = 0;
440 int h = 0;
441 switch(pad_stride_info.round())
442 {
443 case DimensionRoundingType::FLOOR:
444 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
445 h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
446 break;
447 case DimensionRoundingType::CEIL:
448 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
449 h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
450 break;
451 default:
452 ARM_COMPUTE_ERROR("Unsupported rounding type");
453 }
454
455 return std::make_pair(static_cast<int>(w), static_cast<int>(h));
456}
457
SiCong Lie357a252020-08-09 20:05:52 +0100458bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100459{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100460 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
461 const bool is_quantized_type = is_data_type_quantized(dt);
462 const bool is_first_dim = (axis == 0);
463
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100464 return !is_first_dim || is_min_max || is_quantized_type;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100465}
466
SiCong Lie357a252020-08-09 20:05:52 +0100467QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000468{
469 // Note: Output quantization info for softmax should always have
470 // * Softmax with QASYMM8: scale = 1/256, offset = 0
471 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
472 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
473 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
474 if(is_data_type_quantized_asymmetric_signed(input_type))
475 {
476 if(is_log)
477 {
478 return QuantizationInfo(16.f / 256, 127);
479 }
480 else
481 {
482 return QuantizationInfo(1.f / 256, -128);
483 }
484 }
485 return QuantizationInfo(1.f / 256, 0);
486}
487
SiCong Lie357a252020-08-09 20:05:52 +0100488std::pair<int32_t, int32_t> get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info)
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000489{
490 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
491 const auto a = act_info.a();
492 const auto b = act_info.b();
493 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
494 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
495 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
496
497 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int;
498 const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
499
500 return std::make_pair(min_activation, max_activation);
501}
502
Giorgio Arena4112eed2020-10-23 14:24:26 +0100503std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensor *> tensors)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100504{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100505 std::unordered_map<const ITensorInfo *, PaddingSize> res;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100506
507 for(const ITensor *tensor : tensors)
508 {
509 if(tensor)
510 {
Giorgio Arena4112eed2020-10-23 14:24:26 +0100511 res.insert({ tensor->info(), tensor->info()->padding() });
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100512 }
513 }
514
515 return res;
516}
517
Giorgio Arena4112eed2020-10-23 14:24:26 +0100518std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100519{
Giorgio Arena4112eed2020-10-23 14:24:26 +0100520 std::unordered_map<const ITensorInfo *, PaddingSize> res;
521
522 for(const ITensorInfo *info : infos)
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100523 {
Giorgio Arena4112eed2020-10-23 14:24:26 +0100524 if(info)
525 {
526 res.insert({ info, info->padding() });
527 }
528 }
529
530 return res;
531}
532
533bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map)
534{
535 return std::find_if(padding_map.begin(), padding_map.end(), [](const std::pair<const ITensorInfo *, PaddingSize> &padding_info)
536 {
537 return (padding_info.first->padding() != padding_info.second);
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100538 })
539 != padding_map.end();
540}
541
giuros01edc21e42018-11-16 14:45:31 +0000542#ifdef ARM_COMPUTE_ASSERTS_ENABLED
SiCong Lie357a252020-08-09 20:05:52 +0100543void 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 +0100544{
545 switch(dt)
546 {
547 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100548 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100549 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
550 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100551 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100552 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100553 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100554 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100555 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
556 break;
557 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100558 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100559 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
560 break;
561 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100562 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100563 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
564 break;
565 case DataType::U32:
566 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
567 break;
568 case DataType::S32:
569 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
570 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000571 case DataType::BFLOAT16:
572 print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573 break;
574 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100575 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100576 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000577 case DataType::F32:
578 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
579 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100580 default:
581 ARM_COMPUTE_ERROR("Undefined element size for given data type");
582 }
583}
584
SiCong Lie357a252020-08-09 20:05:52 +0100585int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100586{
587 switch(dt)
588 {
589 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100590 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100591 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100592 case DataType::S8:
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100593 case DataType::QSYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100594 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100595 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100596 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
597 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100598 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100599 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100600 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100601 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100602 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
603 case DataType::U32:
604 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
605 case DataType::S32:
606 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000607 case DataType::BFLOAT16:
608 return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100609 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100610 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000611 case DataType::F32:
612 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100613 default:
614 ARM_COMPUTE_ERROR("Undefined element size for given data type");
615 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000616 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100617}
giuros01edc21e42018-11-16 14:45:31 +0000618#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
SiCong Lie357a252020-08-09 20:05:52 +0100619
620} // namespace arm_compute