blob: a2146522f76858a8fccbfa4dba06e60b522485fe [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2016-2023 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_UTILS_H
25#define ARM_COMPUTE_UTILS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Error.h"
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010028#include "arm_compute/core/PixelValue.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Types.h"
30
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <numeric>
33#include <sstream>
34#include <string>
35#include <type_traits>
Giorgio Arena1e2af2a2020-10-15 17:39:41 +010036#include <unordered_map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <utility>
Matthew Bentham314d3e22023-06-23 10:53:52 +000038
39/* Convenience / backwards compatibility includes */
40#include "arm_compute/core/utils/ActivationFunctionUtils.h"
41#include "arm_compute/core/utils/DataLayoutUtils.h"
42#include "arm_compute/core/utils/DataTypeUtils.h"
43#include "arm_compute/core/utils/FormatUtils.h"
44#include "arm_compute/core/utils/InterpolationPolicyUtils.h"
45#include "arm_compute/core/utils/StringUtils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
47namespace arm_compute
48{
Giorgio Arena1e2af2a2020-10-15 17:39:41 +010049class ITensor;
Giorgio Arena4112eed2020-10-23 14:24:26 +010050class ITensorInfo;
Matthew Benthamf1aeab92023-05-30 13:35:34 +000051class ActivationLayerInfo;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +010052
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053/** Load an entire file in memory
54 *
55 * @param[in] filename Name of the file to read.
56 * @param[in] binary Is it a binary file ?
57 *
58 * @return The content of the file.
59 */
60std::string read_file(const std::string &filename, bool binary);
61
Pablo Tello35767bc2018-12-05 17:36:30 +000062/** Permutes the given dimensions according the permutation vector
63 *
64 * @param[in,out] dimensions Dimensions to be permuted.
65 * @param[in] perm Vector describing the permutation.
66 *
67 */
68template <typename T>
69inline void permute_strides(Dimensions<T> &dimensions, const PermutationVector &perm)
70{
71 const auto old_dim = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 for (unsigned int i = 0; i < perm.num_dimensions(); ++i)
Pablo Tello35767bc2018-12-05 17:36:30 +000073 {
74 T dimension_val = old_dim[i];
75 dimensions.set(perm[i], dimension_val);
76 }
77}
78
Georgios Pinitas4074c992018-01-30 18:13:46 +000079/** Calculate padding requirements in case of SAME padding
80 *
81 * @param[in] input_shape Input shape
82 * @param[in] weights_shape Weights shape
83 * @param[in] conv_info Convolution information (containing strides)
Isabella Gottardi6a914402019-01-30 15:45:42 +000084 * @param[in] data_layout (Optional) Data layout of the input and weights tensor
Pablo Tello01bbacb2019-04-30 10:32:42 +010085 * @param[in] dilation (Optional) Dilation factor used in the convolution.
Giorgio Arena17203582019-08-02 16:00:41 +010086 * @param[in] rounding_type (Optional) Dimension rounding type when down-scaling.
Georgios Pinitas4074c992018-01-30 18:13:46 +000087 *
88 * @return PadStrideInfo for SAME padding
89 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090PadStrideInfo calculate_same_pad(TensorShape input_shape,
91 TensorShape weights_shape,
92 PadStrideInfo conv_info,
93 DataLayout data_layout = DataLayout::NCHW,
94 const Size2D &dilation = Size2D(1u, 1u),
Giorgio Arena17203582019-08-02 16:00:41 +010095 const DimensionRoundingType &rounding_type = DimensionRoundingType::FLOOR);
Georgios Pinitas4074c992018-01-30 18:13:46 +000096
Pablo Tellof5f34bb2017-08-22 13:34:13 +010097/** Returns expected width and height of the deconvolution's output tensor.
98 *
Matthew Jacksonb9070a42019-08-22 16:13:27 +010099 * @param[in] in_width Width of input tensor (Number of columns)
100 * @param[in] in_height Height of input tensor (Number of rows)
101 * @param[in] kernel_width Kernel width.
102 * @param[in] kernel_height Kernel height.
103 * @param[in] pad_stride_info Pad and stride information.
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100104 *
105 * @return A pair with the new width in the first position and the new height in the second.
106 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width,
108 unsigned int in_height,
109 unsigned int kernel_width,
110 unsigned int kernel_height,
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100111 const PadStrideInfo &pad_stride_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113/** Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
114 *
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100115 * @param[in] width Width of input tensor (Number of columns)
116 * @param[in] height Height of input tensor (Number of rows)
117 * @param[in] kernel_width Kernel width.
118 * @param[in] kernel_height Kernel height.
119 * @param[in] pad_stride_info Pad and stride information.
Alex Gilday7da29b62018-03-23 14:16:00 +0000120 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 *
122 * @return A pair with the new width in the first position and the new height in the second.
123 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124std::pair<unsigned int, unsigned int> scaled_dimensions(int width,
125 int height,
126 int kernel_width,
127 int kernel_height,
Pablo Tello01bbacb2019-04-30 10:32:42 +0100128 const PadStrideInfo &pad_stride_info,
129 const Size2D &dilation = Size2D(1U, 1U));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100131/** Returns calculated width and height of output scaled tensor depending on dimensions rounding mode.
132 *
133 * @param[in] width Width of input tensor (Number of columns)
134 * @param[in] height Height of input tensor (Number of rows)
135 * @param[in] kernel_width Kernel width.
136 * @param[in] kernel_height Kernel height.
137 * @param[in] pad_stride_info Pad and stride information.
138 *
139 * @return A pair with the new width in the first position and the new height in the second, returned values can be < 1
140 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141std::pair<int, int> scaled_dimensions_signed(
142 int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100143
ramelg0137515692022-02-26 22:06:20 +0000144/** Returns calculated width, height and depth of output scaled tensor depending on dimensions rounding mode.
145 *
146 * @param[in] width Width of input tensor
147 * @param[in] height Height of input tensor
148 * @param[in] depth Depth of input tensor
149 * @param[in] kernel_width Kernel width.
150 * @param[in] kernel_height Kernel height.
151 * @param[in] kernel_depth Kernel depth.
152 * @param[in] pool3d_info Pad and stride and round information for 3d pooling
153 *
154 * @return A tuple with the new width in the first position, the new height in the second, and the new depth in the third.
155 * Returned values can be < 1
156 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157std::tuple<int, int, int> scaled_3d_dimensions_signed(int width,
158 int height,
159 int depth,
160 int kernel_width,
161 int kernel_height,
162 int kernel_depth,
ramelg0137515692022-02-26 22:06:20 +0000163 const Pooling3dLayerInfo &pool3d_info);
164
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100165/** Check if the given reduction operation should be handled in a serial way.
166 *
167 * @param[in] op Reduction operation to perform
168 * @param[in] dt Data type
169 * @param[in] axis Axis along which to reduce
170 *
171 * @return True if the given reduction operation should be handled in a serial way.
172 */
173bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis);
174
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000175/** Returns output quantization information for softmax layer
176 *
177 * @param[in] input_type The data type of the input tensor
178 * @param[in] is_log True for log softmax
179 *
180 * @return Quantization information for the output tensor
181 */
182QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log);
183
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000184/** Returns a pair of minimum and maximum values for a quantized activation
185 *
186 * @param[in] act_info The information for activation
187 * @param[in] data_type The used data type
188 * @param[in] oq_info The output quantization information
189 *
190 * @return The pair with minimum and maximum values
191 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192std::pair<int32_t, int32_t> get_quantized_activation_min_max(const ActivationLayerInfo &act_info,
193 DataType data_type,
194 UniformQuantizationInfo oq_info);
Sang-Hoon Park4715cf92020-01-08 16:02:47 +0000195
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196/** Convert a channel identity into a string.
197 *
198 * @param[in] channel @ref Channel to be translated to string.
199 *
200 * @return The string describing the channel.
201 */
202const std::string &string_from_channel(Channel channel);
Matthew Bentham314d3e22023-06-23 10:53:52 +0000203
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204/** Translates a given border mode policy to a string.
205 *
206 * @param[in] border_mode @ref BorderMode to be translated to string.
207 *
208 * @return The string describing the border mode.
209 */
210const std::string &string_from_border_mode(BorderMode border_mode);
211/** Translates a given normalization type to a string.
212 *
213 * @param[in] type @ref NormType to be translated to string.
214 *
215 * @return The string describing the normalization type.
216 */
217const std::string &string_from_norm_type(NormType type);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100218/** Translates a given pooling type to a string.
219 *
220 * @param[in] type @ref PoolingType to be translated to string.
221 *
222 * @return The string describing the pooling type.
223 */
224const std::string &string_from_pooling_type(PoolingType type);
SiCongLic4270cf2021-12-22 11:22:40 +0000225/** Check if the pool region is entirely outside the input tensor
226 *
227 * @param[in] info @ref PoolingLayerInfo to be checked.
228 *
229 * @return True if the pool region is entirely outside the input tensor, False otherwise.
230 */
231bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info);
ramelg0137515692022-02-26 22:06:20 +0000232/** Check if the 3d pool region is entirely outside the input tensor
233 *
234 * @param[in] info @ref Pooling3dLayerInfo to be checked.
235 *
236 * @return True if the pool region is entirely outside the input tensor, False otherwise.
237 */
238bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info);
239/** Check if the 3D padding is symmetric i.e. padding in each opposite sides are euqal (left=right, top=bottom and front=back)
240 *
241 * @param[in] info @ref Padding3D input 3D padding object to check if it is symmetric
242 *
243 * @return True if padding is symmetric
244 */
Pablo Marquez Tello205ba242023-07-12 14:29:58 +0100245inline bool is_symmetric(const Padding3D &info)
ramelg0137515692022-02-26 22:06:20 +0000246{
247 return ((info.left == info.right) && (info.top == info.bottom) && (info.front == info.back));
248}
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100249/** Translates a given GEMMLowp output stage to a string.
250 *
251 * @param[in] output_stage @ref GEMMLowpOutputStageInfo to be translated to string.
252 *
253 * @return The string describing the GEMMLowp output stage
254 */
255const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100256/** Convert a PixelValue to a string, represented through the specific data type
257 *
258 * @param[in] value The PixelValue to convert
259 * @param[in] data_type The type to be used to convert the @p value
260 *
261 * @return String representation of the PixelValue through the given data type.
262 */
263std::string string_from_pixel_value(const PixelValue &value, const DataType data_type);
Matthew Bentham314d3e22023-06-23 10:53:52 +0000264
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100265/** Stores padding information before configuring a kernel
266 *
Giorgio Arena4112eed2020-10-23 14:24:26 +0100267 * @param[in] infos list of tensor infos to store the padding info for
268 *
269 * @return An unordered map where each tensor info pointer is paired with its original padding info
270 */
271std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos);
272/** Stores padding information before configuring a kernel
273 *
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100274 * @param[in] tensors list of tensors to store the padding info for
275 *
Giorgio Arena4112eed2020-10-23 14:24:26 +0100276 * @return An unordered map where each tensor info pointer is paired with its original padding info
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100277 */
Giorgio Arena4112eed2020-10-23 14:24:26 +0100278std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensor *> tensors);
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100279/** Check if the previously stored padding info has changed after configuring a kernel
280 *
Giorgio Arena4112eed2020-10-23 14:24:26 +0100281 * @param[in] padding_map an unordered map where each tensor info pointer is paired with its original padding info
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100282 *
Giorgio Arena4112eed2020-10-23 14:24:26 +0100283 * @return true if any of the tensor infos has changed its paddings
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100284 */
Giorgio Arena4112eed2020-10-23 14:24:26 +0100285bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map);
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100286
Michalis Spyrouf63885b2019-01-16 14:18:09 +0000287/** Returns the number of elements required to go from start to end with the wanted step
288 *
289 * @param[in] start start value
290 * @param[in] end end value
291 * @param[in] step step value between each number in the wanted sequence
292 *
293 * @return number of elements to go from start value to end value using the wanted step
294 */
295inline size_t num_of_elements_in_range(const float start, const float end, const float step)
296{
297 ARM_COMPUTE_ERROR_ON_MSG(step == 0, "Range Step cannot be 0");
298 return size_t(std::ceil((end - start) / step));
299}
300
giuros01edc21e42018-11-16 14:45:31 +0000301#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302/** Print consecutive elements to an output stream.
303 *
304 * @param[out] s Output stream to print the elements to.
305 * @param[in] ptr Pointer to print the elements from.
306 * @param[in] n Number of elements to print.
307 * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0.
308 * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter
309 */
310template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100311void print_consecutive_elements_impl(
312 std::ostream &s, const T *ptr, unsigned int n, int stream_width = 0, const std::string &element_delim = " ")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100313{
314 using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type;
Michalis Spyrou53860dd2019-07-01 14:20:56 +0100315 std::ios stream_status(nullptr);
316 stream_status.copyfmt(s);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100318 for (unsigned int i = 0; i < n; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 {
320 // Set stream width as it is not a "sticky" stream manipulator
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100321 if (stream_width != 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 {
323 s.width(stream_width);
324 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100325
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100326 if (std::is_same<typename std::decay<T>::type, half>::value)
Anthony Barbier7068f992017-10-26 15:23:08 +0100327 {
328 // We use T instead of print_type here is because the std::is_floating_point<half> returns false and then the print_type becomes int.
329 s << std::right << static_cast<T>(ptr[i]) << element_delim;
330 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100331 else if (std::is_same<typename std::decay<T>::type, bfloat16>::value)
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000332 {
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000333 // We use T instead of print_type here is because the std::is_floating_point<bfloat16> returns false and then the print_type becomes int.
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000334 s << std::right << float(ptr[i]) << element_delim;
335 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100336 else
337 {
338 s << std::right << static_cast<print_type>(ptr[i]) << element_delim;
339 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340 }
Michalis Spyrou53860dd2019-07-01 14:20:56 +0100341
342 // Restore output stream flags
343 s.copyfmt(stream_status);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344}
345
346/** Identify the maximum width of n consecutive elements.
347 *
348 * @param[in] s The output stream which will be used to print the elements. Used to extract the stream format.
349 * @param[in] ptr Pointer to the elements.
350 * @param[in] n Number of elements.
351 *
352 * @return The maximum width of the elements.
353 */
354template <typename T>
355int max_consecutive_elements_display_width_impl(std::ostream &s, const T *ptr, unsigned int n)
356{
357 using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type;
358
359 int max_width = -1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360 for (unsigned int i = 0; i < n; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361 {
362 std::stringstream ss;
363 ss.copyfmt(s);
Anthony Barbier7068f992017-10-26 15:23:08 +0100364
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100365 if (std::is_same<typename std::decay<T>::type, half>::value)
Anthony Barbier7068f992017-10-26 15:23:08 +0100366 {
367 // We use T instead of print_type here is because the std::is_floating_point<half> returns false and then the print_type becomes int.
368 ss << static_cast<T>(ptr[i]);
369 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100370 else if (std::is_same<typename std::decay<T>::type, bfloat16>::value)
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000371 {
372 // We use T instead of print_type here is because the std::is_floating_point<bfloat> returns false and then the print_type becomes int.
373 ss << float(ptr[i]);
374 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100375 else
376 {
377 ss << static_cast<print_type>(ptr[i]);
378 }
379
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380 max_width = std::max<int>(max_width, ss.str().size());
381 }
382 return max_width;
383}
384
385/** Print consecutive elements to an output stream.
386 *
387 * @param[out] s Output stream to print the elements to.
388 * @param[in] dt Data type of the elements
389 * @param[in] ptr Pointer to print the elements from.
390 * @param[in] n Number of elements to print.
391 * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0.
392 * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter
393 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100394void print_consecutive_elements(std::ostream &s,
395 DataType dt,
396 const uint8_t *ptr,
397 unsigned int n,
398 int stream_width,
399 const std::string &element_delim = " ");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400
401/** Identify the maximum width of n consecutive elements.
402 *
403 * @param[in] s Output stream to print the elements to.
404 * @param[in] dt Data type of the elements
405 * @param[in] ptr Pointer to print the elements from.
406 * @param[in] n Number of elements to print.
407 *
408 * @return The maximum width of the elements.
409 */
410int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n);
giuros01edc21e42018-11-16 14:45:31 +0000411#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100412} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000413#endif /*ARM_COMPUTE_UTILS_H */