blob: a29aea4fae1d8421b770861536ad841190afdf44 [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
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010026#if defined(FIXED_POINT_POSITION)
27
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)
32#define DIV_OP(x, y, type, size) DIV_SAT_OP_EXPAND((x), (y), type, size, FIXED_POINT_POSITION)
33#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 Barbier6ff3b192017-09-04 18:44:23 +010040#else
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
48#if defined USE_F16
49#define MINVAL -HALF_MAX
50#define SELECT_DATA_TYPE short
51#else
52#define MINVAL -FLT_MAX
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053#define SELECT_DATA_TYPE int
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010054#endif
55
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056#endif
57
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 Pinitase5f8fd62017-06-23 18:03:44 +010067 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: QS8/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)
72 * @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 +010073 * @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 +010074 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
75 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
76 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
77 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
78 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
79 * @param[in] width Input image width
80 */
81__kernel void softmax_layer_max(
82 IMAGE_DECLARATION(src),
83 IMAGE_DECLARATION(dst),
84 uint width)
85{
86 Image src = CONVERT_TO_IMAGE_STRUCT(src);
87 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
88
89 // Initialize local maximum
90 VEC_DATA_TYPE(DATA_TYPE, 16)
91 max_val = (VEC_DATA_TYPE(DATA_TYPE, 16))type_min;
92
93 // Calculate max of row
94 const uint width4 = width >> 4;
95 for(uint i = 0; i < width4; i++)
96 {
97 VEC_DATA_TYPE(DATA_TYPE, 16)
98 data = vload16(0, (__global DATA_TYPE *)offset(&src, i << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010099 max_val = MAX_OP(data, max_val, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 }
101
102#if defined NON_MULTIPLE_OF_16
103 // Handle non multiple of 16
104 VEC_DATA_TYPE(DATA_TYPE, 16)
105 data = vload16(0, (__global DATA_TYPE *)offset(&src, width4 << 4, 0));
106 VEC_DATA_TYPE(SELECT_DATA_TYPE, 16)
107 widx = CONVERT(((uint16)(width4 << 4) + idx16) < width, VEC_DATA_TYPE(SELECT_DATA_TYPE, 16));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100108 max_val = MAX_OP(max_val, select(type_min, data, widx), DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109#endif
110
111 // Perform max reduction
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100112 max_val.s01234567 = MAX_OP(max_val.s01234567, max_val.s89ABCDEF, DATA_TYPE, 8);
113 max_val.s0123 = MAX_OP(max_val.s0123, max_val.s4567, DATA_TYPE, 4);
114 max_val.s01 = MAX_OP(max_val.s01, max_val.s23, DATA_TYPE, 2);
115 max_val.s0 = MAX_OP(max_val.s0, max_val.s1, DATA_TYPE, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
117 // Store result
118 *((__global DATA_TYPE *)dst.ptr) = max_val.s0;
119}
120
121/** Shifts the values of the input tensor by the max calculated in softmax_layer_max kernel,
122 * then gets the exponent of each element as sums all elements across each row.
123 *
124 * @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 +0100125 * @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 +0100126 * @note In case the input is not multiple of 16 -DNON_MULTIPLE_OF_16 must be passed.
127 *
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100128 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: QS8/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
130 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
131 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
132 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
133 * @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 +0100134 * @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 +0100135 * @param[in] max_stride_x Stride of the max values tensor in X dimension (in bytes)
136 * @param[in] max_step_x max_stride_x * number of elements along X processed per workitem(in bytes)
137 * @param[in] max_stride_y Stride of the max values tensor in Y dimension (in bytes)
138 * @param[in] max_step_y max_stride_y * number of elements along Y processed per workitem(in bytes)
139 * @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 +0100140 * @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 +0100141 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
142 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
143 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
144 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
145 * @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 +0100146 * @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 +0100147 * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes)
148 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
149 * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes)
150 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
151 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor
152 * @param[in] width Input image width
153 */
154__kernel void softmax_layer_shift_exp_sum(
155 IMAGE_DECLARATION(src),
156 IMAGE_DECLARATION(max),
157 IMAGE_DECLARATION(dst),
158 IMAGE_DECLARATION(sum),
159 uint width)
160{
161 Image src = CONVERT_TO_IMAGE_STRUCT(src);
162 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
163 Image max = CONVERT_TO_IMAGE_STRUCT(max);
164 Image sum = CONVERT_TO_IMAGE_STRUCT(sum);
165
166 // Load max value of 1D logits vector (row)
167 DATA_TYPE max_val = *((__global DATA_TYPE *)offset(&max, 0, 0));
168
169 // Set sum vector
170 VEC_DATA_TYPE(DATA_TYPE, 16)
171 sum1D = 0;
172
173 // Shift values, exp and sum
174 const uint width4 = width >> 4;
175 for(uint i = 0; i < width4; i++)
176 {
177 VEC_DATA_TYPE(DATA_TYPE, 16)
178 data = vload16(0, (__global DATA_TYPE *)offset(&src, i << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100179 data = SUB_OP(data, max_val, DATA_TYPE, 16);
180 data = EXP_OP(data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 vstore16(data, 0, (__global DATA_TYPE *)offset(&dst, i << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100182 sum1D = ADD_OP(sum1D, data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 }
184
185#if defined NON_MULTIPLE_OF_16
186 // Handle non multiple of 16
187 VEC_DATA_TYPE(DATA_TYPE, 16)
188 data = vload16(0, (__global DATA_TYPE *)offset(&src, width4 << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100189 data = SUB_OP(data, max_val, DATA_TYPE, 16);
190 data = EXP_OP(data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 VEC_DATA_TYPE(SELECT_DATA_TYPE, 16)
192 widx = CONVERT(((uint16)(width4 << 4) + idx16) < width, VEC_DATA_TYPE(SELECT_DATA_TYPE, 16));
193 data = select(0, data, widx);
194 vstore16(data, 0, (__global DATA_TYPE *)offset(&dst, width4 << 4, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100195 sum1D = ADD_OP(sum1D, data, DATA_TYPE, 16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196#endif
197
198 // Perform min/max reduction
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100199 sum1D.s01234567 = ADD_OP(sum1D.s01234567, sum1D.s89ABCDEF, DATA_TYPE, 8);
200 sum1D.s0123 = ADD_OP(sum1D.s0123, sum1D.s4567, DATA_TYPE, 4);
201 sum1D.s01 = ADD_OP(sum1D.s01, sum1D.s23, DATA_TYPE, 2);
202 sum1D.s0 = ADD_OP(sum1D.s0, sum1D.s1, DATA_TYPE, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203
204 // Calculate and store result
205 *((__global DATA_TYPE *)sum.ptr) = sum1D.s0;
206}
207
208/** Divides all the values of the input tensor by the sum calculated from softmax_layer_shift_exp_sum kernel.
209 *
210 * @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 +0100211 * @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 +0100212 *
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100213 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: QS8/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
215 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
216 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
217 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
218 * @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 +0100219 * @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 +0100220 * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes)
221 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
222 * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes)
223 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
224 * @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 +0100225 * @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 +0100226 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
227 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
228 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
229 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
230 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
231 */
232__kernel void softmax_layer_norm(
233 IMAGE_DECLARATION(src),
234 IMAGE_DECLARATION(sum),
235 IMAGE_DECLARATION(dst))
236{
237 Image src = CONVERT_TO_IMAGE_STRUCT(src);
238 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
239 Image sum = CONVERT_TO_IMAGE_STRUCT_NO_STEP(sum);
240
241 // Load max value of 1D logits vector (row)
242 DATA_TYPE sum_val = *((__global DATA_TYPE *)offset(&sum, 0, get_global_id(1)));
243 VEC_DATA_TYPE(DATA_TYPE, 16)
244 data = vload16(0, (__global DATA_TYPE *)offset(&src, 0, 0));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100245 vstore16(DIV_OP(data, sum_val, DATA_TYPE, 16), 0, (__global DATA_TYPE *)offset(&dst, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246}