blob: 6080dc45ba9aa45a32d19117effe2eb0d7e254c0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou780db4e2017-11-23 09:49:51 +00002 * Copyright (c) 2016, 2017, 2018 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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025#include "arm_compute/core/Utils.h"
26
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +000027#include "support/ToolchainSupport.h"
28
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
36using namespace arm_compute;
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000037#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038std::string arm_compute::build_information()
39{
40 static const std::string information =
41#include "arm_compute_version.embed"
42 ;
43 return information;
44}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000045#endif /* DOXYGEN_SKIP_THIS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046std::string arm_compute::read_file(const std::string &filename, bool binary)
47{
48 std::string out;
49 std::ifstream fs;
50
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000051#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 try
53 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000054#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
56 std::ios_base::openmode mode = std::ios::in;
57
58 if(binary)
59 {
60 mode |= std::ios::binary;
61 }
62
63 fs.open(filename, mode);
64
65 // Go to the end of the file
66 fs.seekg(0, std::ios::end);
67 // Reserve the memory required to store the file's content
68 out.reserve(fs.tellg());
69 // Go back to the beginning of the file
70 fs.seekg(0, std::ios::beg);
71 // Copy the content of the file
72 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000073#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 }
75 catch(const std::ifstream::failure &e)
76 {
77 ARM_COMPUTE_ERROR("Accessing %s: %s", filename.c_str(), e.what());
78 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000079#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 return out;
82}
83
84const std::string &arm_compute::string_from_format(Format format)
85{
86 static std::map<Format, const std::string> formats_map =
87 {
88 { Format::UNKNOWN, "UNKNOWN" },
89 { Format::U8, "U8" },
90 { Format::S16, "S16" },
91 { Format::U16, "U16" },
92 { Format::S32, "S32" },
93 { Format::U32, "U32" },
94 { Format::F16, "F16" },
95 { Format::F32, "F32" },
96 { Format::UV88, "UV88" },
97 { Format::RGB888, "RGB888" },
98 { Format::RGBA8888, "RGBA8888" },
99 { Format::YUV444, "YUV444" },
100 { Format::YUYV422, "YUYV422" },
101 { Format::NV12, "NV12" },
102 { Format::NV21, "NV21" },
103 { Format::IYUV, "IYUV" },
104 { Format::UYVY422, "UYVY422" }
105 };
106
107 return formats_map[format];
108}
109
110const std::string &arm_compute::string_from_channel(Channel channel)
111{
112 static std::map<Channel, const std::string> channels_map =
113 {
114 { Channel::UNKNOWN, "UNKNOWN" },
115 { Channel::R, "R" },
116 { Channel::G, "G" },
117 { Channel::B, "B" },
118 { Channel::A, "A" },
119 { Channel::Y, "Y" },
120 { Channel::U, "U" },
121 { Channel::V, "V" },
122 { Channel::C0, "C0" },
123 { Channel::C1, "C1" },
124 { Channel::C2, "C2" },
125 { Channel::C3, "C3" }
126 };
127
128 return channels_map[channel];
129}
130
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000131const std::string &arm_compute::string_from_data_layout(DataLayout dl)
132{
133 static std::map<DataLayout, const std::string> dl_map =
134 {
135 { DataLayout::UNKNOWN, "UNKNOWN" },
136 { DataLayout::NCHW, "NCHW" },
137 { DataLayout::NHWC, "NHWC" },
138 };
139
140 return dl_map[dl];
141}
142
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143const std::string &arm_compute::string_from_data_type(DataType dt)
144{
145 static std::map<DataType, const std::string> dt_map =
146 {
147 { DataType::UNKNOWN, "UNKNOWN" },
148 { DataType::S8, "S8" },
149 { DataType::U8, "U8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 { DataType::S16, "S16" },
151 { DataType::U16, "U16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 { DataType::S32, "S32" },
153 { DataType::U32, "U32" },
154 { DataType::S64, "S64" },
155 { DataType::U64, "U64" },
156 { DataType::F16, "F16" },
157 { DataType::F32, "F32" },
158 { DataType::F64, "F64" },
159 { DataType::SIZET, "SIZET" },
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100160 { DataType::QASYMM8, "QASYMM8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 };
162
163 return dt_map[dt];
164}
165
166const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
167{
168 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
169 {
170 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
171 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
172 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
173 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
174 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100175 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100176 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
178 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
179 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
180 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
181 };
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:
286 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
287 ss << int32_t(value.get<int8_t>());
288 converted_string = ss.str();
289 break;
290 case DataType::U16:
291 ss << value.get<uint16_t>();
292 converted_string = ss.str();
293 break;
294 case DataType::S16:
295 ss << value.get<int16_t>();
296 converted_string = ss.str();
297 break;
298 case DataType::U32:
299 ss << value.get<uint32_t>();
300 converted_string = ss.str();
301 break;
302 case DataType::S32:
303 ss << value.get<int32_t>();
304 converted_string = ss.str();
305 break;
306 case DataType::F32:
307 converted_string = float_to_string_with_full_precision(value.get<float>());
308 break;
309 case DataType::F16:
310 static_assert(sizeof(half) == 2, "Half must be 16 bit");
311 ss << value.get<half>();
312 converted_string = ss.str();
313 break;
314 default:
315 ARM_COMPUTE_ERROR("Not handled");
316 }
317
318 return converted_string;
319}
320
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321std::string arm_compute::lower_string(const std::string &val)
322{
323 std::string res = val;
324 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
325 return res;
326}
327
Georgios Pinitas4074c992018-01-30 18:13:46 +0000328PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info)
329{
330 const auto &strides = conv_info.stride();
331 const int out_width = std::ceil(float(input_shape.x()) / float(strides.first));
332 const int out_height = std::ceil(float(input_shape.y()) / float(strides.second));
333 const int pad_width = ((out_width - 1) * strides.first + weights_shape.x() - input_shape.x());
334 const int pad_height = ((out_height - 1) * strides.second + weights_shape.y() - input_shape.y());
335 const int same_pad_left = pad_width / 2;
336 const int same_pad_top = pad_height / 2;
337 const int same_pad_right = pad_width - same_pad_left;
338 const int same_pad_bottom = pad_height - same_pad_top;
339
340 return PadStrideInfo(strides.first, strides.second, same_pad_left, same_pad_right, same_pad_top, same_pad_bottom, DimensionRoundingType::CEIL);
341}
342
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100343const std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(
344 unsigned int in_width, unsigned int in_height, unsigned int kernel_width, unsigned int kernel_height, unsigned int padx, unsigned int pady,
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100345 unsigned int stride_x, unsigned int stride_y)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100346{
347 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100348 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < 2 * padx);
349 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < 2 * pady);
350 const int w = stride_x * (in_width - 1) + kernel_width - 2 * padx;
351 const int h = stride_y * (in_height - 1) + kernel_height - 2 * pady;
352
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100353 return std::make_pair<unsigned int, unsigned int>(w, h);
354}
355
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100356const std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
357 unsigned int kernel_width, unsigned int kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000358 const PadStrideInfo &pad_stride_info,
359 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360{
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100361 const unsigned int pad_left = pad_stride_info.pad_left();
362 const unsigned int pad_top = pad_stride_info.pad_top();
363 const unsigned int pad_right = pad_stride_info.pad_right();
364 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
365 const unsigned int stride_x = pad_stride_info.stride().first;
366 const unsigned int stride_y = pad_stride_info.stride().second;
367 unsigned int w = 0;
368 unsigned int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100369 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370 {
371 case DimensionRoundingType::FLOOR:
Alex Gilday7da29b62018-03-23 14:16:00 +0000372 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
373 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 +0100374 break;
375 case DimensionRoundingType::CEIL:
Alex Gilday7da29b62018-03-23 14:16:00 +0000376 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
377 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 +0100378 break;
379 default:
380 ARM_COMPUTE_ERROR("Unsupported rounding type");
381 }
382
383 // Make sure that border operations will start from inside the input and not the padded area
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100384 if(((w - 1) * stride_x) >= (width + pad_left))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100385 {
386 --w;
387 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100388 if(((h - 1) * stride_y) >= (height + pad_top))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100389 {
390 --h;
391 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100392 ARM_COMPUTE_ERROR_ON(((w - 1) * stride_x) >= (width + pad_left));
393 ARM_COMPUTE_ERROR_ON(((h - 1) * stride_y) >= (height + pad_top));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394
395 return std::make_pair(w, h);
396}
397
398void 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)
399{
400 switch(dt)
401 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000402 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403 case DataType::U8:
404 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
405 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100406 case DataType::S8:
407 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
408 break;
409 case DataType::U16:
410 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
411 break;
412 case DataType::S16:
413 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
414 break;
415 case DataType::U32:
416 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
417 break;
418 case DataType::S32:
419 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
420 break;
421 case DataType::F32:
422 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
423 break;
424 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100425 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426 break;
427 default:
428 ARM_COMPUTE_ERROR("Undefined element size for given data type");
429 }
430}
431
432int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
433{
434 switch(dt)
435 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000436 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100437 case DataType::U8:
438 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439 case DataType::S8:
440 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
441 case DataType::U16:
442 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100443 case DataType::S16:
444 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
445 case DataType::U32:
446 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
447 case DataType::S32:
448 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
449 case DataType::F32:
450 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
451 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100452 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100453 default:
454 ARM_COMPUTE_ERROR("Undefined element size for given data type");
455 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000456 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100457}