blob: 9b243803936f0fb4d767288c108f4998957ddded [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "helpers.h"
25
Anthony Barbierac69aa12017-07-03 17:39:37 +010026#ifdef FIXED_POINT_POSITION
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010027
28#include "fixed_point.h"
29#define MAX_OP(x, y, type, size) MAX_OP_EXPAND(x, y, type, size)
30#define ADD_OP(x, y, type, size) ADD_SAT_OP_EXPAND((x), (y), type, size)
31#define SUB_OP(x, y, type, size) SUB_SAT_OP_EXPAND((x), (y), type, size)
steniu010c7614f2017-06-23 17:00:26 +010032#define DIV_OP(x, y, type, size) DIV_SAT_OP_VEC_EXPAND((x), (y), type, size, FIXED_POINT_POSITION)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010033#define EXP_OP(x, type, size) EXP_OP_EXPAND((x), type, size, FIXED_POINT_POSITION)
34
35#define MIN_VAL_EXPAND(type) type##_MIN
36#define MIN_VAL(type) MIN_VAL_EXPAND(type)
37#define MINVAL MIN_VAL(DATA_TYPE)
38#define SELECT_DATA_TYPE EXPAND(DATA_TYPE)
39
Anthony Barbierac69aa12017-07-03 17:39:37 +010040#else /* FIXED_POINT_POSITION */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010041
42#define MAX_OP(x, y, type, size) max((x), (y))
43#define ADD_OP(x, y, type, size) ((x) + (y))
44#define SUB_OP(x, y, type, size) ((x) - (y))
45#define DIV_OP(x, y, type, size) ((x) / (y))
46#define EXP_OP(x, type, size) exp((x))
47
Anthony Barbierac69aa12017-07-03 17:39:37 +010048#ifdef USE_F16
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010049#define MINVAL -HALF_MAX
50#define SELECT_DATA_TYPE short
Anthony Barbierac69aa12017-07-03 17:39:37 +010051#else /* USE_F16 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010052#define MINVAL -FLT_MAX
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053#define SELECT_DATA_TYPE int
Anthony Barbierac69aa12017-07-03 17:39:37 +010054#endif /* USE_F16 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010055
Anthony Barbierac69aa12017-07-03 17:39:37 +010056#endif /* FIXED_POINT_POSITION */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057
58__constant VEC_DATA_TYPE(DATA_TYPE, 16) type_min = (VEC_DATA_TYPE(DATA_TYPE, 16))(MINVAL);
59__constant uint16 idx16 = (uint16)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
60
61/** Identifies the maximum value across the 1st dimension.
62 *
63 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010064 * @note Fixed point position must be given as a preprocessor argument using -DFIXED_POINT_POSITION=pos. e.g. DFIXED_POINT_POSITION=4
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 * @note In case the input is not multiple of 16 -DNON_MULTIPLE_OF_16 must be passed.
66 *
Georgios Pinitas09796752017-07-10 16:05:21 +010067 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: QS8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
69 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
70 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
71 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +010072 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
73 * @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 +010074 * @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 +010075 * @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 +010076 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
77 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
78 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
79 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +010080 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
81 * @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 +010082 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
83 * @param[in] width Input image width
84 */
85__kernel void softmax_layer_max(
steniu010d523cc2017-07-13 14:24:23 +010086 TENSOR3D_DECLARATION(src),
87 TENSOR3D_DECLARATION(dst),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 uint width)
89{
steniu010d523cc2017-07-13 14:24:23 +010090 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
91 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
93 // Initialize local maximum
94 VEC_DATA_TYPE(DATA_TYPE, 16)
95 max_val = (VEC_DATA_TYPE(DATA_TYPE, 16))type_min;
96
97 // Calculate max of row
98 const uint width4 = width >> 4;
99 for(uint i = 0; i < width4; i++)
100 {
101 VEC_DATA_TYPE(DATA_TYPE, 16)
102 data = vload16(0, (__global DATA_TYPE *)offset(&src, i << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100103 max_val = MAX_OP(data, max_val, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 }
105
Anthony Barbierac69aa12017-07-03 17:39:37 +0100106#ifdef NON_MULTIPLE_OF_16
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 // Handle non multiple of 16
108 VEC_DATA_TYPE(DATA_TYPE, 16)
109 data = vload16(0, (__global DATA_TYPE *)offset(&src, width4 << 4, 0));
110 VEC_DATA_TYPE(SELECT_DATA_TYPE, 16)
111 widx = CONVERT(((uint16)(width4 << 4) + idx16) < width, VEC_DATA_TYPE(SELECT_DATA_TYPE, 16));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100112 max_val = MAX_OP(max_val, select(type_min, data, widx), DATA_TYPE, 16);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100113#endif /* NON_MULTIPLE_OF_16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115 // Perform max reduction
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100116 max_val.s01234567 = MAX_OP(max_val.s01234567, max_val.s89ABCDEF, DATA_TYPE, 8);
117 max_val.s0123 = MAX_OP(max_val.s0123, max_val.s4567, DATA_TYPE, 4);
118 max_val.s01 = MAX_OP(max_val.s01, max_val.s23, DATA_TYPE, 2);
119 max_val.s0 = MAX_OP(max_val.s0, max_val.s1, DATA_TYPE, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
121 // Store result
122 *((__global DATA_TYPE *)dst.ptr) = max_val.s0;
123}
124
125/** Shifts the values of the input tensor by the max calculated in softmax_layer_max kernel,
126 * then gets the exponent of each element as sums all elements across each row.
127 *
128 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100129 * @note Fixed point position must be given as a preprocessor argument using -DFIXED_POINT_POSITION=pos. e.g. DFIXED_POINT_POSITION=4
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 * @note In case the input is not multiple of 16 -DNON_MULTIPLE_OF_16 must be passed.
131 *
Georgios Pinitas09796752017-07-10 16:05:21 +0100132 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: QS8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
134 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
135 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
136 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100137 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
138 * @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 +0100139 * @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 +0100140 * @param[in] max_ptr Pointer to the max values tensor slice. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 * @param[in] max_stride_x Stride of the max values tensor in X dimension (in bytes)
142 * @param[in] max_step_x max_stride_x * number of elements along X processed per workitem(in bytes)
143 * @param[in] max_stride_y Stride of the max values tensor in Y dimension (in bytes)
144 * @param[in] max_step_y max_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100145 * @param[in] max_stride_z Stride of the max values tensor in Z dimension (in bytes)
146 * @param[in] max_step_z max_stride_z * number of elements along Z processed per workitem(in bytes)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 * @param[in] max_offset_first_element_in_bytes The offset of the first element in the max values tensor
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100148 * @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 +0100149 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
150 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
151 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
152 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100153 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
154 * @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 +0100155 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100156 * @param[out] sum_ptr Pointer to the sum values tensor slice. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes)
158 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
159 * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100160 * @param[in] sum_step_y sum_stride_z * number of elements along Z processed per workitem(in bytes)
161 * @param[in] sum_stride_z Stride of the sum values tensor in Z dimension (in bytes)
162 * @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 +0100163 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor
164 * @param[in] width Input image width
165 */
166__kernel void softmax_layer_shift_exp_sum(
steniu010d523cc2017-07-13 14:24:23 +0100167 TENSOR3D_DECLARATION(src),
168 TENSOR3D_DECLARATION(max),
169 TENSOR3D_DECLARATION(dst),
170 TENSOR3D_DECLARATION(sum),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 uint width)
172{
steniu010d523cc2017-07-13 14:24:23 +0100173 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
174 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
175 Image max = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(max);
176 Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
178 // Load max value of 1D logits vector (row)
179 DATA_TYPE max_val = *((__global DATA_TYPE *)offset(&max, 0, 0));
180
181 // Set sum vector
182 VEC_DATA_TYPE(DATA_TYPE, 16)
183 sum1D = 0;
184
185 // Shift values, exp and sum
186 const uint width4 = width >> 4;
187 for(uint i = 0; i < width4; i++)
188 {
189 VEC_DATA_TYPE(DATA_TYPE, 16)
190 data = vload16(0, (__global DATA_TYPE *)offset(&src, i << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100191 data = SUB_OP(data, max_val, DATA_TYPE, 16);
192 data = EXP_OP(data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 vstore16(data, 0, (__global DATA_TYPE *)offset(&dst, i << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100194 sum1D = ADD_OP(sum1D, data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 }
196
Anthony Barbierac69aa12017-07-03 17:39:37 +0100197#ifdef NON_MULTIPLE_OF_16
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 // Handle non multiple of 16
199 VEC_DATA_TYPE(DATA_TYPE, 16)
200 data = vload16(0, (__global DATA_TYPE *)offset(&src, width4 << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100201 data = SUB_OP(data, max_val, DATA_TYPE, 16);
202 data = EXP_OP(data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 VEC_DATA_TYPE(SELECT_DATA_TYPE, 16)
204 widx = CONVERT(((uint16)(width4 << 4) + idx16) < width, VEC_DATA_TYPE(SELECT_DATA_TYPE, 16));
205 data = select(0, data, widx);
206 vstore16(data, 0, (__global DATA_TYPE *)offset(&dst, width4 << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100207 sum1D = ADD_OP(sum1D, data, DATA_TYPE, 16);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100208#endif /* NON_MULTIPLE_OF_16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
210 // Perform min/max reduction
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100211 sum1D.s01234567 = ADD_OP(sum1D.s01234567, sum1D.s89ABCDEF, DATA_TYPE, 8);
212 sum1D.s0123 = ADD_OP(sum1D.s0123, sum1D.s4567, DATA_TYPE, 4);
213 sum1D.s01 = ADD_OP(sum1D.s01, sum1D.s23, DATA_TYPE, 2);
214 sum1D.s0 = ADD_OP(sum1D.s0, sum1D.s1, DATA_TYPE, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215
216 // Calculate and store result
217 *((__global DATA_TYPE *)sum.ptr) = sum1D.s0;
218}
219
220/** Divides all the values of the input tensor by the sum calculated from softmax_layer_shift_exp_sum kernel.
221 *
222 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100223 * @note Fixed point position must be given as a preprocessor argument using -DFIXED_POINT_POSITION=pos. e.g. DFIXED_POINT_POSITION=4
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 *
Georgios Pinitas09796752017-07-10 16:05:21 +0100225 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: QS8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
227 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
228 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
229 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100230 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
231 * @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 +0100232 * @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 +0100233 * @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 +0100234 * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes)
235 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
236 * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes)
237 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100238 * @param[in] sum_stride_z Stride of the sum values tensor in Z dimension (in bytes)
239 * @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 +0100240 * @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 +0100241 * @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 +0100242 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
243 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
244 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
245 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
steniu010d523cc2017-07-13 14:24:23 +0100246 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
247 * @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 +0100248 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
249 */
250__kernel void softmax_layer_norm(
steniu010d523cc2017-07-13 14:24:23 +0100251 TENSOR3D_DECLARATION(src),
252 TENSOR3D_DECLARATION(sum),
253 TENSOR3D_DECLARATION(dst))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100254{
steniu010d523cc2017-07-13 14:24:23 +0100255 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
256 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
257 Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258
259 // Load max value of 1D logits vector (row)
260 DATA_TYPE sum_val = *((__global DATA_TYPE *)offset(&sum, 0, get_global_id(1)));
261 VEC_DATA_TYPE(DATA_TYPE, 16)
262 data = vload16(0, (__global DATA_TYPE *)offset(&src, 0, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100263 vstore16(DIV_OP(data, sum_val, DATA_TYPE, 16), 0, (__global DATA_TYPE *)offset(&dst, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264}