SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2023 Arm Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ACL_ARM_COMPUTE_CORE_CORETYPES |
| 25 | #define ACL_ARM_COMPUTE_CORE_CORETYPES |
| 26 | |
| 27 | #include "arm_compute/core/Strides.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 29 | #include "support/Half.h" |
| 30 | |
| 31 | /** CoreTypes.h groups together essential small types that are used across functions */ |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | /** 16-bit floating point type */ |
| 36 | using half = half_float::half; |
| 37 | /** Permutation vector */ |
| 38 | using PermutationVector = Strides; |
| 39 | |
| 40 | /** Available channels */ |
| 41 | enum class Channel |
| 42 | { |
| 43 | UNKNOWN, /** Unknown channel format */ |
| 44 | C0, /**< First channel (used by formats with unknown channel types). */ |
| 45 | C1, /**< Second channel (used by formats with unknown channel types). */ |
| 46 | C2, /**< Third channel (used by formats with unknown channel types). */ |
| 47 | C3, /**< Fourth channel (used by formats with unknown channel types). */ |
| 48 | R, /**< Red channel. */ |
| 49 | G, /**< Green channel. */ |
| 50 | B, /**< Blue channel. */ |
| 51 | A, /**< Alpha channel. */ |
| 52 | Y, /**< Luma channel. */ |
| 53 | U, /**< Cb/U channel. */ |
| 54 | V /**< Cr/V/Value channel. */ |
| 55 | }; |
| 56 | |
| 57 | /** Image colour formats */ |
| 58 | enum class Format |
| 59 | { |
| 60 | UNKNOWN, /**< Unknown image format */ |
| 61 | U8, /**< 1 channel, 1 U8 per channel */ |
| 62 | S16, /**< 1 channel, 1 S16 per channel */ |
| 63 | U16, /**< 1 channel, 1 U16 per channel */ |
| 64 | S32, /**< 1 channel, 1 S32 per channel */ |
| 65 | U32, /**< 1 channel, 1 U32 per channel */ |
| 66 | S64, /**< 1 channel, 1 S64 per channel */ |
| 67 | U64, /**< 1 channel, 1 U64 per channel */ |
| 68 | BFLOAT16, /**< 16-bit brain floating-point number */ |
| 69 | F16, /**< 1 channel, 1 F16 per channel */ |
| 70 | F32, /**< 1 channel, 1 F32 per channel */ |
| 71 | UV88, /**< 2 channel, 1 U8 per channel */ |
| 72 | RGB888, /**< 3 channels, 1 U8 per channel */ |
| 73 | RGBA8888, /**< 4 channels, 1 U8 per channel */ |
| 74 | YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */ |
| 75 | YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */ |
| 76 | NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */ |
| 77 | NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */ |
| 78 | IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */ |
| 79 | UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */ |
| 80 | }; |
| 81 | |
| 82 | /** Available data types */ |
| 83 | enum class DataType |
| 84 | { |
| 85 | UNKNOWN, /**< Unknown data type */ |
| 86 | U8, /**< unsigned 8-bit number */ |
| 87 | S8, /**< signed 8-bit number */ |
| 88 | QSYMM8, /**< quantized, symmetric fixed-point 8-bit number */ |
| 89 | QASYMM8, /**< quantized, asymmetric fixed-point 8-bit number unsigned */ |
| 90 | QASYMM8_SIGNED, /**< quantized, asymmetric fixed-point 8-bit number signed */ |
| 91 | QSYMM8_PER_CHANNEL, /**< quantized, symmetric per channel fixed-point 8-bit number */ |
| 92 | U16, /**< unsigned 16-bit number */ |
| 93 | S16, /**< signed 16-bit number */ |
| 94 | QSYMM16, /**< quantized, symmetric fixed-point 16-bit number */ |
| 95 | QASYMM16, /**< quantized, asymmetric fixed-point 16-bit number */ |
| 96 | U32, /**< unsigned 32-bit number */ |
| 97 | S32, /**< signed 32-bit number */ |
| 98 | U64, /**< unsigned 64-bit number */ |
| 99 | S64, /**< signed 64-bit number */ |
| 100 | BFLOAT16, /**< 16-bit brain floating-point number */ |
| 101 | F16, /**< 16-bit floating-point number */ |
| 102 | F32, /**< 32-bit floating-point number */ |
| 103 | F64, /**< 64-bit floating-point number */ |
| 104 | SIZET /**< size_t */ |
| 105 | }; |
| 106 | |
| 107 | /** [DataLayout enum definition] **/ |
| 108 | |
| 109 | /** Supported tensor data layouts */ |
| 110 | enum class DataLayout |
| 111 | { |
| 112 | UNKNOWN, /**< Unknown data layout */ |
| 113 | NCHW, /**< Num samples, channels, height, width */ |
| 114 | NHWC, /**< Num samples, height, width, channels */ |
| 115 | NCDHW, /**< Num samples, channels, depth, height, width */ |
| 116 | NDHWC /**< Num samples, depth, height, width, channels */ |
| 117 | }; |
| 118 | /** [DataLayout enum definition] **/ |
| 119 | |
| 120 | /** Supported tensor data layout dimensions */ |
| 121 | enum class DataLayoutDimension |
| 122 | { |
| 123 | CHANNEL, /**< channel */ |
| 124 | HEIGHT, /**< height */ |
| 125 | WIDTH, /**< width */ |
| 126 | DEPTH, /**< depth */ |
| 127 | BATCHES /**< batches */ |
| 128 | }; |
| 129 | |
| 130 | /** Dimension rounding type when down-scaling on CNNs |
| 131 | * @note Used in pooling and convolution layer |
| 132 | */ |
| 133 | enum class DimensionRoundingType |
| 134 | { |
| 135 | FLOOR, /**< Floor rounding */ |
| 136 | CEIL /**< Ceil rounding */ |
| 137 | }; |
| 138 | |
| 139 | class PadStrideInfo |
| 140 | { |
| 141 | public: |
| 142 | /** Constructor |
| 143 | * |
| 144 | * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1. |
| 145 | * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1. |
| 146 | * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0. |
| 147 | * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0. |
| 148 | * @param[in] round (Optional) Dimensions rounding. Defaults to @ref DimensionRoundingType::FLOOR. |
| 149 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 150 | PadStrideInfo(unsigned int stride_x = 1, |
| 151 | unsigned int stride_y = 1, |
| 152 | unsigned int pad_x = 0, |
| 153 | unsigned int pad_y = 0, |
| 154 | DimensionRoundingType round = DimensionRoundingType::FLOOR) |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 155 | : _stride(std::make_pair(stride_x, stride_y)), |
| 156 | _pad_left(pad_x), |
| 157 | _pad_top(pad_y), |
| 158 | _pad_right(pad_x), |
| 159 | _pad_bottom(pad_y), |
| 160 | _round_type(round) |
| 161 | { |
| 162 | } |
| 163 | /** Constructor |
| 164 | * |
| 165 | * @param[in] stride_x Stride, in elements, across x. |
| 166 | * @param[in] stride_y Stride, in elements, across y. |
| 167 | * @param[in] pad_left Padding across x on the left, in elements. |
| 168 | * @param[in] pad_right Padding across x on the right, in elements. |
| 169 | * @param[in] pad_top Padding across y on the top, in elements. |
| 170 | * @param[in] pad_bottom Padding across y on the bottom, in elements. |
| 171 | * @param[in] round Dimensions rounding. |
| 172 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | PadStrideInfo(unsigned int stride_x, |
| 174 | unsigned int stride_y, |
| 175 | unsigned int pad_left, |
| 176 | unsigned int pad_right, |
| 177 | unsigned int pad_top, |
| 178 | unsigned int pad_bottom, |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 179 | DimensionRoundingType round) |
| 180 | : _stride(std::make_pair(stride_x, stride_y)), |
| 181 | _pad_left(pad_left), |
| 182 | _pad_top(pad_top), |
| 183 | _pad_right(pad_right), |
| 184 | _pad_bottom(pad_bottom), |
| 185 | _round_type(round) |
| 186 | { |
| 187 | } |
| 188 | /** Get the stride. |
| 189 | * |
| 190 | * @return a pair: stride x, stride y. |
| 191 | */ |
| 192 | std::pair<unsigned int, unsigned int> stride() const |
| 193 | { |
| 194 | return _stride; |
| 195 | } |
| 196 | /** Check whether the padding is symmetric. |
| 197 | * |
| 198 | * @return True if the padding is symmetric. |
| 199 | */ |
| 200 | bool padding_is_symmetric() const |
| 201 | { |
| 202 | return (_pad_left == _pad_right) && (_pad_top == _pad_bottom); |
| 203 | } |
| 204 | /** Get the padding. |
| 205 | * |
| 206 | * @note This should only be used when the padding is symmetric. |
| 207 | * |
| 208 | * @return a pair: padding left/right, padding top/bottom |
| 209 | */ |
| 210 | std::pair<unsigned int, unsigned int> pad() const |
| 211 | { |
| 212 | //this accessor should be used only when padding is symmetric |
| 213 | ARM_COMPUTE_ERROR_ON(!padding_is_symmetric()); |
| 214 | return std::make_pair(_pad_left, _pad_top); |
| 215 | } |
| 216 | |
| 217 | /** Get the left padding */ |
| 218 | unsigned int pad_left() const |
| 219 | { |
| 220 | return _pad_left; |
| 221 | } |
| 222 | /** Get the right padding */ |
| 223 | unsigned int pad_right() const |
| 224 | { |
| 225 | return _pad_right; |
| 226 | } |
| 227 | /** Get the top padding */ |
| 228 | unsigned int pad_top() const |
| 229 | { |
| 230 | return _pad_top; |
| 231 | } |
| 232 | /** Get the bottom padding */ |
| 233 | unsigned int pad_bottom() const |
| 234 | { |
| 235 | return _pad_bottom; |
| 236 | } |
| 237 | |
| 238 | /** Get the rounding type */ |
| 239 | DimensionRoundingType round() const |
| 240 | { |
| 241 | return _round_type; |
| 242 | } |
| 243 | |
| 244 | /** Check whether this has any padding */ |
| 245 | bool has_padding() const |
| 246 | { |
| 247 | return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0); |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | std::pair<unsigned int, unsigned int> _stride; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 252 | unsigned int _pad_left; |
| 253 | unsigned int _pad_top; |
| 254 | unsigned int _pad_right; |
| 255 | unsigned int _pad_bottom; |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 256 | |
| 257 | DimensionRoundingType _round_type; |
| 258 | }; |
| 259 | |
| 260 | /** Memory layouts for the weights tensor. |
| 261 | * |
| 262 | * * UNSPECIFIED is used to select kernels that do not run in |
| 263 | * variable weights mode. |
| 264 | * |
| 265 | * * ANY is used to query the kernel database to retrieve any of the |
| 266 | * kernels that runs in variable weights mode. Once a kernel is |
| 267 | * found, the specific format expected by the kernel can be |
| 268 | * retrieved by the user for reordering the weights tensor |
| 269 | * accordingly. |
| 270 | * |
| 271 | * The other values OHWIo{interleave_by}i{block_by} describe the |
| 272 | * memory layout of a 4D tensor with layout OHWI that has been |
| 273 | * transformed into a 4D tensor with dimensions O'HWI' where: |
| 274 | * |
| 275 | * O' = first multiple of {interleave_by} s.t. O<=O' |
| 276 | * I' = first multiple of {block_by} s.t. I<=I' |
| 277 | * |
| 278 | * The total size of the dst tensor is O' x H x W x I' |
| 279 | * |
| 280 | * The access function of the tensor with layout |
| 281 | * OHWIo{interleave_by}i{block_by} and size O'HWI' is a 6-parameter |
| 282 | * access function, where the 6 parameters are computed as follows: |
| 283 | * |
| 284 | * x5 = floor(o/{interleave_by}) RANGE [0, O'/{interleave_by} -1] SIZE: O'/{interleave_by} |
| 285 | * |
| 286 | * x4 = h RANGE [0, H-1] SIZE: H |
| 287 | * x3 = w RANGE [0, W-1] SIZE: W |
| 288 | * x2 = floor(i/{block_by}) RANGE [0, I'/{block_by} -1] SIZE: I'/{block_by} |
| 289 | * x1 = o%{interleave_by} RANGE [0, {interleave_by} -1] SIZE: {interleave_by} |
| 290 | * x0 = i%{block_by} RANGE [0, {block_by} -1] SIZE: {block_by} |
| 291 | * TOTAL SIZE: O' * H * W * I' |
| 292 | * |
| 293 | * 4D 6D |
| 294 | * ----------------- ----------------------------------- |
| 295 | * value(o, h, w, i) = x5 * H * W * I' * {interleave_by} |
| 296 | * + x4 * W * I' * {interleave_by} |
| 297 | * + x3 * I' * {interleave_by} |
| 298 | * + x2 * {interleave_by} * {block_by} |
| 299 | * + x1 * {block_by} |
| 300 | * + x0 |
| 301 | * |
| 302 | * Notice that in arm_gemm the 4D tensor of dimension O'HWI' created |
| 303 | * for the OHWIo{interleave_by}i{block_by} format is in reality seen |
| 304 | * as a 2D tensor, where the number of rows is O'/{interleave_by} |
| 305 | * and the number of columns is {interleave_by} * H * W * I'. |
| 306 | * |
| 307 | * The postfix *_bf16 is for the memory layout needed for the |
| 308 | * fast-mode kernels, in which the weights are passed in bfloat16 |
| 309 | * format. |
| 310 | */ |
| 311 | enum class WeightFormat |
| 312 | { |
| 313 | UNSPECIFIED = 0x1, |
| 314 | ANY = 0x2, |
| 315 | OHWI = 0x100100, |
| 316 | OHWIo2 = 0x100200, |
| 317 | OHWIo4 = 0x100400, |
| 318 | OHWIo8 = 0x100800, |
| 319 | OHWIo16 = 0x101000, |
| 320 | OHWIo32 = 0x102000, |
| 321 | OHWIo64 = 0x104000, |
| 322 | OHWIo128 = 0x108000, |
| 323 | OHWIo4i2 = 0x200400, |
| 324 | OHWIo4i2_bf16 = 0x200410, |
| 325 | OHWIo8i2 = 0x200800, |
| 326 | OHWIo8i2_bf16 = 0x200810, |
| 327 | OHWIo16i2 = 0x201000, |
| 328 | OHWIo16i2_bf16 = 0x201010, |
| 329 | OHWIo32i2 = 0x202000, |
| 330 | OHWIo32i2_bf16 = 0x202010, |
| 331 | OHWIo64i2 = 0x204000, |
| 332 | OHWIo64i2_bf16 = 0x204010, |
| 333 | OHWIo4i4 = 0x400400, |
| 334 | OHWIo4i4_bf16 = 0x400410, |
| 335 | OHWIo8i4 = 0x400800, |
| 336 | OHWIo8i4_bf16 = 0x400810, |
| 337 | OHWIo16i4 = 0x401000, |
| 338 | OHWIo16i4_bf16 = 0x401010, |
| 339 | OHWIo32i4 = 0x402000, |
| 340 | OHWIo32i4_bf16 = 0x402010, |
| 341 | OHWIo64i4 = 0x404000, |
| 342 | OHWIo64i4_bf16 = 0x404010, |
| 343 | OHWIo2i8 = 0x800200, |
| 344 | OHWIo4i8 = 0x800400, |
| 345 | OHWIo8i8 = 0x800800, |
| 346 | OHWIo16i8 = 0x801000, |
| 347 | OHWIo32i8 = 0x802000, |
| 348 | OHWIo64i8 = 0x804000 |
| 349 | }; |
| 350 | |
| 351 | } // namespace arm_compute |
| 352 | #endif /* ACL_ARM_COMPUTE_CORE_CORETYPES */ |