blob: 01f5de47cf4a3b4d48c3ce5fb64f34b0e1067306 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Arena2d1a8352020-10-26 15:04:08 +000026#if defined(DATA_TYPE) && defined(MIN_VALUE) && defined(VECTOR_SIZE) && defined(VECTOR_SIZE_LEFTOVER)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028/** Divides all the values of the input tensor by the sum calculated from softmax_layer_shift_exp_sum kernel.
29 *
Giorgio Arena2d1a8352020-10-26 15:04:08 +000030 * @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 Barbier6ff3b192017-09-04 18:44:23 +010035 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010036 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037 * @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)
steniu010d523cc2017-07-13 14:24:23 +010041 * @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 Barbier6ff3b192017-09-04 18:44:23 +010043 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010044 * @param[in] sum_ptr Pointer to the sum values tensor slice. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 * @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)
steniu010d523cc2017-07-13 14:24:23 +010049 * @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 Barbier6ff3b192017-09-04 18:44:23 +010051 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010052 * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 * @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)
steniu010d523cc2017-07-13 14:24:23 +010057 * @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 Barbier6ff3b192017-09-04 18:44:23 +010059 * @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(
steniu010d523cc2017-07-13 14:24:23 +010062 TENSOR3D_DECLARATION(src),
63 TENSOR3D_DECLARATION(sum),
64 TENSOR3D_DECLARATION(dst))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
Giorgio Arena2d1a8352020-10-26 15:04:08 +000066 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
steniu010d523cc2017-07-13 14:24:23 +010071 Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
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 Arena2d1a8352020-10-26 15:04:08 +000075 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 Parka0205b92020-07-07 09:36:09 +010079 sum_val = log(sum_val);
Giorgio Arena2d1a8352020-10-26 15:04:08 +000080 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 Barbier6ff3b192017-09-04 18:44:23 +010086}
Chunosovd6afedc2017-11-06 22:09:45 +070087
Giorgio Arena2d1a8352020-10-26 15:04:08 +000088#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
Chunosovd6afedc2017-11-06 22:09:45 +070098/** 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 Arena2d1a8352020-10-26 15:04:08 +0000101 * @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
Chunosovd6afedc2017-11-06 22:09:45 +0700105 * @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 Arena2d1a8352020-10-26 15:04:08 +0000107 * @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
Chunosovd6afedc2017-11-06 22:09:45 +0700109 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100110 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16/F32
Chunosovd6afedc2017-11-06 22:09:45 +0700111 * @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
Chunosovd6afedc2017-11-06 22:09:45 +0700142 */
143__kernel void softmax_layer_max_shift_exp_sum_serial(
144 TENSOR3D_DECLARATION(src),
145 TENSOR3D_DECLARATION(maxo),
146 TENSOR3D_DECLARATION(dst),
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000147 TENSOR3D_DECLARATION(sum))
Chunosovd6afedc2017-11-06 22:09:45 +0700148{
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000149 __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
Chunosovd6afedc2017-11-06 22:09:45 +0700152 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 Arena2d1a8352020-10-26 15:04:08 +0000157 VEC_TYPE beta = (VEC_TYPE)BETA;
Chunosovd6afedc2017-11-06 22:09:45 +0700158#endif /* BETA */
159
160 // Initialize local maximum
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000161 VEC_TYPE max_val_vec = (VEC_TYPE)(MINVAL);
Chunosovd6afedc2017-11-06 22:09:45 +0700162
163#ifdef NON_MULTIPLE_OF_VECTOR_SIZE
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000164 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));
Chunosovd6afedc2017-11-06 22:09:45 +0700167#endif /* NON_MULTIPLE_OF_VECTOR_SIZE */
168
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000169 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
Chunosovd6afedc2017-11-06 22:09:45 +0700175 // Perform max reduction
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000176 DATA_TYPE max_val = MAX_REDUCE(max_val_vec, VECTOR_SIZE);
177 *((__global DATA_TYPE *)maxo.ptr) = max_val;
Chunosovd6afedc2017-11-06 22:09:45 +0700178
179 /* Second section */
180
Chunosovd6afedc2017-11-06 22:09:45 +0700181 // Set sum vector
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000182 VEC_TYPE sum1D = 0;
Chunosovd6afedc2017-11-06 22:09:45 +0700183
184#ifdef NON_MULTIPLE_OF_VECTOR_SIZE
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000185 data -= max_val;
Chunosovd6afedc2017-11-06 22:09:45 +0700186#ifdef BETA
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000187 data *= beta;
Chunosovd6afedc2017-11-06 22:09:45 +0700188#endif /* BETA */
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000189#ifdef LOG_SOFTMAX
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000190 VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER)
191 (data, 0, (__global DATA_TYPE *)dst_addr);
192 data = exp(data);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000193 data = select(0, data, widx);
194#else /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000195 data = exp(data);
Chunosovd6afedc2017-11-06 22:09:45 +0700196 data = select(0, data, widx);
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000197 VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER)
198 (data, 0, (__global DATA_TYPE *)dst_addr);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000199#endif /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000200 sum1D += data;
Chunosovd6afedc2017-11-06 22:09:45 +0700201#endif /* NON_MULTIPLE_OF_VECTOR_SIZE */
202
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000203 // 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 }
Chunosovd6afedc2017-11-06 22:09:45 +0700222
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000223 // Perform sum reduction
224 *((__global DATA_TYPE *)sum.ptr) = SUM_REDUCE(sum1D, VECTOR_SIZE);
Chunosovd6afedc2017-11-06 22:09:45 +0700225}
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 Arena2d1a8352020-10-26 15:04:08 +0000230 * @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
Chunosovd6afedc2017-11-06 22:09:45 +0700234 * @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 Arena2d1a8352020-10-26 15:04:08 +0000236 * @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
Chunosovd6afedc2017-11-06 22:09:45 +0700238 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100239 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16/F32
Chunosovd6afedc2017-11-06 22:09:45 +0700240 * @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
Chunosovd6afedc2017-11-06 22:09:45 +0700271 */
272__kernel void softmax_layer_max_shift_exp_sum_parallel(
273 TENSOR3D_DECLARATION(src),
274 TENSOR3D_DECLARATION(maxo),
275 TENSOR3D_DECLARATION(dst),
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000276 TENSOR3D_DECLARATION(sum))
Chunosovd6afedc2017-11-06 22:09:45 +0700277{
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000278 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
Chunosovd6afedc2017-11-06 22:09:45 +0700284 Image maxo = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(maxo);
285 Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(sum);
286
Chunosovd6afedc2017-11-06 22:09:45 +0700287#ifdef BETA
288 // Initialize beta
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000289 VEC_TYPE beta = (VEC_TYPE)BETA;
Chunosovd6afedc2017-11-06 22:09:45 +0700290#endif /* BETA */
291
292 // Define one temporary vector per work-item.
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000293 __local VEC_TYPE tmp_local[GRID_SIZE];
Chunosovd6afedc2017-11-06 22:09:45 +0700294 __local DATA_TYPE max_local;
295
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000296 VEC_TYPE max_val_vec = (VEC_TYPE)(MINVAL);
297
Chunosovd6afedc2017-11-06 22:09:45 +0700298 // Number of iterations per work-item.
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000299 const uint width = (SRC_WIDTH / GRID_SIZE) >> LOG_VECTOR_SIZE;
Chunosovd6afedc2017-11-06 22:09:45 +0700300 // Calculate max of row
301 uint i = 0;
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000302 for(; i < width; ++i)
Chunosovd6afedc2017-11-06 22:09:45 +0700303 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000304 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);
Chunosovd6afedc2017-11-06 22:09:45 +0700306 }
307#ifdef NON_MULTIPLE_OF_GRID_SIZE
308 // How many work-items needed to complete the computation.
309 //TODO: Optimize this calculation (avoid %).
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000310 int boundary_workitems = (SRC_WIDTH % (GRID_SIZE * VECTOR_SIZE)) / VECTOR_SIZE;
Chunosovd6afedc2017-11-06 22:09:45 +0700311 if(lid < boundary_workitems)
312 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000313 VEC_TYPE data_max = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
314 max_val_vec = max(data_max, max_val_vec);
Chunosovd6afedc2017-11-06 22:09:45 +0700315 }
316#ifdef NON_MULTIPLE_OF_VECTOR_SIZE
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000317 SELECT_TYPE widx;
318 if(lid == 0)
Chunosovd6afedc2017-11-06 22:09:45 +0700319 {
320 // Handle non multiple of 4
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000321 VEC_TYPE data_max = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE)));
322 widx = (SELECT_TYPE)VECTOR_SIZE_LEFTOVER > VEC_OFFS(SELECT_DATA_TYPE(DATA_TYPE), VECTOR_SIZE);
323 max_val_vec = max(max_val_vec, select((VEC_TYPE)(MINVAL), data_max, widx));
Chunosovd6afedc2017-11-06 22:09:45 +0700324 }
325#endif /* NON_MULTIPLE_OF_VECTOR_SIZE */
326#endif /* NON_MULTIPLE_OF_GRID_SIZE */
327 tmp_local[lid] = max_val_vec;
328
329 barrier(CLK_LOCAL_MEM_FENCE);
330
331 if(GRID_SIZE >= 256)
332 {
333 if(lid < 128)
334 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000335 tmp_local[lid] = max(tmp_local[lid + 128], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700336 }
337 barrier(CLK_LOCAL_MEM_FENCE);
338 }
339 if(GRID_SIZE >= 128)
340 {
341 if(lid < 64)
342 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000343 tmp_local[lid] = max(tmp_local[lid + 64], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700344 }
345 barrier(CLK_LOCAL_MEM_FENCE);
346 }
347 if(GRID_SIZE >= 64)
348 {
349 if(lid < 32)
350 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000351 tmp_local[lid] = max(tmp_local[lid + 32], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700352 }
353 barrier(CLK_LOCAL_MEM_FENCE);
354 }
355 if(GRID_SIZE >= 32)
356 {
357 if(lid < 16)
358 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000359 tmp_local[lid] = max(tmp_local[lid + 16], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700360 }
361 barrier(CLK_LOCAL_MEM_FENCE);
362 }
363 if(GRID_SIZE >= 16)
364 {
365 if(lid < 8)
366 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000367 tmp_local[lid] = max(tmp_local[lid + 8], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700368 }
369 barrier(CLK_LOCAL_MEM_FENCE);
370 }
371 if(GRID_SIZE >= 8)
372 {
373 if(lid < 4)
374 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000375 tmp_local[lid] = max(tmp_local[lid + 4], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700376 }
377 barrier(CLK_LOCAL_MEM_FENCE);
378 }
379 if(GRID_SIZE >= 4)
380 {
381 if(lid < 2)
382 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000383 tmp_local[lid] = max(tmp_local[lid + 2], tmp_local[lid]);
Chunosovd6afedc2017-11-06 22:09:45 +0700384 }
385 barrier(CLK_LOCAL_MEM_FENCE);
386 }
387 if(lid == 0)
388 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000389 max_val_vec = max(tmp_local[lid + 1], tmp_local[lid]);
390 max_local = MAX_REDUCE(max_val_vec, VECTOR_SIZE);
Chunosovd6afedc2017-11-06 22:09:45 +0700391 }
392 barrier(CLK_LOCAL_MEM_FENCE);
393
394 /* Second section */
395
396 // Set sum vector
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000397 VEC_TYPE sum1D = 0;
Chunosovd6afedc2017-11-06 22:09:45 +0700398 DATA_TYPE max_val = max_local;
399
400 // Shift values, exp and sum
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000401 for(i = 0; i < width; ++i)
Chunosovd6afedc2017-11-06 22:09:45 +0700402 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000403 VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
404 data -= max_val;
Chunosovd6afedc2017-11-06 22:09:45 +0700405#ifdef BETA
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000406 data *= beta;
Chunosovd6afedc2017-11-06 22:09:45 +0700407#endif /* BETA */
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000408#ifdef LOG_SOFTMAX
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000409 VSTORE(VECTOR_SIZE)
410 (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
411 data = exp(data);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000412#else /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000413 data = exp(data);
414 VSTORE(VECTOR_SIZE)
415 (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000416#endif /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000417 sum1D += data;
Chunosovd6afedc2017-11-06 22:09:45 +0700418 }
419#ifdef NON_MULTIPLE_OF_GRID_SIZE
420 //TODO: Optimize the calculation (avoid %).
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000421 boundary_workitems = (SRC_WIDTH % (GRID_SIZE * VECTOR_SIZE)) / VECTOR_SIZE;
Chunosovd6afedc2017-11-06 22:09:45 +0700422 if(lid < boundary_workitems)
423 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000424 VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(__global DATA_TYPE *)(src_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
425 data -= max_val;
Chunosovd6afedc2017-11-06 22:09:45 +0700426#ifdef BETA
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000427 data *= beta;
Chunosovd6afedc2017-11-06 22:09:45 +0700428#endif /* BETA */
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000429#ifdef LOG_SOFTMAX
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000430 VSTORE(VECTOR_SIZE)
431 (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
432 data = exp(data);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000433#else /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000434 data = exp(data);
435 VSTORE(VECTOR_SIZE)
436 (data, 0, (__global DATA_TYPE *)(dst_addr + (i * GRID_SIZE * VECTOR_SIZE) * sizeof(DATA_TYPE)));
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000437#endif /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000438 sum1D += data;
Chunosovd6afedc2017-11-06 22:09:45 +0700439 }
440#ifdef NON_MULTIPLE_OF_VECTOR_SIZE
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000441 if(lid == 0)
Chunosovd6afedc2017-11-06 22:09:45 +0700442 {
443 // Handle non multiple of vector size ((GRID_SIZE * i * 4) + 4, 0); move 4 float positions ahead, *4 is due to the stride
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000444 VEC_TYPE data = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(src_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE)));
445 data -= max_val;
Chunosovd6afedc2017-11-06 22:09:45 +0700446#ifdef BETA
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000447 data *= beta;
Chunosovd6afedc2017-11-06 22:09:45 +0700448#endif /* BETA */
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000449#ifdef LOG_SOFTMAX
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000450 VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER)
451 (data, 0, (__global DATA_TYPE *)(dst_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE)));
452 data = exp(data);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000453 data = select(0, data, widx);
454#else /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000455 data = exp(data);
Chunosovd6afedc2017-11-06 22:09:45 +0700456 data = select(0, data, widx);
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000457 VSTORE_PARTIAL(VECTOR_SIZE, VECTOR_SIZE_LEFTOVER)
458 (data, 0, (__global DATA_TYPE *)(dst_addr - VECTOR_SIZE_LEFTOVER * sizeof(DATA_TYPE)));
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000459#endif /* LOG_SOFTMAX */
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000460 sum1D += data;
Chunosovd6afedc2017-11-06 22:09:45 +0700461 }
462#endif /* NON_MULTIPLE_OF_VECTOR_SIZE */
463#endif /* NON_MULTIPLE_OF_GRID_SIZE */
464 tmp_local[lid] = sum1D;
465
466 barrier(CLK_LOCAL_MEM_FENCE);
467
468 if(GRID_SIZE >= 256)
469 {
470 if(lid < 128)
471 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000472 tmp_local[lid] += tmp_local[lid + 128];
Chunosovd6afedc2017-11-06 22:09:45 +0700473 }
474 barrier(CLK_LOCAL_MEM_FENCE);
475 }
476 if(GRID_SIZE >= 128)
477 {
478 if(lid < 64)
479 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000480 tmp_local[lid] += tmp_local[lid + 64];
Chunosovd6afedc2017-11-06 22:09:45 +0700481 }
482 barrier(CLK_LOCAL_MEM_FENCE);
483 }
484 if(GRID_SIZE >= 64)
485 {
486 if(lid < 32)
487 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000488 tmp_local[lid] += tmp_local[lid + 32];
Chunosovd6afedc2017-11-06 22:09:45 +0700489 }
490 barrier(CLK_LOCAL_MEM_FENCE);
491 }
492 if(GRID_SIZE >= 32)
493 {
494 if(lid < 16)
495 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000496 tmp_local[lid] += tmp_local[lid + 16];
Chunosovd6afedc2017-11-06 22:09:45 +0700497 }
498 barrier(CLK_LOCAL_MEM_FENCE);
499 }
500 if(GRID_SIZE >= 16)
501 {
502 if(lid < 8)
503 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000504 tmp_local[lid] += tmp_local[lid + 8];
Chunosovd6afedc2017-11-06 22:09:45 +0700505 }
506 barrier(CLK_LOCAL_MEM_FENCE);
507 }
508 if(GRID_SIZE >= 8)
509 {
510 if(lid < 4)
511 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000512 tmp_local[lid] += tmp_local[lid + 4];
Chunosovd6afedc2017-11-06 22:09:45 +0700513 }
514 barrier(CLK_LOCAL_MEM_FENCE);
515 }
516 if(GRID_SIZE >= 4)
517 {
518 if(lid < 2)
519 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000520 tmp_local[lid] += tmp_local[lid + 2];
Chunosovd6afedc2017-11-06 22:09:45 +0700521 }
522 barrier(CLK_LOCAL_MEM_FENCE);
523 }
524 if(lid == 0)
525 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000526 sum1D = (tmp_local[lid + 1] + tmp_local[lid]);
527 // Perform sum reduction
528 *((__global DATA_TYPE *)sum.ptr) = SUM_REDUCE(sum1D, VECTOR_SIZE);
Chunosovd6afedc2017-11-06 22:09:45 +0700529 }
530}
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000531
532#endif // defined(SRC_WIDTH) && defined(LOG_VECTOR_SIZE) && defined(MINVAL)
533#endif // defined(DATA_TYPE) && defined(MIN_VALUE) && defined(VECTOR_SIZE) && defined(VECTOR_SIZE_LEFTOVER)