blob: f5db6e15025a4fd46d9d14daf7843e007963867c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +00002 * Copyright (c) 2016-2020 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#include "support/ToolchainSupport.h"
29
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
37using namespace arm_compute;
Georgios Pinitas51545e42020-02-11 15:29:01 +000038
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039std::string arm_compute::read_file(const std::string &filename, bool binary)
40{
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
77const std::string &arm_compute::string_from_format(Format format)
78{
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
103const std::string &arm_compute::string_from_channel(Channel channel)
104{
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
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000124const std::string &arm_compute::string_from_data_layout(DataLayout dl)
125{
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
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136const std::string &arm_compute::string_from_data_type(DataType dt)
137{
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
164const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
165{
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" },
morgolock6b3865a2020-03-04 14:57:46 +0000181 { ActivationLayerInfo::ActivationFunction::HARD_SWISH, "HARD_SWISH" }
182
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 };
184
185 return act_map[act];
186}
187
188const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
189{
190 static std::map<MatrixPattern, const std::string> pattern_map =
191 {
192 { MatrixPattern::BOX, "BOX" },
193 { MatrixPattern::CROSS, "CROSS" },
194 { MatrixPattern::DISK, "DISK" },
195 { MatrixPattern::OTHER, "OTHER" },
196 };
197
198 return pattern_map[pattern];
199}
200
201const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
202{
203 static std::map<NonLinearFilterFunction, const std::string> func_map =
204 {
205 { NonLinearFilterFunction::MAX, "MAX" },
206 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
207 { NonLinearFilterFunction::MIN, "MIN" },
208 };
209
210 return func_map[function];
211}
212
213const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
214{
215 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
216 {
217 { InterpolationPolicy::AREA, "AREA" },
218 { InterpolationPolicy::BILINEAR, "BILINEAR" },
219 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
220 };
221
222 return interpolation_policy_map[policy];
223}
224
225const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
226{
227 static std::map<BorderMode, const std::string> border_mode_map =
228 {
229 { BorderMode::UNDEFINED, "UNDEFINED" },
230 { BorderMode::CONSTANT, "CONSTANT" },
231 { BorderMode::REPLICATE, "REPLICATE" },
232 };
233
234 return border_mode_map[border_mode];
235}
236
237const std::string &arm_compute::string_from_norm_type(NormType type)
238{
239 static std::map<NormType, const std::string> norm_type_map =
240 {
241 { NormType::IN_MAP_1D, "IN_MAP_1D" },
242 { NormType::IN_MAP_2D, "IN_MAP_2D" },
243 { NormType::CROSS_MAP, "CROSS_MAP" },
244 };
245
246 return norm_type_map[type];
247}
248
Georgios Pinitascdf51452017-08-31 14:21:36 +0100249const std::string &arm_compute::string_from_pooling_type(PoolingType type)
250{
251 static std::map<PoolingType, const std::string> pool_type_map =
252 {
253 { PoolingType::MAX, "MAX" },
254 { PoolingType::AVG, "AVG" },
255 { PoolingType::L2, "L2" },
256 };
257
258 return pool_type_map[type];
259}
260
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100261const std::string &arm_compute::string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
262{
263 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
264 {
265 { GEMMLowpOutputStageType::NONE, "" },
266 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
267 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
268 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
269 };
270
271 return output_stage_map[output_stage];
272}
273
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100274std::string arm_compute::string_from_pixel_value(const PixelValue &value, const DataType data_type)
275{
276 std::stringstream ss;
277 std::string converted_string;
278
279 switch(data_type)
280 {
281 case DataType::U8:
282 case DataType::QASYMM8:
283 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
284 ss << uint32_t(value.get<uint8_t>());
285 converted_string = ss.str();
286 break;
287 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100288 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100289 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100290 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
291 ss << int32_t(value.get<int8_t>());
292 converted_string = ss.str();
293 break;
294 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100295 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100296 ss << value.get<uint16_t>();
297 converted_string = ss.str();
298 break;
299 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100300 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100301 ss << value.get<int16_t>();
302 converted_string = ss.str();
303 break;
304 case DataType::U32:
305 ss << value.get<uint32_t>();
306 converted_string = ss.str();
307 break;
308 case DataType::S32:
309 ss << value.get<int32_t>();
310 converted_string = ss.str();
311 break;
312 case DataType::F32:
313 converted_string = float_to_string_with_full_precision(value.get<float>());
314 break;
315 case DataType::F16:
316 static_assert(sizeof(half) == 2, "Half must be 16 bit");
317 ss << value.get<half>();
318 converted_string = ss.str();
319 break;
320 default:
321 ARM_COMPUTE_ERROR("Not handled");
322 }
323
324 return converted_string;
325}
326
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327std::string arm_compute::lower_string(const std::string &val)
328{
329 std::string res = val;
330 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
331 return res;
332}
333
Giorgio Arena17203582019-08-02 16:00:41 +0100334PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation,
335 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000336{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000337 const auto &strides = conv_info.stride();
338 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1.");
339
Giorgio Arena17203582019-08-02 16:00:41 +0100340 const unsigned int width_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
341 const unsigned int height_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
342 const unsigned int in_width = input_shape[width_idx];
343 const unsigned int in_height = input_shape[height_idx];
344 const unsigned int kernel_width = weights_shape[width_idx];
345 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100346
347 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100348 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
349 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
350 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100351
352 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100353 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
354 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100355
356 // Calculate total pad
Giorgio Arena17203582019-08-02 16:00:41 +0100357 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
358 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 +0100359
360 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100361 const unsigned int pad_left = pad_width / 2;
362 const unsigned int pad_top = pad_height / 2;
363 const unsigned int pad_right = pad_width - pad_left;
364 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000365
Giorgio Arena17203582019-08-02 16:00:41 +0100366 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
367
368 // Check for correctness of predicted output shape against the one calculated using the generated info
369 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
370 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
371 ARM_COMPUTE_UNUSED(out_dims);
372
373 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000374}
375
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100376std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
377 unsigned int kernel_width, unsigned int kernel_height,
378 const PadStrideInfo &pad_stride_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100379{
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100380 const unsigned int pad_left = pad_stride_info.pad_left();
381 const unsigned int pad_top = pad_stride_info.pad_top();
382 const unsigned int pad_right = pad_stride_info.pad_right();
383 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
384 const unsigned int stride_x = pad_stride_info.stride().first;
385 const unsigned int stride_y = pad_stride_info.stride().second;
386
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100387 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100388 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right));
389 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom));
390 const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right);
391 const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100392
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100393 return std::make_pair<unsigned int, unsigned int>(w, h);
394}
395
Georgios Pinitas80838f12019-12-12 18:23:13 +0000396std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(int width, int height,
397 int kernel_width, int kernel_height,
Pablo Tello01bbacb2019-04-30 10:32:42 +0100398 const PadStrideInfo &pad_stride_info,
399 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400{
Georgios Pinitas80838f12019-12-12 18:23:13 +0000401 const int dilation_x = dilation.x();
402 const int dilation_y = dilation.y();
403 const int pad_left = pad_stride_info.pad_left();
404 const int pad_top = pad_stride_info.pad_top();
405 const int pad_right = pad_stride_info.pad_right();
406 const int pad_bottom = pad_stride_info.pad_bottom();
407 const int stride_x = pad_stride_info.stride().first;
408 const int stride_y = pad_stride_info.stride().second;
409 int w = 0;
410 int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100411 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100412 {
413 case DimensionRoundingType::FLOOR:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000414 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
415 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 +0100416 break;
417 case DimensionRoundingType::CEIL:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000418 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
419 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 +0100420 break;
421 default:
422 ARM_COMPUTE_ERROR("Unsupported rounding type");
423 }
424
Georgios Pinitas80838f12019-12-12 18:23:13 +0000425 w = std::max(1, w);
426 h = std::max(1, h);
427 return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428}
429
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100430bool arm_compute::needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
431{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100432 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
433 const bool is_quantized_type = is_data_type_quantized(dt);
434 const bool is_first_dim = (axis == 0);
435
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100436 return !is_first_dim || is_min_max || is_quantized_type;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100437}
438
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000439QuantizationInfo arm_compute::get_softmax_output_quantization_info(DataType input_type, bool is_log)
440{
441 // Note: Output quantization info for softmax should always have
442 // * Softmax with QASYMM8: scale = 1/256, offset = 0
443 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
444 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
445 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
446 if(is_data_type_quantized_asymmetric_signed(input_type))
447 {
448 if(is_log)
449 {
450 return QuantizationInfo(16.f / 256, 127);
451 }
452 else
453 {
454 return QuantizationInfo(1.f / 256, -128);
455 }
456 }
457 return QuantizationInfo(1.f / 256, 0);
458}
459
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000460float arm_compute::calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners)
461{
462 const size_t offset = align_corners ? 1 : 0;
463 const auto in = input_size - offset;
464 const auto out = output_size - offset;
465
466 ARM_COMPUTE_ERROR_ON((input_size == 0 || output_size == 0) && offset == 1);
467 ARM_COMPUTE_ERROR_ON(out == 0);
468
469 return static_cast<float>(in) / static_cast<float>(out);
470}
471
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000472std::pair<int32_t, int32_t> arm_compute::get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info)
473{
474 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
475 const auto a = act_info.a();
476 const auto b = act_info.b();
477 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
478 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
479 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
480
481 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int;
482 const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
483
484 return std::make_pair(min_activation, max_activation);
485}
486
giuros01edc21e42018-11-16 14:45:31 +0000487#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100488void arm_compute::print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim)
489{
490 switch(dt)
491 {
492 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100493 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100494 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
495 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100497 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100498 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100499 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
500 break;
501 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100502 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100503 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
504 break;
505 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100506 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100507 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
508 break;
509 case DataType::U32:
510 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
511 break;
512 case DataType::S32:
513 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
514 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000515 case DataType::BFLOAT16:
516 print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100517 break;
518 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100519 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100520 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000521 case DataType::F32:
522 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
523 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100524 default:
525 ARM_COMPUTE_ERROR("Undefined element size for given data type");
526 }
527}
528
529int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
530{
531 switch(dt)
532 {
533 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100534 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100535 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100537 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100538 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100539 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
540 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100541 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100542 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100543 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100544 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100545 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
546 case DataType::U32:
547 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
548 case DataType::S32:
549 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000550 case DataType::BFLOAT16:
551 return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100552 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100553 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000554 case DataType::F32:
555 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100556 default:
557 ARM_COMPUTE_ERROR("Undefined element size for given data type");
558 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000559 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100560}
giuros01edc21e42018-11-16 14:45:31 +0000561#endif /* ARM_COMPUTE_ASSERTS_ENABLED */