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 | */ |
Jaroslaw Rzepecki | 0a878ae | 2017-11-22 17:16:39 +0000 | [diff] [blame] | 24 | |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame^] | 25 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/ActivationLayerInfo.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame^] | 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/utils/StringUtils.h" |
Jaroslaw Rzepecki | 0a878ae | 2017-11-22 17:16:39 +0000 | [diff] [blame] | 30 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <cmath> |
| 33 | #include <cstdint> |
| 34 | #include <fstream> |
| 35 | #include <map> |
| 36 | #include <string> |
| 37 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
| 40 | std::string read_file(const std::string &filename, bool binary) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | { |
| 42 | std::string out; |
| 43 | std::ifstream fs; |
| 44 | |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 45 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | try |
| 47 | { |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 48 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 50 | std::ios_base::openmode mode = std::ios::in; |
| 51 | |
| 52 | if(binary) |
| 53 | { |
| 54 | mode |= std::ios::binary; |
| 55 | } |
| 56 | |
| 57 | fs.open(filename, mode); |
| 58 | |
| 59 | // Go to the end of the file |
| 60 | fs.seekg(0, std::ios::end); |
| 61 | // Reserve the memory required to store the file's content |
| 62 | out.reserve(fs.tellg()); |
| 63 | // Go back to the beginning of the file |
| 64 | fs.seekg(0, std::ios::beg); |
| 65 | // Copy the content of the file |
| 66 | out.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>()); |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 67 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | } |
| 69 | catch(const std::ifstream::failure &e) |
| 70 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 73 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
| 75 | return out; |
| 76 | } |
| 77 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 78 | const std::string &string_from_channel(Channel channel) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | { |
| 80 | static std::map<Channel, const std::string> channels_map = |
| 81 | { |
| 82 | { Channel::UNKNOWN, "UNKNOWN" }, |
| 83 | { Channel::R, "R" }, |
| 84 | { Channel::G, "G" }, |
| 85 | { Channel::B, "B" }, |
| 86 | { Channel::A, "A" }, |
| 87 | { Channel::Y, "Y" }, |
| 88 | { Channel::U, "U" }, |
| 89 | { Channel::V, "V" }, |
| 90 | { Channel::C0, "C0" }, |
| 91 | { Channel::C1, "C1" }, |
| 92 | { Channel::C2, "C2" }, |
| 93 | { Channel::C3, "C3" } |
| 94 | }; |
| 95 | |
| 96 | return channels_map[channel]; |
| 97 | } |
| 98 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 99 | const std::string &string_from_border_mode(BorderMode border_mode) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | { |
| 101 | static std::map<BorderMode, const std::string> border_mode_map = |
| 102 | { |
| 103 | { BorderMode::UNDEFINED, "UNDEFINED" }, |
| 104 | { BorderMode::CONSTANT, "CONSTANT" }, |
| 105 | { BorderMode::REPLICATE, "REPLICATE" }, |
| 106 | }; |
| 107 | |
| 108 | return border_mode_map[border_mode]; |
| 109 | } |
| 110 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 111 | const std::string &string_from_norm_type(NormType type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | { |
| 113 | static std::map<NormType, const std::string> norm_type_map = |
| 114 | { |
| 115 | { NormType::IN_MAP_1D, "IN_MAP_1D" }, |
| 116 | { NormType::IN_MAP_2D, "IN_MAP_2D" }, |
| 117 | { NormType::CROSS_MAP, "CROSS_MAP" }, |
| 118 | }; |
| 119 | |
| 120 | return norm_type_map[type]; |
| 121 | } |
| 122 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 123 | const std::string &string_from_pooling_type(PoolingType type) |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 124 | { |
| 125 | static std::map<PoolingType, const std::string> pool_type_map = |
| 126 | { |
| 127 | { PoolingType::MAX, "MAX" }, |
| 128 | { PoolingType::AVG, "AVG" }, |
| 129 | { PoolingType::L2, "L2" }, |
| 130 | }; |
| 131 | |
| 132 | return pool_type_map[type]; |
| 133 | } |
| 134 | |
SiCongLi | c4270cf | 2021-12-22 11:22:40 +0000 | [diff] [blame] | 135 | bool is_pool_region_entirely_outside_input(const PoolingLayerInfo &info) |
| 136 | { |
| 137 | if(info.is_global_pooling || info.exclude_padding || info.pool_size.x() == 0 || info.pool_size.y() == 0) |
| 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | const auto ps = info.pad_stride_info; |
| 142 | const auto pool_le_padding_x = info.pool_size.x() <= std::max({ ps.pad_left(), ps.pad_right() }); |
| 143 | const auto pool_le_padding_y = info.pool_size.y() <= std::max({ ps.pad_top(), ps.pad_bottom() }); |
| 144 | return pool_le_padding_x || pool_le_padding_y; |
| 145 | } |
| 146 | |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 147 | bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info) |
| 148 | { |
| 149 | if(info.is_global_pooling || info.pool_size.x() == 0 || info.pool_size.y() == 0 || info.pool_size.z() == 0) |
| 150 | { |
| 151 | return false; |
| 152 | } |
| 153 | const auto ps = info.padding; |
| 154 | const auto pool_le_padding_x = info.pool_size.x() <= std::max({ ps.left, ps.right }); |
| 155 | const auto pool_le_padding_y = info.pool_size.y() <= std::max({ ps.top, ps.bottom }); |
| 156 | const auto pool_le_padding_z = info.pool_size.z() <= std::max({ ps.front, ps.back }); |
| 157 | return pool_le_padding_x || pool_le_padding_y || pool_le_padding_z; |
| 158 | } |
| 159 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 160 | const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 161 | { |
| 162 | static std::map<GEMMLowpOutputStageType, const std::string> output_stage_map = |
| 163 | { |
| 164 | { GEMMLowpOutputStageType::NONE, "" }, |
| 165 | { GEMMLowpOutputStageType::QUANTIZE_DOWN, "quantize_down" }, |
| 166 | { GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT, "quantize_down_fixedpoint" }, |
| 167 | { GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT, "quantize_down_float" } |
| 168 | }; |
| 169 | |
| 170 | return output_stage_map[output_stage]; |
| 171 | } |
| 172 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 173 | std::string string_from_pixel_value(const PixelValue &value, const DataType data_type) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 174 | { |
| 175 | std::stringstream ss; |
| 176 | std::string converted_string; |
| 177 | |
| 178 | switch(data_type) |
| 179 | { |
| 180 | case DataType::U8: |
| 181 | case DataType::QASYMM8: |
| 182 | // Needs conversion to 32 bit, otherwise interpreted as ASCII values |
| 183 | ss << uint32_t(value.get<uint8_t>()); |
| 184 | converted_string = ss.str(); |
| 185 | break; |
| 186 | case DataType::S8: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 187 | case DataType::QASYMM8_SIGNED: |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 188 | case DataType::QSYMM8_PER_CHANNEL: |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 189 | // Needs conversion to 32 bit, otherwise interpreted as ASCII values |
| 190 | ss << int32_t(value.get<int8_t>()); |
| 191 | converted_string = ss.str(); |
| 192 | break; |
| 193 | case DataType::U16: |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 194 | case DataType::QASYMM16: |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 195 | ss << value.get<uint16_t>(); |
| 196 | converted_string = ss.str(); |
| 197 | break; |
| 198 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 199 | case DataType::QSYMM16: |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 200 | ss << value.get<int16_t>(); |
| 201 | converted_string = ss.str(); |
| 202 | break; |
| 203 | case DataType::U32: |
| 204 | ss << value.get<uint32_t>(); |
| 205 | converted_string = ss.str(); |
| 206 | break; |
| 207 | case DataType::S32: |
| 208 | ss << value.get<int32_t>(); |
| 209 | converted_string = ss.str(); |
| 210 | break; |
| 211 | case DataType::F32: |
| 212 | converted_string = float_to_string_with_full_precision(value.get<float>()); |
| 213 | break; |
| 214 | case DataType::F16: |
| 215 | static_assert(sizeof(half) == 2, "Half must be 16 bit"); |
| 216 | ss << value.get<half>(); |
| 217 | converted_string = ss.str(); |
| 218 | break; |
| 219 | default: |
| 220 | ARM_COMPUTE_ERROR("Not handled"); |
| 221 | } |
| 222 | |
| 223 | return converted_string; |
| 224 | } |
| 225 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 226 | PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation, |
| 227 | const DimensionRoundingType &rounding_type) |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 228 | { |
Sang-Hoon Park | 46023ed | 2019-11-12 16:26:26 +0000 | [diff] [blame] | 229 | const auto &strides = conv_info.stride(); |
| 230 | ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1."); |
| 231 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 232 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 233 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 234 | const unsigned int in_width = input_shape[width_idx]; |
| 235 | const unsigned int in_height = input_shape[height_idx]; |
| 236 | const unsigned int kernel_width = weights_shape[width_idx]; |
| 237 | const unsigned int kernel_height = weights_shape[height_idx]; |
Georgios Pinitas | 4c75851 | 2019-07-10 19:49:11 +0100 | [diff] [blame] | 238 | |
| 239 | // Calculate output dimensions |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 240 | const auto is_ceil = static_cast<unsigned int>(rounding_type == DimensionRoundingType::CEIL); |
| 241 | const unsigned int out_width = ((in_width - is_ceil) + strides.first - 1) / strides.first + is_ceil; |
| 242 | const unsigned int out_height = ((in_height - is_ceil) + strides.second - 1) / strides.second + is_ceil; |
Georgios Pinitas | 4c75851 | 2019-07-10 19:49:11 +0100 | [diff] [blame] | 243 | |
| 244 | // Calculate effective weights sizes |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 245 | const int real_weight_width = (kernel_width - 1) * dilation.x() + 1; |
| 246 | const int real_weight_height = (kernel_height - 1) * dilation.y() + 1; |
Georgios Pinitas | 4c75851 | 2019-07-10 19:49:11 +0100 | [diff] [blame] | 247 | |
| 248 | // Calculate total pad |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 249 | const int pad_width = std::max(0, static_cast<int>((out_width - 1) * strides.first + real_weight_width - in_width)); |
| 250 | const int pad_height = std::max(0, static_cast<int>((out_height - 1) * strides.second + real_weight_height - in_height)); |
Georgios Pinitas | 4c75851 | 2019-07-10 19:49:11 +0100 | [diff] [blame] | 251 | |
| 252 | // Calculate individual paddings |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 253 | const unsigned int pad_left = pad_width / 2; |
| 254 | const unsigned int pad_top = pad_height / 2; |
| 255 | const unsigned int pad_right = pad_width - pad_left; |
| 256 | const unsigned int pad_bottom = pad_height - pad_top; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 257 | |
Giorgio Arena | 1720358 | 2019-08-02 16:00:41 +0100 | [diff] [blame] | 258 | PadStrideInfo same_info(strides.first, strides.second, pad_left, pad_right, pad_top, pad_bottom, rounding_type); |
| 259 | |
| 260 | // Check for correctness of predicted output shape against the one calculated using the generated info |
| 261 | const auto out_dims = scaled_dimensions(in_width, in_height, kernel_width, kernel_height, same_info, dilation); |
| 262 | ARM_COMPUTE_ERROR_ON(out_dims.first != out_width || out_dims.second != out_height); |
| 263 | ARM_COMPUTE_UNUSED(out_dims); |
| 264 | |
| 265 | return same_info; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 266 | } |
| 267 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 268 | std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, |
| 269 | unsigned int kernel_width, unsigned int kernel_height, |
| 270 | const PadStrideInfo &pad_stride_info) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 271 | { |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 272 | const unsigned int pad_left = pad_stride_info.pad_left(); |
| 273 | const unsigned int pad_top = pad_stride_info.pad_top(); |
| 274 | const unsigned int pad_right = pad_stride_info.pad_right(); |
| 275 | const unsigned int pad_bottom = pad_stride_info.pad_bottom(); |
| 276 | const unsigned int stride_x = pad_stride_info.stride().first; |
| 277 | const unsigned int stride_y = pad_stride_info.stride().second; |
| 278 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 279 | ARM_COMPUTE_ERROR_ON(in_width < 1 || in_height < 1); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 280 | ARM_COMPUTE_ERROR_ON(((in_width - 1) * stride_x + kernel_width) < (pad_left + pad_right)); |
| 281 | ARM_COMPUTE_ERROR_ON(((in_height - 1) * stride_y + kernel_height) < (pad_top + pad_bottom)); |
| 282 | const int w = stride_x * (in_width - 1) + kernel_width - (pad_left + pad_right); |
| 283 | const int h = stride_y * (in_height - 1) + kernel_height - (pad_top + pad_bottom); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 284 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 285 | return std::make_pair<unsigned int, unsigned int>(w, h); |
| 286 | } |
| 287 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 288 | std::pair<unsigned int, unsigned int> scaled_dimensions(int width, int height, |
| 289 | int kernel_width, int kernel_height, |
| 290 | const PadStrideInfo &pad_stride_info, |
| 291 | const Size2D &dilation) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 292 | { |
Georgios Pinitas | 80838f1 | 2019-12-12 18:23:13 +0000 | [diff] [blame] | 293 | const int dilation_x = dilation.x(); |
| 294 | const int dilation_y = dilation.y(); |
| 295 | const int pad_left = pad_stride_info.pad_left(); |
| 296 | const int pad_top = pad_stride_info.pad_top(); |
| 297 | const int pad_right = pad_stride_info.pad_right(); |
| 298 | const int pad_bottom = pad_stride_info.pad_bottom(); |
| 299 | const int stride_x = pad_stride_info.stride().first; |
| 300 | const int stride_y = pad_stride_info.stride().second; |
| 301 | int w = 0; |
| 302 | int h = 0; |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 303 | switch(pad_stride_info.round()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 304 | { |
| 305 | case DimensionRoundingType::FLOOR: |
Georgios Pinitas | 80838f1 | 2019-12-12 18:23:13 +0000 | [diff] [blame] | 306 | w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1)); |
| 307 | h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - (dilation_y * (kernel_height - 1) + 1)) / stride_y) + 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 308 | break; |
| 309 | case DimensionRoundingType::CEIL: |
Georgios Pinitas | 80838f1 | 2019-12-12 18:23:13 +0000 | [diff] [blame] | 310 | w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - (dilation_x * (kernel_width - 1) + 1)) / stride_x) + 1)); |
| 311 | h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - (dilation_y * (kernel_height - 1) + 1)) / stride_y) + 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 312 | break; |
| 313 | default: |
| 314 | ARM_COMPUTE_ERROR("Unsupported rounding type"); |
| 315 | } |
| 316 | |
Georgios Pinitas | 80838f1 | 2019-12-12 18:23:13 +0000 | [diff] [blame] | 317 | w = std::max(1, w); |
| 318 | h = std::max(1, h); |
| 319 | return std::make_pair(static_cast<unsigned int>(w), static_cast<unsigned int>(h)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Freddie Liardet | afcbb8f | 2021-05-04 12:41:16 +0100 | [diff] [blame] | 322 | std::pair<int, int> scaled_dimensions_signed(int width, int height, |
| 323 | int kernel_width, int kernel_height, |
| 324 | const PadStrideInfo &pad_stride_info) |
| 325 | { |
| 326 | const int pad_left = pad_stride_info.pad_left(); |
| 327 | const int pad_top = pad_stride_info.pad_top(); |
| 328 | const int pad_right = pad_stride_info.pad_right(); |
| 329 | const int pad_bottom = pad_stride_info.pad_bottom(); |
| 330 | const int stride_x = pad_stride_info.stride().first; |
| 331 | const int stride_y = pad_stride_info.stride().second; |
| 332 | int w = 0; |
| 333 | int h = 0; |
| 334 | switch(pad_stride_info.round()) |
| 335 | { |
| 336 | case DimensionRoundingType::FLOOR: |
| 337 | w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1)); |
| 338 | h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1)); |
| 339 | break; |
| 340 | case DimensionRoundingType::CEIL: |
| 341 | w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1)); |
| 342 | h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1)); |
| 343 | break; |
| 344 | default: |
| 345 | ARM_COMPUTE_ERROR("Unsupported rounding type"); |
| 346 | } |
| 347 | |
| 348 | return std::make_pair(static_cast<int>(w), static_cast<int>(h)); |
| 349 | } |
| 350 | |
ramelg01 | 3751569 | 2022-02-26 22:06:20 +0000 | [diff] [blame] | 351 | std::tuple<int, int, int> scaled_3d_dimensions_signed(int width, int height, int depth, |
| 352 | int kernel_width, int kernel_height, int kernel_depth, |
| 353 | const Pooling3dLayerInfo &pool3d_info) |
| 354 | { |
| 355 | const int pad_left = pool3d_info.padding.left; |
| 356 | const int pad_top = pool3d_info.padding.top; |
| 357 | const int pad_right = pool3d_info.padding.right; |
| 358 | const int pad_bottom = pool3d_info.padding.bottom; |
| 359 | const int pad_front = pool3d_info.padding.front; |
| 360 | const int pad_back = pool3d_info.padding.back; |
| 361 | const int stride_x = pool3d_info.stride.x(); |
| 362 | const int stride_y = pool3d_info.stride.y(); |
| 363 | const int stride_z = pool3d_info.stride.z(); |
| 364 | int w = 0; |
| 365 | int h = 0; |
| 366 | int d = 0; |
| 367 | |
| 368 | switch(pool3d_info.round_type) |
| 369 | { |
| 370 | case DimensionRoundingType::FLOOR: |
| 371 | w = static_cast<int>(std::floor((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1)); |
| 372 | h = static_cast<int>(std::floor((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1)); |
| 373 | d = static_cast<int>(std::floor((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1)); |
| 374 | break; |
| 375 | case DimensionRoundingType::CEIL: |
| 376 | w = static_cast<int>(std::ceil((static_cast<float>(width + pad_left + pad_right - kernel_width) / stride_x) + 1)); |
| 377 | h = static_cast<int>(std::ceil((static_cast<float>(height + pad_top + pad_bottom - kernel_height) / stride_y) + 1)); |
| 378 | d = static_cast<int>(std::ceil((static_cast<float>(depth + pad_front + pad_back - kernel_depth) / stride_z) + 1)); |
| 379 | break; |
| 380 | default: |
| 381 | ARM_COMPUTE_ERROR("Unsupported rounding type"); |
| 382 | } |
| 383 | |
| 384 | return std::make_tuple(static_cast<int>(w), static_cast<int>(h), static_cast<int>(d)); |
| 385 | } |
| 386 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 387 | bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis) |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 388 | { |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 389 | const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN); |
| 390 | const bool is_quantized_type = is_data_type_quantized(dt); |
| 391 | const bool is_first_dim = (axis == 0); |
| 392 | |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 393 | return !is_first_dim || is_min_max || is_quantized_type; |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 394 | } |
| 395 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 396 | QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log) |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 397 | { |
| 398 | // Note: Output quantization info for softmax should always have |
| 399 | // * Softmax with QASYMM8: scale = 1/256, offset = 0 |
| 400 | // * Softmax with QASYMM8_SIGNED: scale = 1/256, offset = -128 |
| 401 | // * LogSoftmax with QASYMM8: scale = 1/256, offset = 0 |
| 402 | // * LogSoftmax with QASYMM8_SIGNED: scale = 16/256, offset = 127 |
| 403 | if(is_data_type_quantized_asymmetric_signed(input_type)) |
| 404 | { |
| 405 | if(is_log) |
| 406 | { |
| 407 | return QuantizationInfo(16.f / 256, 127); |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | return QuantizationInfo(1.f / 256, -128); |
| 412 | } |
| 413 | } |
| 414 | return QuantizationInfo(1.f / 256, 0); |
| 415 | } |
| 416 | |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 417 | 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] | 418 | { |
| 419 | const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type); |
| 420 | const auto a = act_info.a(); |
| 421 | const auto b = act_info.b(); |
| 422 | const int a_int = is_qasymm8_signed ? quantize_qasymm8_signed(a, oq_info) : quantize_qasymm8(a, oq_info); |
| 423 | const int b_int = is_qasymm8_signed ? quantize_qasymm8_signed(b, oq_info) : quantize_qasymm8(b, oq_info); |
| 424 | const auto type_max_value = std::get<1>(get_min_max(data_type)).get<int32_t>(); |
| 425 | |
| 426 | const int32_t min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? oq_info.offset : b_int; |
| 427 | const int32_t max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? type_max_value : a_int; |
| 428 | |
| 429 | return std::make_pair(min_activation, max_activation); |
| 430 | } |
| 431 | |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 432 | 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] | 433 | { |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 434 | std::unordered_map<const ITensorInfo *, PaddingSize> res; |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 435 | |
| 436 | for(const ITensor *tensor : tensors) |
| 437 | { |
| 438 | if(tensor) |
| 439 | { |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 440 | res.insert({ tensor->info(), tensor->info()->padding() }); |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | return res; |
| 445 | } |
| 446 | |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 447 | std::unordered_map<const ITensorInfo *, PaddingSize> get_padding_info(std::initializer_list<const ITensorInfo *> infos) |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 448 | { |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 449 | std::unordered_map<const ITensorInfo *, PaddingSize> res; |
| 450 | |
| 451 | for(const ITensorInfo *info : infos) |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 452 | { |
Giorgio Arena | 4112eed | 2020-10-23 14:24:26 +0100 | [diff] [blame] | 453 | if(info) |
| 454 | { |
| 455 | res.insert({ info, info->padding() }); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return res; |
| 460 | } |
| 461 | |
| 462 | bool has_padding_changed(const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map) |
| 463 | { |
| 464 | return std::find_if(padding_map.begin(), padding_map.end(), [](const std::pair<const ITensorInfo *, PaddingSize> &padding_info) |
| 465 | { |
| 466 | return (padding_info.first->padding() != padding_info.second); |
Giorgio Arena | 1e2af2a | 2020-10-15 17:39:41 +0100 | [diff] [blame] | 467 | }) |
| 468 | != padding_map.end(); |
| 469 | } |
| 470 | |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 471 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 472 | void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 473 | { |
| 474 | switch(dt) |
| 475 | { |
| 476 | case DataType::U8: |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 477 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 478 | print_consecutive_elements_impl<uint8_t>(s, ptr, n, stream_width, element_delim); |
| 479 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 480 | case DataType::S8: |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 481 | case DataType::QSYMM8: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 482 | case DataType::QASYMM8_SIGNED: |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 483 | case DataType::QSYMM8_PER_CHANNEL: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 484 | print_consecutive_elements_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n, stream_width, element_delim); |
| 485 | break; |
| 486 | case DataType::U16: |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 487 | case DataType::QASYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 488 | print_consecutive_elements_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n, stream_width, element_delim); |
| 489 | break; |
| 490 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 491 | case DataType::QSYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 492 | print_consecutive_elements_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n, stream_width, element_delim); |
| 493 | break; |
| 494 | case DataType::U32: |
| 495 | print_consecutive_elements_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n, stream_width, element_delim); |
| 496 | break; |
| 497 | case DataType::S32: |
| 498 | print_consecutive_elements_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n, stream_width, element_delim); |
| 499 | break; |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 500 | case DataType::BFLOAT16: |
| 501 | print_consecutive_elements_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n, stream_width, element_delim); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 502 | break; |
| 503 | case DataType::F16: |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 504 | print_consecutive_elements_impl<half>(s, reinterpret_cast<const half *>(ptr), n, stream_width, element_delim); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 505 | break; |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 506 | case DataType::F32: |
| 507 | print_consecutive_elements_impl<float>(s, reinterpret_cast<const float *>(ptr), n, stream_width, element_delim); |
| 508 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 509 | default: |
| 510 | ARM_COMPUTE_ERROR("Undefined element size for given data type"); |
| 511 | } |
| 512 | } |
| 513 | |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 514 | int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 515 | { |
| 516 | switch(dt) |
| 517 | { |
| 518 | case DataType::U8: |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 519 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 520 | return max_consecutive_elements_display_width_impl<uint8_t>(s, ptr, n); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 521 | case DataType::S8: |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 522 | case DataType::QSYMM8: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 523 | case DataType::QASYMM8_SIGNED: |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 524 | case DataType::QSYMM8_PER_CHANNEL: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 525 | return max_consecutive_elements_display_width_impl<int8_t>(s, reinterpret_cast<const int8_t *>(ptr), n); |
| 526 | case DataType::U16: |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 527 | case DataType::QASYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 528 | return max_consecutive_elements_display_width_impl<uint16_t>(s, reinterpret_cast<const uint16_t *>(ptr), n); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 529 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 530 | case DataType::QSYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 531 | return max_consecutive_elements_display_width_impl<int16_t>(s, reinterpret_cast<const int16_t *>(ptr), n); |
| 532 | case DataType::U32: |
| 533 | return max_consecutive_elements_display_width_impl<uint32_t>(s, reinterpret_cast<const uint32_t *>(ptr), n); |
| 534 | case DataType::S32: |
| 535 | return max_consecutive_elements_display_width_impl<int32_t>(s, reinterpret_cast<const int32_t *>(ptr), n); |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 536 | case DataType::BFLOAT16: |
| 537 | return max_consecutive_elements_display_width_impl<bfloat16>(s, reinterpret_cast<const bfloat16 *>(ptr), n); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 538 | case DataType::F16: |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 539 | return max_consecutive_elements_display_width_impl<half>(s, reinterpret_cast<const half *>(ptr), n); |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 540 | case DataType::F32: |
| 541 | return max_consecutive_elements_display_width_impl<float>(s, reinterpret_cast<const float *>(ptr), n); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 542 | default: |
| 543 | ARM_COMPUTE_ERROR("Undefined element size for given data type"); |
| 544 | } |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 545 | return 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 546 | } |
giuros01 | edc21e4 | 2018-11-16 14:45:31 +0000 | [diff] [blame] | 547 | #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ |
SiCong Li | e357a25 | 2020-08-09 20:05:52 +0100 | [diff] [blame] | 548 | |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 549 | } // namespace arm_compute |