blob: ff4dc8ec38ee41b41189f1c70479fd0fdbca9276 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2018 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
Michele Di Giorgio6c928342017-06-22 16:55:57 +010026#define MUL_OP(x, y) ((x) * (y))
27#define ADD_OP(x, y) ((x) + (y))
28#define DIV_OP(x, y) ((x) / (y))
29#define POW_OP(x, y) pow((x), (y))
30#define SQCVT_SAT(a) (a)
31
32#define LOAD_OP(offset, ptr) vload4(offset, ptr)
33#define STORE_OP(data, offset, ptr) vstore4(data, offset, ptr)
34
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000035#if defined(NUM_SLICES)
Georgios Pinitas01624362017-11-30 10:53:31 +000036/** Apply cross-map normalization.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037 *
38 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
Michele Di Giorgio6c928342017-06-22 16:55:57 +010039 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010040 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
41 * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192
Michele Di Giorgio6c928342017-06-22 16:55:57 +010042 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010044 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010045 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
46 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
47 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
48 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
49 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
50 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
51 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
52 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
53 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
54 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
55 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
56 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
57 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
58 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
59 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 */
61__kernel void normalization_layer_cross_map(TENSOR3D_DECLARATION(input),
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010062 TENSOR3D_DECLARATION(output))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063{
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010064 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
65 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066
Michele Di Giorgio6c928342017-06-22 16:55:57 +010067 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
68 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
69 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
70 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
71 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
72 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
73 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
74 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 const int current_slice = get_global_id(2);
Georgios Pinitas01624362017-11-30 10:53:31 +000077 const int left_slice = max(-(int)RADIUS, -current_slice);
78 const int right_slice = min((int)RADIUS, (int)NUM_SLICES - 1 - current_slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
80 for(int i = left_slice; i <= right_slice; i++)
81 {
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010082 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Georgios Pinitas624b7782017-11-15 16:17:22 +000083 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, 0, 0, i));
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010084 acc = ADD_OP(acc, MUL_OP(values, values));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 }
86
Michele Di Giorgio6c928342017-06-22 16:55:57 +010087 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
88 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
89 normalized = POW_OP(acc, beta_v);
90 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
91 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
Michele Di Giorgio6c928342017-06-22 16:55:57 +010093 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094}
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000095#endif /* defined(NUM_SLICES) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
Georgios Pinitas7f32d012018-10-11 18:41:19 +010097#if defined(WIDTH_SIZE)
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000098/** Apply in-map normalization when tensors are in the NCHW data layout format.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 *
100 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100101 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100102 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100103 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100105 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100106 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
107 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
108 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
109 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
110 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
111 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
112 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
113 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
114 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
115 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
116 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
117 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
118 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
119 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
120 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 */
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000122__kernel void normalization_layer_in_map_nchw(TENSOR3D_DECLARATION(input),
123 TENSOR3D_DECLARATION(output))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100125 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
126 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100128 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
129 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
130 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
131 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
132 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
133 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
134 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
135 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136
Georgios Pinitas01624362017-11-30 10:53:31 +0000137 const int current_col = get_global_id(0) << 2;
138 const int left_pos = max(-(int)RADIUS, -3 - current_col);
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100139 const int right_pos = min((int)RADIUS, (int)WIDTH_SIZE - 1 - current_col);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
Georgios Pinitas01624362017-11-30 10:53:31 +0000141#if defined(IN_MAP_2D)
142 const int current_row = get_global_id(1);
143 const int first_row = max(-(int)RADIUS, -current_row);
144 const int last_row = min((int)RADIUS, (int)get_global_size(1) - 1 - current_row);
145#endif /* defined(IN_MAP_2D) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
Georgios Pinitas01624362017-11-30 10:53:31 +0000147#if defined(IN_MAP_2D)
148 for(int j = first_row; j <= last_row; ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 {
Georgios Pinitas01624362017-11-30 10:53:31 +0000150#endif /* defined(IN_MAP_2D) */
151 for(int i = left_pos; i <= right_pos; ++i)
152 {
153#if defined(IN_MAP_2D)
154 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
155 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, j, 0));
156#else /* defined(IN_MAP_2D) */
157 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
158 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, 0, 0));
159#endif /* defined(IN_MAP_2D) */
160 acc = ADD_OP(acc, MUL_OP(values, values));
161 }
162#if defined(IN_MAP_2D)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
Georgios Pinitas01624362017-11-30 10:53:31 +0000164#endif /* defined(IN_MAP_2D) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100166 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
167 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
168 normalized = POW_OP(acc, beta_v);
169 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
170 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100172 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173}
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100174#endif // defined(WIDTH_SIZE)
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000175
176#if defined(NUM_SLICES)
177/** Apply in-map normalization when tensors are in the NHWC data layout format.
178 *
179 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
180 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
181 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
182 * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192
183 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
184 *
185 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
186 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
187 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
188 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
189 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
190 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
191 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
192 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
193 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
194 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
195 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
196 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
197 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
198 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
199 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
200 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
201 */
202__kernel void normalization_layer_in_map_nhwc(TENSOR3D_DECLARATION(input),
203 TENSOR3D_DECLARATION(output))
204{
205 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
206 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
207
208 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
209 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
210 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
211 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
212 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
213 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
214 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
215 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
216
217 const int current_cols = get_global_id(1);
218 const int first_col = max(-(int)RADIUS, -current_cols);
219 const int last_col = min((int)RADIUS, (int)get_global_size(1) - 1 - current_cols);
220
221#if defined(IN_MAP_2D)
222 const int current_rows = get_global_id(2);
223 const int first_row = max(-(int)RADIUS, -current_rows);
224 const int last_row = min((int)RADIUS, (int)NUM_SLICES - 1 - current_rows);
225#endif /* defined(IN_MAP_2D) */
226
227#if defined(IN_MAP_2D)
228 for(int j = first_row; j <= last_row; ++j)
229 {
230#endif /* defined(IN_MAP_2D) */
231 for(int i = first_col; i <= last_col; ++i)
232 {
233#if defined(IN_MAP_2D)
234 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
235 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, 0, i, j));
236#else /* defined(IN_MAP_2D) */
237 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
238 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, 0, i, 0));
239#endif /* defined(IN_MAP_2D) */
240 acc = ADD_OP(acc, MUL_OP(values, values));
241 }
242#if defined(IN_MAP_2D)
243 }
244#endif /* defined(IN_MAP_2D) */
245
246 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
247 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
248 normalized = POW_OP(acc, beta_v);
249 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
250 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
251
252 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
253}
254#endif /* defined(NUM_SLICES) */