blob: 632b4a537471bcdbec15beaeaf94cad1f42e6cb6 [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
26#if defined USE_F16
27#define MINVAL HALF_MIN
28#define SELECT_DATA_TYPE short
29#define DATA_TYPE half
30#else
31#define MINVAL FLT_MIN
32#define SELECT_DATA_TYPE int
33#define DATA_TYPE float
34#endif
35
36__constant VEC_DATA_TYPE(DATA_TYPE, 16) type_min = (VEC_DATA_TYPE(DATA_TYPE, 16))(MINVAL);
37__constant uint16 idx16 = (uint16)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
38
39/** Identifies the maximum value across the 1st dimension.
40 *
41 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
42 * @note In case F16 is used -DUSE_HALF must be passed otherwise the kernel will default to used F32.
43 * @note In case the input is not multiple of 16 -DNON_MULTIPLE_OF_16 must be passed.
44 *
45 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16, F32
46 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
47 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
49 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
51 * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: F16, F32
52 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
53 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
54 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
55 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
56 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
57 * @param[in] width Input image width
58 */
59__kernel void softmax_layer_max(
60 IMAGE_DECLARATION(src),
61 IMAGE_DECLARATION(dst),
62 uint width)
63{
64 Image src = CONVERT_TO_IMAGE_STRUCT(src);
65 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
66
67 // Initialize local maximum
68 VEC_DATA_TYPE(DATA_TYPE, 16)
69 max_val = (VEC_DATA_TYPE(DATA_TYPE, 16))type_min;
70
71 // Calculate max of row
72 const uint width4 = width >> 4;
73 for(uint i = 0; i < width4; i++)
74 {
75 VEC_DATA_TYPE(DATA_TYPE, 16)
76 data = vload16(0, (__global DATA_TYPE *)offset(&src, i << 4, 0));
77 max_val = max(data, max_val);
78 }
79
80#if defined NON_MULTIPLE_OF_16
81 // Handle non multiple of 16
82 VEC_DATA_TYPE(DATA_TYPE, 16)
83 data = vload16(0, (__global DATA_TYPE *)offset(&src, width4 << 4, 0));
84 VEC_DATA_TYPE(SELECT_DATA_TYPE, 16)
85 widx = CONVERT(((uint16)(width4 << 4) + idx16) < width, VEC_DATA_TYPE(SELECT_DATA_TYPE, 16));
86 max_val = max(max_val, select(type_min, data, widx));
87#endif
88
89 // Perform max reduction
90 max_val.s01234567 = max(max_val.s01234567, max_val.s89ABCDEF);
91 max_val.s0123 = max(max_val.s0123, max_val.s4567);
92 max_val.s01 = max(max_val.s01, max_val.s23);
93 max_val.s0 = max(max_val.s0, max_val.s1);
94
95 // Store result
96 *((__global DATA_TYPE *)dst.ptr) = max_val.s0;
97}
98
99/** Shifts the values of the input tensor by the max calculated in softmax_layer_max kernel,
100 * then gets the exponent of each element as sums all elements across each row.
101 *
102 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
103 * @note In case F16 is used -DUSE_HALF must be passed otherwise the kernel will default to used F32.
104 * @note In case the input is not multiple of 16 -DNON_MULTIPLE_OF_16 must be passed.
105 *
106 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16, F32
107 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
108 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
109 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
110 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
111 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
112 * @param[in] max_ptr Pointer to the max values tensor slice. Supported data types: F16, F32
113 * @param[in] max_stride_x Stride of the max values tensor in X dimension (in bytes)
114 * @param[in] max_step_x max_stride_x * number of elements along X processed per workitem(in bytes)
115 * @param[in] max_stride_y Stride of the max values tensor in Y dimension (in bytes)
116 * @param[in] max_step_y max_stride_y * number of elements along Y processed per workitem(in bytes)
117 * @param[in] max_offset_first_element_in_bytes The offset of the first element in the max values tensor
118 * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: F16, F32
119 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
120 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
121 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
122 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
123 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
124 * @param[out] sum_ptr Pointer to the sum values tensor slice. Supported data types: F16, F32
125 * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes)
126 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
127 * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes)
128 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
129 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor
130 * @param[in] width Input image width
131 */
132__kernel void softmax_layer_shift_exp_sum(
133 IMAGE_DECLARATION(src),
134 IMAGE_DECLARATION(max),
135 IMAGE_DECLARATION(dst),
136 IMAGE_DECLARATION(sum),
137 uint width)
138{
139 Image src = CONVERT_TO_IMAGE_STRUCT(src);
140 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
141 Image max = CONVERT_TO_IMAGE_STRUCT(max);
142 Image sum = CONVERT_TO_IMAGE_STRUCT(sum);
143
144 // Load max value of 1D logits vector (row)
145 DATA_TYPE max_val = *((__global DATA_TYPE *)offset(&max, 0, 0));
146
147 // Set sum vector
148 VEC_DATA_TYPE(DATA_TYPE, 16)
149 sum1D = 0;
150
151 // Shift values, exp and sum
152 const uint width4 = width >> 4;
153 for(uint i = 0; i < width4; i++)
154 {
155 VEC_DATA_TYPE(DATA_TYPE, 16)
156 data = vload16(0, (__global DATA_TYPE *)offset(&src, i << 4, 0));
157 data = exp(data - max_val);
158 vstore16(data, 0, (__global DATA_TYPE *)offset(&dst, i << 4, 0));
159 sum1D += data;
160 }
161
162#if defined NON_MULTIPLE_OF_16
163 // Handle non multiple of 16
164 VEC_DATA_TYPE(DATA_TYPE, 16)
165 data = vload16(0, (__global DATA_TYPE *)offset(&src, width4 << 4, 0));
166 data = exp(data - max_val);
167 VEC_DATA_TYPE(SELECT_DATA_TYPE, 16)
168 widx = CONVERT(((uint16)(width4 << 4) + idx16) < width, VEC_DATA_TYPE(SELECT_DATA_TYPE, 16));
169 data = select(0, data, widx);
170 vstore16(data, 0, (__global DATA_TYPE *)offset(&dst, width4 << 4, 0));
171 sum1D += data;
172#endif
173
174 // Perform min/max reduction
175 sum1D.s01234567 = sum1D.s01234567 + sum1D.s89ABCDEF;
176 sum1D.s0123 = sum1D.s0123 + sum1D.s4567;
177 sum1D.s01 = sum1D.s01 + sum1D.s23;
178 sum1D.s0 = sum1D.s0 + sum1D.s1;
179
180 // Calculate and store result
181 *((__global DATA_TYPE *)sum.ptr) = sum1D.s0;
182}
183
184/** Divides all the values of the input tensor by the sum calculated from softmax_layer_shift_exp_sum kernel.
185 *
186 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
187 *
188 * @param[in] src_ptr Pointer to the source tensor slice. Supported data types: F16, F32
189 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
190 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
191 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
192 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
193 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
194 * @param[in] sum_ptr Pointer to the sum values tensor slice. Supported data types: F16, F32
195 * @param[in] sum_stride_x Stride of the sum values tensor in X dimension (in bytes)
196 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
197 * @param[in] sum_stride_y Stride of the sum values tensor in Y dimension (in bytes)
198 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
199 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the sum values tensor
200 * @param[out] dst_ptr Pointer to the destination tensor slice. Supported data types: F16, F32
201 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
202 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
203 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
204 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
205 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
206 */
207__kernel void softmax_layer_norm(
208 IMAGE_DECLARATION(src),
209 IMAGE_DECLARATION(sum),
210 IMAGE_DECLARATION(dst))
211{
212 Image src = CONVERT_TO_IMAGE_STRUCT(src);
213 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
214 Image sum = CONVERT_TO_IMAGE_STRUCT_NO_STEP(sum);
215
216 // Load max value of 1D logits vector (row)
217 DATA_TYPE sum_val = *((__global DATA_TYPE *)offset(&sum, 0, get_global_id(1)));
218 VEC_DATA_TYPE(DATA_TYPE, 16)
219 data = vload16(0, (__global DATA_TYPE *)offset(&src, 0, 0));
220 vstore16(data / sum_val, 0, (__global DATA_TYPE *)offset(&dst, 0, 0));
221}