blob: fbe3406239eebebbfccfafc36e5130c1a5b7de99 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Manuel Bottini7a452fe2021-03-31 18:22:59 +01002 * Copyright (c) 2016-2021 Arm Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +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
Manuel Bottini7a452fe2021-03-31 18:22:59 +010026#if defined(VEC_SIZE_X) && defined(VEC_SIZE_LEFTOVER_X)
Michalis Spyrou5538d342018-11-14 08:10:13 +000027/** This kernel performs l2 normalization on x-axis
Michalis Spyrou04f089c2017-08-08 17:42:38 +010028 *
29 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Manuel Bottini7a452fe2021-03-31 18:22:59 +010030 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE_X=size. e.g. -DVEC_SIZE_X=16
31 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER_X is; x_dimension % VEC_SIZE_X. e.g. -DVEC_SIZE_LEFTOVER_X=1
Michalis Spyrou04f089c2017-08-08 17:42:38 +010032 *
Manuel Bottini7a452fe2021-03-31 18:22:59 +010033 * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32
34 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
35 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
36 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
37 * @param[in] input_step_y input_stride_y * number of elements along X processed per workitem(in bytes)
38 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
39 * @param[in] sum_ptr Pointer to the source tensor. Supported data types: F16/F32
40 * @param[in] sum_stride_x Stride of the source tensor in X dimension (in bytes)
41 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
42 * @param[in] sum_stride_y Stride of the source tensor in Y dimension (in bytes)
43 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
44 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the source tensor
45 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
46 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
47 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
49 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
51 * @param[in] epsilon Epsilon value
Michalis Spyrou04f089c2017-08-08 17:42:38 +010052 */
Michalis Spyrou5538d342018-11-14 08:10:13 +000053__kernel void l2_normalize_x(
Manuel Bottini7a452fe2021-03-31 18:22:59 +010054 IMAGE_DECLARATION(input),
Usama Arifae0001e2019-03-26 13:44:01 +000055 IMAGE_DECLARATION(sum),
Manuel Bottini7a452fe2021-03-31 18:22:59 +010056 IMAGE_DECLARATION(output),
Michalis Spyrou04f089c2017-08-08 17:42:38 +010057 DATA_TYPE epsilon)
58{
Manuel Bottini7a452fe2021-03-31 18:22:59 +010059 // Offset computation
60 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE_X - (VEC_SIZE_X - VEC_SIZE_LEFTOVER_X) % VEC_SIZE_X), 0);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010061
Manuel Bottini7a452fe2021-03-31 18:22:59 +010062 // Address computation
63 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y;
64 __global uchar *sum_addr = sum_ptr + sum_offset_first_element_in_bytes + get_global_id(1) * sum_stride_y;
65 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * output_stride_y;
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010066
Manuel Bottini7a452fe2021-03-31 18:22:59 +010067 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
68 in = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)input_addr);
69
70 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
71 normalize_value = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X))rsqrt(fmax(*((__global DATA_TYPE *)sum_addr), epsilon));
72
73 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
74 data0 = in * normalize_value;
75
76 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE_X, VEC_SIZE_LEFTOVER_X, VEC_SIZE_LEFTOVER_X != 0 && get_global_id(0) == 0);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010077}
78
Michalis Spyrou5538d342018-11-14 08:10:13 +000079/** This kernel performs l2 normalization on y-axis.
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010080 *
81 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Manuel Bottini7a452fe2021-03-31 18:22:59 +010082 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE_X=size. e.g. -DVEC_SIZE_X=16
83 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER_X is; x_dimension % VEC_SIZE_X. e.g. -DVEC_SIZE_LEFTOVER_X=1
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010084 *
Manuel Bottini7a452fe2021-03-31 18:22:59 +010085 * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32
86 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
87 * @param[in] input_step_x input_stride_x * number of elements along Y processed per workitem(in bytes)
88 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
89 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
90 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
91 * @param[in] sum_ptr Pointer to the source tensor. Supported data types: F16/F32
92 * @param[in] sum_stride_x Stride of the source tensor in X dimension (in bytes)
93 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
94 * @param[in] sum_stride_y Stride of the source tensor in Y dimension (in bytes)
95 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
96 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the source tensor
97 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
98 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
99 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
100 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
101 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
102 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
103 * @param[in] epsilon Epsilon value
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100104 */
Michalis Spyrou5538d342018-11-14 08:10:13 +0000105__kernel void l2_normalize_y(
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100106 IMAGE_DECLARATION(input),
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100107 IMAGE_DECLARATION(sum),
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100108 IMAGE_DECLARATION(output),
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100109 DATA_TYPE epsilon)
110{
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100111 // Offset computation
112 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE_X - (VEC_SIZE_X - VEC_SIZE_LEFTOVER_X) % VEC_SIZE_X), 0);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100113
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100114 // Address computation
115 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y;
116 __global uchar *sum_addr = sum_ptr + sum_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE);
117 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * output_stride_y;
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100118
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100119 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
120 in = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)input_addr);
121 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
122 sums = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)sum_addr);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100123
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100124 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
125 normalize_value = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X))rsqrt(fmax(sums, epsilon));
126
127 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
128 data0 = in * normalize_value;
129
130 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE_X, VEC_SIZE_LEFTOVER_X, VEC_SIZE_LEFTOVER_X != 0 && get_global_id(0) == 0);
Michalis Spyrou5538d342018-11-14 08:10:13 +0000131}
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100132
Michalis Spyrou5538d342018-11-14 08:10:13 +0000133/** This kernel performs l2 normalization on z-axis.
134 *
135 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100136 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE_X=size. e.g. -DVEC_SIZE_X=16
137 * @note The leftover size in the X dimension shoud be given as preprocessor argument using -DVEC_SIZE_LEFTOVER_X is; x_dimension % VEC_SIZE_X. e.g. -DVEC_SIZE_LEFTOVER_X=1
Michalis Spyrou5538d342018-11-14 08:10:13 +0000138 *
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100139 * @param[in] input_ptr Pointer to the source tensor. Supported data types: F16/F32
140 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
141 * @param[in] input_step_x input_stride_x * number of elements along Y processed per workitem(in bytes)
142 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
143 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
144 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
145 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
146 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
147 * @param[in] sum_ptr Pointer to the source tensor. Supported data types: F16/F32
148 * @param[in] sum_stride_x Stride of the source tensor in X dimension (in bytes)
149 * @param[in] sum_step_x sum_stride_x * number of elements along X processed per workitem(in bytes)
150 * @param[in] sum_stride_y Stride of the source tensor in Y dimension (in bytes)
151 * @param[in] sum_step_y sum_stride_y * number of elements along Y processed per workitem(in bytes)
152 * @param[in] sum_stride_z Stride of the source tensor in Z dimension (in bytes)
153 * @param[in] sum_step_z sum_stride_z * number of elements along Z processed per workitem(in bytes)
154 * @param[in] sum_offset_first_element_in_bytes The offset of the first element in the source tensor
155 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
156 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
157 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
158 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
159 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
160 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
161 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
162 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
163 * @param[in] epsilon Epsilon value
Michalis Spyrou5538d342018-11-14 08:10:13 +0000164 */
165__kernel void l2_normalize_z(
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100166 TENSOR3D_DECLARATION(input),
Michalis Spyrou5538d342018-11-14 08:10:13 +0000167 TENSOR3D_DECLARATION(sum),
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100168 TENSOR3D_DECLARATION(output),
Michalis Spyrou5538d342018-11-14 08:10:13 +0000169 DATA_TYPE epsilon)
170{
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100171 // Offset computation
172 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE_X - (VEC_SIZE_X - VEC_SIZE_LEFTOVER_X) % VEC_SIZE_X), 0);
Michalis Spyrou5538d342018-11-14 08:10:13 +0000173
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100174 // Address computation
175 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z;
176 __global uchar *sum_addr = sum_ptr + sum_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * sum_stride_y;
177 __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;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000178
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100179 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
180 in = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)input_addr);
181 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
182 sums = VLOAD(VEC_SIZE_X)(0, (__global DATA_TYPE *)sum_addr);
Michalis Spyrou5538d342018-11-14 08:10:13 +0000183
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100184 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X)
185 data0 = in * ((VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE_X))(rsqrt(fmax(sums, epsilon))));
186
187 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE_X, VEC_SIZE_LEFTOVER_X, VEC_SIZE_LEFTOVER_X != 0 && get_global_id(0) == 0);
188}
189#endif // defined(VEC_SIZE_X) && defined(VEC_SIZE_LEFTOVER_X)