blob: f6230c01998237268846a60f6a57c727c1bfe536 [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" },
159 { ActivationLayerInfo::ActivationFunction::SOFT_RELU, "SRELU" },
160 { ActivationLayerInfo::ActivationFunction::SQRT, "SQRT" },
161 { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" },
162 { ActivationLayerInfo::ActivationFunction::TANH, "TANH" },
163 };
164
165 return act_map[act];
166}
167
168const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern)
169{
170 static std::map<MatrixPattern, const std::string> pattern_map =
171 {
172 { MatrixPattern::BOX, "BOX" },
173 { MatrixPattern::CROSS, "CROSS" },
174 { MatrixPattern::DISK, "DISK" },
175 { MatrixPattern::OTHER, "OTHER" },
176 };
177
178 return pattern_map[pattern];
179}
180
181const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function)
182{
183 static std::map<NonLinearFilterFunction, const std::string> func_map =
184 {
185 { NonLinearFilterFunction::MAX, "MAX" },
186 { NonLinearFilterFunction::MEDIAN, "MEDIAN" },
187 { NonLinearFilterFunction::MIN, "MIN" },
188 };
189
190 return func_map[function];
191}
192
193const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy)
194{
195 static std::map<InterpolationPolicy, const std::string> interpolation_policy_map =
196 {
197 { InterpolationPolicy::AREA, "AREA" },
198 { InterpolationPolicy::BILINEAR, "BILINEAR" },
199 { InterpolationPolicy::NEAREST_NEIGHBOR, "NEAREST_NEIGHBOUR" },
200 };
201
202 return interpolation_policy_map[policy];
203}
204
205const std::string &arm_compute::string_from_border_mode(BorderMode border_mode)
206{
207 static std::map<BorderMode, const std::string> border_mode_map =
208 {
209 { BorderMode::UNDEFINED, "UNDEFINED" },
210 { BorderMode::CONSTANT, "CONSTANT" },
211 { BorderMode::REPLICATE, "REPLICATE" },
212 };
213
214 return border_mode_map[border_mode];
215}
216
217const std::string &arm_compute::string_from_norm_type(NormType type)
218{
219 static std::map<NormType, const std::string> norm_type_map =
220 {
221 { NormType::IN_MAP_1D, "IN_MAP_1D" },
222 { NormType::IN_MAP_2D, "IN_MAP_2D" },
223 { NormType::CROSS_MAP, "CROSS_MAP" },
224 };
225
226 return norm_type_map[type];
227}
228
229std::string arm_compute::lower_string(const std::string &val)
230{
231 std::string res = val;
232 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
233 return res;
234}
235
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100236const std::pair<unsigned int, unsigned int> arm_compute::scaled_dimensions(unsigned int width, unsigned int height,
237 unsigned int kernel_width, unsigned int kernel_height,
238 const PadStrideInfo &pad_stride_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239{
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100240 const unsigned int pad_x = pad_stride_info.pad().first;
241 const unsigned int pad_y = pad_stride_info.pad().second;
242 const unsigned int stride_x = pad_stride_info.stride().first;
243 const unsigned int stride_y = pad_stride_info.stride().second;
244 unsigned int w = 0;
245 unsigned int h = 0;
246 switch(pad_stride_info.round())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247 {
248 case DimensionRoundingType::FLOOR:
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100249 w = static_cast<unsigned int>(std::floor((static_cast<float>(width + 2 * pad_x - kernel_width) / stride_x) + 1));
250 h = static_cast<unsigned int>(std::floor((static_cast<float>(height + 2 * pad_y - kernel_height) / stride_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251 break;
252 case DimensionRoundingType::CEIL:
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100253 w = static_cast<unsigned int>(std::ceil((static_cast<float>(width + 2 * pad_x - kernel_width) / stride_x) + 1));
254 h = static_cast<unsigned int>(std::ceil((static_cast<float>(height + 2 * pad_y - kernel_height) / stride_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 break;
256 default:
257 ARM_COMPUTE_ERROR("Unsupported rounding type");
258 }
259
260 // Make sure that border operations will start from inside the input and not the padded area
261 if(((w - 1) * stride_x) >= (width + pad_x))
262 {
263 --w;
264 }
265 if(((h - 1) * stride_y) >= (height + pad_y))
266 {
267 --h;
268 }
269 ARM_COMPUTE_ERROR_ON(((w - 1) * stride_x) >= (width + pad_x));
270 ARM_COMPUTE_ERROR_ON(((h - 1) * stride_y) >= (height + pad_y));
271
272 return std::make_pair(w, h);
273}
274
275void 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)
276{
277 switch(dt)
278 {
279 case DataType::U8:
280 print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim);
281 break;
282 case DataType::QS8:
283 case DataType::S8:
284 print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim);
285 break;
286 case DataType::U16:
287 print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim);
288 break;
289 case DataType::S16:
290 print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim);
291 break;
292 case DataType::U32:
293 print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim);
294 break;
295 case DataType::S32:
296 print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim);
297 break;
298 case DataType::F32:
299 print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim);
300 break;
301 case DataType::F16:
302 break;
303 default:
304 ARM_COMPUTE_ERROR("Undefined element size for given data type");
305 }
306}
307
308int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n)
309{
310 switch(dt)
311 {
312 case DataType::U8:
313 return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n);
314 case DataType::QS8:
315 case DataType::S8:
316 return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n);
317 case DataType::U16:
318 return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n);
319 case DataType::S16:
320 return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n);
321 case DataType::U32:
322 return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n);
323 case DataType::S32:
324 return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n);
325 case DataType::F32:
326 return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n);
327 case DataType::F16:
328 return 0;
329 default:
330 ARM_COMPUTE_ERROR("Undefined element size for given data type");
331 }
332}