blob: a6a5771ec13b2a6b3a5ef805f96c40f32ff88afe [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
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100255std::string arm_compute::string_from_pixel_value(const PixelValue &value, const DataType data_type)
256{
257 std::stringstream ss;
258 std::string converted_string;
259
260 switch(data_type)
261 {
262 case DataType::U8:
263 case DataType::QASYMM8:
264 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
265 ss << uint32_t(value.get<uint8_t>());
266 converted_string = ss.str();
267 break;
268 case DataType::S8:
269 // Needs conversion to 32 bit, otherwise interpreted as ASCII values
270 ss << int32_t(value.get<int8_t>());
271 converted_string = ss.str();
272 break;
273 case DataType::U16:
274 ss << value.get<uint16_t>();
275 converted_string = ss.str();
276 break;
277 case DataType::S16:
278 ss << value.get<int16_t>();
279 converted_string = ss.str();
280 break;
281 case DataType::U32:
282 ss << value.get<uint32_t>();
283 converted_string = ss.str();
284 break;
285 case DataType::S32:
286 ss << value.get<int32_t>();
287 converted_string = ss.str();
288 break;
289 case DataType::F32:
290 converted_string = float_to_string_with_full_precision(value.get<float>());
291 break;
292 case DataType::F16:
293 static_assert(sizeof(half) == 2, "Half must be 16 bit");
294 ss << value.get<half>();
295 converted_string = ss.str();
296 break;
297 default:
298 ARM_COMPUTE_ERROR("Not handled");
299 }
300
301 return converted_string;
302}
303
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304std::string arm_compute::lower_string(const std::string &val)
305{
306 std::string res = val;
307 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
308 return res;
309}
310
Georgios Pinitas4074c992018-01-30 18:13:46 +0000311PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info)
312{
313 const auto &strides = conv_info.stride();
314 const int out_width = std::ceil(float(input_shape.x()) / float(strides.first));
315 const int out_height = std::ceil(float(input_shape.y()) / float(strides.second));
316 const int pad_width = ((out_width - 1) * strides.first + weights_shape.x() - input_shape.x());
317 const int pad_height = ((out_height - 1) * strides.second + weights_shape.y() - input_shape.y());
318 const int same_pad_left = pad_width / 2;
319 const int same_pad_top = pad_height / 2;
320 const int same_pad_right = pad_width - same_pad_left;
321 const int same_pad_bottom = pad_height - same_pad_top;
322
323 return PadStrideInfo(strides.first, strides.second, same_pad_left, same_pad_right, same_pad_top, same_pad_bottom, DimensionRoundingType::CEIL);
324}
325
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100326TensorShape arm_compute::deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, TensorShape input, TensorShape weights)
327{
328 TensorShape out_shape(input);
329 out_shape.set(0, out_dims.first);
330 out_shape.set(1, out_dims.second);
331 out_shape.set(2, weights[3]);
332 return out_shape;
333}
334
335const std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(
336 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 +0100337 unsigned int stride_x, unsigned int stride_y)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100338{
339 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100340 ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < 2 * padx);
341 ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < 2 * pady);
342 const int w = stride_x * (in_width - 1) + kernel_width - 2 * padx;
343 const int h = stride_y * (in_height - 1) + kernel_height - 2 * pady;
344
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100345 return std::make_pair<unsigned int, unsigned int>(w, h);
346}
347
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100348const std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
349 unsigned int kernel_width, unsigned int kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000350 const PadStrideInfo &pad_stride_info,
351 const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352{
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100353 const unsigned int pad_left = pad_stride_info.pad_left();
354 const unsigned int pad_top = pad_stride_info.pad_top();
355 const unsigned int pad_right = pad_stride_info.pad_right();
356 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
357 const unsigned int stride_x = pad_stride_info.stride().first;
358 const unsigned int stride_y = pad_stride_info.stride().second;
359 unsigned int w = 0;
360 unsigned int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100361 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362 {
363 case DimensionRoundingType::FLOOR:
Alex Gilday7da29b62018-03-23 14:16:00 +0000364 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
365 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 +0100366 break;
367 case DimensionRoundingType::CEIL:
Alex Gilday7da29b62018-03-23 14:16:00 +0000368 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation.x() * (kernel_width - 1) + 1)) / stride_x) + 1));
369 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 +0100370 break;
371 default:
372 ARM_COMPUTE_ERROR("Unsupported rounding type");
373 }
374
375 // Make sure that border operations will start from inside the input and not the padded area
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100376 if(((w - 1) * stride_x) >= (width + pad_left))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377 {
378 --w;
379 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100380 if(((h - 1) * stride_y) >= (height + pad_top))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381 {
382 --h;
383 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100384 ARM_COMPUTE_ERROR_ON(((w - 1) * stride_x) >= (width + pad_left));
385 ARM_COMPUTE_ERROR_ON(((h - 1) * stride_y) >= (height + pad_top));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100386
387 return std::make_pair(w, h);
388}
389
390void 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)
391{
392 switch(dt)
393 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000394 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395 case DataType::U8:
396 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
397 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100398 case DataType::S8:
399 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
400 break;
401 case DataType::U16:
402 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
403 break;
404 case DataType::S16:
405 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
406 break;
407 case DataType::U32:
408 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
409 break;
410 case DataType::S32:
411 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
412 break;
413 case DataType::F32:
414 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
415 break;
416 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100417 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418 break;
419 default:
420 ARM_COMPUTE_ERROR("Undefined element size for given data type");
421 }
422}
423
424int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
425{
426 switch(dt)
427 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000428 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429 case DataType::U8:
430 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431 case DataType::S8:
432 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
433 case DataType::U16:
434 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100435 case DataType::S16:
436 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
437 case DataType::U32:
438 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
439 case DataType::S32:
440 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
441 case DataType::F32:
442 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
443 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100444 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100445 default:
446 ARM_COMPUTE_ERROR("Undefined element size for given data type");
447 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000448 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449}