Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2023 Arm Limited. |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [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 | */ |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 24 | |
Georgios Pinitas | 9fc3be6 | 2021-05-29 04:01:51 +0100 | [diff] [blame] | 25 | #include "activation_float_helpers.h" |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 26 | #include "helpers.h" |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 27 | #include "helpers_asymm.h" |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 28 | #include "tile_helpers.h" |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 29 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 30 | //! @cond Doxygen_Suppress |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 31 | /** OpenCL kernel to compute the direct convolution. |
| 32 | * |
| 33 | * @note Data layout supported: NHWC |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 34 | * @note Data type supported: F32/F16/QASYMM8/QASYMM8_SIGNED |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 35 | * @note The accumulation data type must be passed at compile time using -DACC_DATA_TYPE (e.g. -DDATA_TYPE_PROMOTED=half) |
| 36 | * @note The convolution padding (left and top) must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (e.g. -DPAD_LEFT=2, -DPAD_TOP=2) |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 37 | * @note The convolution strides must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y (e.g. -DSTRIDE_X=2, -DSTRIDE_Y=2) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 38 | * @note The spatial dimensions of the weights must be passed at compile time using -DWEI_WIDTH and -DWEI_HEIGHT (e.g. -DWEI_WIDTH=9, -DWEI_HEIGHT=9) |
| 39 | * @note The spatial dimensions of the source tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT (e.g. -DSRC_WIDTH=96, -DSRC_HEIGHT=64) |
| 40 | * @note The spatial dimensions of the destination tensor must be passed at compile time using -DDST_WIDTH and -DDST_HEIGHT (e.g. -DDST_WIDTH=96, -DDST_HEIGHT=64) |
| 41 | * @note The channels of the source tensor must be passed at compile time using -DSRC_CHANNELS (e.g. -DSRC_CHANNELS=64) |
| 42 | * @note The channels of the destination tensor must be passed at compile time using -DDST_CHANNELS (e.g. -DDDST_CHANNELS=64) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 43 | * @note The tensor type ("BUFFER" or "IMAGE") of the source tensor must be passed at compile time using -DSRC_TENSOR_TYPE (e.g. -DSRC_TENSOR_TYPE=BUFFER) |
| 44 | * @note The tensor type ("BUFFER" or "IMAGE") of the weights tensor must be passed at compile time using -DWEI_TENSOR_TYPE (e.g. -DWEI_TENSOR_TYPE=BUFFER) |
| 45 | * @note The tensor type ("BUFFER" or "IMAGE") of the destination tensor must be passed at compile time using -DDST_TENSOR_TYPE (e.g. -DDST_TENSOR_TYPE=BUFFER) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 46 | * @note The data type of the source tensor must be passed at compile time using -DSRC_DATA_TYPE (e.g. -DSRC_DATA_TYPE=float) |
| 47 | * @note The data type of the weights tensor must be passed at compile time using -DWEI_DATA_TYPE (e.g. -DWEI_DATA_TYPE=float) |
| 48 | * @note The data type of the destination tensor must be passed at compile time using -DDST_DATA_TYPE (e.g. -DDST_DATA_TYPE=float) |
| 49 | * @note The data type of the accumulators must be passed at compile time using -DACC_DATA_TYPE (e.g. -DACC_DATA_TYPE=float) |
| 50 | * @note The number of M0 rows (width*height) to process must be passed at compile time using -DM0 (e.g. -DM0=2) |
| 51 | * @note The number of N0 output channels to process must be passed at compile time using -DN0 (e.g. -DN0=2) |
| 52 | * @note The number of K0 inner accumulations must be passed at compile time using -DK0 (e.g. -DK0=2) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 53 | * @note The size of the partial store block in x must be passed at compile time using -DPARTIAL_N0 (e.g. -DPARTIAL_N0=1) |
| 54 | * @note The zero value must be passed at compile time using -DZERO_VALUE (e.g. -DZERO_VALUE=0) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 55 | * @note Only the following configurations of M0, N0 and K0 are currently supported: |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 56 | * - M0 = 1, 2, 3, 4, 5, 6, 7, and 8 |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 57 | * - N0 = 2, 3, 4, 8, 16 |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 58 | * - K0 = 2, 3, 4, 8, 16 (only 4, 8 and 16 if WEI_TENSOR_TYPE=IMAGE) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 59 | * |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 60 | *@note In case of QASYMM8/QASYMM8_SIGNED, the following extra information must be passed at compile time: |
| 61 | * - -DIS_QUANTIZED |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 62 | * - The destination quantization multiplier e.g. -DDST_MULTIPLIER=1234 |
| 63 | * - The destination quantization shift e.g. -DDST_SHIFT=4 |
| 64 | * - The destination offset e.g. -DDST_OFFSET=4 |
| 65 | * - The source offset e.g. -DSRC_OFFSET=4 |
| 66 | * - The weights offset e.g. -DWEI_OFFSET=4 |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 67 | * - The quantized zero value e.g. -DZERO_VALUE=4 |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 68 | * |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 69 | * @param[in] src_img (Not supported) Read only cl_image object for the source tensor. Included when SRC_TENSOR_TYPE=IMAGE |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 70 | * @param[in] src_ptr Pointer to the source tensor. Supported data type: F16/F32/QASYMM8 |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 71 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 72 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 73 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes) |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 74 | * @param[in] src_c The size of the channels dimension of the source tensor |
| 75 | * @param[in] src_w The size of the width dimension of the source tensor |
| 76 | * @param[in] src_h The size of the height dimension of the source tensor |
| 77 | * @param[in] src_n The size of the batches dimension of the source tensor |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 78 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 79 | * @param[out] dst_img (Not supported) Write only cl_image object for the destination tensor. Included when DST_TENSOR_TYPE=IMAGE |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 80 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data type: same as @p src_ptr |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 81 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 82 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 83 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes) |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 84 | * @param[in] dst_c The size of the channels dimension of the destination tensor |
| 85 | * @param[in] dst_w The size of the width dimension of the destination tensor |
| 86 | * @param[in] dst_h The size of the height dimension of the destination tensor |
| 87 | * @param[in] dst_n The size of the batches dimension of the destination tensor |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 88 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 89 | * @param[in] wei_img (Optional) Read only cl_image object for the weights tensor. Included when WEI_TENSOR_TYPE=IMAGE |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 90 | * @param[in] wei_ptr Pointer to the weights tensor. Supported data type: same as @p src_ptr |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 91 | * @param[in] wei_stride_y Stride of the weights tensor in Y dimension (in bytes) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 92 | * @param[in] wei_stride_z Stride of the weights tensor in Z dimension (in bytes) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 93 | * @param[in] wei_stride_w Stride of the weights tensor in W dimension (in bytes) |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 94 | * @param[in] wei_c The size of the channels dimension of the weights tensor |
| 95 | * @param[in] wei_w The size of the width dimension of the weights tensor |
| 96 | * @param[in] wei_h The size of the height dimension of the weights tensor |
| 97 | * @param[in] wei_n The size of the batches dimension of the weights tensor |
| 98 | * @param[in] wei_offset_first_element_in_bytes The offset of the first element in the weights matrix |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 99 | * @param[in] bia_ptr (Optional) Pointer to the bias tensor Supported data type: same as @p src_ptr (if F32/F16) or S32 (if QASYMM8/QASYMM8_SIGNED) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 100 | * @param[in] bia_stride_x (Optional) Stride of the bias tensor in X dimension (in bytes) |
| 101 | * @param[in] bia_step_x (Optional) bia_stride_x * number of elements along X processed per workitem(in bytes) |
| 102 | * @param[in] bia_offset_first_element_in_bytes (Optional) The offset of the first element in the bias matrix |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 103 | */ |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 104 | //! @endcond |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 105 | __kernel void direct_convolution_nhwc( |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 106 | TENSOR4D_RO_T(src, SRC_TENSOR_TYPE), |
| 107 | TENSOR4D_WO_T(dst, DST_TENSOR_TYPE), |
| 108 | TENSOR4D_RO_T(wei, WEI_TENSOR_TYPE) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 109 | #if defined(HAS_BIAS) |
Michele Di Giorgio | 97e2580 | 2021-03-25 12:37:45 +0000 | [diff] [blame] | 110 | , |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 111 | VECTOR_DECLARATION(bia) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 112 | #endif // defined(HAS_BIAS) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 113 | ) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 114 | { |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 115 | // All the tensor dimensions are passed at compile time. |
| 116 | // In case of dynamic tensor support, the following dimensions should be passed as function argument. |
| 117 | #define _IWEI_WIDTH WEI_WIDTH |
| 118 | #define _IWEI_HEIGHT WEI_HEIGHT |
Gian Marco Iodice | 3394f3e | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 119 | #define _ISRC_WIDTH SRC_WIDTH |
| 120 | #define _ISRC_HEIGHT SRC_HEIGHT |
| 121 | #define _ISRC_CHANNELS SRC_CHANNELS |
| 122 | #define _IDST_WIDTH DST_WIDTH |
| 123 | #define _IDST_HEIGHT DST_HEIGHT |
| 124 | #define _IDST_CHANNELS DST_CHANNELS |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 125 | #define _IY_MULTIPLIER (_IWEI_WIDTH * _IWEI_HEIGHT) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 126 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 127 | // If quantized, the output tile has to be quantized first before being stored to global memory |
| 128 | #if defined(IS_QUANTIZED) |
| 129 | #define _IOUTPUT_TILE cq |
| 130 | #else // defined(IS_QUANTIZED) |
| 131 | #define _IOUTPUT_TILE c |
| 132 | #endif // defined(IS_QUANTIZED) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 133 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 134 | const int cout = GET_SPATIAL_IDX(0, N0, PARTIAL_N0); // OFM |
| 135 | const int mout = GET_SPATIAL_IDX(1, M0, 0); // WIDTH x HEIGHT |
| 136 | const int bout = GET_SPATIAL_IDX(2, 1, 0); // BATCH SIZE IDX |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 137 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 138 | // .v = access the whole vector (OpenCL vector) |
| 139 | // .s[x] = access the vector element at position x (scalar access) |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 140 | TILE(int, 1, M0, xi); |
| 141 | TILE(int, 1, M0, yi); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 142 | |
| 143 | // Convert the linear index to coordinate |
Giorgio Arena | bdd16d1 | 2021-05-13 16:58:51 +0100 | [diff] [blame] | 144 | LOOP_UNROLLING(int, i, 0, 1, M0, |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 145 | { |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 146 | xi[0].s[i] = ((mout + i) % _IDST_WIDTH) * STRIDE_X; |
| 147 | yi[0].s[i] = ((mout + i) / _IDST_WIDTH) * STRIDE_Y; |
| 148 | xi[0].s[i] -= PAD_LEFT; |
| 149 | yi[0].s[i] -= PAD_TOP; |
Giorgio Arena | bdd16d1 | 2021-05-13 16:58:51 +0100 | [diff] [blame] | 150 | }) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 151 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 152 | // Initialize the accumulators |
Gian Marco Iodice | 598e3a8 | 2021-04-13 15:53:20 +0100 | [diff] [blame] | 153 | TILE(ACC_DATA_TYPE, M0, N0, c); |
| 154 | |
Giorgio Arena | bdd16d1 | 2021-05-13 16:58:51 +0100 | [diff] [blame] | 155 | LOOP_UNROLLING(int, i, 0, 1, M0, |
Gian Marco Iodice | 598e3a8 | 2021-04-13 15:53:20 +0100 | [diff] [blame] | 156 | { |
| 157 | c[i].v = 0; |
Giorgio Arena | bdd16d1 | 2021-05-13 16:58:51 +0100 | [diff] [blame] | 158 | }) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 159 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 160 | for(int i = 0; i < (_IWEI_WIDTH * _IWEI_HEIGHT); ++i) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 161 | { |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 162 | int xk = i % _IWEI_WIDTH; |
| 163 | int yk = i / _IWEI_WIDTH; |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 164 | |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 165 | TILE(int, 1, M0, my); |
Gian Marco Iodice | 3394f3e | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 166 | |
| 167 | LOOP_UNROLLING(int, i, 0, 1, M0, |
| 168 | { |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 169 | int x_s = xi[0].s[i] + xk; |
| 170 | int y_s = yi[0].s[i] + yk; |
| 171 | my[0].s[i] = x_s + y_s *_ISRC_WIDTH; |
| 172 | my[0].s[i] = my[0].s[i] + bout * (int)(_ISRC_WIDTH * _ISRC_HEIGHT); |
| 173 | my[0].s[i] = select(-1, my[0].s[i], x_s >= 0); |
| 174 | my[0].s[i] = select(-1, my[0].s[i], x_s < _ISRC_WIDTH); |
| 175 | my[0].s[i] = select(-1, my[0].s[i], y_s >= 0); |
| 176 | my[0].s[i] = select(-1, my[0].s[i], y_s < _ISRC_HEIGHT); |
Gian Marco Iodice | 3394f3e | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 177 | }) |
| 178 | |
| 179 | int ck = 0; |
| 180 | for(; ck <= (_ISRC_CHANNELS - K0); ck += K0) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 181 | { |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 182 | TILE(SRC_DATA_TYPE, M0, K0, a); |
| 183 | TILE(WEI_DATA_TYPE, N0, K0, b); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 184 | |
Adnan AlSinan | c6b7913 | 2022-07-21 11:06:05 +0100 | [diff] [blame] | 185 | // Initialize tiles |
Giorgio Arena | bdd16d1 | 2021-05-13 16:58:51 +0100 | [diff] [blame] | 186 | LOOP_UNROLLING(int, i, 0, 1, M0, |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 187 | { |
| 188 | a[i].v = ZERO_VALUE; |
Giorgio Arena | bdd16d1 | 2021-05-13 16:58:51 +0100 | [diff] [blame] | 189 | }) |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 190 | |
Gian Marco Iodice | 85260d8 | 2022-12-16 15:34:27 +0000 | [diff] [blame] | 191 | LOOP_UNROLLING(int, i, 0, 1, N0, |
| 192 | { |
| 193 | b[i].v = ZERO_VALUE; |
| 194 | }) |
| 195 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 196 | // Load tile from the src tensor |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 197 | T_LOAD2D_INDIRECT(SRC_DATA_TYPE, M0, K0, SRC_TENSOR_TYPE, src, ck, src_stride_y, my, a); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 198 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 199 | // Load tile from the weights tensor |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 200 | T_LOAD(WEI_DATA_TYPE, N0, K0, WEI_TENSOR_TYPE, wei, ck, cout * _IY_MULTIPLIER + i, _IY_MULTIPLIER, wei_stride_y, b); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 201 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 202 | // Compute the matrix multiplication between two tiles |
| 203 | T_MMUL(SRC_DATA_TYPE, WEI_DATA_TYPE, ACC_DATA_TYPE, M0, N0, K0, NT, T, a, b, c); |
| 204 | |
| 205 | // Apply the offset correction (correction usually needed for asymmetric quantized computation) |
| 206 | // The computation is not performed if both SRC_OFFSET and WEI_OFFSET are zero |
| 207 | T_OFFSET_CORRECTION(ACC_DATA_TYPE, M0, N0, K0, SRC_OFFSET, WEI_OFFSET, a, b, c); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 210 | // This #if directive should be removed in case of dynamic tensor support |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 211 | #if defined(LEFTOVER_LOOP) |
| 212 | // Left-over accumulations |
Gian Marco Iodice | 3394f3e | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 213 | for(; ck < _ISRC_CHANNELS; ++ck) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 214 | { |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 215 | TILE(SRC_DATA_TYPE, M0, 1, a); |
| 216 | TILE(WEI_DATA_TYPE, N0, 1, b); |
| 217 | |
Adnan AlSinan | c6b7913 | 2022-07-21 11:06:05 +0100 | [diff] [blame] | 218 | // Initialize tiles |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 219 | LOOP_UNROLLING(int, i, 0, 1, M0, |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 220 | { |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 221 | a[i].v = ZERO_VALUE; |
| 222 | }) |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 223 | |
Adnan AlSinan | c6b7913 | 2022-07-21 11:06:05 +0100 | [diff] [blame] | 224 | LOOP_UNROLLING(int, i, 0, 1, N0, |
| 225 | { |
| 226 | b[i].v = ZERO_VALUE; |
| 227 | }) |
| 228 | |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 229 | // Load tile from the src tensor |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 230 | T_LOAD2D_INDIRECT(SRC_DATA_TYPE, M0, 1, SRC_TENSOR_TYPE, src, ck, src_stride_y, my, a); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 231 | |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 232 | // Load tile from the weights tensor |
| 233 | // The T_LOAD for the left-over elements can only use BUFFER because we load one element per iteration |
| 234 | T_LOAD(WEI_DATA_TYPE, N0, 1, BUFFER, wei, ck, cout * _IY_MULTIPLIER + i, _IY_MULTIPLIER, wei_stride_y, b); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 235 | |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 236 | // Compute the matrix multiplication between two tiles |
| 237 | T_MMUL(SRC_DATA_TYPE, WEI_DATA_TYPE, ACC_DATA_TYPE, M0, N0, 1, NT, T, a, b, c); |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 238 | |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 239 | // Apply the offset correction (operation usually needed for asymmetric quantized computation) |
| 240 | // The computation is not performed if both SRC_OFFSET and WEI_OFFSET are zero |
| 241 | T_OFFSET_CORRECTION(ACC_DATA_TYPE, M0, N0, 1, SRC_OFFSET, WEI_OFFSET, a, b, c); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 242 | } |
Giorgio Arena | 8d07127 | 2021-12-07 13:49:10 +0000 | [diff] [blame] | 243 | #endif // defined(LEFTOVER_LOOP) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 246 | // Offset correction required for the quantized asymmetric computation |
| 247 | // The computation is not performed if both SRC_OFFSET and WEI_OFFSET are zero |
| 248 | T_ADD_CONSTANT(ACC_DATA_TYPE, M0, N0, c, (_IWEI_WIDTH * _IWEI_HEIGHT * _ISRC_CHANNELS * SRC_OFFSET * WEI_OFFSET), c); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 249 | |
| 250 | #if defined(HAS_BIAS) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 251 | TILE(BIA_DATA_TYPE, 1, N0, bias0); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 252 | |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 253 | T_LOAD(BIA_DATA_TYPE, 1, N0, BUFFER, bia, cout, 0, 1, 0, bias0); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 254 | |
| 255 | // c = c + bias[broadcasted] |
Michalis Spyrou | b1fcefd | 2022-06-15 19:02:28 +0100 | [diff] [blame] | 256 | T_ELTWISE_BROADCAST_ADD_X(ACC_DATA_TYPE, M0, N0, c, bias0, c); |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 257 | |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 258 | #endif // HAS_BIAS |
| 259 | |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 260 | #if defined(IS_QUANTIZED) |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 261 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 262 | TILE(DST_DATA_TYPE, M0, N0, cq); |
Gian Marco Iodice | ff1fe3e | 2021-01-02 09:58:51 +0000 | [diff] [blame] | 263 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 264 | // Quantize the tile |
| 265 | T_QUANTIZE8_ASYMMETRIC(ACC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, c, cq); |
Gian Marco Iodice | d95c3e8 | 2021-01-19 17:39:02 +0000 | [diff] [blame] | 266 | #endif // defined(IS_QUANTIZED) |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 267 | |
Georgios Pinitas | 9fc3be6 | 2021-05-29 04:01:51 +0100 | [diff] [blame] | 268 | // Apply activation |
| 269 | T_ACTIVATION(DST_DATA_TYPE, M0, N0, ACTIVATION_TYPE, A_VAL, B_VAL, _IOUTPUT_TILE, _IOUTPUT_TILE); |
| 270 | |
Gian Marco Iodice | 3394f3e | 2022-09-16 14:14:21 +0100 | [diff] [blame] | 271 | TILE(uint, M0, 1, dst_indirect_y); |
| 272 | |
| 273 | // Calculate the destination indirect Y |
| 274 | LOOP_UNROLLING(int, i, 0, 1, M0, |
| 275 | { |
| 276 | dst_indirect_y[i].v = (uint)min(mout + i, (int)(_IDST_WIDTH * _IDST_HEIGHT) - 1); |
| 277 | dst_indirect_y[i].v += bout * (int)(_IDST_WIDTH * _IDST_HEIGHT); |
| 278 | }) |
| 279 | |
| 280 | bool x_cond = PARTIAL_N0 != 0 && get_global_id(0) == 0; |
| 281 | |
Gian Marco Iodice | 5c9eed8 | 2021-03-19 11:26:20 +0000 | [diff] [blame] | 282 | // _IOUTPUT_TILE: c = fp32/fp16, cq=qasymm8 |
| 283 | // Store the tile in reverse order so the invalid values are overwritten with the valid ones |
| 284 | T_STORE_INDIRECT_WIDTH_SELECT(DST_DATA_TYPE, M0, N0, PARTIAL_N0, DST_TENSOR_TYPE, dst, cout, dst_stride_y, x_cond, _IOUTPUT_TILE, dst_indirect_y); |
| 285 | |
| 286 | #undef _IWEI_WIDTH |
| 287 | #undef _IWEI_HEIGHT |
| 288 | #undef _ISRC_WIDTH |
| 289 | #undef _ISRC_HEIGHT |
| 290 | #undef _ISRC_CHANNELS |
| 291 | #undef _IDST_WIDTH |
| 292 | #undef _IDST_HEIGHT |
| 293 | #undef _IDST_CHANNELS |
Gian Marco Iodice | 0b76f7d | 2021-04-08 17:20:00 +0100 | [diff] [blame] | 294 | #undef _IY_MULTIPLIER |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 295 | } |