blob: af50bbbaf7c3a28f921244d6dbc7779fc90ac4f4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
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
27#include "arm_compute/core/FixedPoint.h"
28
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +000029#include "support/ToolchainSupport.h"
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <algorithm>
32#include <cmath>
33#include <cstdint>
34#include <fstream>
35#include <map>
36#include <string>
37
38using namespace arm_compute;
39
40std::string arm_compute::build_information()
41{
42 static const std::string information =
43#include "arm_compute_version.embed"
44 ;
45 return information;
46}
47
48std::string arm_compute::read_file(const std::string &filename, bool binary)
49{
50 std::string out;
51 std::ifstream fs;
52
53 try
54 {
55 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>());
73 }
74 catch(const std::ifstream::failure &e)
75 {
76 ARM_COMPUTE_ERROR("Accessing %s: %s", filename.c_str(), e.what());
77 }
78
79 return out;
80}
81
82const std::string &arm_compute::string_from_format(Format format)
83{
84 static std::map<Format, const std::string> formats_map =
85 {
86 { Format::UNKNOWN, "UNKNOWN" },
87 { Format::U8, "U8" },
88 { Format::S16, "S16" },
89 { Format::U16, "U16" },
90 { Format::S32, "S32" },
91 { Format::U32, "U32" },
92 { Format::F16, "F16" },
93 { Format::F32, "F32" },
94 { Format::UV88, "UV88" },
95 { Format::RGB888, "RGB888" },
96 { Format::RGBA8888, "RGBA8888" },
97 { Format::YUV444, "YUV444" },
98 { Format::YUYV422, "YUYV422" },
99 { Format::NV12, "NV12" },
100 { Format::NV21, "NV21" },
101 { Format::IYUV, "IYUV" },
102 { Format::UYVY422, "UYVY422" }
103 };
104
105 return formats_map[format];
106}
107
108const std::string &arm_compute::string_from_channel(Channel channel)
109{
110 static std::map<Channel, const std::string> channels_map =
111 {
112 { Channel::UNKNOWN, "UNKNOWN" },
113 { Channel::R, "R" },
114 { Channel::G, "G" },
115 { Channel::B, "B" },
116 { Channel::A, "A" },
117 { Channel::Y, "Y" },
118 { Channel::U, "U" },
119 { Channel::V, "V" },
120 { Channel::C0, "C0" },
121 { Channel::C1, "C1" },
122 { Channel::C2, "C2" },
123 { Channel::C3, "C3" }
124 };
125
126 return channels_map[channel];
127}
128
129const std::string &arm_compute::string_from_data_type(DataType dt)
130{
131 static std::map<DataType, const std::string> dt_map =
132 {
133 { DataType::UNKNOWN, "UNKNOWN" },
134 { DataType::S8, "S8" },
135 { DataType::U8, "U8" },
136 { DataType::QS8, "QS8" },
137 { DataType::S16, "S16" },
138 { DataType::U16, "U16" },
139 { DataType::QS16, "QS16" },
140 { DataType::S32, "S32" },
141 { DataType::U32, "U32" },
142 { DataType::S64, "S64" },
143 { DataType::U64, "U64" },
144 { DataType::F16, "F16" },
145 { DataType::F32, "F32" },
146 { DataType::F64, "F64" },
147 { DataType::SIZET, "SIZET" },
148 };
149
150 return dt_map[dt];
151}
152
153const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
154{
155 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
156 {
157 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
158 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
159 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
160 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
161 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100162 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100163 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
165 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
166 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
167 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
168 };
169
170 return act_map[act];
171}
172
173const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
174{
175 static std::map<MatrixPattern, const std::string> pattern_map =
176 {
177 { MatrixPattern::BOX, "BOX" },
178 { MatrixPattern::CROSS, "CROSS" },
179 { MatrixPattern::DISK, "DISK" },
180 { MatrixPattern::OTHER, "OTHER" },
181 };
182
183 return pattern_map[pattern];
184}
185
186const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
187{
188 static std::map<NonLinearFilterFunction, const std::string> func_map =
189 {
190 { NonLinearFilterFunction::MAX, "MAX" },
191 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
192 { NonLinearFilterFunction::MIN, "MIN" },
193 };
194
195 return func_map[function];
196}
197
198const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
199{
200 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
201 {
202 { InterpolationPolicy::AREA, "AREA" },
203 { InterpolationPolicy::BILINEAR, "BILINEAR" },
204 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
205 };
206
207 return interpolation_policy_map[policy];
208}
209
210const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
211{
212 static std::map<BorderMode, const std::string> border_mode_map =
213 {
214 { BorderMode::UNDEFINED, "UNDEFINED" },
215 { BorderMode::CONSTANT, "CONSTANT" },
216 { BorderMode::REPLICATE, "REPLICATE" },
217 };
218
219 return border_mode_map[border_mode];
220}
221
222const std::string &arm_compute::string_from_norm_type(NormType type)
223{
224 static std::map<NormType, const std::string> norm_type_map =
225 {
226 { NormType::IN_MAP_1D, "IN_MAP_1D" },
227 { NormType::IN_MAP_2D, "IN_MAP_2D" },
228 { NormType::CROSS_MAP, "CROSS_MAP" },
229 };
230
231 return norm_type_map[type];
232}
233
Georgios Pinitascdf51452017-08-31 14:21:36 +0100234const std::string &arm_compute::string_from_pooling_type(PoolingType type)
235{
236 static std::map<PoolingType, const std::string> pool_type_map =
237 {
238 { PoolingType::MAX, "MAX" },
239 { PoolingType::AVG, "AVG" },
240 { PoolingType::L2, "L2" },
241 };
242
243 return pool_type_map[type];
244}
245
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246std::string arm_compute::lower_string(const std::string &val)
247{
248 std::string res = val;
249 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
250 return res;
251}
252
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100253TensorShape arm_compute::deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, TensorShape input, TensorShape weights)
254{
255 TensorShape out_shape(input);
256 out_shape.set(0, out_dims.first);
257 out_shape.set(1, out_dims.second);
258 out_shape.set(2, weights[3]);
259 return out_shape;
260}
261
262const std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(
263 unsigned int in_width, unsigned int in_height, unsigned int kernel_width, unsigned int kernel_height, unsigned int padx, unsigned int pady,
264 unsigned int ax, unsigned int ay, float upscalex, float upscaley, DimensionRoundingType round)
265{
266 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
267 ARM_COMPUTE_ERROR_ON(((in_width - 1) * upscalex + kernel_width + ax) < 2.f * padx);
268 ARM_COMPUTE_ERROR_ON(((in_height - 1) * upscaley + kernel_height + ay) < 2.f * pady);
269 const float fw = (in_width - 1) * upscalex - 2.f * padx + kernel_width + ax;
270 const float fh = (in_height - 1) * upscaley - 2.f * pady + kernel_height + ay;
271 int w = 0;
272 int h = 0;
273 switch(round)
274 {
275 case DimensionRoundingType::FLOOR:
276 w = std::floor(fw);
277 h = std::floor(fh);
278 break;
279 case DimensionRoundingType::CEIL:
280 w = std::ceil(fw);
281 h = std::ceil(fh);
282 break;
283 default:
284 ARM_COMPUTE_ERROR("Not supported");
285 break;
286 }
287 return std::make_pair<unsigned int, unsigned int>(w, h);
288}
289
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100290const std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
291 unsigned int kernel_width, unsigned int kernel_height,
292 const PadStrideInfo &pad_stride_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293{
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100294 const unsigned int pad_left = pad_stride_info.pad_left();
295 const unsigned int pad_top = pad_stride_info.pad_top();
296 const unsigned int pad_right = pad_stride_info.pad_right();
297 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
298 const unsigned int stride_x = pad_stride_info.stride().first;
299 const unsigned int stride_y = pad_stride_info.stride().second;
300 unsigned int w = 0;
301 unsigned int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100302 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 {
304 case DimensionRoundingType::FLOOR:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100305 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
306 h = static_cast<unsigned int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307 break;
308 case DimensionRoundingType::CEIL:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100309 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
310 h = static_cast<unsigned int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311 break;
312 default:
313 ARM_COMPUTE_ERROR("Unsupported rounding type");
314 }
315
316 // Make sure that border operations will start from inside the input and not the padded area
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100317 if(((w - 1) * stride_x) >= (width + pad_left))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318 {
319 --w;
320 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100321 if(((h - 1) * stride_y) >= (height + pad_top))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 {
323 --h;
324 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100325 ARM_COMPUTE_ERROR_ON(((w - 1) * stride_x) >= (width + pad_left));
326 ARM_COMPUTE_ERROR_ON(((h - 1) * stride_y) >= (height + pad_top));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327
328 return std::make_pair(w, h);
329}
330
331void 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)
332{
333 switch(dt)
334 {
335 case DataType::U8:
336 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
337 break;
338 case DataType::QS8:
339 case DataType::S8:
340 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
341 break;
342 case DataType::U16:
343 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
344 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100345 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100346 case DataType::S16:
347 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
348 break;
349 case DataType::U32:
350 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
351 break;
352 case DataType::S32:
353 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
354 break;
355 case DataType::F32:
356 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
357 break;
358 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100359 print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360 break;
361 default:
362 ARM_COMPUTE_ERROR("Undefined element size for given data type");
363 }
364}
365
366int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
367{
368 switch(dt)
369 {
370 case DataType::U8:
371 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
372 case DataType::QS8:
373 case DataType::S8:
374 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
375 case DataType::U16:
376 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100377 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378 case DataType::S16:
379 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
380 case DataType::U32:
381 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
382 case DataType::S32:
383 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
384 case DataType::F32:
385 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
386 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100387 return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388 default:
389 ARM_COMPUTE_ERROR("Undefined element size for given data type");
390 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000391 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392}
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +0000393
394int arm_compute::round(float x, RoundingPolicy rounding_policy)
395{
396 using namespace std;
397 int rounded = 0;
398 switch(rounding_policy)
399 {
400 case RoundingPolicy::TO_ZERO:
401 {
402 rounded = static_cast<int>(x);
403 break;
404 }
405 case RoundingPolicy::TO_NEAREST_UP:
406 {
407 rounded = static_cast<int>(support::cpp11::round(x));
408 break;
409 }
410 case RoundingPolicy::TO_NEAREST_EVEN:
411 {
412 ARM_COMPUTE_ERROR("TO_NEAREST_EVEN rounding policy is not supported.");
413 break;
414 }
415 default:
416 {
417 ARM_COMPUTE_ERROR("Unsupported rounding policy.");
418 break;
419 }
420 }
421
422 return rounded;
423}