blob: fb86d78cb78554d198145fecacd7c419d2e7afd1 [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" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 };
182
183 return act_map[act];
184}
185
186const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
187{
188 static std::map<MatrixPattern, const std::string> pattern_map =
189 {
190 { MatrixPattern::BOX, "BOX" },
191 { MatrixPattern::CROSS, "CROSS" },
192 { MatrixPattern::DISK, "DISK" },
193 { MatrixPattern::OTHER, "OTHER" },
194 };
195
196 return pattern_map[pattern];
197}
198
199const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
200{
201 static std::map<NonLinearFilterFunction, const std::string> func_map =
202 {
203 { NonLinearFilterFunction::MAX, "MAX" },
204 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
205 { NonLinearFilterFunction::MIN, "MIN" },
206 };
207
208 return func_map[function];
209}
210
211const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
212{
213 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
214 {
215 { InterpolationPolicy::AREA, "AREA" },
216 { InterpolationPolicy::BILINEAR, "BILINEAR" },
217 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
218 };
219
220 return interpolation_policy_map[policy];
221}
222
223const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
224{
225 static std::map<BorderMode, const std::string> border_mode_map =
226 {
227 { BorderMode::UNDEFINED, "UNDEFINED" },
228 { BorderMode::CONSTANT, "CONSTANT" },
229 { BorderMode::REPLICATE, "REPLICATE" },
230 };
231
232 return border_mode_map[border_mode];
233}
234
235const std::string &arm_compute::string_from_norm_type(NormType type)
236{
237 static std::map<NormType, const std::string> norm_type_map =
238 {
239 { NormType::IN_MAP_1D, "IN_MAP_1D" },
240 { NormType::IN_MAP_2D, "IN_MAP_2D" },
241 { NormType::CROSS_MAP, "CROSS_MAP" },
242 };
243
244 return norm_type_map[type];
245}
246
Georgios Pinitascdf51452017-08-31 14:21:36 +0100247const std::string &arm_compute::string_from_pooling_type(PoolingType type)
248{
249 static std::map<PoolingType, const std::string> pool_type_map =
250 {
251 { PoolingType::MAX, "MAX" },
252 { PoolingType::AVG, "AVG" },
253 { PoolingType::L2, "L2" },
254 };
255
256 return pool_type_map[type];
257}
258
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100259const std::string &arm_compute::string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
260{
261 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
262 {
263 { GEMMLowpOutputStageType::NONE, "" },
264 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
265 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
266 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
267 };
268
269 return output_stage_map[output_stage];
270}
271
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100272std::string arm_compute::string_from_pixel_value(const PixelValue &value, const DataType data_type)
273{
274 std::stringstream ss;
275 std::string converted_string;
276
277 switch(data_type)
278 {
279 case DataType::U8:
280 case DataType::QASYMM8:
281 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
282 ss << uint32_t(value.get<uint8_t>());
283 converted_string = ss.str();
284 break;
285 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100286 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100287 case DataType::QSYMM8_PER_CHANNEL:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100288 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
289 ss << int32_t(value.get<int8_t>());
290 converted_string = ss.str();
291 break;
292 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100293 case DataType::QASYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100294 ss << value.get<uint16_t>();
295 converted_string = ss.str();
296 break;
297 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100298 case DataType::QSYMM16:
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100299 ss << value.get<int16_t>();
300 converted_string = ss.str();
301 break;
302 case DataType::U32:
303 ss << value.get<uint32_t>();
304 converted_string = ss.str();
305 break;
306 case DataType::S32:
307 ss << value.get<int32_t>();
308 converted_string = ss.str();
309 break;
310 case DataType::F32:
311 converted_string = float_to_string_with_full_precision(value.get<float>());
312 break;
313 case DataType::F16:
314 static_assert(sizeof(half) == 2, "Half must be 16 bit");
315 ss << value.get<half>();
316 converted_string = ss.str();
317 break;
318 default:
319 ARM_COMPUTE_ERROR("Not handled");
320 }
321
322 return converted_string;
323}
324
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325std::string arm_compute::lower_string(const std::string &val)
326{
327 std::string res = val;
328 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
329 return res;
330}
331
Giorgio Arena17203582019-08-02 16:00:41 +0100332PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation,
333 const DimensionRoundingType &rounding_type)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000334{
Sang-Hoon Park46023ed2019-11-12 16:26:26 +0000335 const auto &strides = conv_info.stride();
336 ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1.");
337
Giorgio Arena17203582019-08-02 16:00:41 +0100338 const unsigned int width_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
339 const unsigned int height_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
340 const unsigned int in_width = input_shape[width_idx];
341 const unsigned int in_height = input_shape[height_idx];
342 const unsigned int kernel_width = weights_shape[width_idx];
343 const unsigned int kernel_height = weights_shape[height_idx];
Georgios Pinitas4c758512019-07-10 19:49:11 +0100344
345 // Calculate output dimensions
Giorgio Arena17203582019-08-02 16:00:41 +0100346 const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL);
347 const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil;
348 const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100349
350 // Calculate effective weights sizes
Giorgio Arena17203582019-08-02 16:00:41 +0100351 const int real_weight_width = (kernel_width - 1) * dilation.x() + 1;
352 const int real_weight_height = (kernel_height - 1) * dilation.y() + 1;
Georgios Pinitas4c758512019-07-10 19:49:11 +0100353
354 // Calculate total pad
Giorgio Arena17203582019-08-02 16:00:41 +0100355 const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width));
356 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 +0100357
358 // Calculate individual paddings
Giorgio Arena17203582019-08-02 16:00:41 +0100359 const unsigned int pad_left = pad_width / 2;
360 const unsigned int pad_top = pad_height / 2;
361 const unsigned int pad_right = pad_width - pad_left;
362 const unsigned int pad_bottom = pad_height - pad_top;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000363
Giorgio Arena17203582019-08-02 16:00:41 +0100364 PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type);
365
366 // Check for correctness of predicted output shape against the one calculated using the generated info
367 const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation);
368 ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height);
369 ARM_COMPUTE_UNUSED(out_dims);
370
371 return same_info;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000372}
373
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100374std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
375 unsigned int kernel_width, unsigned int kernel_height,
376 const PadStrideInfo &pad_stride_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100377{
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100378 const unsigned int pad_left = pad_stride_info.pad_left();
379 const unsigned int pad_top = pad_stride_info.pad_top();
380 const unsigned int pad_right = pad_stride_info.pad_right();
381 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
382 const unsigned int stride_x = pad_stride_info.stride().first;
383 const unsigned int stride_y = pad_stride_info.stride().second;
384
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100385 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100386 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right));
387 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom));
388 const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right);
389 const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100390
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100391 return std::make_pair<unsigned int, unsigned int>(w, h);
392}
393
Georgios Pinitas80838f12019-12-12 18:23:13 +0000394std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(int width, int height,
395 int kernel_width, int kernel_height,
Pablo Tello01bbacb2019-04-30 10:32:42 +0100396 const PadStrideInfo &pad_stride_info,
397 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100398{
Georgios Pinitas80838f12019-12-12 18:23:13 +0000399 const int dilation_x = dilation.x();
400 const int dilation_y = dilation.y();
401 const int pad_left = pad_stride_info.pad_left();
402 const int pad_top = pad_stride_info.pad_top();
403 const int pad_right = pad_stride_info.pad_right();
404 const int pad_bottom = pad_stride_info.pad_bottom();
405 const int stride_x = pad_stride_info.stride().first;
406 const int stride_y = pad_stride_info.stride().second;
407 int w = 0;
408 int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100409 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100410 {
411 case DimensionRoundingType::FLOOR:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000412 w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
413 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 +0100414 break;
415 case DimensionRoundingType::CEIL:
Georgios Pinitas80838f12019-12-12 18:23:13 +0000416 w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1));
417 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 +0100418 break;
419 default:
420 ARM_COMPUTE_ERROR("Unsupported rounding type");
421 }
422
Georgios Pinitas80838f12019-12-12 18:23:13 +0000423 w = std::max(1, w);
424 h = std::max(1, h);
425 return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426}
427
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100428bool arm_compute::needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
429{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100430 const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN);
431 const bool is_quantized_type = is_data_type_quantized(dt);
432 const bool is_first_dim = (axis == 0);
433
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100434 return !is_first_dim || is_min_max || is_quantized_type;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100435}
436
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000437QuantizationInfo arm_compute::get_softmax_output_quantization_info(DataType input_type, bool is_log)
438{
439 // Note: Output quantization info for softmax should always have
440 // * Softmax with QASYMM8: scale = 1/256, offset = 0
441 // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128
442 // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0
443 // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127
444 if(is_data_type_quantized_asymmetric_signed(input_type))
445 {
446 if(is_log)
447 {
448 return QuantizationInfo(16.f / 256, 127);
449 }
450 else
451 {
452 return QuantizationInfo(1.f / 256, -128);
453 }
454 }
455 return QuantizationInfo(1.f / 256, 0);
456}
457
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000458float arm_compute::calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners)
459{
460 const size_t offset = align_corners ? 1 : 0;
461 const auto in = input_size - offset;
462 const auto out = output_size - offset;
463
464 ARM_COMPUTE_ERROR_ON((input_size == 0 || output_size == 0) && offset == 1);
465 ARM_COMPUTE_ERROR_ON(out == 0);
466
467 return static_cast<float>(in) / static_cast<float>(out);
468}
469
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000470std::pair<int32_t, int32_t> arm_compute::get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info)
471{
472 const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type);
473 const auto a = act_info.a();
474 const auto b = act_info.b();
475 const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info);
476 const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info);
477 const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>();
478
479 const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int;
480 const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int;
481
482 return std::make_pair(min_activation, max_activation);
483}
484
giuros01edc21e42018-11-16 14:45:31 +0000485#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100486void 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)
487{
488 switch(dt)
489 {
490 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100491 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100492 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
493 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100494 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100495 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100496 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100497 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
498 break;
499 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100500 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
502 break;
503 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100504 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
506 break;
507 case DataType::U32:
508 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
509 break;
510 case DataType::S32:
511 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
512 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000513 case DataType::BFLOAT16:
514 print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100515 break;
516 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100517 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100518 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000519 case DataType::F32:
520 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
521 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100522 default:
523 ARM_COMPUTE_ERROR("Undefined element size for given data type");
524 }
525}
526
527int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
528{
529 switch(dt)
530 {
531 case DataType::U8:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100532 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100534 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100535 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100536 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100537 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
538 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100539 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100540 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100541 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100542 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100543 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
544 case DataType::U32:
545 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
546 case DataType::S32:
547 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000548 case DataType::BFLOAT16:
549 return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100550 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100551 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000552 case DataType::F32:
553 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100554 default:
555 ARM_COMPUTE_ERROR("Undefined element size for given data type");
556 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000557 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100558}
giuros01edc21e42018-11-16 14:45:31 +0000559#endif /* ARM_COMPUTE_ASSERTS_ENABLED */