blob: 456920882473db49e33019bb699f72f5ff07b79a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini21c28952021-04-08 12:50:12 +01002 * Copyright (c) 2017-2021 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"
Manuel Bottini21c28952021-04-08 12:50:12 +010025#include "tile_helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Michele Di Giorgio6c928342017-06-22 16:55:57 +010027#define MUL_OP(x, y) ((x) * (y))
28#define ADD_OP(x, y) ((x) + (y))
29#define DIV_OP(x, y) ((x) / (y))
30#define POW_OP(x, y) pow((x), (y))
31#define SQCVT_SAT(a) (a)
32
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000033#if defined(NUM_SLICES)
Georgios Pinitas01624362017-11-30 10:53:31 +000034/** Apply cross-map normalization.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035 *
36 * @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 +010037 * @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 +010038 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
39 * @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 +010040 * @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 +010041 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010042 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010043 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
44 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
45 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
46 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
47 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
48 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
49 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
50 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
51 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
52 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
53 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
54 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
55 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
56 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
57 * @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 +010058 */
Manuel Bottini21c28952021-04-08 12:50:12 +010059__kernel void normalization_layer_cross_map_nchw(TENSOR3D_DECLARATION(input),
60 TENSOR3D_DECLARATION(output))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061{
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010062 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
63 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064
Michele Di Giorgio6c928342017-06-22 16:55:57 +010065 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
66 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
67 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
68 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
69 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
70 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
71 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
72 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 const int current_slice = get_global_id(2);
Georgios Pinitas01624362017-11-30 10:53:31 +000075 const int left_slice = max(-(int)RADIUS, -current_slice);
76 const int right_slice = min((int)RADIUS, (int)NUM_SLICES - 1 - current_slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077
78 for(int i = left_slice; i <= right_slice; i++)
79 {
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010080 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +010081 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&in, 0, 0, i));
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010082 acc = ADD_OP(acc, MUL_OP(values, values));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 }
84
Michele Di Giorgio6c928342017-06-22 16:55:57 +010085 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
86 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
87 normalized = POW_OP(acc, beta_v);
88 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +010089 normalized_pixel = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
Manuel Bottini21c28952021-04-08 12:50:12 +010091 VSTORE(VEC_SIZE)
92 (normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093}
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000094#endif /* defined(NUM_SLICES) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Georgios Pinitas7f32d012018-10-11 18:41:19 +010096#if defined(WIDTH_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +010097/** Apply cross-map normalization.
98 *
99 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
100 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
101 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
102 * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192
103 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
104 *
105 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
106 * @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 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 destination 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
121 */
122__kernel void normalization_layer_cross_map_nhwc(TENSOR3D_DECLARATION(input),
123 TENSOR3D_DECLARATION(output))
124{
125 // Offset computation
126 const uint x_offs = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER);
127
128 // Address computation
129 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z;
130 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * output_stride_y + get_global_id(2) * output_stride_z;
131
132 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
133 acc = 0;
134 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
135 coeff_v = SQCVT_SAT(COEFF);
136 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
137 beta_v = SQCVT_SAT(BETA);
138 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
139 kappa_v = SQCVT_SAT(KAPPA);
140
141 const int left_slice = max((int)0, (int)x_offs - (int)RADIUS);
142 const int right_slice = min((int)WIDTH_SIZE - 1, (int)x_offs + (int)RADIUS);
143
144 for(int i = left_slice; i <= right_slice; ++i)
145 {
146 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
147 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + i * sizeof(DATA_TYPE)));
148 acc = ADD_OP(acc, MUL_OP(values, values));
149 }
150
151 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
152 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
153 normalized = POW_OP(acc, beta_v);
154 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
155 normalized_pixel0 = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + x_offs * sizeof(DATA_TYPE))), normalized);
156
157 STORE_VECTOR_SELECT(normalized_pixel, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
158}
159
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000160/** Apply in-map normalization when tensors are in the NCHW data layout format.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 *
162 * @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 +0100163 * @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 +0100164 * @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 +0100165 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
Manuel Bottini21c28952021-04-08 12:50:12 +0100166 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER is; x_dimension % VEC_SIZE. e.g. -DVEC_SIZE_LEFTOVER=1
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100168 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100169 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
170 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
171 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
172 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
173 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
174 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
175 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
176 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
177 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
178 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
179 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
180 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
181 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
182 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
183 * @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 +0100184 */
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000185__kernel void normalization_layer_in_map_nchw(TENSOR3D_DECLARATION(input),
186 TENSOR3D_DECLARATION(output))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187{
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100188 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
189 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100191 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100192 acc = 0;
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100193 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100194 coeff_v = SQCVT_SAT(COEFF);
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100195 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100196 beta_v = SQCVT_SAT(BETA);
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100197 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100198 kappa_v = SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
Georgios Pinitas01624362017-11-30 10:53:31 +0000200 const int current_col = get_global_id(0) << 2;
201 const int left_pos = max(-(int)RADIUS, -3 - current_col);
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100202 const int right_pos = min((int)RADIUS, (int)WIDTH_SIZE - 1 - current_col);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203
Georgios Pinitas01624362017-11-30 10:53:31 +0000204#if defined(IN_MAP_2D)
205 const int current_row = get_global_id(1);
206 const int first_row = max(-(int)RADIUS, -current_row);
207 const int last_row = min((int)RADIUS, (int)get_global_size(1) - 1 - current_row);
208#endif /* defined(IN_MAP_2D) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
Georgios Pinitas01624362017-11-30 10:53:31 +0000210#if defined(IN_MAP_2D)
211 for(int j = first_row; j <= last_row; ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212 {
Georgios Pinitas01624362017-11-30 10:53:31 +0000213#endif /* defined(IN_MAP_2D) */
214 for(int i = left_pos; i <= right_pos; ++i)
215 {
216#if defined(IN_MAP_2D)
217 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100218 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, j, 0));
Georgios Pinitas01624362017-11-30 10:53:31 +0000219#else /* defined(IN_MAP_2D) */
220 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100221 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, 0, 0));
Georgios Pinitas01624362017-11-30 10:53:31 +0000222#endif /* defined(IN_MAP_2D) */
223 acc = ADD_OP(acc, MUL_OP(values, values));
224 }
225#if defined(IN_MAP_2D)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 }
Georgios Pinitas01624362017-11-30 10:53:31 +0000227#endif /* defined(IN_MAP_2D) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100229 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
230 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
231 normalized = POW_OP(acc, beta_v);
232 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100233 normalized_pixel = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
Manuel Bottini21c28952021-04-08 12:50:12 +0100235 VSTORE(VEC_SIZE)
236 (normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237}
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100238#endif // defined(WIDTH_SIZE)
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000239
Manuel Bottini21c28952021-04-08 12:50:12 +0100240#if defined(NUM_SLICES) && defined(DIM1_SIZE)
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000241/** Apply in-map normalization when tensors are in the NHWC data layout format.
242 *
243 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
244 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
245 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
246 * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192
247 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
248 *
249 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
250 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
251 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
252 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
253 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
254 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
255 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
256 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
257 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
258 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
259 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
260 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
261 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
262 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
263 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
264 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
265 */
266__kernel void normalization_layer_in_map_nhwc(TENSOR3D_DECLARATION(input),
267 TENSOR3D_DECLARATION(output))
268{
Manuel Bottini21c28952021-04-08 12:50:12 +0100269 // Offset computation
270 const uint x_offs = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER);
271 const int current_cols = get_global_id(1);
272 const int current_rows = get_global_id(2);
273
274 // Address computation
275 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE);
276 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + current_cols * output_stride_y + current_rows * output_stride_z;
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000277
278 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100279 acc = 0;
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000280 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100281 coeff_v = SQCVT_SAT(COEFF);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000282 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100283 beta_v = SQCVT_SAT(BETA);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000284 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100285 kappa_v = SQCVT_SAT(KAPPA);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000286
Manuel Bottini21c28952021-04-08 12:50:12 +0100287 const int first_col = max(0, current_cols - (int)RADIUS);
288 const int last_col = min((int)DIM1_SIZE - 1, current_cols + (int)RADIUS);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000289
290#if defined(IN_MAP_2D)
Manuel Bottini21c28952021-04-08 12:50:12 +0100291 const int first_row = max(0, current_rows - (int)RADIUS);
292 const int last_row = min((int)NUM_SLICES - 1, current_rows + (int)RADIUS);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000293#endif /* defined(IN_MAP_2D) */
294
295#if defined(IN_MAP_2D)
296 for(int j = first_row; j <= last_row; ++j)
297 {
Manuel Bottini21c28952021-04-08 12:50:12 +0100298#else // defined(IN_MAP_2D)
299 const int j = current_rows;
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000300#endif /* defined(IN_MAP_2D) */
301 for(int i = first_col; i <= last_col; ++i)
302 {
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000303 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100304 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + i * input_stride_y + j * input_stride_z));
305 acc = ADD_OP(acc, MUL_OP(values, values));
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000306 }
307#if defined(IN_MAP_2D)
308 }
309#endif /* defined(IN_MAP_2D) */
310
311 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
312 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
313 normalized = POW_OP(acc, beta_v);
314 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Manuel Bottini21c28952021-04-08 12:50:12 +0100315 normalized_pixel0 = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + current_cols * output_stride_y + current_rows * output_stride_z)), normalized);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000316
Manuel Bottini21c28952021-04-08 12:50:12 +0100317 STORE_VECTOR_SELECT(normalized_pixel, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000318}
Manuel Bottini21c28952021-04-08 12:50:12 +0100319#endif // defined(NUM_SLICES) && defined(DIM1_SIZE)