blob: bd6911fd2b30bc5d4afb420c9df634964a5b76a7 [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 */
24#include "arm_compute/core/Utils.h"
25
26#include "arm_compute/core/FixedPoint.h"
27
28#include <algorithm>
29#include <cmath>
30#include <cstdint>
31#include <fstream>
32#include <map>
33#include <string>
34
35using namespace arm_compute;
36
37std::string arm_compute::build_information()
38{
39 static const std::string information =
40#include "arm_compute_version.embed"
41 ;
42 return information;
43}
44
45std::string arm_compute::read_file(const std::string &filename, bool binary)
46{
47 std::string out;
48 std::ifstream fs;
49
50 try
51 {
52 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
53 std::ios_base::openmode mode = std::ios::in;
54
55 if(binary)
56 {
57 mode |= std::ios::binary;
58 }
59
60 fs.open(filename, mode);
61
62 // Go to the end of the file
63 fs.seekg(0, std::ios::end);
64 // Reserve the memory required to store the file's content
65 out.reserve(fs.tellg());
66 // Go back to the beginning of the file
67 fs.seekg(0, std::ios::beg);
68 // Copy the content of the file
69 out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
70 }
71 catch(const std::ifstream::failure &e)
72 {
73 ARM_COMPUTE_ERROR("Accessing %s: %s", filename.c_str(), e.what());
74 }
75
76 return out;
77}
78
79const std::string &arm_compute::string_from_format(Format format)
80{
81 static std::map<Format, const std::string> formats_map =
82 {
83 { Format::UNKNOWN, "UNKNOWN" },
84 { Format::U8, "U8" },
85 { Format::S16, "S16" },
86 { Format::U16, "U16" },
87 { Format::S32, "S32" },
88 { Format::U32, "U32" },
89 { Format::F16, "F16" },
90 { Format::F32, "F32" },
91 { Format::UV88, "UV88" },
92 { Format::RGB888, "RGB888" },
93 { Format::RGBA8888, "RGBA8888" },
94 { Format::YUV444, "YUV444" },
95 { Format::YUYV422, "YUYV422" },
96 { Format::NV12, "NV12" },
97 { Format::NV21, "NV21" },
98 { Format::IYUV, "IYUV" },
99 { Format::UYVY422, "UYVY422" }
100 };
101
102 return formats_map[format];
103}
104
105const std::string &arm_compute::string_from_channel(Channel channel)
106{
107 static std::map<Channel, const std::string> channels_map =
108 {
109 { Channel::UNKNOWN, "UNKNOWN" },
110 { Channel::R, "R" },
111 { Channel::G, "G" },
112 { Channel::B, "B" },
113 { Channel::A, "A" },
114 { Channel::Y, "Y" },
115 { Channel::U, "U" },
116 { Channel::V, "V" },
117 { Channel::C0, "C0" },
118 { Channel::C1, "C1" },
119 { Channel::C2, "C2" },
120 { Channel::C3, "C3" }
121 };
122
123 return channels_map[channel];
124}
125
126const std::string &arm_compute::string_from_data_type(DataType dt)
127{
128 static std::map<DataType, const std::string> dt_map =
129 {
130 { DataType::UNKNOWN, "UNKNOWN" },
131 { DataType::S8, "S8" },
132 { DataType::U8, "U8" },
133 { DataType::QS8, "QS8" },
134 { DataType::S16, "S16" },
135 { DataType::U16, "U16" },
136 { DataType::QS16, "QS16" },
137 { DataType::S32, "S32" },
138 { DataType::U32, "U32" },
139 { DataType::S64, "S64" },
140 { DataType::U64, "U64" },
141 { DataType::F16, "F16" },
142 { DataType::F32, "F32" },
143 { DataType::F64, "F64" },
144 { DataType::SIZET, "SIZET" },
145 };
146
147 return dt_map[dt];
148}
149
150const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
151{
152 static std::map<ActivationLayerInfo::ActivationFunction, const std::string> act_map =
153 {
154 { ActivationLayerInfo::ActivationFunction::ABS, "ABS" },
155 { ActivationLayerInfo::ActivationFunction::LINEAR, "LINEAR" },
156 { ActivationLayerInfo::ActivationFunction::LOGISTIC, "LOGISTIC" },
157 { ActivationLayerInfo::ActivationFunction::RELU, "RELU" },
158 { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, "BRELU" },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100159 { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, "LU_BRELU" },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100160 { ActivationLayerInfo::ActivationFunction::LEAKY_RELU, "LRELU" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
162 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
163 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
164 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
165 };
166
167 return act_map[act];
168}
169
170const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
171{
172 static std::map<MatrixPattern, const std::string> pattern_map =
173 {
174 { MatrixPattern::BOX, "BOX" },
175 { MatrixPattern::CROSS, "CROSS" },
176 { MatrixPattern::DISK, "DISK" },
177 { MatrixPattern::OTHER, "OTHER" },
178 };
179
180 return pattern_map[pattern];
181}
182
183const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
184{
185 static std::map<NonLinearFilterFunction, const std::string> func_map =
186 {
187 { NonLinearFilterFunction::MAX, "MAX" },
188 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
189 { NonLinearFilterFunction::MIN, "MIN" },
190 };
191
192 return func_map[function];
193}
194
195const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
196{
197 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
198 {
199 { InterpolationPolicy::AREA, "AREA" },
200 { InterpolationPolicy::BILINEAR, "BILINEAR" },
201 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
202 };
203
204 return interpolation_policy_map[policy];
205}
206
207const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
208{
209 static std::map<BorderMode, const std::string> border_mode_map =
210 {
211 { BorderMode::UNDEFINED, "UNDEFINED" },
212 { BorderMode::CONSTANT, "CONSTANT" },
213 { BorderMode::REPLICATE, "REPLICATE" },
214 };
215
216 return border_mode_map[border_mode];
217}
218
219const std::string &arm_compute::string_from_norm_type(NormType type)
220{
221 static std::map<NormType, const std::string> norm_type_map =
222 {
223 { NormType::IN_MAP_1D, "IN_MAP_1D" },
224 { NormType::IN_MAP_2D, "IN_MAP_2D" },
225 { NormType::CROSS_MAP, "CROSS_MAP" },
226 };
227
228 return norm_type_map[type];
229}
230
Georgios Pinitascdf51452017-08-31 14:21:36 +0100231const std::string &arm_compute::string_from_pooling_type(PoolingType type)
232{
233 static std::map<PoolingType, const std::string> pool_type_map =
234 {
235 { PoolingType::MAX, "MAX" },
236 { PoolingType::AVG, "AVG" },
237 { PoolingType::L2, "L2" },
238 };
239
240 return pool_type_map[type];
241}
242
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243std::string arm_compute::lower_string(const std::string &val)
244{
245 std::string res = val;
246 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
247 return res;
248}
249
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100250TensorShape arm_compute::deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, TensorShape input, TensorShape weights)
251{
252 TensorShape out_shape(input);
253 out_shape.set(0, out_dims.first);
254 out_shape.set(1, out_dims.second);
255 out_shape.set(2, weights[3]);
256 return out_shape;
257}
258
259const std::pair<unsigned int, unsigned int> arm_compute::deconvolution_output_dimensions(
260 unsigned int in_width, unsigned int in_height, unsigned int kernel_width, unsigned int kernel_height, unsigned int padx, unsigned int pady,
261 unsigned int ax, unsigned int ay, float upscalex, float upscaley, DimensionRoundingType round)
262{
263 ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1);
264 ARM_COMPUTE_ERROR_ON(((in_width - 1) * upscalex + kernel_width + ax) < 2.f * padx);
265 ARM_COMPUTE_ERROR_ON(((in_height - 1) * upscaley + kernel_height + ay) < 2.f * pady);
266 const float fw = (in_width - 1) * upscalex - 2.f * padx + kernel_width + ax;
267 const float fh = (in_height - 1) * upscaley - 2.f * pady + kernel_height + ay;
268 int w = 0;
269 int h = 0;
270 switch(round)
271 {
272 case DimensionRoundingType::FLOOR:
273 w = std::floor(fw);
274 h = std::floor(fh);
275 break;
276 case DimensionRoundingType::CEIL:
277 w = std::ceil(fw);
278 h = std::ceil(fh);
279 break;
280 default:
281 ARM_COMPUTE_ERROR("Not supported");
282 break;
283 }
284 return std::make_pair<unsigned int, unsigned int>(w, h);
285}
286
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100287const std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
288 unsigned int kernel_width, unsigned int kernel_height,
289 const PadStrideInfo &pad_stride_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290{
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100291 const unsigned int pad_left = pad_stride_info.pad_left();
292 const unsigned int pad_top = pad_stride_info.pad_top();
293 const unsigned int pad_right = pad_stride_info.pad_right();
294 const unsigned int pad_bottom = pad_stride_info.pad_bottom();
295 const unsigned int stride_x = pad_stride_info.stride().first;
296 const unsigned int stride_y = pad_stride_info.stride().second;
297 unsigned int w = 0;
298 unsigned int h = 0;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100299 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300 {
301 case DimensionRoundingType::FLOOR:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100302 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
303 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 +0100304 break;
305 case DimensionRoundingType::CEIL:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100306 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1));
307 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 +0100308 break;
309 default:
310 ARM_COMPUTE_ERROR("Unsupported rounding type");
311 }
312
313 // Make sure that border operations will start from inside the input and not the padded area
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100314 if(((w - 1) * stride_x) >= (width + pad_left))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315 {
316 --w;
317 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100318 if(((h - 1) * stride_y) >= (height + pad_top))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 {
320 --h;
321 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100322 ARM_COMPUTE_ERROR_ON(((w - 1) * stride_x) >= (width + pad_left));
323 ARM_COMPUTE_ERROR_ON(((h - 1) * stride_y) >= (height + pad_top));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324
325 return std::make_pair(w, h);
326}
327
328void 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)
329{
330 switch(dt)
331 {
332 case DataType::U8:
333 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
334 break;
335 case DataType::QS8:
336 case DataType::S8:
337 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
338 break;
339 case DataType::U16:
340 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
341 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100342 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343 case DataType::S16:
344 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
345 break;
346 case DataType::U32:
347 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
348 break;
349 case DataType::S32:
350 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
351 break;
352 case DataType::F32:
353 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
354 break;
355 case DataType::F16:
356 break;
357 default:
358 ARM_COMPUTE_ERROR("Undefined element size for given data type");
359 }
360}
361
362int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
363{
364 switch(dt)
365 {
366 case DataType::U8:
367 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
368 case DataType::QS8:
369 case DataType::S8:
370 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
371 case DataType::U16:
372 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100373 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100374 case DataType::S16:
375 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
376 case DataType::U32:
377 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
378 case DataType::S32:
379 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
380 case DataType::F32:
381 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
382 case DataType::F16:
383 return 0;
384 default:
385 ARM_COMPUTE_ERROR("Undefined element size for given data type");
386 }
Georgios Pinitas3faea252017-10-30 14:13:50 +0000387 return 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388}