blob: 78c3dba25a7a9fcebb1fab13fbc9338dd3068414 [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;
37
38std::string arm_compute::build_information()
39{
40 static const std::string information =
41#include "arm_compute_version.embed"
42 ;
43 return information;
44}
45
46std::string arm_compute::read_file(const std::string &filename, bool binary)
47{
48 std::string out;
49 std::ifstream fs;
50
51 try
52 {
53 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
54 std::ios_base::openmode mode = std::ios::in;
55
56 if(binary)
57 {
58 mode |= std::ios::binary;
59 }
60
61 fs.open(filename, mode);
62
63 // Go to the end of the file
64 fs.seekg(0, std::ios::end);
65 // Reserve the memory required to store the file's content
66 out.reserve(fs.tellg());
67 // Go back to the beginning of the file
68 fs.seekg(0, std::ios::beg);
69 // Copy the content of the file
70 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
71 }
72 catch(const std::ifstream::failure &e)
73 {
74 ARM_COMPUTE_ERROR("Accessing %s: %s", filename.c_str(), e.what());
75 }
76
77 return out;
78}
79
80const std::string &arm_compute::string_from_format(Format format)
81{
82 static std::map<Format, const std::string> formats_map =
83 {
84 { Format::UNKNOWN, "UNKNOWN" },
85 { Format::U8, "U8" },
86 { Format::S16, "S16" },
87 { Format::U16, "U16" },
88 { Format::S32, "S32" },
89 { Format::U32, "U32" },
90 { Format::F16, "F16" },
91 { Format::F32, "F32" },
92 { Format::UV88, "UV88" },
93 { Format::RGB888, "RGB888" },
94 { Format::RGBA8888, "RGBA8888" },
95 { Format::YUV444, "YUV444" },
96 { Format::YUYV422, "YUYV422" },
97 { Format::NV12, "NV12" },
98 { Format::NV21, "NV21" },
99 { Format::IYUV, "IYUV" },
100 { Format::UYVY422, "UYVY422" }
101 };
102
103 return formats_map[format];
104}
105
106const std::string &arm_compute::string_from_channel(Channel channel)
107{
108 static std::map<Channel, const std::string> channels_map =
109 {
110 { Channel::UNKNOWN, "UNKNOWN" },
111 { Channel::R, "R" },
112 { Channel::G, "G" },
113 { Channel::B, "B" },
114 { Channel::A, "A" },
115 { Channel::Y, "Y" },
116 { Channel::U, "U" },
117 { Channel::V, "V" },
118 { Channel::C0, "C0" },
119 { Channel::C1, "C1" },
120 { Channel::C2, "C2" },
121 { Channel::C3, "C3" }
122 };
123
124 return channels_map[channel];
125}
126
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000127const std::string &arm_compute::string_from_data_layout(DataLayout dl)
128{
129 static std::map<DataLayout, const std::string> dl_map =
130 {
131 { DataLayout::UNKNOWN, "UNKNOWN" },
132 { DataLayout::NCHW, "NCHW" },
133 { DataLayout::NHWC, "NHWC" },
134 };
135
136 return dl_map[dl];
137}
138
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139const std::string &arm_compute::string_from_data_type(DataType dt)
140{
141 static std::map<DataType, const std::string> dt_map =
142 {
143 { DataType::UNKNOWN, "UNKNOWN" },
144 { DataType::S8, "S8" },
145 { DataType::U8, "U8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 { DataType::S16, "S16" },
147 { DataType::U16, "U16" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 { DataType::S32, "S32" },
149 { DataType::U32, "U32" },
150 { DataType::S64, "S64" },
151 { DataType::U64, "U64" },
152 { DataType::F16, "F16" },
153 { DataType::F32, "F32" },
154 { DataType::F64, "F64" },
155 { DataType::SIZET, "SIZET" },
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100156 { DataType::QASYMM8, "QASYMM8" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 };
158
159 return dt_map[dt];
160}
161
162const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
163{
164 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
165 {
166 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
167 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
168 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
169 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
170 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100171 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100172 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
174 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
175 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
176 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
177 };
178
179 return act_map[act];
180}
181
182const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
183{
184 static std::map<MatrixPattern, const std::string> pattern_map =
185 {
186 { MatrixPattern::BOX, "BOX" },
187 { MatrixPattern::CROSS, "CROSS" },
188 { MatrixPattern::DISK, "DISK" },
189 { MatrixPattern::OTHER, "OTHER" },
190 };
191
192 return pattern_map[pattern];
193}
194
195const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
196{
197 static std::map<NonLinearFilterFunction, const std::string> func_map =
198 {
199 { NonLinearFilterFunction::MAX, "MAX" },
200 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
201 { NonLinearFilterFunction::MIN, "MIN" },
202 };
203
204 return func_map[function];
205}
206
207const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
208{
209 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
210 {
211 { InterpolationPolicy::AREA, "AREA" },
212 { InterpolationPolicy::BILINEAR, "BILINEAR" },
213 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
214 };
215
216 return interpolation_policy_map[policy];
217}
218
219const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
220{
221 static std::map<BorderMode, const std::string> border_mode_map =
222 {
223 { BorderMode::UNDEFINED, "UNDEFINED" },
224 { BorderMode::CONSTANT, "CONSTANT" },
225 { BorderMode::REPLICATE, "REPLICATE" },
226 };
227
228 return border_mode_map[border_mode];
229}
230
231const std::string &arm_compute::string_from_norm_type(NormType type)
232{
233 static std::map<NormType, const std::string> norm_type_map =
234 {
235 { NormType::IN_MAP_1D, "IN_MAP_1D" },
236 { NormType::IN_MAP_2D, "IN_MAP_2D" },
237 { NormType::CROSS_MAP, "CROSS_MAP" },
238 };
239
240 return norm_type_map[type];
241}
242
Georgios Pinitascdf51452017-08-31 14:21:36 +0100243const std::string &arm_compute::string_from_pooling_type(PoolingType type)
244{
245 static std::map<PoolingType, const std::string> pool_type_map =
246 {
247 { PoolingType::MAX, "MAX" },
248 { PoolingType::AVG, "AVG" },
249 { PoolingType::L2, "L2" },
250 };
251
252 return pool_type_map[type];
253}
254
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100255const std::string &arm_compute::string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
256{
257 static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map =
258 {
259 { GEMMLowpOutputStageType::NONE, "" },
260 { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" },
261 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" },
262 { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" }
263 };
264
265 return output_stage_map[output_stage];
266}
267
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100268std::string arm_compute::string_from_pixel_value(const PixelValue &value, const DataType data_type)
269{
270 std::stringstream ss;
271 std::string converted_string;
272
273 switch(data_type)
274 {
275 case DataType::U8:
276 case DataType::QASYMM8:
277 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
278 ss << uint32_t(value.get<uint8_t>());
279 converted_string = ss.str();
280 break;
281 case DataType::S8:
282 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
283 ss << int32_t(value.get<int8_t>());
284 converted_string = ss.str();
285 break;
286 case DataType::U16:
287 ss << value.get<uint16_t>();
288 converted_string = ss.str();
289 break;
290 case DataType::S16:
291 ss << value.get<int16_t>();
292 converted_string = ss.str();
293 break;
294 case DataType::U32:
295 ss << value.get<uint32_t>();
296 converted_string = ss.str();
297 break;
298 case DataType::S32:
299 ss << value.get<int32_t>();
300 converted_string = ss.str();
301 break;
302 case DataType::F32:
303 converted_string = float_to_string_with_full_precision(value.get<float>());
304 break;
305 case DataType::F16:
306 static_assert(sizeof(half) == 2, "Half must be 16 bit");
307 ss << value.get<half>();
308 converted_string = ss.str();
309 break;
310 default:
311 ARM_COMPUTE_ERROR("Not handled");
312 }
313
314 return converted_string;
315}
316
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317std::string arm_compute::lower_string(const std::string &val)
318{
319 std::string res = val;
320 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
321 return res;
322}
323
Georgios Pinitas4074c992018-01-30 18:13:46 +0000324PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info)
325{
326 const auto &strides = conv_info.stride();
327 const int out_width = std::ceil(float(input_shape.x()) / float(strides.first));
328 const int out_height = std::ceil(float(input_shape.y()) / float(strides.second));
329 const int pad_width = ((out_width - 1) * strides.first + weights_shape.x() - input_shape.x());
330 const int pad_height = ((out_height - 1) * strides.second + weights_shape.y() - input_shape.y());
331 const int same_pad_left = pad_width / 2;
332 const int same_pad_top = pad_height / 2;
333 const int same_pad_right = pad_width - same_pad_left;
334 const int same_pad_bottom = pad_height - same_pad_top;
335
336 return PadStrideInfo(strides.first, strides.second, same_pad_left, same_pad_right, same_pad_top, same_pad_bottom, DimensionRoundingType::CEIL);
337}
338
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100339const std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(
340 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 +0100341 unsigned int stride_x, unsigned int stride_y)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100342{
343 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100344 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < 2 * padx);
345 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < 2 * pady);
346 const int w = stride_x * (in_width - 1) + kernel_width - 2 * padx;
347 const int h = stride_y * (in_height - 1) + kernel_height - 2 * pady;
348
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100349 return std::make_pair<unsigned int, unsigned int>(w, h);
350}
351
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100352const std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
353 unsigned int kernel_width, unsigned int kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000354 const PadStrideInfo &pad_stride_info,
355 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356{
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100357 const unsigned int pad_left = pad_stride_info.pad_left();
358 const unsigned int pad_top = pad_stride_info.pad_top();
359 const unsigned int pad_right = pad_stride_info.pad_right();
360 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
361 const unsigned int stride_x = pad_stride_info.stride().first;
362 const unsigned int stride_y = pad_stride_info.stride().second;
363 unsigned int w = 0;
364 unsigned int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100365 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366 {
367 case DimensionRoundingType::FLOOR:
Alex Gilday7da29b62018-03-23 14:16:00 +0000368 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
369 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 +0100370 break;
371 case DimensionRoundingType::CEIL:
Alex Gilday7da29b62018-03-23 14:16:00 +0000372 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
373 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 +0100374 break;
375 default:
376 ARM_COMPUTE_ERROR("Unsupported rounding type");
377 }
378
379 // Make sure that border operations will start from inside the input and not the padded area
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100380 if(((w - 1) * stride_x) >= (width + pad_left))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381 {
382 --w;
383 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100384 if(((h - 1) * stride_y) >= (height + pad_top))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100385 {
386 --h;
387 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100388 ARM_COMPUTE_ERROR_ON(((w - 1) * stride_x) >= (width + pad_left));
389 ARM_COMPUTE_ERROR_ON(((h - 1) * stride_y) >= (height + pad_top));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390
391 return std::make_pair(w, h);
392}
393
394void 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)
395{
396 switch(dt)
397 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000398 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100399 case DataType::U8:
400 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
401 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100402 case DataType::S8:
403 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
404 break;
405 case DataType::U16:
406 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
407 break;
408 case DataType::S16:
409 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
410 break;
411 case DataType::U32:
412 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
413 break;
414 case DataType::S32:
415 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
416 break;
417 case DataType::F32:
418 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
419 break;
420 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100421 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100422 break;
423 default:
424 ARM_COMPUTE_ERROR("Undefined element size for given data type");
425 }
426}
427
428int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
429{
430 switch(dt)
431 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000432 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433 case DataType::U8:
434 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100435 case DataType::S8:
436 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
437 case DataType::U16:
438 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439 case DataType::S16:
440 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
441 case DataType::U32:
442 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
443 case DataType::S32:
444 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
445 case DataType::F32:
446 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
447 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100448 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449 default:
450 ARM_COMPUTE_ERROR("Undefined element size for given data type");
451 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000452 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100453}