blob: f930804ce36b2f2e3ed6b88c17d40fdbc700bc65 [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;
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000038#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039std::string arm_compute::build_information()
40{
41 static const std::string information =
42#include "arm_compute_version.embed"
43 ;
44 return information;
45}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000046#endif /* DOXYGEN_SKIP_THIS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047std::string arm_compute::read_file(const std::string &filename, bool binary)
48{
49 std::string out;
50 std::ifstream fs;
51
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000052#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 try
54 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000055#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
57 std::ios_base::openmode mode = std::ios::in;
58
59 if(binary)
60 {
61 mode |= std::ios::binary;
62 }
63
64 fs.open(filename, mode);
65
66 // Go to the end of the file
67 fs.seekg(0, std::ios::end);
68 // Reserve the memory required to store the file's content
69 out.reserve(fs.tellg());
70 // Go back to the beginning of the file
71 fs.seekg(0, std::ios::beg);
72 // Copy the content of the file
73 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000074#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 }
76 catch(const std::ifstream::failure &e)
77 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +010078 ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000080#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
82 return out;
83}
84
85const std::string &arm_compute::string_from_format(Format format)
86{
87 static std::map<Format, const std::string> formats_map =
88 {
89 { Format::UNKNOWN, "UNKNOWN" },
90 { Format::U8, "U8" },
91 { Format::S16, "S16" },
92 { Format::U16, "U16" },
93 { Format::S32, "S32" },
94 { Format::U32, "U32" },
95 { Format::F16, "F16" },
96 { Format::F32, "F32" },
97 { Format::UV88, "UV88" },
98 { Format::RGB888, "RGB888" },
99 { Format::RGBA8888, "RGBA8888" },
100 { Format::YUV444, "YUV444" },
101 { Format::YUYV422, "YUYV422" },
102 { Format::NV12, "NV12" },
103 { Format::NV21, "NV21" },
104 { Format::IYUV, "IYUV" },
105 { Format::UYVY422, "UYVY422" }
106 };
107
108 return formats_map[format];
109}
110
111const std::string &arm_compute::string_from_channel(Channel channel)
112{
113 static std::map<Channel, const std::string> channels_map =
114 {
115 { Channel::UNKNOWN, "UNKNOWN" },
116 { Channel::R, "R" },
117 { Channel::G, "G" },
118 { Channel::B, "B" },
119 { Channel::A, "A" },
120 { Channel::Y, "Y" },
121 { Channel::U, "U" },
122 { Channel::V, "V" },
123 { Channel::C0, "C0" },
124 { Channel::C1, "C1" },
125 { Channel::C2, "C2" },
126 { Channel::C3, "C3" }
127 };
128
129 return channels_map[channel];
130}
131
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000132const std::string &arm_compute::string_from_data_layout(DataLayout dl)
133{
134 static std::map<DataLayout, const std::string> dl_map =
135 {
136 { DataLayout::UNKNOWN, "UNKNOWN" },
137 { DataLayout::NCHW, "NCHW" },
138 { DataLayout::NHWC, "NHWC" },
139 };
140
141 return dl_map[dl];
142}
143
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144const std::string &arm_compute::string_from_data_type(DataType dt)
145{
146 static std::map<DataType, const std::string> dt_map =
147 {
148 { DataType::UNKNOWN, "UNKNOWN" },
149 { DataType::S8, "S8" },
150 { DataType::U8, "U8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 { DataType::S16, "S16" },
152 { DataType::U16, "U16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 { DataType::S32, "S32" },
154 { DataType::U32, "U32" },
155 { DataType::S64, "S64" },
156 { DataType::U64, "U64" },
157 { DataType::F16, "F16" },
158 { DataType::F32, "F32" },
159 { DataType::F64, "F64" },
160 { DataType::SIZET, "SIZET" },
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100161 { DataType::QSYMM8, "QSYMM8" },
162 { DataType::QSYMM8_PER_CHANNEL, "QSYMM8_PER_CHANNEL" },
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100163 { DataType::QASYMM8, "QASYMM8" },
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100164 { DataType::QASYMM8_SIGNED, "QASYMM8_SIGNED" },
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100165 { DataType::QSYMM16, "QSYMM16" },
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100166 { DataType::QASYMM16, "QASYMM16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 };
168
169 return dt_map[dt];
170}
171
172const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
173{
174 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
175 {
176 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
177 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
178 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
179 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
180 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100181 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100182 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100184 { ActivationLayerInfo::ActivationFunction::ELU, "ELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
186 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
187 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
Usama Arif6a98a6e2019-05-10 17:07:27 +0100188 { ActivationLayerInfo::ActivationFunction::IDENTITY, "IDENTITY" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189 };
190
191 return act_map[act];
192}
193
194const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
195{
196 static std::map<MatrixPattern, const std::string> pattern_map =
197 {
198 { MatrixPattern::BOX, "BOX" },
199 { MatrixPattern::CROSS, "CROSS" },
200 { MatrixPattern::DISK, "DISK" },
201 { MatrixPattern::OTHER, "OTHER" },
202 };
203
204 return pattern_map[pattern];
205}
206
207const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
208{
209 static std::map<NonLinearFilterFunction, const std::string> func_map =
210 {
211 { NonLinearFilterFunction::MAX, "MAX" },
212 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
213 { NonLinearFilterFunction::MIN, "MIN" },
214 };
215
216 return func_map[function];
217}
218
219const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
220{
221 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
222 {
223 { InterpolationPolicy::AREA, "AREA" },
224 { InterpolationPolicy::BILINEAR, "BILINEAR" },
225 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
226 };
227
228 return interpolation_policy_map[policy];
229}
230
231const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
232{
233 static std::map<BorderMode, const std::string> border_mode_map =
234 {
235 { BorderMode::UNDEFINED, "UNDEFINED" },
236 { BorderMode::CONSTANT, "CONSTANT" },
237 { BorderMode::REPLICATE, "REPLICATE" },
238 };
239
240 return border_mode_map[border_mode];
241}
242
243const std::string &arm_compute::string_from_norm_type(NormType type)
244{
245 static std::map<NormType, const std::string> norm_type_map =
246 {
247 { NormType::IN_MAP_1D, "IN_MAP_1D" },
248 { NormType::IN_MAP_2D, "IN_MAP_2D" },
249 { NormType::CROSS_MAP, "CROSS_MAP" },
250 };
251
252 return norm_type_map[type];
253}
254
Georgios Pinitascdf51452017-08-31 14:21:36 +0100255const std::string &arm_compute::string_from_pooling_type(PoolingType type)
256{
257 static std::map<PoolingType, const std::string> pool_type_map =
258 {
259 { PoolingType::MAX, "MAX" },
260 { PoolingType::AVG, "AVG" },
261 { PoolingType::L2, "L2" },
262 };
263
264 return pool_type_map[type];
265}
266
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100267const std::string &arm_compute::string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
268{
269 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
270 {
271 { GEMMLowpOutputStageType::NONE, "" },
272 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
273 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
274 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
275 };
276
277 return output_stage_map[output_stage];
278}
279
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100280std::string arm_compute::string_from_pixel_value(const PixelValue &value, const DataType data_type)
281{
282 std::stringstream ss;
283 std::string converted_string;
284
285 switch(data_type)
286 {
287 case DataType::U8:
288 case DataType::QASYMM8:
289 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
290 ss << uint32_t(value.get<uint8_t>());
291 converted_string = ss.str();
292 break;
293 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100294 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100295 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100296 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
297 ss << int32_t(value.get<int8_t>());
298 converted_string = ss.str();
299 break;
300 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100301 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100302 ss << value.get<uint16_t>();
303 converted_string = ss.str();
304 break;
305 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100306 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100307 ss << value.get<int16_t>();
308 converted_string = ss.str();
309 break;
310 case DataType::U32:
311 ss << value.get<uint32_t>();
312 converted_string = ss.str();
313 break;
314 case DataType::S32:
315 ss << value.get<int32_t>();
316 converted_string = ss.str();
317 break;
318 case DataType::F32:
319 converted_string = float_to_string_with_full_precision(value.get<float>());
320 break;
321 case DataType::F16:
322 static_assert(sizeof(half) == 2, "Half must be 16 bit");
323 ss << value.get<half>();
324 converted_string = ss.str();
325 break;
326 default:
327 ARM_COMPUTE_ERROR("Not handled");
328 }
329
330 return converted_string;
331}
332
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333std::string arm_compute::lower_string(const std::string &val)
334{
335 std::string res = val;
336 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
337 return res;
338}
339
Giorgio Arena17203582019-08-02 16:00:41 +0100340PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation,
341 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000342{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000343 const auto &strides = conv_info.stride();
344 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1.");
345
Giorgio Arena17203582019-08-02 16:00:41 +0100346 const unsigned int width_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
347 const unsigned int height_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
348 const unsigned int in_width = input_shape[width_idx];
349 const unsigned int in_height = input_shape[height_idx];
350 const unsigned int kernel_width = weights_shape[width_idx];
351 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100352
353 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100354 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
355 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
356 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100357
358 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100359 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
360 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100361
362 // Calculate total pad
Giorgio Arena17203582019-08-02 16:00:41 +0100363 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
364 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 +0100365
366 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100367 const unsigned int pad_left = pad_width / 2;
368 const unsigned int pad_top = pad_height / 2;
369 const unsigned int pad_right = pad_width - pad_left;
370 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000371
Giorgio Arena17203582019-08-02 16:00:41 +0100372 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
373
374 // Check for correctness of predicted output shape against the one calculated using the generated info
375 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
376 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
377 ARM_COMPUTE_UNUSED(out_dims);
378
379 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000380}
381
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100382std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
383 unsigned int kernel_width, unsigned int kernel_height,
384 const PadStrideInfo &pad_stride_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100385{
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100386 const unsigned int pad_left = pad_stride_info.pad_left();
387 const unsigned int pad_top = pad_stride_info.pad_top();
388 const unsigned int pad_right = pad_stride_info.pad_right();
389 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
390 const unsigned int stride_x = pad_stride_info.stride().first;
391 const unsigned int stride_y = pad_stride_info.stride().second;
392
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100393 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100394 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right));
395 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom));
396 const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right);
397 const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100398
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100399 return std::make_pair<unsigned int, unsigned int>(w, h);
400}
401
Pablo Tello01bbacb2019-04-30 10:32:42 +0100402std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
403 unsigned int kernel_width, unsigned int kernel_height,
404 const PadStrideInfo &pad_stride_info,
405 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100406{
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100407 const unsigned int pad_left = pad_stride_info.pad_left();
408 const unsigned int pad_top = pad_stride_info.pad_top();
409 const unsigned int pad_right = pad_stride_info.pad_right();
410 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
411 const unsigned int stride_x = pad_stride_info.stride().first;
412 const unsigned int stride_y = pad_stride_info.stride().second;
413 unsigned int w = 0;
414 unsigned int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100415 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100416 {
417 case DimensionRoundingType::FLOOR:
Alex Gilday7da29b62018-03-23 14:16:00 +0000418 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
419 h = static_cast<unsigned 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 +0100420 break;
421 case DimensionRoundingType::CEIL:
Alex Gilday7da29b62018-03-23 14:16:00 +0000422 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
423 h = static_cast<unsigned 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 +0100424 break;
425 default:
426 ARM_COMPUTE_ERROR("Unsupported rounding type");
427 }
428
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429 return std::make_pair(w, h);
430}
431
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100432bool arm_compute::needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
433{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100434 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
435 const bool is_quantized_type = is_data_type_quantized(dt);
436 const bool is_first_dim = (axis == 0);
437
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100438 return !is_first_dim || is_min_max || is_quantized_type;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100439}
440
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000441QuantizationInfo arm_compute::get_softmax_output_quantization_info(DataType input_type, bool is_log)
442{
443 // Note: Output quantization info for softmax should always have
444 // * Softmax with QASYMM8: scale = 1/256, offset = 0
445 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
446 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
447 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
448 if(is_data_type_quantized_asymmetric_signed(input_type))
449 {
450 if(is_log)
451 {
452 return QuantizationInfo(16.f / 256, 127);
453 }
454 else
455 {
456 return QuantizationInfo(1.f / 256, -128);
457 }
458 }
459 return QuantizationInfo(1.f / 256, 0);
460}
461
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000462float arm_compute::calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners)
463{
464 const size_t offset = align_corners ? 1 : 0;
465 const auto in = input_size - offset;
466 const auto out = output_size - offset;
467
468 ARM_COMPUTE_ERROR_ON((input_size == 0 || output_size == 0) && offset == 1);
469 ARM_COMPUTE_ERROR_ON(out == 0);
470
471 return static_cast<float>(in) / static_cast<float>(out);
472}
473
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000474std::pair<int32_t, int32_t> arm_compute::get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info)
475{
476 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
477 const auto a = act_info.a();
478 const auto b = act_info.b();
479 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
480 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
481 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
482
483 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int;
484 const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
485
486 return std::make_pair(min_activation, max_activation);
487}
488
giuros01edc21e42018-11-16 14:45:31 +0000489#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100490void 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)
491{
492 switch(dt)
493 {
494 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100495 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
497 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100498 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100499 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100500 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
502 break;
503 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100504 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
506 break;
507 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100508 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100509 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
510 break;
511 case DataType::U32:
512 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
513 break;
514 case DataType::S32:
515 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
516 break;
517 case DataType::F32:
518 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
519 break;
520 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100521 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100522 break;
523 default:
524 ARM_COMPUTE_ERROR("Undefined element size for given data type");
525 }
526}
527
528int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
529{
530 switch(dt)
531 {
532 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100533 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100534 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100535 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100536 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100537 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100538 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
539 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100540 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100541 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100542 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100543 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100544 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
545 case DataType::U32:
546 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
547 case DataType::S32:
548 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
549 case DataType::F32:
550 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
551 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100552 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100553 default:
554 ARM_COMPUTE_ERROR("Undefined element size for given data type");
555 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000556 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100557}
giuros01edc21e42018-11-16 14:45:31 +0000558#endif /* ARM_COMPUTE_ASSERTS_ENABLED */