Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 1 | /* |
Giorgio Arena | 8bcb65f | 2021-01-12 11:29:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [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 | */ |
| 24 | |
| 25 | #include "helpers_asymm.h" |
| 26 | |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 27 | #ifndef VEC_SIZE |
| 28 | #if defined(N0) |
| 29 | #define VEC_SIZE N0 |
| 30 | #else /* defined(N0) */ |
| 31 | #define VEC_SIZE 8 |
| 32 | #endif /* defined(N0) */ |
| 33 | #endif /* VEC_SIZE */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 34 | |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 35 | #if defined(ACTIVATION_TYPE) && defined(CONST_0) |
Manuel Bottini | 30dbeef | 2019-06-26 16:23:03 +0100 | [diff] [blame] | 36 | #include "activation_layer_quant.cl" |
| 37 | #define ACTIVATION_FUNC(x) PERFORM_ACTIVATION_QUANT(ACTIVATION_TYPE, x) |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 38 | #else /* defined(ACTIVATION_TYPE) && defined(CONST_0) */ |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 39 | #define ACTIVATION_FUNC(x) (x) |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 40 | #endif /* defined(ACTIVATION_TYPE) && defined(CONST_0) */ |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 41 | |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 42 | #define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE) |
| 43 | #define VEC_FLOAT VEC_DATA_TYPE(float, VEC_SIZE) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 44 | #define VEC_SHORT VEC_DATA_TYPE(short, VEC_SIZE) |
| 45 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 46 | #if defined(DATA_TYPE) && defined(WEIGHTS_TYPE) |
| 47 | |
| 48 | #define VEC_TYPE(size) VEC_DATA_TYPE(DATA_TYPE, size) |
| 49 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 50 | #if defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && ((defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)) || defined(REAL_MULTIPLIER)) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 51 | |
| 52 | #if defined(WEIGHTS_PROMOTED_TYPE) |
| 53 | #define VEC_WEIGHTS_PROMOTED_TYPE(size) VEC_DATA_TYPE(WEIGHTS_PROMOTED_TYPE, size) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 54 | |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 55 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
| 56 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 57 | #define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), val); |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 58 | #else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 59 | #define ARM_DOT(x, y, val) val += arm_dot((x), (y)); |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 60 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) |
| 61 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 62 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 63 | #if defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) && defined(DEPTH_MULTIPLIER) && defined(DST_CHANNELS) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 64 | |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 65 | #if CONV_STRIDE_X > 3 |
| 66 | #error "Stride X not supported" |
| 67 | #endif /* CONV_STRIDE_X > 3 */ |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 68 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 69 | #if !defined(IS_DOT8) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 70 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 71 | #if DILATION_X == 1 |
| 72 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 73 | #if CONV_STRIDE_X == 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 74 | #define GET_VALUES(first_value, left, middle, right) \ |
| 75 | ({ \ |
| 76 | int8 temp0 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value)), int8); \ |
| 77 | int2 temp1 = CONVERT(vload2(0, (__global DATA_TYPE *)(first_value + 8 * sizeof(DATA_TYPE))), int2); \ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 78 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 79 | left = CONVERT(temp0.s01234567, int8); \ |
| 80 | middle = CONVERT((int8)(temp0.s1234, temp0.s567, temp1.s0), int8); \ |
| 81 | right = CONVERT((int8)(temp0.s2345, temp0.s67, temp1.s01), int8); \ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 82 | }) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 83 | #elif CONV_STRIDE_X == 2 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 84 | #define GET_VALUES(first_value, left, middle, right) \ |
| 85 | ({ \ |
| 86 | int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \ |
| 87 | int temp1 = CONVERT(*((__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))), int); \ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 88 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 89 | left = CONVERT(temp0.s02468ace, int8); \ |
| 90 | middle = CONVERT(temp0.s13579bdf, int8); \ |
| 91 | right = CONVERT((int8)(temp0.s2468, temp0.sace, temp1), int8); \ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 92 | }) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 93 | #else /* CONV_STRIDE_X */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 94 | #define GET_VALUES(first_value, left, middle, right) \ |
| 95 | ({ \ |
| 96 | int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \ |
| 97 | int8 temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))), int8); \ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 98 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 99 | left = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \ |
| 100 | middle = CONVERT((int8)(temp0.s147a, temp0.sd, temp1.s036), int8); \ |
| 101 | right = CONVERT((int8)(temp0.s258b, temp0.se, temp1.s147), int8); \ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 102 | }) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 103 | #endif /* CONV_STRIDE_X */ |
| 104 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 105 | #else /* DILATION_X == 1 */ |
| 106 | |
| 107 | #if CONV_STRIDE_X == 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 108 | #define GET_VALUES(first_value, left, middle, right) \ |
| 109 | ({ \ |
| 110 | left = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value)), int8); \ |
| 111 | middle = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))), int8); \ |
| 112 | right = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))), int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 113 | }) |
| 114 | #elif CONV_STRIDE_X == 2 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 115 | #define GET_VALUES(first_value, left, middle, right) \ |
| 116 | ({ \ |
| 117 | int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \ |
| 118 | left = CONVERT(temp0.s02468ace, int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 119 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 120 | temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))), int16); \ |
| 121 | middle = CONVERT(temp0.s02468ace, int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 122 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 123 | temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))), int16); \ |
| 124 | right = CONVERT(temp0.s02468ace, int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 125 | }) |
| 126 | #else /* CONV_STRIDE_X */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 127 | #define GET_VALUES(first_value, left, middle, right) \ |
| 128 | ({ \ |
| 129 | int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \ |
| 130 | int8 temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))), int8); \ |
| 131 | left = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 132 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 133 | temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))), int16); \ |
| 134 | temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + (16 + DILATION_X) * sizeof(DATA_TYPE))), int8); \ |
| 135 | middle = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 136 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 137 | temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))), int16); \ |
| 138 | temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + (16 + 2 * DILATION_X) * sizeof(DATA_TYPE))), int8); \ |
| 139 | right = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 140 | }) |
| 141 | |
| 142 | #endif /* CONV_STRIDE_X */ |
| 143 | #endif /* DILATION_X==1 */ |
| 144 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 145 | /** This function computes the depthwise convolution quantized. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 146 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 147 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 148 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 149 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 150 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 151 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 152 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 153 | * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes) |
| 154 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 155 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 156 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 157 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 158 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 159 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 160 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 161 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 162 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 163 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 164 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 165 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 166 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 167 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 168 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 169 | * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes) |
| 170 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 171 | * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32 |
| 172 | * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes) |
| 173 | * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 174 | * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector |
| 175 | * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32 |
| 176 | * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes) |
| 177 | * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 178 | * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector |
| 179 | * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32 |
| 180 | * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) |
| 181 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 182 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 183 | */ |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 184 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 185 | __kernel void dwc_3x3_native_quantized8_nchw( |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 186 | TENSOR3D_DECLARATION(src), |
| 187 | TENSOR3D_DECLARATION(dst), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 188 | TENSOR3D_DECLARATION(weights), |
| 189 | VECTOR_DECLARATION(output_multipliers), |
| 190 | VECTOR_DECLARATION(output_shifts) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 191 | #if defined(HAS_BIAS) |
Georgios Pinitas | 5b2191e | 2018-02-22 12:56:51 +0000 | [diff] [blame] | 192 | , |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 193 | VECTOR_DECLARATION(biases) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 194 | #endif //defined(HAS_BIAS) |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 195 | ) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 196 | { |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 197 | __global uchar *src_addr = src_ptr + get_global_id(0) * src_step_x + get_global_id(1) * src_step_y + get_global_id(2) * src_step_z; |
| 198 | Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst); |
| 199 | Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights); |
| 200 | Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_multipliers); |
| 201 | Vector output_shifts = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_shifts); |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 202 | |
| 203 | // Extract channel and linearized batch indices |
| 204 | const int channel = get_global_id(2) % DST_CHANNELS; |
| 205 | const int batch = get_global_id(2) / DST_CHANNELS; |
| 206 | |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 207 | #if defined(HAS_BIAS) |
| 208 | Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 209 | |
Georgios Pinitas | 728d3cf | 2018-09-21 13:41:35 +0100 | [diff] [blame] | 210 | int bias_value = *((__global int *)(vector_offset(&biases, channel))); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 211 | #endif //defined(HAS_BIAS) |
| 212 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 213 | // Load relevant input and weights data (Accounts depth multiplier when indexing input, OFM = IFM * DEPTH_MULTIPLIER) |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 214 | src_addr -= batch * (DST_CHANNELS / DEPTH_MULTIPLIER) * (DEPTH_MULTIPLIER - 1) * src_step_z + (channel - (channel / DEPTH_MULTIPLIER)) * src_step_z; |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 215 | __global uchar *weights_addr = weights.ptr + get_global_id(0) * weights_step_x + get_global_id(1) * weights_step_y + channel * weights_step_z; |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 216 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 217 | VEC_DATA_TYPE(WEIGHTS_TYPE, 3) |
| 218 | w0 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 0 * weights_stride_y)); |
| 219 | VEC_DATA_TYPE(WEIGHTS_TYPE, 3) |
| 220 | w1 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 1 * weights_stride_y)); |
| 221 | VEC_DATA_TYPE(WEIGHTS_TYPE, 3) |
| 222 | w2 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * weights_stride_y)); |
| 223 | |
| 224 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 225 | const int output_multiplier = *((__global int *)vector_offset(&output_multipliers, channel)); |
| 226 | const int output_shift = *((__global int *)vector_offset(&output_shifts, channel)); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 227 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 228 | |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 229 | int8 values0 = 0; |
| 230 | int8 sum0 = 0; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 231 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 232 | int8 values1 = 0; |
| 233 | int8 sum1 = 0; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 234 | #endif /* CONV_STRIDE_Y &&DILATION_Y==1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 235 | |
| 236 | // Row0 |
| 237 | int8 left, middle, right; |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 238 | GET_VALUES(src_addr + 0 * src_stride_y, left, middle, right); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 239 | values0 += left * (int8)(w0.s0); |
| 240 | values0 += middle * (int8)(w0.s1); |
| 241 | values0 += right * (int8)(w0.s2); |
| 242 | |
| 243 | #if WEIGHTS_OFFSET != 0 |
| 244 | sum0 += left + middle + right; |
| 245 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 246 | |
| 247 | // Row1 |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 248 | GET_VALUES(src_addr + DILATION_Y * src_stride_y, left, middle, right); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 249 | values0 += left * (int8)(w1.s0); |
| 250 | values0 += middle * (int8)(w1.s1); |
| 251 | values0 += right * (int8)(w1.s2); |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 252 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 253 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 254 | values1 += left * (int8)(w0.s0); |
| 255 | values1 += middle * (int8)(w0.s1); |
| 256 | values1 += right * (int8)(w0.s2); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 257 | #endif /* CONV_STRIDE_Y && DILATION_Y== 1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 258 | |
| 259 | #if WEIGHTS_OFFSET != 0 |
| 260 | int8 tmp = left + middle + right; |
| 261 | sum0 += tmp; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 262 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 263 | sum1 += tmp; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 264 | #endif /* CONV_STRIDE_Y &&DILATION_Y== 1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 265 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 266 | |
| 267 | // Row2 |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 268 | GET_VALUES(src_addr + 2 * DILATION_Y * src_stride_y, left, middle, right); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 269 | values0 += left * (int8)(w2.s0); |
| 270 | values0 += middle * (int8)(w2.s1); |
| 271 | values0 += right * (int8)(w2.s2); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 272 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 273 | values1 += left * (int8)(w1.s0); |
| 274 | values1 += middle * (int8)(w1.s1); |
| 275 | values1 += right * (int8)(w1.s2); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 276 | #endif /* CONV_STRIDE_Y &&DILATION_Y == 1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 277 | |
| 278 | #if WEIGHTS_OFFSET != 0 |
| 279 | tmp = left + middle + right; |
| 280 | sum0 += tmp; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 281 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 282 | sum1 += tmp; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 283 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 284 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 285 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 286 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 287 | // Row3 |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 288 | GET_VALUES(src_addr + 3 * src_stride_y, left, middle, right); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 289 | values1 += left * (int8)(w2.s0); |
| 290 | values1 += middle * (int8)(w2.s1); |
| 291 | values1 += right * (int8)(w2.s2); |
| 292 | |
| 293 | #if WEIGHTS_OFFSET != 0 |
| 294 | sum1 += left + middle + right; |
| 295 | #endif /* WEIGHTS_OFFSET != 0 */ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 296 | #endif /* CONV_STRIDE_Y && DILATION_Y == 1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 297 | |
| 298 | #if defined(HAS_BIAS) |
| 299 | values0 += (int8)(bias_value); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 300 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 301 | values1 += (int8)(bias_value); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 302 | #endif /* CONV_STRIDE_Y & &DILATION_Y == 1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 303 | #endif //defined(HAS_BIAS) |
| 304 | |
| 305 | #if WEIGHTS_OFFSET != 0 |
| 306 | values0 += sum0 * (int8)(WEIGHTS_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 307 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 308 | values1 += sum1 * (int8)(WEIGHTS_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 309 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 310 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 311 | |
| 312 | #if INPUT_OFFSET != 0 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 313 | VEC_WEIGHTS_PROMOTED_TYPE(3) |
| 314 | tmp_we = CONVERT(w0, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w1, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w2, VEC_WEIGHTS_PROMOTED_TYPE(3)); |
| 315 | |
| 316 | WEIGHTS_PROMOTED_TYPE sum_weights = tmp_we.s0 + tmp_we.s1 + tmp_we.s2; |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 317 | values0 += sum_weights * (int8)(INPUT_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 318 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 319 | values1 += sum_weights * (int8)(INPUT_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 320 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 321 | #endif /* INPUT_OFFSET != 0 */ |
| 322 | |
| 323 | #if K_OFFSET != 0 |
| 324 | values0 += (int8)(K_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 325 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 326 | values1 += (int8)(K_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 327 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/ |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 328 | #endif /* K_OFFSET != 0 */ |
| 329 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 330 | #if defined(REAL_MULTIPLIER) |
| 331 | |
| 332 | values0 = CONVERT(round(CONVERT(values0, float8) * (float8)REAL_MULTIPLIER), int8); |
| 333 | |
| 334 | #else // defined(REAL_MULTIPLIER) |
| 335 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 336 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 337 | int8 res0_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, output_multiplier, output_shift, 8); |
| 338 | int8 res0_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, output_multiplier, output_shift, 8); |
| 339 | values0 = select(res0_shift_lt0, res0_shift_gt0, (int8)(output_shift) >= 0); |
| 340 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 341 | #if OUTPUT_SHIFT < 0 |
| 342 | values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 343 | #else // OUTPUT_SHIFT < 0 |
| 344 | values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 345 | #endif // OUTPUT_OFFSET < 0 |
| 346 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 347 | |
| 348 | #endif // defined(REAL_MULTIPLIER) |
| 349 | |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 350 | values0 += (int8)OUTPUT_OFFSET; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 351 | VEC_TYPE(8) |
| 352 | res0 = CONVERT_SAT(values0, VEC_TYPE(8)); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 353 | |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 354 | vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 355 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 356 | #if defined(REAL_MULTIPLIER) |
| 357 | |
| 358 | values1 = CONVERT(round(CONVERT(values1, float8) * (float8)REAL_MULTIPLIER), int8); |
| 359 | |
| 360 | #else // defined(REAL_MULTIPLIER) |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 361 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 362 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 363 | int8 res1_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, output_multiplier, output_shift, 8); |
| 364 | int8 res1_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, output_multiplier, output_shift, 8); |
| 365 | values1 = select(res1_shift_lt0, res1_shift_gt0, (int8)(output_shift) >= 0); |
| 366 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 367 | #if OUTPUT_SHIFT < 0 |
| 368 | values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 369 | #else // OUTPUT_SHIFT < 0 |
| 370 | values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 371 | #endif // OUTPUT_OFFSET < 0 |
| 372 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 373 | |
| 374 | #endif // defined(REAL_MULTIPLIER) |
| 375 | |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 376 | values1 += (int8)OUTPUT_OFFSET; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 377 | VEC_TYPE(8) |
| 378 | res1 = CONVERT_SAT(values1, VEC_TYPE(8)); |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 379 | |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 380 | vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 381 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/ |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 382 | } |
Giorgio Arena | 287b570 | 2018-02-16 11:01:04 +0000 | [diff] [blame] | 383 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 384 | #else // !defined(IS_DOT8) |
| 385 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 386 | #if DILATION_X == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 387 | #if CONV_STRIDE_X == 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 388 | #define GET_VALUES(first_value, left, middle, right) \ |
| 389 | ({ \ |
| 390 | VEC_TYPE(8) \ |
| 391 | temp0 = vload8(0, (__global DATA_TYPE *)(first_value)); \ |
| 392 | VEC_TYPE(2) \ |
| 393 | temp1 = vload2(0, (__global DATA_TYPE *)(first_value + 8 * sizeof(DATA_TYPE))); \ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 394 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 395 | left = temp0.s01234567; \ |
| 396 | middle = (VEC_TYPE(8))(temp0.s1234, temp0.s567, temp1.s0); \ |
| 397 | right = (VEC_TYPE(8))(temp0.s2345, temp0.s67, temp1.s01); \ |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 398 | }) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 399 | #elif CONV_STRIDE_X == 2 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 400 | #define GET_VALUES(first_value, left, middle, right) \ |
| 401 | ({ \ |
| 402 | VEC_TYPE(16) \ |
| 403 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \ |
| 404 | DATA_TYPE temp1 = *((__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))); \ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 405 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 406 | left = temp0.s02468ace; \ |
| 407 | middle = temp0.s13579bdf; \ |
| 408 | right = (VEC_TYPE(8))(temp0.s2468, temp0.sace, temp1); \ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 409 | }) |
| 410 | #else /* CONV_STRIDE_X */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 411 | #define GET_VALUES(first_value, left, middle, right) \ |
| 412 | ({ \ |
| 413 | VEC_TYPE(16) \ |
| 414 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \ |
| 415 | VEC_TYPE(8) \ |
| 416 | temp1 = vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))); \ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 417 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 418 | left = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \ |
| 419 | middle = (VEC_TYPE(8))(temp0.s147a, temp0.sd, temp1.s036); \ |
| 420 | right = (VEC_TYPE(8))(temp0.s258b, temp0.se, temp1.s147); \ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 421 | }) |
| 422 | #endif /* CONV_STRIDE_X */ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 423 | #else /*DILATION_X==1*/ |
| 424 | |
| 425 | #if CONV_STRIDE_X == 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 426 | #define GET_VALUES(first_value, left, middle, right) \ |
| 427 | ({ \ |
| 428 | left = vload8(0, (__global DATA_TYPE *)(first_value)); \ |
| 429 | middle = vload8(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \ |
| 430 | right = vload8(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 431 | }) |
| 432 | #elif CONV_STRIDE_X == 2 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 433 | #define GET_VALUES(first_value, left, middle, right) \ |
| 434 | ({ \ |
| 435 | VEC_TYPE(16) \ |
| 436 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \ |
| 437 | left = temp0.s02468ace; \ |
| 438 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \ |
| 439 | middle = temp0.s02468ace; \ |
| 440 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \ |
| 441 | right = temp0.s02468ace; \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 442 | }) |
| 443 | #else /* CONV_STRIDE_X */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 444 | #define GET_VALUES(first_value, left, middle, right) \ |
| 445 | ({ \ |
| 446 | VEC_TYPE(16) \ |
| 447 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \ |
| 448 | VEC_TYPE(8) \ |
| 449 | temp1 = vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE)))); \ |
| 450 | left = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 451 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 452 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \ |
| 453 | temp1 = vload8(0, (__global DATA_TYPE *)(first_value + (16 + DILATION_X) * sizeof(DATA_TYPE))); \ |
| 454 | middle = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 455 | \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 456 | temp0 = vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \ |
| 457 | temp1 = vload8(0, (__global DATA_TYPE *)(first_value + (16 + 2 * DILATION_X) * sizeof(DATA_TYPE))); \ |
| 458 | right = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 459 | }) |
| 460 | |
| 461 | #endif /* CONV_STRIDE_X */ |
| 462 | #endif /*DILATION_X==1*/ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 463 | /** This function computes the depthwise convolution quantized using dot product when the data layout is NCHW. |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 464 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 465 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 466 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 467 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 468 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 469 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 470 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 471 | * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes) |
| 472 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 473 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 474 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 475 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 476 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 477 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 478 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 479 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 480 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 481 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 482 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 483 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 484 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 485 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 486 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 487 | * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes) |
| 488 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 489 | * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32 |
| 490 | * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes) |
| 491 | * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 492 | * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector |
| 493 | * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32 |
| 494 | * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes) |
| 495 | * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 496 | * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector |
| 497 | * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32 |
| 498 | * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) |
| 499 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 500 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 501 | */ |
| 502 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 503 | __kernel void dwc_3x3_native_quantized8_dot8_nchw( |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 504 | TENSOR3D_DECLARATION(src), |
| 505 | TENSOR3D_DECLARATION(dst), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 506 | TENSOR3D_DECLARATION(weights), |
| 507 | VECTOR_DECLARATION(output_multipliers), |
| 508 | VECTOR_DECLARATION(output_shifts) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 509 | #if defined(HAS_BIAS) |
| 510 | , |
| 511 | VECTOR_DECLARATION(biases) |
| 512 | #endif //defined(HAS_BIAS) |
| 513 | ) |
| 514 | { |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 515 | __global uchar *src_addr = src_ptr + get_global_id(0) * src_step_x + get_global_id(1) * src_step_y + get_global_id(2) * src_step_z; |
| 516 | Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst); |
| 517 | Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights); |
| 518 | Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_multipliers); |
| 519 | Vector output_shifts = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_shifts); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 520 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 521 | // Extract channel and linearized batch indices |
| 522 | const int channel = get_global_id(2) % DST_CHANNELS; |
| 523 | const int batch = get_global_id(2) / DST_CHANNELS; |
| 524 | |
| 525 | #if defined(HAS_BIAS) |
| 526 | Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); |
| 527 | |
| 528 | const int bias_value = *((__global int *)(vector_offset(&biases, channel))); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 529 | #endif //defined(HAS_BIAS) |
| 530 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 531 | // Load relevant input and weights data (Accounts depth multiplier when indexing input, OFM = IFM * DEPTH_MULTIPLIER) |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 532 | src_addr -= batch * (DST_CHANNELS / DEPTH_MULTIPLIER) * (DEPTH_MULTIPLIER - 1) * src_step_z + (channel - (channel / DEPTH_MULTIPLIER)) * src_step_z; |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 533 | __global uchar *weights_addr = weights.ptr + get_global_id(0) * weights_step_x + get_global_id(1) * weights_step_y + channel * weights_step_z; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 534 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 535 | VEC_TYPE(3) |
| 536 | w0 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 0 * weights_stride_y)); |
| 537 | VEC_TYPE(3) |
| 538 | w1 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 1 * weights_stride_y)); |
| 539 | VEC_TYPE(3) |
| 540 | w2 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * weights_stride_y)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 541 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 542 | const int output_multiplier = *((__global int *)vector_offset(&output_multipliers, 0)); |
| 543 | const int output_shift = *((__global int *)vector_offset(&output_shifts, 0)); |
| 544 | |
| 545 | VEC_TYPE(8) |
| 546 | left0, middle0, right0; |
| 547 | VEC_TYPE(8) |
| 548 | left1, middle1, right1; |
| 549 | VEC_TYPE(8) |
| 550 | left2, middle2, right2; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 551 | |
| 552 | int8 values0 = 0; |
| 553 | int8 sum0 = 0; |
| 554 | |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 555 | GET_VALUES(src_addr + 0 * src_stride_y, left0, middle0, right0); |
| 556 | GET_VALUES(src_addr + DILATION_Y * src_stride_y, left1, middle1, right1); |
| 557 | GET_VALUES(src_addr + 2 * DILATION_Y * src_stride_y, left2, middle2, right2); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 558 | |
| 559 | #if WEIGHTS_OFFSET != 0 |
| 560 | sum0 += convert_int8(left0) + convert_int8(middle0) + convert_int8(right0); |
| 561 | sum0 += convert_int8(left1) + convert_int8(middle1) + convert_int8(right1); |
| 562 | sum0 += convert_int8(left2) + convert_int8(middle2) + convert_int8(right2); |
| 563 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 564 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 565 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 566 | // If conv_stride_y is equals to 1, we compute two output rows |
| 567 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 568 | VEC_TYPE(8) |
| 569 | left3, middle3, right3; |
| 570 | int8 values1 = 0; |
| 571 | int8 sum1 = 0; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 572 | |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 573 | GET_VALUES(src_addr + 3 * src_stride_y, left3, middle3, right3); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 574 | |
| 575 | #if WEIGHTS_OFFSET != 0 |
| 576 | sum1 += convert_int8(left1) + convert_int8(middle1) + convert_int8(right1); |
| 577 | sum1 += convert_int8(left2) + convert_int8(middle2) + convert_int8(right2); |
| 578 | sum1 += convert_int8(left3) + convert_int8(middle3) + convert_int8(right3); |
| 579 | #endif /* WEIGHTS_OFFSET != 0 */ |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 580 | #endif // CONV_STRIDE_Y == 1 && DILATION_Y==1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 581 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 582 | ARM_DOT((VEC_TYPE(4))(left0.s0, middle0.s0, right0.s0, left1.s0), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s0); |
| 583 | ARM_DOT((VEC_TYPE(4))(middle1.s0, right1.s0, left2.s0, middle2.s0), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s0); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 584 | values0.s0 += right2.s0 * w2.s2; |
| 585 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 586 | ARM_DOT((VEC_TYPE(4))(left0.s1, middle0.s1, right0.s1, left1.s1), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s1); |
| 587 | ARM_DOT((VEC_TYPE(4))(middle1.s1, right1.s1, left2.s1, middle2.s1), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s1); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 588 | values0.s1 += right2.s1 * w2.s2; |
| 589 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 590 | ARM_DOT((VEC_TYPE(4))(left0.s2, middle0.s2, right0.s2, left1.s2), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s2); |
| 591 | ARM_DOT((VEC_TYPE(4))(middle1.s2, right1.s2, left2.s2, middle2.s2), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s2); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 592 | values0.s2 += right2.s2 * w2.s2; |
| 593 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 594 | ARM_DOT((VEC_TYPE(4))(left0.s3, middle0.s3, right0.s3, left1.s3), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s3); |
| 595 | ARM_DOT((VEC_TYPE(4))(middle1.s3, right1.s3, left2.s3, middle2.s3), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s3); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 596 | values0.s3 += right2.s3 * w2.s2; |
| 597 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 598 | ARM_DOT((VEC_TYPE(4))(left0.s4, middle0.s4, right0.s4, left1.s4), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s4); |
| 599 | ARM_DOT((VEC_TYPE(4))(middle1.s4, right1.s4, left2.s4, middle2.s4), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s4); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 600 | values0.s4 += right2.s4 * w2.s2; |
| 601 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 602 | ARM_DOT((VEC_TYPE(4))(left0.s5, middle0.s5, right0.s5, left1.s5), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s5); |
| 603 | ARM_DOT((VEC_TYPE(4))(middle1.s5, right1.s5, left2.s5, middle2.s5), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s5); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 604 | values0.s5 += right2.s5 * w2.s2; |
| 605 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 606 | ARM_DOT((VEC_TYPE(4))(left0.s6, middle0.s6, right0.s6, left1.s6), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s6); |
| 607 | ARM_DOT((VEC_TYPE(4))(middle1.s6, right1.s6, left2.s6, middle2.s6), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s6); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 608 | values0.s6 += right2.s6 * w2.s2; |
| 609 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 610 | ARM_DOT((VEC_TYPE(4))(left0.s7, middle0.s7, right0.s7, left1.s7), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s7); |
| 611 | ARM_DOT((VEC_TYPE(4))(middle1.s7, right1.s7, left2.s7, middle2.s7), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s7); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 612 | values0.s7 += right2.s7 * w2.s2; |
| 613 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 614 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 615 | ARM_DOT((VEC_TYPE(4))(left1.s0, middle1.s0, right1.s0, left2.s0), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s0); |
| 616 | ARM_DOT((VEC_TYPE(4))(middle2.s0, right2.s0, left3.s0, middle3.s0), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s0); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 617 | values1.s0 += right3.s0 * w2.s2; |
| 618 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 619 | ARM_DOT((VEC_TYPE(4))(left1.s1, middle1.s1, right1.s1, left2.s1), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s1); |
| 620 | ARM_DOT((VEC_TYPE(4))(middle2.s1, right2.s1, left3.s1, middle3.s1), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s1); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 621 | values1.s1 += right3.s1 * w2.s2; |
| 622 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 623 | ARM_DOT((VEC_TYPE(4))(left1.s2, middle1.s2, right1.s2, left2.s2), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s2); |
| 624 | ARM_DOT((VEC_TYPE(4))(middle2.s2, right2.s2, left3.s2, middle3.s2), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s2); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 625 | values1.s2 += right3.s2 * w2.s2; |
| 626 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 627 | ARM_DOT((VEC_TYPE(4))(left1.s3, middle1.s3, right1.s3, left2.s3), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s3); |
| 628 | ARM_DOT((VEC_TYPE(4))(middle2.s3, right2.s3, left3.s3, middle3.s3), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s3); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 629 | values1.s3 += right3.s3 * w2.s2; |
| 630 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 631 | ARM_DOT((VEC_TYPE(4))(left1.s4, middle1.s4, right1.s4, left2.s4), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s4); |
| 632 | ARM_DOT((VEC_TYPE(4))(middle2.s4, right2.s4, left3.s4, middle3.s4), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s4); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 633 | values1.s4 += right3.s4 * w2.s2; |
| 634 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 635 | ARM_DOT((VEC_TYPE(4))(left1.s5, middle1.s5, right1.s5, left2.s5), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s5); |
| 636 | ARM_DOT((VEC_TYPE(4))(middle2.s5, right2.s5, left3.s5, middle3.s5), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s5); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 637 | values1.s5 += right3.s5 * w2.s2; |
| 638 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 639 | ARM_DOT((VEC_TYPE(4))(left1.s6, middle1.s6, right1.s6, left2.s6), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s6); |
| 640 | ARM_DOT((VEC_TYPE(4))(middle2.s6, right2.s6, left3.s6, middle3.s6), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s6); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 641 | values1.s6 += right3.s6 * w2.s2; |
| 642 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 643 | ARM_DOT((VEC_TYPE(4))(left1.s7, middle1.s7, right1.s7, left2.s7), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s7); |
| 644 | ARM_DOT((VEC_TYPE(4))(middle2.s7, right2.s7, left3.s7, middle3.s7), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s7); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 645 | values1.s7 += right3.s7 * w2.s2; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 646 | #endif // CONV_STRIDE_Y == 1 && DILATION_Y==1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 647 | |
| 648 | #if defined(HAS_BIAS) |
| 649 | values0 += (int8)(bias_value); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 650 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 651 | values1 += (int8)(bias_value); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 652 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 653 | #endif //defined(HAS_BIAS) |
| 654 | |
| 655 | #if WEIGHTS_OFFSET != 0 |
| 656 | values0 += sum0 * (int8)(WEIGHTS_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 657 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 658 | values1 += sum1 * (int8)(WEIGHTS_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 659 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 660 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 661 | |
| 662 | #if INPUT_OFFSET != 0 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 663 | WEIGHTS_PROMOTED_TYPE sum_weights = 0; |
| 664 | VEC_WEIGHTS_PROMOTED_TYPE(3) |
| 665 | tmp_we = CONVERT(w0, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w1, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w2, VEC_WEIGHTS_PROMOTED_TYPE(3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 666 | sum_weights += tmp_we.s0 + tmp_we.s1 + tmp_we.s2; |
| 667 | values0 += sum_weights * (int8)(INPUT_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 668 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 669 | values1 += sum_weights * (int8)(INPUT_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 670 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 671 | #endif /* INPUT_OFFSET != 0 */ |
| 672 | |
| 673 | #if K_OFFSET != 0 |
| 674 | values0 += (int8)(K_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 675 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 676 | values1 += (int8)(K_OFFSET); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 677 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 678 | #endif /* K_OFFSET != 0 */ |
| 679 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 680 | #if defined(REAL_MULTIPLIER) |
| 681 | |
| 682 | values0 = CONVERT(round(CONVERT(values0, float8) * (float8)REAL_MULTIPLIER), int8); |
| 683 | |
| 684 | #else // defined(REAL_MULTIPLIER) |
| 685 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 686 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 687 | int8 res0_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, output_multiplier, output_shift, 8); |
| 688 | int8 res0_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, output_multiplier, output_shift, 8); |
| 689 | values0 = select(res0_shift_lt0, res0_shift_gt0, (int8)(output_shift) >= 0); |
| 690 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 691 | #if OUTPUT_SHIFT < 0 |
| 692 | values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 693 | #else // OUTPUT_SHIFT < 0 |
| 694 | values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 695 | #endif // OUTPUT_OFFSET < 0 |
| 696 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 697 | |
| 698 | #endif // defined(REAL_MULTIPLIER) |
| 699 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 700 | values0 += (int8)OUTPUT_OFFSET; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 701 | VEC_TYPE(8) |
| 702 | res0 = CONVERT_SAT(values0, VEC_TYPE(8)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 703 | |
| 704 | vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 705 | #if CONV_STRIDE_Y == 1 && DILATION_Y == 1 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 706 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 707 | #if defined(REAL_MULTIPLIER) |
| 708 | |
| 709 | values1 = CONVERT(round(CONVERT(values1, float8) * (float8)REAL_MULTIPLIER), int8); |
| 710 | |
| 711 | #else // defined(REAL_MULTIPLIER) |
| 712 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 713 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 714 | int8 res1_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, output_multiplier, output_shift, 8); |
| 715 | int8 res1_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, output_multiplier, output_shift, 8); |
| 716 | values1 = select(res1_shift_lt0, res1_shift_gt0, (int8)(output_shift) >= 0); |
| 717 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 718 | #if OUTPUT_SHIFT < 0 |
| 719 | values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 720 | #else // OUTPUT_SHIFT < 0 |
| 721 | values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8); |
| 722 | #endif // OUTPUT_OFFSET < 0 |
| 723 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 724 | |
| 725 | #endif // defined(REAL_MULTIPLIER) |
| 726 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 727 | values1 += (int8)OUTPUT_OFFSET; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 728 | VEC_TYPE(8) |
| 729 | res1 = CONVERT_SAT(values1, VEC_TYPE(8)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 730 | |
| 731 | vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 732 | #endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 735 | #endif // !defined(IS_DOT8) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 736 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 737 | #endif /* defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) && defined(DEPTH_MULTIPLIER) && defined(DST_CHANNELS) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 738 | |
| 739 | #if defined(VEC_SIZE) && defined(SRC_DIM_1) && defined(SRC_DIM_2) && defined(CONV_PAD_TOP) && defined(CONV_PAD_LEFT) |
| 740 | |
| 741 | #define asymm_mult_by_quant_multiplier_less_than_one(x, y, z) ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, y, z, VEC_SIZE) |
| 742 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 743 | #define MULTIPLY_ADD(x, y, acc) acc += CONVERT(CONVERT(x, VEC_WEIGHTS_PROMOTED_TYPE(VEC_SIZE)) * CONVERT(y, VEC_WEIGHTS_PROMOTED_TYPE(VEC_SIZE)), VEC_INT) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 744 | |
| 745 | #if WEIGHTS_OFFSET != 0 |
| 746 | #define MULTIPLY_ADD_ACCUMULATE(x, y, acc, sum) \ |
| 747 | ({ \ |
| 748 | sum += CONVERT(x, VEC_INT); \ |
| 749 | MULTIPLY_ADD(x, y, acc); \ |
| 750 | }) |
| 751 | #else /* WEIGHTS_OFFSET != 0 */ |
| 752 | #define MULTIPLY_ADD_ACCUMULATE(x, y, acc, sum) MULTIPLY_ADD(x, y, acc) |
| 753 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 754 | |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 755 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 756 | #define DOT_PRODUCT(acc, val0, val1, val2, val3, val4, val5, val6, val7, val8, w0, w1) \ |
| 757 | ({ \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 758 | ARM_DOT((VEC_TYPE(4))(val0, val1, val2, val3), w0.s0123, acc); \ |
| 759 | ARM_DOT((VEC_TYPE(4))(val4, val5, val6, val7), w0.s4567, acc); \ |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 760 | acc += val8 * w1; \ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 761 | }) |
| 762 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 763 | #define DOT_PRODUCT_REDUCTION(sum, val0, val1, val2, val3, val4, val5, val6, val7, val8) \ |
| 764 | ({ \ |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 765 | sum = val0; \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 766 | ARM_DOT((VEC_TYPE(4))(val1, val2, val3, val4), (VEC_TYPE(4))1, sum); \ |
| 767 | ARM_DOT((VEC_TYPE(4))(val5, val6, val7, val8), (VEC_TYPE(4))1, sum); \ |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 768 | }) |
| 769 | |
| 770 | #define DOT_PRODUCT_REDUCTION_WEIGHTS(sum, w0, w1) \ |
| 771 | ({ \ |
| 772 | sum = w1; \ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 773 | ARM_DOT(w0.s0123, (VEC_TYPE(4))1, sum); \ |
| 774 | ARM_DOT(w0.s4567, (VEC_TYPE(4))1, sum); \ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 775 | }) |
| 776 | |
Georgios Pinitas | daa3855 | 2018-08-28 17:43:18 +0100 | [diff] [blame] | 777 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 778 | |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 779 | #if defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && VEC_SIZE == 4 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 780 | /** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width or height is not 1. |
| 781 | * |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 782 | * @note This kernel assumes VEC_SIZE is 4. |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 783 | * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel. |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 784 | * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2) |
| 785 | * @note Dimension two of the input tensor (height for NHWC data layout) must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM_2=112) |
| 786 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1) |
| 787 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1) |
| 788 | * @note The convolution stride along the width must be passed at compile time using -DCONV_STRIDE_X (e.g. -DCONV_STRIDE_Y=X) |
| 789 | * @note The convolution stride along the height must be passed at compile time using -DCONV_STRIDE_Y (e.g. -DCONV_STRIDE_Y=1) |
| 790 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 791 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 792 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 793 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 794 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 795 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 796 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 797 | * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes) |
| 798 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes) |
| 799 | * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes) |
| 800 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 801 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 802 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 803 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 804 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 805 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 806 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 807 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 808 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 809 | * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes) |
| 810 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 811 | * @param[in] weights_ptr Pointer to the weights tensor reshaped. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 812 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 813 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 814 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 815 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 816 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 817 | * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32 |
| 818 | * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes) |
| 819 | * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 820 | * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector |
| 821 | * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32 |
| 822 | * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes) |
| 823 | * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 824 | * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector |
| 825 | * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32 |
| 826 | * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) |
| 827 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 828 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector |
| 829 | * @param[in] max_offset Max offset for the input tensor |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 830 | */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 831 | __kernel void dwc_3x3_reshaped_quantized8_nhwc( |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 832 | TENSOR4D_DECLARATION(src), |
| 833 | TENSOR4D_DECLARATION(dst), |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 834 | IMAGE_DECLARATION(weights), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 835 | VECTOR_DECLARATION(output_multipliers), |
| 836 | VECTOR_DECLARATION(output_shifts), |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 837 | #if defined(HAS_BIAS) |
| 838 | VECTOR_DECLARATION(biases), |
| 839 | #endif /* defined(HAS_BIAS) */ |
| 840 | int max_offset) |
| 841 | { |
| 842 | const int x = get_global_id(0); // channels |
| 843 | const int y = get_global_id(1); // spatial coordinate x |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 844 | #if defined(DST_DEPTH) |
| 845 | int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y |
| 846 | int b = get_global_id(2) / (int)DST_DEPTH; // batch |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 847 | #else // defined(DST_DEPTH) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 848 | int z = get_global_id(2); // spatial coordinate y |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 849 | #endif // defined(DST_DEPTH) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 850 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 851 | __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 852 | |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 853 | #if defined(DST_DEPTH) |
| 854 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w; |
| 855 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 856 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE; |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 857 | #endif /* defined(DST_DEPTH) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 858 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 859 | int z_coord = 0; |
| 860 | int4 offset = 0; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 861 | int4 y_coord = ((int4)(y * CONV_STRIDE_X) + (int4)(0, DILATION_X * 1, DILATION_X * 2, DILATION_X * 3)) - (int)CONV_PAD_LEFT; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 862 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 863 | // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1 |
| 864 | y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1); |
| 865 | y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1); |
| 866 | y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1); |
| 867 | y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1); |
| 868 | |
| 869 | int4 y_offset = convert_int4(y_coord * (int)src_stride_y); |
| 870 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 871 | // We compute VEC_SIZEx1x1 [C,W,H] elements |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 872 | VEC_INT acc = 0, sum = 0; |
| 873 | |
| 874 | // Load weights |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 875 | VEC_DATA_TYPE(WEIGHTS_TYPE, 16) |
| 876 | w0_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr)); |
| 877 | VEC_DATA_TYPE(WEIGHTS_TYPE, 16) |
| 878 | w1_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16)); |
| 879 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 880 | w8 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * 16)); |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 881 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 882 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 883 | w0 = w0_tmp.s0123; |
| 884 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 885 | w1 = w0_tmp.s4567; |
| 886 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 887 | w2 = w0_tmp.s89AB; |
| 888 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 889 | w3 = w0_tmp.sCDEF; |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 890 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 891 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 892 | w4 = w1_tmp.s0123; |
| 893 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 894 | w5 = w1_tmp.s4567; |
| 895 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 896 | w6 = w1_tmp.s89AB; |
| 897 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 898 | w7 = w1_tmp.sCDEF; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 899 | |
| 900 | #if INPUT_OFFSET != 0 |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 901 | VEC_INT sum_we = CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT) |
| 902 | + CONVERT(w3, VEC_INT) + CONVERT(w4, VEC_INT) + CONVERT(w5, VEC_INT) |
| 903 | + CONVERT(w6, VEC_INT) + CONVERT(w7, VEC_INT) + CONVERT(w8, VEC_INT); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 904 | #endif /* INPUT_OFFSET != 0 */ |
| 905 | |
| 906 | // Load input values |
| 907 | // z == 0 |
| 908 | // Clamp z_coord as for z = 0, it can be negative |
| 909 | // z_coord is casted to unsigned int in order to use just a min() operation |
| 910 | // A "-1" 32 bit signed variable converted to unsigned gives 4294967295 |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 911 | z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 912 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
| 913 | offset = y_offset + (int4)(z_coord * src_stride_z); |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 914 | offset = min(offset, (int4)max_offset); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 915 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 916 | VEC_TYPE(VEC_SIZE) |
| 917 | values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 918 | VEC_TYPE(VEC_SIZE) |
| 919 | values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 920 | VEC_TYPE(VEC_SIZE) |
| 921 | values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 922 | |
| 923 | // z == 1 |
| 924 | // z_coord can be only negative for z = 0 so we do not need to clamp it |
| 925 | // Moreover z_coord cannot be out-of-bound for z = 1 so we do not need to clamp the offset |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 926 | z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP + DILATION_Y; |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 927 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 928 | offset = y_offset + (int4)(z_coord * src_stride_z); |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 929 | offset = min(offset, (int4)max_offset); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 930 | VEC_TYPE(VEC_SIZE) |
| 931 | values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 932 | VEC_TYPE(VEC_SIZE) |
| 933 | values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 934 | VEC_TYPE(VEC_SIZE) |
| 935 | values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 936 | |
| 937 | // z == 2 |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 938 | // Offset can be out-of-bound so we need to check if it is greater than max_offset |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 939 | z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP + DILATION_Y * 2; |
Giorgio Arena | 9f7d55a | 2021-02-08 13:20:24 +0000 | [diff] [blame^] | 940 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 941 | offset = y_offset + (int4)(z_coord * src_stride_z); |
| 942 | offset = min(offset, (int4)max_offset); |
| 943 | VEC_TYPE(VEC_SIZE) |
| 944 | values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 945 | VEC_TYPE(VEC_SIZE) |
| 946 | values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 947 | VEC_TYPE(VEC_SIZE) |
| 948 | values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 949 | |
| 950 | MULTIPLY_ADD_ACCUMULATE(values0, w0, acc, sum); |
| 951 | MULTIPLY_ADD_ACCUMULATE(values1, w1, acc, sum); |
| 952 | MULTIPLY_ADD_ACCUMULATE(values2, w2, acc, sum); |
| 953 | |
| 954 | MULTIPLY_ADD_ACCUMULATE(values3, w3, acc, sum); |
| 955 | MULTIPLY_ADD_ACCUMULATE(values4, w4, acc, sum); |
| 956 | MULTIPLY_ADD_ACCUMULATE(values5, w5, acc, sum); |
| 957 | |
| 958 | MULTIPLY_ADD_ACCUMULATE(values6, w6, acc, sum); |
| 959 | MULTIPLY_ADD_ACCUMULATE(values7, w7, acc, sum); |
| 960 | MULTIPLY_ADD_ACCUMULATE(values8, w8, acc, sum); |
| 961 | |
| 962 | #if defined(HAS_BIAS) |
| 963 | Vector biases = CONVERT_TO_VECTOR_STRUCT(biases); |
| 964 | VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr); |
| 965 | acc += bias_values; |
| 966 | #endif // defined(HAS_BIAS) |
| 967 | |
| 968 | #if WEIGHTS_OFFSET != 0 |
| 969 | acc += WEIGHTS_OFFSET * sum; |
| 970 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 971 | |
| 972 | #if INPUT_OFFSET != 0 |
| 973 | acc += INPUT_OFFSET * sum_we; |
| 974 | #endif /* INPUT_OFFSET != 0 */ |
| 975 | |
| 976 | #if K_OFFSET != 0 |
| 977 | acc += (VEC_INT)K_OFFSET; |
| 978 | #endif /* K_OFFSET != 0 */ |
| 979 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 980 | #if defined(REAL_MULTIPLIER) |
| 981 | |
| 982 | acc = CONVERT(round(CONVERT(acc, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 983 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 984 | #else // defined(REAL_MULTIPLIER) |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 985 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 986 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 987 | Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT(output_multipliers); |
| 988 | Vector output_shifts = CONVERT_TO_VECTOR_STRUCT(output_shifts); |
| 989 | VEC_INT output_multiplier = VLOAD(VEC_SIZE)(0, (__global int *)output_multipliers.ptr); |
| 990 | VEC_INT output_shift = VLOAD(VEC_SIZE)(0, (__global int *)output_shifts.ptr); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 991 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 992 | VEC_INT res_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc, output_multiplier, output_shift, VEC_SIZE); |
| 993 | VEC_INT res_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc, output_multiplier, output_shift); |
| 994 | acc = select(res_shift_lt0, res_shift_gt0, output_shift >= 0); |
| 995 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 996 | #if OUTPUT_SHIFT < 0 |
| 997 | acc = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
| 998 | #else // OUTPUT_SHIFT < 0 |
| 999 | acc = asymm_mult_by_quant_multiplier_less_than_one(acc, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
| 1000 | #endif // OUTPUT_SHIFT < 0 |
| 1001 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1002 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1003 | #endif // defined(REAL_MULTIPLIER) |
| 1004 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1005 | acc += (VEC_INT)OUTPUT_OFFSET; |
| 1006 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1007 | VEC_TYPE(VEC_SIZE) |
| 1008 | res = CONVERT_SAT(acc, VEC_TYPE(VEC_SIZE)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1009 | |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1010 | #if defined(DST_DEPTH) |
| 1011 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z + b * dst_stride_w; |
| 1012 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1013 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z; |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1014 | #endif /* defined(DST_DEPTH) */ |
| 1015 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1016 | VSTORE(VEC_SIZE) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1017 | (ACTIVATION_FUNC(res), 0, (__global DATA_TYPE *)(dst_addr)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1018 | } |
| 1019 | #endif // defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) |
| 1020 | |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1021 | #if defined(NUM_ROWS_PROCESSED) && defined(NUM_PLANES_PROCESSED) && VEC_SIZE == 4 |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1022 | /** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width and height is 1. |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1023 | * |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1024 | * @note This kernel assumes VEC_SIZE is 4. |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1025 | * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel. |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1026 | * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2) |
| 1027 | * @note Dimension two of the input tensor (height for NHWC data layout) must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM_2=112) |
| 1028 | * @note The number of rows processed per thread must be passed at compile time using -DNUM_ROWS_PROCESSED (i.e. -DNUM_ROWS_PROCESSED=2) |
| 1029 | * @note The number of planes processed per thread must be passed at compile time using -DNUM_PLANES_PROCESSED (i.e. -DNUM_PLANES_PROCESSED=2) |
| 1030 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1) |
| 1031 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1). |
| 1032 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 1033 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1034 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1035 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1036 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1037 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1038 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1039 | * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes) |
| 1040 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes) |
| 1041 | * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes) |
| 1042 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1043 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 1044 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1045 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 1046 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1047 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1048 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 1049 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 1050 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 1051 | * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes) |
| 1052 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 1053 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1054 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 1055 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 1056 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 1057 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1058 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 1059 | * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32 |
| 1060 | * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes) |
| 1061 | * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 1062 | * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector |
| 1063 | * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32 |
| 1064 | * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes) |
| 1065 | * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 1066 | * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector |
| 1067 | * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32 |
| 1068 | * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) |
| 1069 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1070 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector |
| 1071 | * @param[in] max_offset Max offset for the input tensor |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1072 | */ |
| 1073 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1074 | __kernel void dwc_3x3_reshaped_quantized8_stride1_nhwc( |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1075 | TENSOR4D_DECLARATION(src), |
| 1076 | TENSOR4D_DECLARATION(dst), |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1077 | IMAGE_DECLARATION(weights), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1078 | VECTOR_DECLARATION(output_multipliers), |
| 1079 | VECTOR_DECLARATION(output_shifts), |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1080 | #if defined(HAS_BIAS) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1081 | VECTOR_DECLARATION(biases), |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1082 | #endif /* defined(HAS_BIAS) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1083 | int max_offset) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1084 | { |
Giorgio Arena | fa23f11 | 2018-06-19 11:27:38 +0100 | [diff] [blame] | 1085 | int x = get_global_id(0); |
| 1086 | int y = get_global_id(1); |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1087 | #if defined(DST_DEPTH) |
| 1088 | int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y |
| 1089 | int b = get_global_id(2) / (int)DST_DEPTH; // batch |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1090 | #else // defined(DST_DEPTH) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1091 | int z = get_global_id(2); // spatial coordinate y |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1092 | #endif // defined(DST_DEPTH) |
Giorgio Arena | fa23f11 | 2018-06-19 11:27:38 +0100 | [diff] [blame] | 1093 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1094 | __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1095 | |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1096 | #if defined(DST_DEPTH) |
| 1097 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w; |
| 1098 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1099 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE; |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1100 | #endif /* defined(DST_DEPTH) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1101 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1102 | int z_coord = 0; |
| 1103 | int4 offset = 0; |
| 1104 | int4 y_coord = ((int4)(y * NUM_ROWS_PROCESSED) + (int4)(0, 1, 2, 3)) - (int)CONV_PAD_LEFT; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1105 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1106 | // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1 |
| 1107 | y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1); |
| 1108 | y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1); |
| 1109 | y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1); |
| 1110 | y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1); |
| 1111 | |
| 1112 | int4 y_offset = convert_int4(y_coord * (int)src_stride_y); |
| 1113 | |
| 1114 | // We compute 4x2x2 [C,W,H] elements |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1115 | VEC_INT acc0 = 0, sum0 = 0; |
| 1116 | VEC_INT acc1 = 0, sum1 = 0; |
| 1117 | VEC_INT acc2 = 0, sum2 = 0; |
| 1118 | VEC_INT acc3 = 0, sum3 = 0; |
| 1119 | |
| 1120 | // Load weights |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1121 | VEC_DATA_TYPE(WEIGHTS_TYPE, 16) |
| 1122 | w0_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr)); |
| 1123 | VEC_DATA_TYPE(WEIGHTS_TYPE, 16) |
| 1124 | w1_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16)); |
| 1125 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1126 | w8 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * 16)); |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1127 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1128 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1129 | w0 = w0_tmp.s0123; |
| 1130 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1131 | w1 = w0_tmp.s4567; |
| 1132 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1133 | w2 = w0_tmp.s89AB; |
| 1134 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1135 | w3 = w0_tmp.sCDEF; |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1136 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1137 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1138 | w4 = w1_tmp.s0123; |
| 1139 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1140 | w5 = w1_tmp.s4567; |
| 1141 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1142 | w6 = w1_tmp.s89AB; |
| 1143 | VEC_DATA_TYPE(WEIGHTS_TYPE, 4) |
| 1144 | w7 = w1_tmp.sCDEF; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1145 | |
| 1146 | #if INPUT_OFFSET != 0 |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 1147 | VEC_INT sum_we = CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT) |
| 1148 | + CONVERT(w3, VEC_INT) + CONVERT(w4, VEC_INT) + CONVERT(w5, VEC_INT) |
| 1149 | + CONVERT(w6, VEC_INT) + CONVERT(w7, VEC_INT) + CONVERT(w8, VEC_INT); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1150 | #endif /* INPUT_OFFSET != 0 */ |
| 1151 | |
| 1152 | // Load input values |
| 1153 | // z == 0 |
| 1154 | // Clamp z_coord as for z = 0, it can be negative |
| 1155 | // z_coord is casted to unsigned int in order to use just a min() operation |
| 1156 | // A "-1" 32 bit signed variable converted to unsigned gives 4294967295 |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 1157 | z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1158 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
| 1159 | offset = y_offset + (int4)(z_coord * src_stride_z); |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 1160 | offset = min(offset, (int4)max_offset); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1161 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1162 | VEC_TYPE(VEC_SIZE) |
| 1163 | values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1164 | VEC_TYPE(VEC_SIZE) |
| 1165 | values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1166 | VEC_TYPE(VEC_SIZE) |
| 1167 | values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1168 | VEC_TYPE(VEC_SIZE) |
| 1169 | values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1170 | |
| 1171 | // z == 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1172 | z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP + 1; |
Giorgio Arena | 8bcb65f | 2021-01-12 11:29:18 +0000 | [diff] [blame] | 1173 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1174 | offset = y_offset + (int4)(z_coord * src_stride_z); |
Giorgio Arena | 8bcb65f | 2021-01-12 11:29:18 +0000 | [diff] [blame] | 1175 | offset = min(offset, (int4)max_offset); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1176 | VEC_TYPE(VEC_SIZE) |
| 1177 | values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1178 | VEC_TYPE(VEC_SIZE) |
| 1179 | values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1180 | VEC_TYPE(VEC_SIZE) |
| 1181 | values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1182 | VEC_TYPE(VEC_SIZE) |
| 1183 | values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1184 | |
| 1185 | // z == 2 |
Giorgio Arena | 8bcb65f | 2021-01-12 11:29:18 +0000 | [diff] [blame] | 1186 | z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP + 2; |
| 1187 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
| 1188 | offset = y_offset + (int4)(z_coord * src_stride_z); |
| 1189 | offset = min(offset, (int4)max_offset); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1190 | VEC_TYPE(VEC_SIZE) |
| 1191 | values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1192 | VEC_TYPE(VEC_SIZE) |
| 1193 | values9 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1194 | VEC_TYPE(VEC_SIZE) |
| 1195 | values10 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1196 | VEC_TYPE(VEC_SIZE) |
| 1197 | values11 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1198 | |
| 1199 | // z == 3 |
Giorgio Arena | 8bcb65f | 2021-01-12 11:29:18 +0000 | [diff] [blame] | 1200 | z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP + 3; |
| 1201 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
| 1202 | offset = y_offset + (int4)(z_coord * src_stride_z); |
| 1203 | offset = min(offset, (int4)max_offset); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1204 | VEC_TYPE(VEC_SIZE) |
| 1205 | values12 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1206 | VEC_TYPE(VEC_SIZE) |
| 1207 | values13 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1208 | VEC_TYPE(VEC_SIZE) |
| 1209 | values14 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1210 | VEC_TYPE(VEC_SIZE) |
| 1211 | values15 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1212 | |
| 1213 | MULTIPLY_ADD_ACCUMULATE(values0, w0, acc0, sum0); |
| 1214 | MULTIPLY_ADD_ACCUMULATE(values1, w1, acc0, sum0); |
| 1215 | MULTIPLY_ADD_ACCUMULATE(values2, w2, acc0, sum0); |
| 1216 | MULTIPLY_ADD_ACCUMULATE(values1, w0, acc1, sum1); |
| 1217 | MULTIPLY_ADD_ACCUMULATE(values2, w1, acc1, sum1); |
| 1218 | MULTIPLY_ADD_ACCUMULATE(values3, w2, acc1, sum1); |
| 1219 | |
| 1220 | MULTIPLY_ADD_ACCUMULATE(values4, w3, acc0, sum0); |
| 1221 | MULTIPLY_ADD_ACCUMULATE(values5, w4, acc0, sum0); |
| 1222 | MULTIPLY_ADD_ACCUMULATE(values6, w5, acc0, sum0); |
| 1223 | MULTIPLY_ADD_ACCUMULATE(values5, w3, acc1, sum1); |
| 1224 | MULTIPLY_ADD_ACCUMULATE(values6, w4, acc1, sum1); |
| 1225 | MULTIPLY_ADD_ACCUMULATE(values7, w5, acc1, sum1); |
| 1226 | |
| 1227 | MULTIPLY_ADD_ACCUMULATE(values8, w6, acc0, sum0); |
| 1228 | MULTIPLY_ADD_ACCUMULATE(values9, w7, acc0, sum0); |
| 1229 | MULTIPLY_ADD_ACCUMULATE(values10, w8, acc0, sum0); |
| 1230 | MULTIPLY_ADD_ACCUMULATE(values9, w6, acc1, sum1); |
| 1231 | MULTIPLY_ADD_ACCUMULATE(values10, w7, acc1, sum1); |
| 1232 | MULTIPLY_ADD_ACCUMULATE(values11, w8, acc1, sum1); |
| 1233 | |
| 1234 | MULTIPLY_ADD_ACCUMULATE(values4, w0, acc2, sum2); |
| 1235 | MULTIPLY_ADD_ACCUMULATE(values5, w1, acc2, sum2); |
| 1236 | MULTIPLY_ADD_ACCUMULATE(values6, w2, acc2, sum2); |
| 1237 | MULTIPLY_ADD_ACCUMULATE(values5, w0, acc3, sum3); |
| 1238 | MULTIPLY_ADD_ACCUMULATE(values6, w1, acc3, sum3); |
| 1239 | MULTIPLY_ADD_ACCUMULATE(values7, w2, acc3, sum3); |
| 1240 | |
| 1241 | MULTIPLY_ADD_ACCUMULATE(values8, w3, acc2, sum2); |
| 1242 | MULTIPLY_ADD_ACCUMULATE(values9, w4, acc2, sum2); |
| 1243 | MULTIPLY_ADD_ACCUMULATE(values10, w5, acc2, sum2); |
| 1244 | MULTIPLY_ADD_ACCUMULATE(values9, w3, acc3, sum3); |
| 1245 | MULTIPLY_ADD_ACCUMULATE(values10, w4, acc3, sum3); |
| 1246 | MULTIPLY_ADD_ACCUMULATE(values11, w5, acc3, sum3); |
| 1247 | |
| 1248 | MULTIPLY_ADD_ACCUMULATE(values12, w6, acc2, sum2); |
| 1249 | MULTIPLY_ADD_ACCUMULATE(values13, w7, acc2, sum2); |
| 1250 | MULTIPLY_ADD_ACCUMULATE(values14, w8, acc2, sum2); |
| 1251 | MULTIPLY_ADD_ACCUMULATE(values13, w6, acc3, sum3); |
| 1252 | MULTIPLY_ADD_ACCUMULATE(values14, w7, acc3, sum3); |
| 1253 | MULTIPLY_ADD_ACCUMULATE(values15, w8, acc3, sum3); |
| 1254 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1255 | #if defined(HAS_BIAS) |
| 1256 | Vector biases = CONVERT_TO_VECTOR_STRUCT(biases); |
| 1257 | |
| 1258 | VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1259 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1260 | acc0 += bias_values; |
| 1261 | acc1 += bias_values; |
| 1262 | acc2 += bias_values; |
| 1263 | acc3 += bias_values; |
| 1264 | #endif /* defined(HAS_BIAS) */ |
| 1265 | |
| 1266 | #if WEIGHTS_OFFSET != 0 |
| 1267 | acc0 += WEIGHTS_OFFSET * sum0; |
| 1268 | acc1 += WEIGHTS_OFFSET * sum1; |
| 1269 | acc2 += WEIGHTS_OFFSET * sum2; |
| 1270 | acc3 += WEIGHTS_OFFSET * sum3; |
| 1271 | #endif /* WEIGHTS_OFFSET != 0 */ |
| 1272 | |
| 1273 | #if INPUT_OFFSET != 0 |
| 1274 | VEC_INT offs = INPUT_OFFSET * sum_we; |
| 1275 | |
| 1276 | acc0 += offs; |
| 1277 | acc1 += offs; |
| 1278 | acc2 += offs; |
| 1279 | acc3 += offs; |
| 1280 | #endif /* INPUT_OFFSET != 0 */ |
| 1281 | |
| 1282 | #if K_OFFSET != 0 |
| 1283 | acc0 += (VEC_INT)K_OFFSET; |
| 1284 | acc1 += (VEC_INT)K_OFFSET; |
| 1285 | acc2 += (VEC_INT)K_OFFSET; |
| 1286 | acc3 += (VEC_INT)K_OFFSET; |
| 1287 | #endif /* K_OFFSET != 0 */ |
| 1288 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1289 | #if defined(REAL_MULTIPLIER) |
| 1290 | |
| 1291 | acc0 = CONVERT(round(CONVERT(acc0, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 1292 | acc1 = CONVERT(round(CONVERT(acc1, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 1293 | acc2 = CONVERT(round(CONVERT(acc2, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 1294 | acc3 = CONVERT(round(CONVERT(acc3, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 1295 | |
| 1296 | #else // defined(REAL_MULTIPLIER) |
| 1297 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1298 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 1299 | Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT(output_multipliers); |
| 1300 | Vector output_shifts = CONVERT_TO_VECTOR_STRUCT(output_shifts); |
| 1301 | VEC_INT output_multiplier = VLOAD(VEC_SIZE)(0, (__global int *)output_multipliers.ptr); |
| 1302 | VEC_INT output_shift = VLOAD(VEC_SIZE)(0, (__global int *)output_shifts.ptr); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1303 | |
Sang-Hoon Park | 4d79d4b | 2020-01-30 14:38:37 +0000 | [diff] [blame] | 1304 | VEC_INT res0_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc0, output_multiplier, output_shift, VEC_SIZE); |
| 1305 | VEC_INT res1_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc1, output_multiplier, output_shift, VEC_SIZE); |
| 1306 | VEC_INT res2_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc2, output_multiplier, output_shift, VEC_SIZE); |
| 1307 | VEC_INT res3_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc3, output_multiplier, output_shift, VEC_SIZE); |
| 1308 | VEC_INT res0_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, output_multiplier, output_shift); |
| 1309 | VEC_INT res1_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc1, output_multiplier, output_shift); |
| 1310 | VEC_INT res2_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc2, output_multiplier, output_shift); |
| 1311 | VEC_INT res3_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc3, output_multiplier, output_shift); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1312 | acc0 = select(res0_shift_lt0, res0_shift_gt0, output_shift >= 0); |
| 1313 | acc1 = select(res1_shift_lt0, res1_shift_gt0, output_shift >= 0); |
| 1314 | acc2 = select(res2_shift_lt0, res2_shift_gt0, output_shift >= 0); |
| 1315 | acc3 = select(res3_shift_lt0, res3_shift_gt0, output_shift >= 0); |
| 1316 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 1317 | #if OUTPUT_SHIFT < 0 |
Michele Di Giorgio | b598afa | 2019-12-20 16:22:20 +0000 | [diff] [blame] | 1318 | acc0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
| 1319 | acc1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
| 1320 | acc2 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc2, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
| 1321 | acc3 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc3, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1322 | #else // OUTPUT_SHIFT < 0 |
Michele Di Giorgio | b598afa | 2019-12-20 16:22:20 +0000 | [diff] [blame] | 1323 | acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
| 1324 | acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
| 1325 | acc2 = asymm_mult_by_quant_multiplier_less_than_one(acc2, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
| 1326 | acc3 = asymm_mult_by_quant_multiplier_less_than_one(acc3, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1327 | #endif // OUTPUT_SHIFT < 0 |
| 1328 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1329 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1330 | #endif // defined(REAL_MULTIPLIER) |
| 1331 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1332 | acc0 += (VEC_INT)OUTPUT_OFFSET; |
| 1333 | acc1 += (VEC_INT)OUTPUT_OFFSET; |
| 1334 | acc2 += (VEC_INT)OUTPUT_OFFSET; |
| 1335 | acc3 += (VEC_INT)OUTPUT_OFFSET; |
| 1336 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1337 | VEC_TYPE(VEC_SIZE) |
| 1338 | res0 = CONVERT_SAT(acc0, VEC_TYPE(VEC_SIZE)); |
| 1339 | VEC_TYPE(VEC_SIZE) |
| 1340 | res1 = CONVERT_SAT(acc1, VEC_TYPE(VEC_SIZE)); |
| 1341 | VEC_TYPE(VEC_SIZE) |
| 1342 | res2 = CONVERT_SAT(acc2, VEC_TYPE(VEC_SIZE)); |
| 1343 | VEC_TYPE(VEC_SIZE) |
| 1344 | res3 = CONVERT_SAT(acc3, VEC_TYPE(VEC_SIZE)); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1345 | |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1346 | #if defined(DST_DEPTH) |
| 1347 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + (z * NUM_PLANES_PROCESSED) * dst_step_z + b * dst_stride_w; |
| 1348 | #else /* defined(DST_DEPTH) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1349 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + (z * NUM_PLANES_PROCESSED) * dst_step_z; |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1350 | #endif /* defined(DST_DEPTH) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1351 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1352 | VSTORE(VEC_SIZE) |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 1353 | (ACTIVATION_FUNC(res0), 0, dst_addr + 0 * dst_stride_y); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1354 | VSTORE(VEC_SIZE) |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 1355 | (ACTIVATION_FUNC(res1), 0, dst_addr + 1 * dst_stride_y); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1356 | |
| 1357 | #if((DST_DIM_2 % NUM_PLANES_PROCESSED) != 0) |
| 1358 | if((z * NUM_PLANES_PROCESSED + 1) < DST_DIM_2) |
| 1359 | #endif // ((DST_DIM_2 % NUM_PLANES_PROCESSED) != 0) |
| 1360 | { |
| 1361 | VSTORE(VEC_SIZE) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1362 | (ACTIVATION_FUNC(res2), 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y + 1 * dst_stride_z)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1363 | VSTORE(VEC_SIZE) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1364 | (ACTIVATION_FUNC(res3), 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y + 1 * dst_stride_z)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1365 | } |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1368 | #if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) && VEC_SIZE == 4 |
| 1369 | /** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width and height is 1 using dot product. |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1370 | * |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1371 | * @note Per-channel quantization is not supported by this kernel. |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1372 | * @note This kernel assumes VEC_SIZE is 4. |
| 1373 | * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel. |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1374 | * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2) |
| 1375 | * @note Dimension two of the input tensor (height for NHWC data layout) must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM_2=112) |
| 1376 | * @note The number of rows processed per thread must be passed at compile time using -DNUM_ROWS_PROCESSED (i.e. -DNUM_ROWS_PROCESSED=2) |
| 1377 | * @note The number of planes processed per thread must be passed at compile time using -DNUM_PLANES_PROCESSED (i.e. -DNUM_PLANES_PROCESSED=2) |
| 1378 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1) |
| 1379 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1). |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1380 | * @note If REAL_MULTIPLIER is passed at compile time (i.e. -DREAL_MULTIPLIER=1.355f), the final quantization is performed using a floating point multiplication. |
| 1381 | * If not, the quantization will be performed using a fixed point multiplication |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1382 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 1383 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1384 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1385 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1386 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1387 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1388 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1389 | * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes) |
| 1390 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes) |
| 1391 | * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes) |
| 1392 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1393 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 1394 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1395 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 1396 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1397 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1398 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 1399 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 1400 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 1401 | * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes) |
| 1402 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 1403 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr |
| 1404 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 1405 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 1406 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 1407 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1408 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 1409 | * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32 |
| 1410 | * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes) |
| 1411 | * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 1412 | * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector |
| 1413 | * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32 |
| 1414 | * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes) |
| 1415 | * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 1416 | * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector |
| 1417 | * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32 |
| 1418 | * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) |
| 1419 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1420 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector |
| 1421 | * @param[in] max_offset The maximum allowed offset for the input tensor |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1422 | */ |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1423 | __kernel void dwc_3x3_reshaped_quantized8_dot8_stride1_nhwc( |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1424 | TENSOR4D_DECLARATION(src), |
| 1425 | TENSOR4D_DECLARATION(dst), |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1426 | IMAGE_DECLARATION(weights), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1427 | VECTOR_DECLARATION(output_multipliers), |
| 1428 | VECTOR_DECLARATION(output_shifts), |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1429 | #if defined(HAS_BIAS) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1430 | VECTOR_DECLARATION(biases), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1431 | #endif // defined(HAS_BIAS) |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1432 | int max_offset) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1433 | { |
Giorgio Arena | fa23f11 | 2018-06-19 11:27:38 +0100 | [diff] [blame] | 1434 | int x = get_global_id(0); |
| 1435 | int y = get_global_id(1); |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1436 | #if defined(DST_DEPTH) |
| 1437 | int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y |
| 1438 | int b = get_global_id(2) / (int)DST_DEPTH; // batch |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1439 | #else // defined(DST_DEPTH) |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1440 | int z = get_global_id(2); // spatial coordinate y |
Pablo Tello | 4710436 | 2019-02-27 13:32:51 +0000 | [diff] [blame] | 1441 | #endif // defined(DST_DEPTH) |
Giorgio Arena | fa23f11 | 2018-06-19 11:27:38 +0100 | [diff] [blame] | 1442 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1443 | __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1444 | |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1445 | #if defined(DST_DEPTH) |
| 1446 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w; |
| 1447 | #else /* defined(DST_DEPTH) */ |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1448 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE; |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1449 | #endif /* defined(DST_DEPTH) */ |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1450 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1451 | int z_coord = 0; |
| 1452 | int4 offset = 0; |
| 1453 | int4 y_coord = ((int4)(y * NUM_ROWS_PROCESSED) + (int4)(0, 1, 2, 3)) - (int)CONV_PAD_LEFT; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1454 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1455 | // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1 |
| 1456 | y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1); |
| 1457 | y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1); |
| 1458 | y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1); |
| 1459 | y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1); |
| 1460 | |
| 1461 | int4 y_offset = convert_int4(y_coord * (int)src_stride_y); |
| 1462 | |
| 1463 | // We compute 4x2x1 [C,W,H] elements |
| 1464 | VEC_INT acc0 = 0; |
| 1465 | VEC_INT acc1 = 0; |
| 1466 | VEC_INT sum0 = 0; |
| 1467 | VEC_INT sum1 = 0; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1468 | |
| 1469 | // Load weights |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1470 | VEC_TYPE(16) |
| 1471 | w0 = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr)); |
| 1472 | VEC_TYPE(16) |
| 1473 | w1 = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16)); |
| 1474 | VEC_TYPE(4) |
| 1475 | w2 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 32)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1476 | |
| 1477 | #if INPUT_OFFSET != 0 |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1478 | // Initilize the final result with the weights reduction multiplied by INPUT_OFFSET |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1479 | DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s0, w0.s01234567, w0.s8); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1480 | DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1); |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1481 | DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s2, w1.s23456789, w1.sA); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1482 | DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s3, (VEC_TYPE(8))((w1.sBCD), (w1.sEF), (w2.s012)), w2.s3); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1483 | |
| 1484 | // Multiply the weights reduction with INPUT_OFFSET |
| 1485 | acc0 = INPUT_OFFSET * acc0; |
| 1486 | |
| 1487 | acc1 = acc0; |
| 1488 | #endif // INPUT_OFFSET != 0 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1489 | |
| 1490 | // Load input values |
| 1491 | // z == 0 |
| 1492 | // Clamp z_coord as for z = 0, it can be negative |
| 1493 | // z_coord is casted to unsigned int in order to use just a min() operation |
| 1494 | // A "-1" 32 bit signed variable converted to unsigned gives 4294967295 |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1495 | z_coord = z - (int)CONV_PAD_TOP; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1496 | z_coord = min((uint)z_coord, (uint)SRC_DIM_2); |
| 1497 | offset = y_offset + (int4)(z_coord * src_stride_z); |
Georgios Pinitas | 4e0d381 | 2018-08-01 20:16:34 +0100 | [diff] [blame] | 1498 | offset = min(offset, (int4)max_offset); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1499 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1500 | VEC_TYPE(VEC_SIZE) |
| 1501 | values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1502 | VEC_TYPE(VEC_SIZE) |
| 1503 | values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1504 | VEC_TYPE(VEC_SIZE) |
| 1505 | values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1506 | VEC_TYPE(VEC_SIZE) |
| 1507 | values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1508 | |
| 1509 | // z == 1 |
| 1510 | // z_coord can be only negative for z = 0 so we do not need to clamp it |
| 1511 | // Moreover z_coord cannot be out-of-bound for z = 1 so we do not need to clamp the offset |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1512 | z_coord = z - (int)CONV_PAD_TOP + 1; |
| 1513 | offset = y_offset + (int4)(z_coord * src_stride_z); |
| 1514 | VEC_TYPE(VEC_SIZE) |
| 1515 | values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1516 | VEC_TYPE(VEC_SIZE) |
| 1517 | values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1518 | VEC_TYPE(VEC_SIZE) |
| 1519 | values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1520 | VEC_TYPE(VEC_SIZE) |
| 1521 | values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1522 | |
| 1523 | // z == 2 |
| 1524 | // After z = 1 we can simply add src_stride_z to offset without updating z_coord |
| 1525 | // However offset can be out-of-bound so we need to check if it is greater than max_offset |
| 1526 | offset += (int4)src_stride_z; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1527 | offset = min(offset, (int4)max_offset); |
| 1528 | VEC_TYPE(VEC_SIZE) |
| 1529 | values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0)); |
| 1530 | VEC_TYPE(VEC_SIZE) |
| 1531 | values9 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1)); |
| 1532 | VEC_TYPE(VEC_SIZE) |
| 1533 | values10 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2)); |
| 1534 | VEC_TYPE(VEC_SIZE) |
| 1535 | values11 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1536 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1537 | DOT_PRODUCT_REDUCTION(sum0.s0, values0.s0, values1.s0, values2.s0, values4.s0, values5.s0, values6.s0, values8.s0, values9.s0, values10.s0); |
| 1538 | DOT_PRODUCT_REDUCTION(sum1.s0, values1.s0, values2.s0, values3.s0, values5.s0, values6.s0, values7.s0, values9.s0, values10.s0, values11.s0); |
| 1539 | DOT_PRODUCT(acc0.s0, values0.s0, values1.s0, values2.s0, values4.s0, values5.s0, values6.s0, values8.s0, values9.s0, values10.s0, w0.s01234567, w0.s8); |
| 1540 | DOT_PRODUCT(acc1.s0, values1.s0, values2.s0, values3.s0, values5.s0, values6.s0, values7.s0, values9.s0, values10.s0, values11.s0, w0.s01234567, w0.s8); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1541 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1542 | DOT_PRODUCT_REDUCTION(sum0.s1, values0.s1, values1.s1, values2.s1, values4.s1, values5.s1, values6.s1, values8.s1, values9.s1, values10.s1); |
| 1543 | DOT_PRODUCT_REDUCTION(sum1.s1, values1.s1, values2.s1, values3.s1, values5.s1, values6.s1, values7.s1, values9.s1, values10.s1, values11.s1); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1544 | DOT_PRODUCT(acc0.s1, values0.s1, values1.s1, values2.s1, values4.s1, values5.s1, values6.s1, values8.s1, values9.s1, values10.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1); |
| 1545 | DOT_PRODUCT(acc1.s1, values1.s1, values2.s1, values3.s1, values5.s1, values6.s1, values7.s1, values9.s1, values10.s1, values11.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1); |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1546 | |
| 1547 | DOT_PRODUCT_REDUCTION(sum0.s2, values0.s2, values1.s2, values2.s2, values4.s2, values5.s2, values6.s2, values8.s2, values9.s2, values10.s2); |
| 1548 | DOT_PRODUCT_REDUCTION(sum1.s2, values1.s2, values2.s2, values3.s2, values5.s2, values6.s2, values7.s2, values9.s2, values10.s2, values11.s2); |
| 1549 | DOT_PRODUCT(acc0.s2, values0.s2, values1.s2, values2.s2, values4.s2, values5.s2, values6.s2, values8.s2, values9.s2, values10.s2, w1.s23456789, w1.sA); |
| 1550 | DOT_PRODUCT(acc1.s2, values1.s2, values2.s2, values3.s2, values5.s2, values6.s2, values7.s2, values9.s2, values10.s2, values11.s2, w1.s23456789, w1.sA); |
| 1551 | |
| 1552 | DOT_PRODUCT_REDUCTION(sum0.s3, values0.s3, values1.s3, values2.s3, values4.s3, values5.s3, values6.s3, values8.s3, values9.s3, values10.s3); |
| 1553 | DOT_PRODUCT_REDUCTION(sum1.s3, values1.s3, values2.s3, values3.s3, values5.s3, values6.s3, values7.s3, values9.s3, values10.s3, values11.s3); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1554 | DOT_PRODUCT(acc0.s3, values0.s3, values1.s3, values2.s3, values4.s3, values5.s3, values6.s3, values8.s3, values9.s3, values10.s3, (VEC_TYPE(8))((w1.sBCD), (w1.sEF), (w2.s012)), w2.s3); |
| 1555 | DOT_PRODUCT(acc1.s3, values1.s3, values2.s3, values3.s3, values5.s3, values6.s3, values7.s3, values9.s3, values10.s3, values11.s3, (VEC_TYPE(8))((w1.sBCD), (w1.sEF), (w2.s012)), w2.s3); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1556 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1557 | #if defined(HAS_BIAS) |
| 1558 | Vector biases = CONVERT_TO_VECTOR_STRUCT(biases); |
| 1559 | |
| 1560 | VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1561 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1562 | acc0 += bias_values; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1563 | acc1 += bias_values; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1564 | |
| 1565 | #endif // defined(HAS_BIAS) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1566 | |
| 1567 | #if WEIGHTS_OFFSET != 0 |
| 1568 | acc0 += WEIGHTS_OFFSET * sum0; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1569 | acc1 += WEIGHTS_OFFSET * sum1; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1570 | #endif // WEIGHTS_OFFSET != 0 |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1571 | |
| 1572 | #if K_OFFSET != 0 |
| 1573 | acc0 += (VEC_INT)K_OFFSET; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1574 | acc1 += (VEC_INT)K_OFFSET; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1575 | |
| 1576 | #endif // K_OFFSET != 0 |
| 1577 | |
| 1578 | #if defined(REAL_MULTIPLIER) |
| 1579 | |
| 1580 | acc0 = CONVERT(round(CONVERT(acc0, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 1581 | acc1 = CONVERT(round(CONVERT(acc1, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT); |
| 1582 | |
| 1583 | #else // defined(REAL_MULTIPLIER) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1584 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1585 | #if OUTPUT_SHIFT < 0 |
| 1586 | acc0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
| 1587 | acc1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE); |
| 1588 | #else // OUTPUT_SHIFT < 0 |
| 1589 | acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
| 1590 | acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); |
| 1591 | #endif // OUTPUT_SHIFT < 0 |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1592 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1593 | #endif // defined(REAL_MULTIPLIER) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1594 | acc0 += (VEC_INT)OUTPUT_OFFSET; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1595 | acc1 += (VEC_INT)OUTPUT_OFFSET; |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1596 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1597 | VEC_TYPE(VEC_SIZE) |
| 1598 | res0 = CONVERT_SAT(acc0, VEC_TYPE(VEC_SIZE)); |
| 1599 | VEC_TYPE(VEC_SIZE) |
| 1600 | res1 = CONVERT_SAT(acc1, VEC_TYPE(VEC_SIZE)); |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1601 | |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1602 | #if defined(DST_DEPTH) |
| 1603 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z + b * dst_stride_w; |
| 1604 | #else /* defined(DST_DEPTH) */ |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1605 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z; |
Georgios Pinitas | 3704464 | 2018-10-30 14:53:25 +0000 | [diff] [blame] | 1606 | #endif /* defined(DST_DEPTH) */ |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1607 | |
| 1608 | VSTORE(VEC_SIZE) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1609 | (ACTIVATION_FUNC(res0), 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y)); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1610 | VSTORE(VEC_SIZE) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1611 | (ACTIVATION_FUNC(res1), 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y)); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1612 | } |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1613 | #endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) && VEC_SIZE==4 |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1614 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1615 | #endif // defined(NUM_ROWS_PROCESSED) && defined(NUM_PLANES_PROCESSED) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 1616 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 1617 | #endif // defined(VEC_SIZE) && defined(SRC_DIM_1) && defined(SRC_DIM_2) && defined(CONV_PAD_TOP) && defined(CONV_PAD_LEFT) |
| 1618 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1619 | #endif // defined(WEIGHTS_PROMOTED_TYPE) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1620 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1621 | #endif // defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && ((defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)) || defined(REAL_MULTIPLIER)) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1622 | |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1623 | #if defined(SRC_DIM1) && defined(SRC_DIM2) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(N0) && defined(DILATION_X) && defined(DILATION_Y) && defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(CONV_PAD_LEFT) && defined(CONV_PAD_TOP) && defined(INPUT_OFFSET) && defined(WEIGHTS_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_SHIFT) && defined(OUTPUT_MULTIPLIER) && defined(VEC_SIZE_LEFTOVER) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1624 | /** This function computes the depthwise convolution for NHWC data layout. This kernel assumes that the weights tensor is NOT reshaped |
| 1625 | * |
| 1626 | * @note The number of elements processed must be passed at compile time using -DN0 (e.g. -DN0=2) |
| 1627 | * @note The depth multiplier must be passed at compile time using -DDEPTH_MULTIPLIER (e.g. -DDEPTH_MULTIPLIER=1) |
| 1628 | * @note The first dimension of the input tensor must be passed at compile time using -DSRC_DIM1 (e.g. -DSRC_DIM1=112) |
| 1629 | * @note The second dimension of the input tensor must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM2=80) |
| 1630 | * @note The kernel width must be passed at compile time using -DKERNEL_WIDTH (e.g. -DKERNEL_WIDTH=5) |
| 1631 | * @note The kernel height must be passed at compile time using -DKERNEL_HEIGHT (e.g. -DKERNEL_HEIGHT=5) |
| 1632 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1) |
| 1633 | * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1) |
| 1634 | * @note The convolution stride along the width must be passed at compile time using -DCONV_STRIDE_X (e.g. -DCONV_STRIDE_Y=X) |
| 1635 | * @note The convolution stride along the height must be passed at compile time using -DCONV_STRIDE_Y (e.g. -DCONV_STRIDE_Y=1) |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1636 | * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE=3. It is defined as the remainder between the input's first dimension and VEC_SIZE |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1637 | * @note It is possible to select the activation function to apply using -DACTIVATION_TYPE e.g. -DACTIVATION_TYPE=relu |
| 1638 | * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively |
| 1639 | * |
Michele Di Giorgio | 4cd4cde | 2020-01-06 14:07:44 +0000 | [diff] [blame] | 1640 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1641 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 1642 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1643 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 1644 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1645 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 1646 | * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes) |
| 1647 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes) |
| 1648 | * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes) |
| 1649 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 1650 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 1651 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 1652 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 1653 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 1654 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1655 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 1656 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 1657 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes) |
| 1658 | * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes) |
| 1659 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michele Di Giorgio | 4cd4cde | 2020-01-06 14:07:44 +0000 | [diff] [blame] | 1660 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1661 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 1662 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 1663 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 1664 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1665 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 1666 | * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes) |
| 1667 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 1668 | * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32 |
| 1669 | * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes) |
| 1670 | * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes) |
| 1671 | * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector |
| 1672 | * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32 |
| 1673 | * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes) |
| 1674 | * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes) |
| 1675 | * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector |
| 1676 | * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32 |
| 1677 | * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) |
| 1678 | * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 1679 | * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1680 | */ |
| 1681 | __kernel void dwc_MxN_native_quantized8_nhwc( |
| 1682 | TENSOR4D_DECLARATION(src), |
| 1683 | TENSOR4D_DECLARATION(dst), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1684 | TENSOR3D_DECLARATION(weights), |
| 1685 | VECTOR_DECLARATION(output_multipliers), |
| 1686 | VECTOR_DECLARATION(output_shifts) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1687 | #if defined(HAS_BIAS) |
Michele Di Giorgio | 1dce310 | 2019-10-22 10:29:03 +0100 | [diff] [blame] | 1688 | , |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1689 | VECTOR_DECLARATION(biases) |
| 1690 | #endif // defined(HAS_BIAS) |
| 1691 | ) |
| 1692 | { |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1693 | int x_offs = max((int)(get_global_id(0) * N0 - (N0 - VEC_SIZE_LEFTOVER) % N0), 0); |
| 1694 | int y = get_global_id(1); // spatial coordinate x |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1695 | #if defined(DST_DEPTH) |
| 1696 | int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y |
| 1697 | int b = get_global_id(2) / (int)DST_DEPTH; // batch |
| 1698 | #else // defined(DST_DEPTH) |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1699 | int z = get_global_id(2); // spatial coordinate y |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1700 | #endif // defined(DST_DEPTH) |
| 1701 | |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1702 | __global uchar *s_addr = src_ptr + src_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE); |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1703 | |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1704 | __global uchar *d_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) * (int)DEPTH_MULTIPLIER + y * dst_stride_y + z * dst_stride_z; |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1705 | |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1706 | __global uchar *w_addr = weights_ptr + weights_offset_first_element_in_bytes + x_offs * sizeof(WEIGHTS_TYPE) * (int)DEPTH_MULTIPLIER; |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1707 | |
| 1708 | #if defined(HAS_BIAS) |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1709 | __global uchar *b_addr = biases_ptr + biases_offset_first_element_in_bytes + x_offs * sizeof(int) * (int)DEPTH_MULTIPLIER; |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1710 | #endif // defined(HAS_BIAS) |
| 1711 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1712 | #if defined(PER_CHANNEL_QUANTIZATION) |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1713 | __global uchar *out_mul_addr = output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes + x_offs * sizeof(int) * (int)DEPTH_MULTIPLIER; |
| 1714 | __global uchar *out_shift_addr = output_shifts_ptr + output_shifts_offset_first_element_in_bytes + x_offs * sizeof(int) * (int)DEPTH_MULTIPLIER; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1715 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
| 1716 | |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1717 | #if defined(DST_DEPTH) |
| 1718 | s_addr += b * src_stride_w; |
| 1719 | d_addr += b * dst_stride_w; |
| 1720 | #endif // defined(DST_DEPTH) |
| 1721 | |
| 1722 | #if DEPTH_MULTIPLIER > 1 |
| 1723 | for(int d = 0; d < (int)DEPTH_MULTIPLIER; ++d) |
| 1724 | { |
| 1725 | #endif // DEPTH_MULTIPLIER > 1 |
| 1726 | // Each work-item computes N0x1x1 elements |
Sang-Hoon Park | 15396ff | 2019-11-07 09:37:29 +0000 | [diff] [blame] | 1727 | VEC_INT res = 0; |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1728 | |
| 1729 | int x_coord = y * CONV_STRIDE_X - (int)CONV_PAD_LEFT; |
| 1730 | int y_coord = z * CONV_STRIDE_Y - (int)CONV_PAD_TOP; |
| 1731 | |
| 1732 | for(int yk = 0; yk < KERNEL_HEIGHT; ++yk) |
| 1733 | { |
| 1734 | if(y_coord >= 0 && y_coord < SRC_DIM2) |
| 1735 | { |
| 1736 | int x_coord_tmp = x_coord; |
| 1737 | |
| 1738 | for(int xk = 0; xk < KERNEL_WIDTH; ++xk) |
| 1739 | { |
| 1740 | if(x_coord_tmp >= 0 && x_coord_tmp < SRC_DIM1) |
| 1741 | { |
| 1742 | int s_offset = x_coord_tmp * (int)src_stride_y + y_coord * (int)src_stride_z; |
| 1743 | int w_offset = xk * weights_stride_y + yk * weights_stride_z; |
| 1744 | |
| 1745 | // Load input and weights values |
Sang-Hoon Park | 15396ff | 2019-11-07 09:37:29 +0000 | [diff] [blame] | 1746 | VEC_INT i = CONVERT(VLOAD(N0)(0, (__global DATA_TYPE *)(s_addr + s_offset)), VEC_INT); |
| 1747 | VEC_INT w = CONVERT(VLOAD(N0)(0, (__global WEIGHTS_TYPE *)(w_addr + w_offset)), VEC_INT); |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1748 | |
Sang-Hoon Park | 15396ff | 2019-11-07 09:37:29 +0000 | [diff] [blame] | 1749 | res += (i + (VEC_INT)INPUT_OFFSET) * (w + (VEC_INT)WEIGHTS_OFFSET); |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1750 | } |
| 1751 | x_coord_tmp += DILATION_X; |
| 1752 | } |
| 1753 | } |
| 1754 | y_coord += DILATION_Y; |
| 1755 | } |
| 1756 | |
| 1757 | #if defined(HAS_BIAS) |
Sang-Hoon Park | 15396ff | 2019-11-07 09:37:29 +0000 | [diff] [blame] | 1758 | VEC_INT bias = VLOAD(N0)(0, (__global int *)(b_addr)); |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1759 | res += bias; |
| 1760 | #endif // defined(HAS_BIAS) |
| 1761 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1762 | #if defined(PER_CHANNEL_QUANTIZATION) |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 1763 | VEC_INT output_multiplier = VLOAD(N0)(0, (__global int *)(out_mul_addr)); |
| 1764 | VEC_INT output_shift = VLOAD(N0)(0, (__global int *)(out_shift_addr)); |
| 1765 | |
| 1766 | VEC_INT res_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(res, output_multiplier, output_shift, N0); |
| 1767 | VEC_INT res_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(res, output_multiplier, output_shift, N0); |
| 1768 | res = select(res_shift_lt0, res_shift_gt0, (VEC_INT)(output_shift) >= 0); |
| 1769 | #else // defined(PER_CHANNEL_QUANTIZATION) |
| 1770 | #if OUTPUT_SHIFT < 0 |
| 1771 | res = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(res, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, N0); |
| 1772 | #else // OUTPUT_SHIFT < 0 |
| 1773 | res = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(res, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, N0); |
| 1774 | #endif // OUTPUT_OFFSET < 0 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1775 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
| 1776 | |
Sang-Hoon Park | 15396ff | 2019-11-07 09:37:29 +0000 | [diff] [blame] | 1777 | res += (VEC_INT)OUTPUT_OFFSET; |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1778 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1779 | VEC_TYPE(VEC_SIZE) |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1780 | res0 = CONVERT_SAT(res, VEC_TYPE(VEC_SIZE)); |
| 1781 | res0 = ACTIVATION_FUNC(res0); |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1782 | |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1783 | STORE_VECTOR_SELECT(res, DATA_TYPE, d_addr, N0, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1784 | |
| 1785 | #if DEPTH_MULTIPLIER > 1 |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1786 | w_addr += sizeof(WEIGHTS_TYPE); |
| 1787 | d_addr += sizeof(DATA_TYPE); |
| 1788 | #if defined(PER_CHANNEL_QUANTIZATION) |
| 1789 | out_mul_addr += sizeof(int); |
| 1790 | out_shift_addr += sizeof(int); |
| 1791 | #endif // defined(PER_CHANNEL_QUANTIZATION) |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 1792 | #if defined(HAS_BIAS) |
| 1793 | b_addr += sizeof(int); |
| 1794 | #endif // defined(HAS_BIAS) |
| 1795 | } |
| 1796 | #endif // DEPTH_MULTIPLIER > 1 |
| 1797 | } |
Giorgio Arena | 79acd77 | 2020-10-22 14:29:50 +0100 | [diff] [blame] | 1798 | #endif // defined(SRC_DIM1) && defined(SRC_DIM2) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defiend(N0) && defined(DILATION_X) && defined(DILATION_Y) && defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(CONV_PAD_LEFT) && defined(CONV_PAD_TOP) && defined(INPUT_OFFSET) && defined(WEIGHTS_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_SHIFT) && defined(OUTPUT_MULTIPLIER) && defined(VEC_SIZE_LEFTOVER) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 1799 | #endif // defined(DATA_TYPE) && defined(WEIGHTS_TYPE) |