Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 223aba9 | 2021-04-28 16:03:32 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "helpers.h" |
| 25 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 26 | #if defined(DATA_TYPE) && defined(MIN_VALUE) && defined(VECTOR_SIZE) && defined(VECTOR_SIZE_LEFTOVER) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | /** Divides all the values of the input tensor by the sum calculated from softmax_layer_shift_exp_sum kernel. |
| 29 | * |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 30 | * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE, e.g. -DDATA_TYPE=float |
| 31 | * @note The zero value for the given data type must be given as a preprocessor argument using -DMIN_VALUE, e.g. -DMIN_VALUE=0 |
| 32 | * @note Vector size should be given as a preprocessor argument using -DVECTOR_SIZE=size. e.g. -DVECTOR_SIZE=16 |
| 33 | * @note Leftover vector size has to be passed at compile time using -DVECTOR_SIZE_LEFTOVER. e.g. -DVECTOR_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VECTOR_SIZE |
| 34 | * @note In case of log softmax, -DLOG_SOFTMAX must be passed. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 36 | * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 38 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 39 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 40 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 41 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 42 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 44 | * @param[in] sum_ptr Pointer to the sum values tensor slice. Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes) |
| 46 | * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes) |
| 47 | * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes) |
| 48 | * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes) |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 49 | * @param[in] sum_stride_z Stride of the sum values tensor in Z dimension (in bytes) |
| 50 | * @param[in] sum_step_z sum_stride_z * number of elements along Z processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 52 | * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 54 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 55 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 56 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 57 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 58 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 60 | */ |
| 61 | __kernel void softmax_layer_norm( |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 62 | TENSOR3D_DECLARATION(src), |
| 63 | TENSOR3D_DECLARATION(sum), |
| 64 | TENSOR3D_DECLARATION(dst)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 66 | const int x_offs = max((int)(get_global_id(0) * VECTOR_SIZE - (VECTOR_SIZE - VECTOR_SIZE_LEFTOVER) % VECTOR_SIZE), 0) * sizeof(DATA_TYPE); |
| 67 | |
| 68 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x_offs + get_global_id(1) * src_stride_y + get_global_id(2) * src_stride_z; |
| 69 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs + get_global_id(1) * dst_stride_y + get_global_id(2) * dst_stride_z; |
| 70 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 71 | Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(sum); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | |
| 73 | // Load max value of 1D logits vector (row) |
| 74 | DATA_TYPE sum_val = *((__global DATA_TYPE *)offset(&sum, 0, get_global_id(1))); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 75 | VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) |
| 76 | data0 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)src_addr); |
| 77 | |
| 78 | #if defined(LOG_SOFTMAX) |
Sang-Hoon Park | a0205b9 | 2020-07-07 09:36:09 +0100 | [diff] [blame] | 79 | sum_val = log(sum_val); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 80 | data0 -= sum_val; |
| 81 | #else // defined(LOG_SOFTMAX) |
| 82 | data0 /= sum_val; |
| 83 | #endif // defined(LOG_SOFTMAX) |
| 84 | |
| 85 | STORE_VECTOR_SELECT(data, DATA_TYPE, dst_addr, VECTOR_SIZE, VECTOR_SIZE_LEFTOVER, VECTOR_SIZE_LEFTOVER != 0 && get_global_id(0) == 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | } |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 87 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 88 | #if defined(SRC_WIDTH) && defined(LOG_VECTOR_SIZE) && defined(MINVAL) |
| 89 | |
| 90 | /* Number of workitems in dimension 0. */ |
| 91 | #if !defined(GRID_SIZE) |
| 92 | #define GRID_SIZE 1 |
| 93 | #endif /* !defined(GRID_SIZE) */ |
| 94 | |
| 95 | #define VEC_TYPE VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) |
| 96 | #define SELECT_TYPE SELECT_VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) |
| 97 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 98 | /** Identifies the maximum value across the 1st dimension and shifts the values of the input tensor by this maximum value, |
| 99 | * then gets the exponent of each element as sums all elements across each row. |
| 100 | * |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 101 | * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE, e.g. -DDATA_TYPE=float |
| 102 | * @note The zero value for the given data type must be given as a preprocessor argument using -DMIN_VALUE, e.g. -DMIN_VALUE=0 |
| 103 | * @note Vector size should be given as a preprocessor argument using -DVECTOR_SIZE=size. e.g. -DVECTOR_SIZE=16 |
| 104 | * @note Leftover vector size has to be passed at compile time using -DVECTOR_SIZE_LEFTOVER. e.g. -DVECTOR_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VECTOR_SIZE |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 105 | * @note In case the input is not a multiple of VECTOR_SIZE (2,4,8,16) -DNON_MULTIPLE_OF_VECTOR_SIZE must be passed. |
| 106 | * @note Beta can be optionally passed at compile time using -DBETA (by default, it is 1.0). |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 107 | * @note In case of log softmax, -DLOG_SOFTMAX must be passed. |
| 108 | * @note Based on the data type, the minimum possible value must be passed using -DMINVAL. For float it should be defined as -FLT_MAX, while for half it should be -HALF_MAX |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 109 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 110 | * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16/F32 |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 111 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 112 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 113 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 114 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 115 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 116 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 117 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 118 | * @param[in] maxo_ptr Pointer to the max values tensor slice. Supported data types: same as @p src_ptr |
| 119 | * @param[in] maxo_stride_x Stride of the max values tensor in X dimension (in bytes) |
| 120 | * @param[in] maxo_step_x max_stride_x * number of elements along X processed per workitem(in bytes) |
| 121 | * @param[in] maxo_stride_y Stride of the max values tensor in Y dimension (in bytes) |
| 122 | * @param[in] maxo_step_y max_stride_y * number of elements along Y processed per workitem(in bytes) |
| 123 | * @param[in] maxo_stride_z Stride of the max values tensor in Z dimension (in bytes) |
| 124 | * @param[in] maxo_step_z max_stride_z * number of elements along Z processed per workitem(in bytes) |
| 125 | * @param[in] maxo_offset_first_element_in_bytes The offset of the first element in the max values tensor |
| 126 | * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: same as @p src_ptr |
| 127 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 128 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 129 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 130 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 131 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 132 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 133 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 134 | * @param[out] sum_ptr Pointer to the sum values tensor slice. Supported data types: same as @p src_ptr |
| 135 | * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes) |
| 136 | * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes) |
| 137 | * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes) |
| 138 | * @param[in] sum_step_y sum_stride_z * number of elements along Z processed per workitem(in bytes) |
| 139 | * @param[in] sum_stride_z Stride of the sum values tensor in Z dimension (in bytes) |
| 140 | * @param[in] sum_step_z sum_stride_z * number of elements along Z processed per workitem(in bytes) |
| 141 | * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 142 | */ |
| 143 | __kernel void softmax_layer_max_shift_exp_sum_serial( |
| 144 | TENSOR3D_DECLARATION(src), |
| 145 | TENSOR3D_DECLARATION(maxo), |
| 146 | TENSOR3D_DECLARATION(dst), |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 147 | TENSOR3D_DECLARATION(sum)) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 148 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 149 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + get_global_id(1) * src_stride_y + get_global_id(2) * src_stride_z; |
| 150 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + get_global_id(1) * dst_stride_y + get_global_id(2) * dst_stride_z; |
| 151 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 152 | Image maxo = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(maxo); |
| 153 | Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(sum); |
| 154 | |
| 155 | #ifdef BETA |
| 156 | // Initialize beta |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 157 | VEC_TYPE beta = (VEC_TYPE)BETA; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 158 | #endif /* BETA */ |
| 159 | |
| 160 | // Initialize local maximum |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 161 | VEC_TYPE max_val_vec = (VEC_TYPE)(MINVAL); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 162 | |
| 163 | #ifdef NON_MULTIPLE_OF_VECTOR_SIZE |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 164 | VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)src_addr); |
| 165 | SELECT_TYPE widx = (SELECT_TYPE)VECTOR_SIZE_LEFTOVER > VEC_OFFS(SELECT_DATA_TYPE(DATA_TYPE), VECTOR_SIZE); |
| 166 | max_val_vec = max(max_val_vec, select((VEC_TYPE)(MINVAL), data, widx)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 167 | #endif /* NON_MULTIPLE_OF_VECTOR_SIZE */ |
| 168 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 169 | for(uint i = VECTOR_SIZE_LEFTOVER; i < SRC_WIDTH; i += VECTOR_SIZE) |
| 170 | { |
| 171 | VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + i * sizeof(DATA_TYPE))); |
| 172 | max_val_vec = max(data, max_val_vec); |
| 173 | } |
| 174 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 175 | // Perform max reduction |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 176 | DATA_TYPE max_val = MAX_REDUCE(max_val_vec, VECTOR_SIZE); |
| 177 | *((__global DATA_TYPE *)maxo.ptr) = max_val; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 178 | |
| 179 | /* Second section */ |
| 180 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 181 | // Set sum vector |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 182 | VEC_TYPE sum1D = 0; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 183 | |
| 184 | #ifdef NON_MULTIPLE_OF_VECTOR_SIZE |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 185 | data -= max_val; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 186 | #ifdef BETA |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 187 | data *= beta; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 188 | #endif /* BETA */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 189 | #ifdef LOG_SOFTMAX |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 190 | VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER) |
| 191 | (data, 0, (__global DATA_TYPE *)dst_addr); |
| 192 | data = exp(data); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 193 | data = select(0, data, widx); |
| 194 | #else /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 195 | data = exp(data); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 196 | data = select(0, data, widx); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 197 | VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER) |
| 198 | (data, 0, (__global DATA_TYPE *)dst_addr); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 199 | #endif /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 200 | sum1D += data; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 201 | #endif /* NON_MULTIPLE_OF_VECTOR_SIZE */ |
| 202 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 203 | // Shift values, exp and sum |
| 204 | for(uint i = VECTOR_SIZE_LEFTOVER; i < SRC_WIDTH; i += VECTOR_SIZE) |
| 205 | { |
| 206 | VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + i * sizeof(DATA_TYPE))); |
| 207 | data -= max_val; |
| 208 | #ifdef BETA |
| 209 | data *= beta; |
| 210 | #endif /* BETA */ |
| 211 | #ifdef LOG_SOFTMAX |
| 212 | VSTORE(VECTOR_SIZE) |
| 213 | (data, 0, (__global DATA_TYPE *)(dst_addr + i * sizeof(DATA_TYPE))); |
| 214 | data = exp(data); |
| 215 | #else /* LOG_SOFTMAX */ |
| 216 | data = exp(data); |
| 217 | VSTORE(VECTOR_SIZE) |
| 218 | (data, 0, (__global DATA_TYPE *)(dst_addr + i * sizeof(DATA_TYPE))); |
| 219 | #endif /* LOG_SOFTMAX */ |
| 220 | sum1D += data; |
| 221 | } |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 222 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 223 | // Perform sum reduction |
| 224 | *((__global DATA_TYPE *)sum.ptr) = SUM_REDUCE(sum1D, VECTOR_SIZE); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** Identifies the maximum value across the 1st dimension and shifts the values of the input tensor by this maximum value, |
| 228 | * then gets the exponent of each element as sums all elements across each row. |
| 229 | * |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 230 | * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE, e.g. -DDATA_TYPE=float |
| 231 | * @note The zero value for the given data type must be given as a preprocessor argument using -DMIN_VALUE, e.g. -DMIN_VALUE=0 |
| 232 | * @note Vector size should be given as a preprocessor argument using -DVECTOR_SIZE=size. e.g. -DVECTOR_SIZE=16 |
| 233 | * @note Leftover vector size has to be passed at compile time using -DVECTOR_SIZE_LEFTOVER. e.g. -DVECTOR_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VECTOR_SIZE |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 234 | * @note In case the input is not a multiple of VECTOR_SIZE (2,4,8,16) -DNON_MULTIPLE_OF_VECTOR_SIZE must be passed. |
| 235 | * @note Beta can be optionally passed at compile time using -DBETA (by default, it is 1.0). |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 236 | * @note In case of log softmax, -DLOG_SOFTMAX must be passed. |
| 237 | * @note Based on the data type, the minimum possible value must be passed using -DMINVAL. For float it should be defined as -FLT_MAX, while for half it should be -HALF_MAX |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 238 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 239 | * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16/F32 |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 240 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 241 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 242 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 243 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 244 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 245 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 246 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 247 | * @param[in] maxo_ptr Pointer to the max values tensor slice. Supported data types: same as @p src_ptr |
| 248 | * @param[in] maxo_stride_x Stride of the max values tensor in X dimension (in bytes) |
| 249 | * @param[in] maxo_step_x max_stride_x * number of elements along X processed per workitem(in bytes) |
| 250 | * @param[in] maxo_stride_y Stride of the max values tensor in Y dimension (in bytes) |
| 251 | * @param[in] maxo_step_y max_stride_y * number of elements along Y processed per workitem(in bytes) |
| 252 | * @param[in] maxo_stride_z Stride of the max values tensor in Z dimension (in bytes) |
| 253 | * @param[in] maxo_step_z max_stride_z * number of elements along Z processed per workitem(in bytes) |
| 254 | * @param[in] maxo_offset_first_element_in_bytes The offset of the first element in the max values tensor |
| 255 | * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: same as @p src_ptr |
| 256 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 257 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 258 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 259 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 260 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 261 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 262 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 263 | * @param[out] sum_ptr Pointer to the sum values tensor slice. Supported data types: same as @p src_ptr |
| 264 | * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes) |
| 265 | * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes) |
| 266 | * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes) |
| 267 | * @param[in] sum_step_y sum_stride_z * number of elements along Z processed per workitem(in bytes) |
| 268 | * @param[in] sum_stride_z Stride of the sum values tensor in Z dimension (in bytes) |
| 269 | * @param[in] sum_step_z sum_stride_z * number of elements along Z processed per workitem(in bytes) |
| 270 | * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 271 | */ |
| 272 | __kernel void softmax_layer_max_shift_exp_sum_parallel( |
| 273 | TENSOR3D_DECLARATION(src), |
| 274 | TENSOR3D_DECLARATION(maxo), |
| 275 | TENSOR3D_DECLARATION(dst), |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 276 | TENSOR3D_DECLARATION(sum)) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 277 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 278 | const uint lid = get_local_id(0); |
| 279 | const uint x_offs = (VECTOR_SIZE_LEFTOVER + lid * VECTOR_SIZE) * sizeof(DATA_TYPE); |
| 280 | |
| 281 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x_offs + get_global_id(1) * src_stride_y + get_global_id(2) * src_stride_z; |
| 282 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs + get_global_id(1) * dst_stride_y + get_global_id(2) * dst_stride_z; |
| 283 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 284 | Image maxo = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(maxo); |
| 285 | Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(sum); |
| 286 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 287 | #ifdef BETA |
| 288 | // Initialize beta |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 289 | VEC_TYPE beta = (VEC_TYPE)BETA; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 290 | #endif /* BETA */ |
| 291 | |
| 292 | // Define one temporary vector per work-item. |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 293 | __local VEC_TYPE tmp_local[GRID_SIZE]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 294 | __local DATA_TYPE max_local; |
| 295 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 296 | VEC_TYPE max_val_vec = (VEC_TYPE)(MINVAL); |
| 297 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 298 | // Number of iterations per work-item. |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 299 | const uint width = (SRC_WIDTH / GRID_SIZE) >> LOG_VECTOR_SIZE; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 300 | // Calculate max of row |
| 301 | uint i = 0; |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 302 | for(; i < width; ++i) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 303 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 304 | VEC_TYPE data_max = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
| 305 | max_val_vec = max(data_max, max_val_vec); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 306 | } |
| 307 | #ifdef NON_MULTIPLE_OF_GRID_SIZE |
| 308 | // How many work-items needed to complete the computation. |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 309 | int boundary_workitems = (SRC_WIDTH % (GRID_SIZE * VECTOR_SIZE)) / VECTOR_SIZE; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 310 | if(lid < boundary_workitems) |
| 311 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 312 | VEC_TYPE data_max = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
| 313 | max_val_vec = max(data_max, max_val_vec); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 314 | } |
| 315 | #ifdef NON_MULTIPLE_OF_VECTOR_SIZE |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 316 | SELECT_TYPE widx; |
| 317 | if(lid == 0) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 318 | { |
| 319 | // Handle non multiple of 4 |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 320 | VEC_TYPE data_max = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE))); |
| 321 | widx = (SELECT_TYPE)VECTOR_SIZE_LEFTOVER > VEC_OFFS(SELECT_DATA_TYPE(DATA_TYPE), VECTOR_SIZE); |
| 322 | max_val_vec = max(max_val_vec, select((VEC_TYPE)(MINVAL), data_max, widx)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 323 | } |
| 324 | #endif /* NON_MULTIPLE_OF_VECTOR_SIZE */ |
| 325 | #endif /* NON_MULTIPLE_OF_GRID_SIZE */ |
| 326 | tmp_local[lid] = max_val_vec; |
| 327 | |
| 328 | barrier(CLK_LOCAL_MEM_FENCE); |
| 329 | |
| 330 | if(GRID_SIZE >= 256) |
| 331 | { |
| 332 | if(lid < 128) |
| 333 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 334 | tmp_local[lid] = max(tmp_local[lid + 128], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 335 | } |
| 336 | barrier(CLK_LOCAL_MEM_FENCE); |
| 337 | } |
| 338 | if(GRID_SIZE >= 128) |
| 339 | { |
| 340 | if(lid < 64) |
| 341 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 342 | tmp_local[lid] = max(tmp_local[lid + 64], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 343 | } |
| 344 | barrier(CLK_LOCAL_MEM_FENCE); |
| 345 | } |
| 346 | if(GRID_SIZE >= 64) |
| 347 | { |
| 348 | if(lid < 32) |
| 349 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 350 | tmp_local[lid] = max(tmp_local[lid + 32], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 351 | } |
| 352 | barrier(CLK_LOCAL_MEM_FENCE); |
| 353 | } |
| 354 | if(GRID_SIZE >= 32) |
| 355 | { |
| 356 | if(lid < 16) |
| 357 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 358 | tmp_local[lid] = max(tmp_local[lid + 16], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 359 | } |
| 360 | barrier(CLK_LOCAL_MEM_FENCE); |
| 361 | } |
| 362 | if(GRID_SIZE >= 16) |
| 363 | { |
| 364 | if(lid < 8) |
| 365 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 366 | tmp_local[lid] = max(tmp_local[lid + 8], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 367 | } |
| 368 | barrier(CLK_LOCAL_MEM_FENCE); |
| 369 | } |
| 370 | if(GRID_SIZE >= 8) |
| 371 | { |
| 372 | if(lid < 4) |
| 373 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 374 | tmp_local[lid] = max(tmp_local[lid + 4], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 375 | } |
| 376 | barrier(CLK_LOCAL_MEM_FENCE); |
| 377 | } |
| 378 | if(GRID_SIZE >= 4) |
| 379 | { |
| 380 | if(lid < 2) |
| 381 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 382 | tmp_local[lid] = max(tmp_local[lid + 2], tmp_local[lid]); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 383 | } |
| 384 | barrier(CLK_LOCAL_MEM_FENCE); |
| 385 | } |
| 386 | if(lid == 0) |
| 387 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 388 | max_val_vec = max(tmp_local[lid + 1], tmp_local[lid]); |
| 389 | max_local = MAX_REDUCE(max_val_vec, VECTOR_SIZE); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 390 | } |
| 391 | barrier(CLK_LOCAL_MEM_FENCE); |
| 392 | |
| 393 | /* Second section */ |
| 394 | |
| 395 | // Set sum vector |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 396 | VEC_TYPE sum1D = 0; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 397 | DATA_TYPE max_val = max_local; |
| 398 | |
| 399 | // Shift values, exp and sum |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 400 | for(i = 0; i < width; ++i) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 401 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 402 | VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
| 403 | data -= max_val; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 404 | #ifdef BETA |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 405 | data *= beta; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 406 | #endif /* BETA */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 407 | #ifdef LOG_SOFTMAX |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 408 | VSTORE(VECTOR_SIZE) |
| 409 | (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
| 410 | data = exp(data); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 411 | #else /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 412 | data = exp(data); |
| 413 | VSTORE(VECTOR_SIZE) |
| 414 | (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 415 | #endif /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 416 | sum1D += data; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 417 | } |
| 418 | #ifdef NON_MULTIPLE_OF_GRID_SIZE |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 419 | boundary_workitems = (SRC_WIDTH % (GRID_SIZE * VECTOR_SIZE)) / VECTOR_SIZE; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 420 | if(lid < boundary_workitems) |
| 421 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 422 | VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
| 423 | data -= max_val; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 424 | #ifdef BETA |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 425 | data *= beta; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 426 | #endif /* BETA */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 427 | #ifdef LOG_SOFTMAX |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 428 | VSTORE(VECTOR_SIZE) |
| 429 | (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
| 430 | data = exp(data); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 431 | #else /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 432 | data = exp(data); |
| 433 | VSTORE(VECTOR_SIZE) |
| 434 | (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE))); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 435 | #endif /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 436 | sum1D += data; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 437 | } |
| 438 | #ifdef NON_MULTIPLE_OF_VECTOR_SIZE |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 439 | if(lid == 0) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 440 | { |
| 441 | // Handle non multiple of vector size ((GRID_SIZE * i * 4) + 4, 0); move 4 float positions ahead, *4 is due to the stride |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 442 | VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE))); |
| 443 | data -= max_val; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 444 | #ifdef BETA |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 445 | data *= beta; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 446 | #endif /* BETA */ |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 447 | #ifdef LOG_SOFTMAX |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 448 | VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER) |
| 449 | (data, 0, (__global DATA_TYPE *)(dst_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE))); |
| 450 | data = exp(data); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 451 | data = select(0, data, widx); |
| 452 | #else /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 453 | data = exp(data); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 454 | data = select(0, data, widx); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 455 | VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER) |
| 456 | (data, 0, (__global DATA_TYPE *)(dst_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE))); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 457 | #endif /* LOG_SOFTMAX */ |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 458 | sum1D += data; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 459 | } |
| 460 | #endif /* NON_MULTIPLE_OF_VECTOR_SIZE */ |
| 461 | #endif /* NON_MULTIPLE_OF_GRID_SIZE */ |
| 462 | tmp_local[lid] = sum1D; |
| 463 | |
| 464 | barrier(CLK_LOCAL_MEM_FENCE); |
| 465 | |
| 466 | if(GRID_SIZE >= 256) |
| 467 | { |
| 468 | if(lid < 128) |
| 469 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 470 | tmp_local[lid] += tmp_local[lid + 128]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 471 | } |
| 472 | barrier(CLK_LOCAL_MEM_FENCE); |
| 473 | } |
| 474 | if(GRID_SIZE >= 128) |
| 475 | { |
| 476 | if(lid < 64) |
| 477 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 478 | tmp_local[lid] += tmp_local[lid + 64]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 479 | } |
| 480 | barrier(CLK_LOCAL_MEM_FENCE); |
| 481 | } |
| 482 | if(GRID_SIZE >= 64) |
| 483 | { |
| 484 | if(lid < 32) |
| 485 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 486 | tmp_local[lid] += tmp_local[lid + 32]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 487 | } |
| 488 | barrier(CLK_LOCAL_MEM_FENCE); |
| 489 | } |
| 490 | if(GRID_SIZE >= 32) |
| 491 | { |
| 492 | if(lid < 16) |
| 493 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 494 | tmp_local[lid] += tmp_local[lid + 16]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 495 | } |
| 496 | barrier(CLK_LOCAL_MEM_FENCE); |
| 497 | } |
| 498 | if(GRID_SIZE >= 16) |
| 499 | { |
| 500 | if(lid < 8) |
| 501 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 502 | tmp_local[lid] += tmp_local[lid + 8]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 503 | } |
| 504 | barrier(CLK_LOCAL_MEM_FENCE); |
| 505 | } |
| 506 | if(GRID_SIZE >= 8) |
| 507 | { |
| 508 | if(lid < 4) |
| 509 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 510 | tmp_local[lid] += tmp_local[lid + 4]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 511 | } |
| 512 | barrier(CLK_LOCAL_MEM_FENCE); |
| 513 | } |
| 514 | if(GRID_SIZE >= 4) |
| 515 | { |
| 516 | if(lid < 2) |
| 517 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 518 | tmp_local[lid] += tmp_local[lid + 2]; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 519 | } |
| 520 | barrier(CLK_LOCAL_MEM_FENCE); |
| 521 | } |
| 522 | if(lid == 0) |
| 523 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 524 | sum1D = (tmp_local[lid + 1] + tmp_local[lid]); |
| 525 | // Perform sum reduction |
| 526 | *((__global DATA_TYPE *)sum.ptr) = SUM_REDUCE(sum1D, VECTOR_SIZE); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 527 | } |
| 528 | } |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 529 | |
| 530 | #endif // defined(SRC_WIDTH) && defined(LOG_VECTOR_SIZE) && defined(MINVAL) |
| 531 | #endif // defined(DATA_TYPE) && defined(MIN_VALUE) && defined(VECTOR_SIZE) && defined(VECTOR_SIZE_LEFTOVER) |