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()); |
| 72 | for(unsigned int i = 0; i < perm.num_dimensions(); ++i) |
| 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 | */ |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 90 | PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout = DataLayout::NCHW, const Size2D &dilation = Size2D(1u, 1u), |
| 91 | const DimensionRoundingType &rounding_type = DimensionRoundingType::FLOOR); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 92 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 93 | /** Returns expected width and height of the deconvolution's output tensor. |
| 94 | * |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 95 | * @param[in] in_width Width of input tensor (Number of columns) |
| 96 | * @param[in] in_height Height of input tensor (Number of rows) |
| 97 | * @param[in] kernel_width Kernel width. |
| 98 | * @param[in] kernel_height Kernel height. |
| 99 | * @param[in] pad_stride_info Pad and stride information. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 100 | * |
| 101 | * @return A pair with the new width in the first position and the new height in the second. |
| 102 | */ |
Pablo Tello | 01bbacb | 2019-04-30 10:32:42 +0100 | [diff] [blame] | 103 | std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, |
| 104 | unsigned int kernel_width, unsigned int kernel_height, |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 105 | const PadStrideInfo &pad_stride_info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 106 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | /** Returns expected width and height of output scaled tensor depending on dimensions rounding mode. |
| 108 | * |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 109 | * @param[in] width Width of input tensor (Number of columns) |
| 110 | * @param[in] height Height of input tensor (Number of rows) |
| 111 | * @param[in] kernel_width Kernel width. |
| 112 | * @param[in] kernel_height Kernel height. |
| 113 | * @param[in] pad_stride_info Pad and stride information. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 114 | * @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] | 115 | * |
| 116 | * @return A pair with the new width in the first position and the new height in the second. |
| 117 | */ |
Georgios Pinitas | 80838f1 | 2019-12-12 18:23:13 +0000 | [diff] [blame] | 118 | std::pair<unsigned int, unsigned int> scaled_dimensions(int width, int height, |
| 119 | int kernel_width, int kernel_height, |
Pablo Tello | 01bbacb | 2019-04-30 10:32:42 +0100 | [diff] [blame] | 120 | const PadStrideInfo &pad_stride_info, |
| 121 | const Size2D &dilation = Size2D(1U, 1U)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
Freddie Liardet | afcbb8f | 2021-05-04 12:41:16 +0100 | [diff] [blame] | 123 | /** Returns calculated width and height of output scaled tensor depending on dimensions rounding mode. |
| 124 | * |
| 125 | * @param[in] width Width of input tensor (Number of columns) |
| 126 | * @param[in] height Height of input tensor (Number of rows) |
| 127 | * @param[in] kernel_width Kernel width. |
| 128 | * @param[in] kernel_height Kernel height. |
| 129 | * @param[in] pad_stride_info Pad and stride information. |
| 130 | * |
| 131 | * @return A pair with the new width in the first position and the new height in the second, returned values can be < 1 |
| 132 | */ |
| 133 | std::pair<int, int> scaled_dimensions_signed(int width, int height, |
| 134 | int kernel_width, int kernel_height, |
| 135 | const PadStrideInfo &pad_stride_info); |
| 136 | |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 137 | /** Returns calculated width, height and depth of output scaled tensor depending on dimensions rounding mode. |
| 138 | * |
| 139 | * @param[in] width Width of input tensor |
| 140 | * @param[in] height Height of input tensor |
| 141 | * @param[in] depth Depth of input tensor |
| 142 | * @param[in] kernel_width Kernel width. |
| 143 | * @param[in] kernel_height Kernel height. |
| 144 | * @param[in] kernel_depth Kernel depth. |
| 145 | * @param[in] pool3d_info Pad and stride and round information for 3d pooling |
| 146 | * |
| 147 | * @return A tuple with the new width in the first position, the new height in the second, and the new depth in the third. |
| 148 | * Returned values can be < 1 |
| 149 | */ |
| 150 | std::tuple<int, int, int> scaled_3d_dimensions_signed(int width, int height, int depth, |
| 151 | int kernel_width, int kernel_height, int kernel_depth, |
| 152 | const Pooling3dLayerInfo &pool3d_info); |
| 153 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 154 | /** Check if the given reduction operation should be handled in a serial way. |
| 155 | * |
| 156 | * @param[in] op Reduction operation to perform |
| 157 | * @param[in] dt Data type |
| 158 | * @param[in] axis Axis along which to reduce |
| 159 | * |
| 160 | * @return True if the given reduction operation should be handled in a serial way. |
| 161 | */ |
| 162 | bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis); |
| 163 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 164 | /** Returns output quantization information for softmax layer |
| 165 | * |
| 166 | * @param[in] input_type The data type of the input tensor |
| 167 | * @param[in] is_log True for log softmax |
| 168 | * |
| 169 | * @return Quantization information for the output tensor |
| 170 | */ |
| 171 | QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log); |
| 172 | |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 173 | /** Returns a pair of minimum and maximum values for a quantized activation |
| 174 | * |
| 175 | * @param[in] act_info The information for activation |
| 176 | * @param[in] data_type The used data type |
| 177 | * @param[in] oq_info The output quantization information |
| 178 | * |
| 179 | * @return The pair with minimum and maximum values |
| 180 | */ |
Pablo Marquez Tello | 205ba24 | 2023-07-12 14:29:58 +0100 | [diff] [blame] | 181 | std::pair<int32_t, int32_t> get_quantized_activation_min_max(const ActivationLayerInfo &act_info, DataType data_type, UniformQuantizationInfo oq_info); |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 182 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | /** Convert a channel identity into a string. |
| 184 | * |
| 185 | * @param[in] channel @ref Channel to be translated to string. |
| 186 | * |
| 187 | * @return The string describing the channel. |
| 188 | */ |
| 189 | const std::string &string_from_channel(Channel channel); |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 190 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | /** Translates a given border mode policy to a string. |
| 192 | * |
| 193 | * @param[in] border_mode @ref BorderMode to be translated to string. |
| 194 | * |
| 195 | * @return The string describing the border mode. |
| 196 | */ |
| 197 | const std::string &string_from_border_mode(BorderMode border_mode); |
| 198 | /** Translates a given normalization type to a string. |
| 199 | * |
| 200 | * @param[in] type @ref NormType to be translated to string. |
| 201 | * |
| 202 | * @return The string describing the normalization type. |
| 203 | */ |
| 204 | const std::string &string_from_norm_type(NormType type); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 205 | /** Translates a given pooling type to a string. |
| 206 | * |
| 207 | * @param[in] type @ref PoolingType to be translated to string. |
| 208 | * |
| 209 | * @return The string describing the pooling type. |
| 210 | */ |
| 211 | const std::string &string_from_pooling_type(PoolingType type); |
SiCongLi | c4270cf | 2021-12-22 11:22:40 +0000 | [diff] [blame] | 212 | /** Check if the pool region is entirely outside the input tensor |
| 213 | * |
| 214 | * @param[in] info @ref PoolingLayerInfo to be checked. |
| 215 | * |
| 216 | * @return True if the pool region is entirely outside the input tensor, False otherwise. |
| 217 | */ |
| 218 | bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info); |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 219 | /** Check if the 3d pool region is entirely outside the input tensor |
| 220 | * |
| 221 | * @param[in] info @ref Pooling3dLayerInfo to be checked. |
| 222 | * |
| 223 | * @return True if the pool region is entirely outside the input tensor, False otherwise. |
| 224 | */ |
| 225 | bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info); |
| 226 | /** Check if the 3D padding is symmetric i.e. padding in each opposite sides are euqal (left=right, top=bottom and front=back) |
| 227 | * |
| 228 | * @param[in] info @ref Padding3D input 3D padding object to check if it is symmetric |
| 229 | * |
| 230 | * @return True if padding is symmetric |
| 231 | */ |
Pablo Marquez Tello | 205ba24 | 2023-07-12 14:29:58 +0100 | [diff] [blame] | 232 | inline bool is_symmetric(const Padding3D &info) |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 233 | { |
| 234 | return ((info.left == info.right) && (info.top == info.bottom) && (info.front == info.back)); |
| 235 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 236 | /** Translates a given GEMMLowp output stage to a string. |
| 237 | * |
| 238 | * @param[in] output_stage @ref GEMMLowpOutputStageInfo to be translated to string. |
| 239 | * |
| 240 | * @return The string describing the GEMMLowp output stage |
| 241 | */ |
| 242 | const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 243 | /** Convert a PixelValue to a string, represented through the specific data type |
| 244 | * |
| 245 | * @param[in] value The PixelValue to convert |
| 246 | * @param[in] data_type The type to be used to convert the @p value |
| 247 | * |
| 248 | * @return String representation of the PixelValue through the given data type. |
| 249 | */ |
| 250 | 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] | 251 | |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 252 | /** Stores padding information before configuring a kernel |
| 253 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 254 | * @param[in] infos list of tensor infos to store the padding info for |
| 255 | * |
| 256 | * @return An unordered map where each tensor info pointer is paired with its original padding info |
| 257 | */ |
| 258 | std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos); |
| 259 | /** Stores padding information before configuring a kernel |
| 260 | * |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 261 | * @param[in] tensors list of tensors to store the padding info for |
| 262 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 263 | * @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] | 264 | */ |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 265 | 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] | 266 | /** Check if the previously stored padding info has changed after configuring a kernel |
| 267 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 268 | * @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] | 269 | * |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 270 | * @return true if any of the tensor infos has changed its paddings |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 271 | */ |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 272 | 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] | 273 | |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 274 | /** Returns the number of elements required to go from start to end with the wanted step |
| 275 | * |
| 276 | * @param[in] start start value |
| 277 | * @param[in] end end value |
| 278 | * @param[in] step step value between each number in the wanted sequence |
| 279 | * |
| 280 | * @return number of elements to go from start value to end value using the wanted step |
| 281 | */ |
| 282 | inline size_t num_of_elements_in_range(const float start, const float end, const float step) |
| 283 | { |
| 284 | ARM_COMPUTE_ERROR_ON_MSG(step == 0, "Range Step cannot be 0"); |
| 285 | return size_t(std::ceil((end - start) / step)); |
| 286 | } |
| 287 | |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 288 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | /** Print consecutive elements to an output stream. |
| 290 | * |
| 291 | * @param[out] s Output stream to print the elements to. |
| 292 | * @param[in] ptr Pointer to print the elements from. |
| 293 | * @param[in] n Number of elements to print. |
| 294 | * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0. |
| 295 | * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter |
| 296 | */ |
| 297 | template <typename T> |
| 298 | void print_consecutive_elements_impl(std::ostream &s, const T *ptr, unsigned int n, int stream_width = 0, const std::string &element_delim = " ") |
| 299 | { |
| 300 | 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] | 301 | std::ios stream_status(nullptr); |
| 302 | stream_status.copyfmt(s); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 303 | |
| 304 | for(unsigned int i = 0; i < n; ++i) |
| 305 | { |
| 306 | // Set stream width as it is not a "sticky" stream manipulator |
| 307 | if(stream_width != 0) |
| 308 | { |
| 309 | s.width(stream_width); |
| 310 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 311 | |
| 312 | if(std::is_same<typename std::decay<T>::type, half>::value) |
| 313 | { |
| 314 | // 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. |
| 315 | s << std::right << static_cast<T>(ptr[i]) << element_delim; |
| 316 | } |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 317 | else if(std::is_same<typename std::decay<T>::type, bfloat16>::value) |
| 318 | { |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 319 | // 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] | 320 | s << std::right << float(ptr[i]) << element_delim; |
| 321 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 322 | else |
| 323 | { |
| 324 | s << std::right << static_cast<print_type>(ptr[i]) << element_delim; |
| 325 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 326 | } |
Michalis Spyrou | 53860dd | 2019-07-01 14:20:56 +0100 | [diff] [blame] | 327 | |
| 328 | // Restore output stream flags |
| 329 | s.copyfmt(stream_status); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** Identify the maximum width of n consecutive elements. |
| 333 | * |
| 334 | * @param[in] s The output stream which will be used to print the elements. Used to extract the stream format. |
| 335 | * @param[in] ptr Pointer to the elements. |
| 336 | * @param[in] n Number of elements. |
| 337 | * |
| 338 | * @return The maximum width of the elements. |
| 339 | */ |
| 340 | template <typename T> |
| 341 | int max_consecutive_elements_display_width_impl(std::ostream &s, const T *ptr, unsigned int n) |
| 342 | { |
| 343 | using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type; |
| 344 | |
| 345 | int max_width = -1; |
| 346 | for(unsigned int i = 0; i < n; ++i) |
| 347 | { |
| 348 | std::stringstream ss; |
| 349 | ss.copyfmt(s); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 350 | |
| 351 | if(std::is_same<typename std::decay<T>::type, half>::value) |
| 352 | { |
| 353 | // 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. |
| 354 | ss << static_cast<T>(ptr[i]); |
| 355 | } |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 356 | else if(std::is_same<typename std::decay<T>::type, bfloat16>::value) |
| 357 | { |
| 358 | // 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. |
| 359 | ss << float(ptr[i]); |
| 360 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 361 | else |
| 362 | { |
| 363 | ss << static_cast<print_type>(ptr[i]); |
| 364 | } |
| 365 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 366 | max_width = std::max<int>(max_width, ss.str().size()); |
| 367 | } |
| 368 | return max_width; |
| 369 | } |
| 370 | |
| 371 | /** Print consecutive elements to an output stream. |
| 372 | * |
| 373 | * @param[out] s Output stream to print the elements to. |
| 374 | * @param[in] dt Data type of the elements |
| 375 | * @param[in] ptr Pointer to print the elements from. |
| 376 | * @param[in] n Number of elements to print. |
| 377 | * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0. |
| 378 | * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter |
| 379 | */ |
| 380 | void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim = " "); |
| 381 | |
| 382 | /** Identify the maximum width of n consecutive elements. |
| 383 | * |
| 384 | * @param[in] s Output stream to print the elements to. |
| 385 | * @param[in] dt Data type of the elements |
| 386 | * @param[in] ptr Pointer to print the elements from. |
| 387 | * @param[in] n Number of elements to print. |
| 388 | * |
| 389 | * @return The maximum width of the elements. |
| 390 | */ |
| 391 | 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] | 392 | #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 393 | } |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 394 | #endif /*ARM_COMPUTE_UTILS_H */ |