Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_UTILS_H |
| 25 | #define ARM_COMPUTE_UTILS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Error.h" |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/PixelValue.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
| 30 | |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include <cmath> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include <numeric> |
| 33 | #include <sstream> |
| 34 | #include <string> |
| 35 | #include <type_traits> |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 36 | #include <unordered_map> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include <utility> |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 38 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
| 47 | namespace arm_compute |
| 48 | { |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 49 | class ITensor; |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 50 | class ITensorInfo; |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 51 | class ActivationLayerInfo; |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 52 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | /** 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 | */ |
| 60 | std::string read_file(const std::string &filename, bool binary); |
| 61 | |
Pablo Tello | 35767bc | 2018-12-05 17:36:30 +0000 | [diff] [blame] | 62 | /** 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 | */ |
| 68 | template <typename T> |
| 69 | inline 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | for (unsigned int i = 0; i < perm.num_dimensions(); ++i) |
Pablo Tello | 35767bc | 2018-12-05 17:36:30 +0000 | [diff] [blame] | 73 | { |
| 74 | T dimension_val = old_dim[i]; |
| 75 | dimensions.set(perm[i], dimension_val); |
| 76 | } |
| 77 | } |
| 78 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 79 | /** 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 Gottardi | 6a91440 | 2019-01-30 15:45:42 +0000 | [diff] [blame] | 84 | * @param[in] data_layout (Optional) Data layout of the input and weights tensor |
Pablo Tello | 01bbacb | 2019-04-30 10:32:42 +0100 | [diff] [blame] | 85 | * @param[in] dilation (Optional) Dilation factor used in the convolution. |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 86 | * @param[in] rounding_type (Optional) Dimension rounding type when down-scaling. |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 87 | * |
| 88 | * @return PadStrideInfo for SAME padding |
| 89 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | PadStrideInfo 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 Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 95 | const DimensionRoundingType &rounding_type = DimensionRoundingType::FLOOR); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 96 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 97 | /** Returns expected width and height of the deconvolution's output tensor. |
| 98 | * |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 99 | * @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 Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 104 | * |
| 105 | * @return A pair with the new width in the first position and the new height in the second. |
| 106 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | std::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 Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 111 | const PadStrideInfo &pad_stride_info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 112 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | /** Returns expected width and height of output scaled tensor depending on dimensions rounding mode. |
| 114 | * |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 115 | * @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 Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 120 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | * |
| 122 | * @return A pair with the new width in the first position and the new height in the second. |
| 123 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 124 | std::pair<unsigned int, unsigned int> scaled_dimensions(int width, |
| 125 | int height, |
| 126 | int kernel_width, |
| 127 | int kernel_height, |
Pablo Tello | 01bbacb | 2019-04-30 10:32:42 +0100 | [diff] [blame] | 128 | const PadStrideInfo &pad_stride_info, |
| 129 | const Size2D &dilation = Size2D(1U, 1U)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | |
Freddie Liardet | afcbb8f | 2021-05-04 12:41:16 +0100 | [diff] [blame] | 131 | /** 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | std::pair<int, int> scaled_dimensions_signed( |
| 142 | int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info); |
Freddie Liardet | afcbb8f | 2021-05-04 12:41:16 +0100 | [diff] [blame] | 143 | |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 144 | /** 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 157 | std::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, |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 163 | const Pooling3dLayerInfo &pool3d_info); |
| 164 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 165 | /** 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 | */ |
| 173 | bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis); |
| 174 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 175 | /** 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 | */ |
| 182 | QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log); |
| 183 | |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 184 | /** 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 192 | std::pair<int32_t, int32_t> get_quantized_activation_min_max(const ActivationLayerInfo &act_info, |
| 193 | DataType data_type, |
| 194 | UniformQuantizationInfo oq_info); |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 195 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | /** 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 | */ |
| 202 | const std::string &string_from_channel(Channel channel); |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 203 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | /** 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 | */ |
| 210 | const 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 | */ |
| 217 | const std::string &string_from_norm_type(NormType type); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 218 | /** 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 | */ |
| 224 | const std::string &string_from_pooling_type(PoolingType type); |
SiCongLi | c4270cf | 2021-12-22 11:22:40 +0000 | [diff] [blame] | 225 | /** 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 | */ |
| 231 | bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info); |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 232 | /** 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 | */ |
| 238 | bool 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 Tello | 205ba24 | 2023-07-12 14:29:58 +0100 | [diff] [blame] | 245 | inline bool is_symmetric(const Padding3D &info) |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 246 | { |
| 247 | return ((info.left == info.right) && (info.top == info.bottom) && (info.front == info.back)); |
| 248 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 249 | /** 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 | */ |
| 255 | const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 256 | /** 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 | */ |
| 263 | std::string string_from_pixel_value(const PixelValue &value, const DataType data_type); |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 264 | |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 265 | /** Stores padding information before configuring a kernel |
| 266 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 267 | * @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 | */ |
| 271 | std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos); |
| 272 | /** Stores padding information before configuring a kernel |
| 273 | * |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 274 | * @param[in] tensors list of tensors to store the padding info for |
| 275 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 276 | * @return An unordered map where each tensor info pointer is paired with its original padding info |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 277 | */ |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 278 | std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensor *> tensors); |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 279 | /** Check if the previously stored padding info has changed after configuring a kernel |
| 280 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 281 | * @param[in] padding_map an unordered map where each tensor info pointer is paired with its original padding info |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 282 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 283 | * @return true if any of the tensor infos has changed its paddings |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 284 | */ |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 285 | bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map); |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 286 | |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 287 | /** 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 | */ |
| 295 | inline 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 | |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 301 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 302 | /** 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 | */ |
| 310 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 311 | void print_consecutive_elements_impl( |
| 312 | std::ostream &s, const T *ptr, unsigned int n, int stream_width = 0, const std::string &element_delim = " ") |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 313 | { |
| 314 | using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type; |
Michalis Spyrou | 53860dd | 2019-07-01 14:20:56 +0100 | [diff] [blame] | 315 | std::ios stream_status(nullptr); |
| 316 | stream_status.copyfmt(s); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 317 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 318 | for (unsigned int i = 0; i < n; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 319 | { |
| 320 | // Set stream width as it is not a "sticky" stream manipulator |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 321 | if (stream_width != 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 322 | { |
| 323 | s.width(stream_width); |
| 324 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 325 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 326 | if (std::is_same<typename std::decay<T>::type, half>::value) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 327 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 331 | else if (std::is_same<typename std::decay<T>::type, bfloat16>::value) |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 332 | { |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 333 | // 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 Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 334 | s << std::right << float(ptr[i]) << element_delim; |
| 335 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 336 | else |
| 337 | { |
| 338 | s << std::right << static_cast<print_type>(ptr[i]) << element_delim; |
| 339 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 340 | } |
Michalis Spyrou | 53860dd | 2019-07-01 14:20:56 +0100 | [diff] [blame] | 341 | |
| 342 | // Restore output stream flags |
| 343 | s.copyfmt(stream_status); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 344 | } |
| 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 | */ |
| 354 | template <typename T> |
| 355 | int 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 360 | for (unsigned int i = 0; i < n; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 361 | { |
| 362 | std::stringstream ss; |
| 363 | ss.copyfmt(s); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 364 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 365 | if (std::is_same<typename std::decay<T>::type, half>::value) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 366 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 370 | else if (std::is_same<typename std::decay<T>::type, bfloat16>::value) |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 371 | { |
| 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 375 | else |
| 376 | { |
| 377 | ss << static_cast<print_type>(ptr[i]); |
| 378 | } |
| 379 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 380 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 394 | void 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 400 | |
| 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 | */ |
| 410 | int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n); |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 411 | #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 412 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 413 | #endif /*ARM_COMPUTE_UTILS_H */ |