blob: 598b734c26b6445ce309cfd7fab315c3de21c2a4 [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
Michele Di Giorgio6c928342017-06-22 16:55:57 +010026#if defined(FIXED_POINT_POSITION)
27
28#include "fixed_point.h"
29#define MUL_OP(x, y) MUL_SAT_OP_EXPAND((x), (y), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)
30#define ADD_OP(x, y) ADD_SAT_OP_EXPAND((x), (y), DATA_TYPE, VEC_SIZE)
31#define DIV_OP(x, y) DIV_SAT_OP_EXPAND((x), (y), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)
32#define EXP_OP(x) EXP_OP_EXPAND((x), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)
33#define LOG_OP(x) LOG_OP_EXPAND((x), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)
34#define POW_OP(x, y) EXP_OP(MUL_OP(LOG_OP((x)), (y)))
35#define SQCVT_SAT(a) SQCVT_SAT_OP_EXPAND((a), DATA_TYPE, FIXED_POINT_POSITION)
36
37#define LOAD_OP(offset, ptr) vload16(offset, ptr)
38#define STORE_OP(data, offset, ptr) vstore16(data, offset, ptr)
39
40#else // FIXED_POINT_POSITION
41
42#define MUL_OP(x, y) ((x) * (y))
43#define ADD_OP(x, y) ((x) + (y))
44#define DIV_OP(x, y) ((x) / (y))
45#define POW_OP(x, y) pow((x), (y))
46#define SQCVT_SAT(a) (a)
47
48#define LOAD_OP(offset, ptr) vload4(offset, ptr)
49#define STORE_OP(data, offset, ptr) vstore4(data, offset, ptr)
50
51#endif // FIXED_POINT_POSITION
52
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053/** Apply cross map normalization.
54 *
55 * @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 +010056 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
57 * @note In case of fixed-point operation -DFIXED_POINT_POSITION=fixed_point_position must be provided: e.g. -DFIXED_POINT_POSITION=3
58 * @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 +010059 *
Michele Di Giorgio6c928342017-06-22 16:55:57 +010060 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: QS8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
62 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
63 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
64 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
65 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
66 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
67 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
Michele Di Giorgio6c928342017-06-22 16:55:57 +010068 * @param[in] squared_input_ptr Pointer to the second source tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 * @param[in] squared_input_stride_x Stride of the second source tensor in X dimension (in bytes)
70 * @param[in] squared_input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
71 * @param[in] squared_input_stride_y Stride of the second source tensor in Y dimension (in bytes)
72 * @param[in] squared_input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
73 * @param[in] squared_input_stride_z Stride of the second source tensor in Z dimension (in bytes)
74 * @param[in] squared_input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
75 * @param[in] squared_input_offset_first_element_in_bytes The offset of the second element in the second source tensor
Michele Di Giorgio6c928342017-06-22 16:55:57 +010076 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
78 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
79 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
80 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
81 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
82 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
83 * @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 +010084 * @param[in] radius Number of elements on the right or left side to normalize across
85 */
86__kernel void normalization_layer_cross_map(TENSOR3D_DECLARATION(input),
87 TENSOR3D_DECLARATION(squared_input),
88 TENSOR3D_DECLARATION(output),
Michele Di Giorgio6c928342017-06-22 16:55:57 +010089 uint radius)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090{
91 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
92 Tensor3D squared_in = CONVERT_TO_TENSOR3D_STRUCT(squared_input);
93 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
94
Michele Di Giorgio6c928342017-06-22 16:55:57 +010095 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
96 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
97 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
98 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
99 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
100 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
101 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
102 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
104 const int num_of_slices = get_global_size(2);
105 const int current_slice = get_global_id(2);
106
107 const int left_slice = max(current_slice - (int)radius, (int)0);
108 const int right_slice = min(current_slice + (int)radius, (int)(num_of_slices - 1));
109
110 for(int i = left_slice; i <= right_slice; i++)
111 {
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100112 acc = ADD_OP(acc, LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&squared_in, 0, 0, i - current_slice)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 }
114
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100115 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
116 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
117 normalized = POW_OP(acc, beta_v);
118 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
119 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100121 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122}
123
124/** Apply in map normalization.
125 *
126 * @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 +0100127 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
128 * @note In case of fixed-point operation -DFIXED_POINT_POSITION=fixed_point_position must be provided: e.g. -DFIXED_POINT_POSITION=3
129 * @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 +0100130 *
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100131 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: QS8/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
133 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
134 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
135 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
136 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
137 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
138 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100139 * @param[in] squared_input_ptr Pointer to the second source tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 * @param[in] squared_input_stride_x Stride of the second source tensor in X dimension (in bytes)
141 * @param[in] squared_input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
142 * @param[in] squared_input_stride_y Stride of the second source tensor in Y dimension (in bytes)
143 * @param[in] squared_input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
144 * @param[in] squared_input_stride_z Stride of the second source tensor in Z dimension (in bytes)
145 * @param[in] squared_input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
146 * @param[in] squared_input_offset_first_element_in_bytes The offset of the second element in the second source tensor
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100147 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
149 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
150 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
151 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
152 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
153 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
154 * @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 +0100155 * @param[in] radius Number of elements on the right or left side to normalize across
156 */
157__kernel void normalization_layer_in_map_1D(TENSOR3D_DECLARATION(input),
158 TENSOR3D_DECLARATION(squared_input),
159 TENSOR3D_DECLARATION(output),
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100160 uint radius)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161{
162 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
163 Tensor3D squared_in = CONVERT_TO_TENSOR3D_STRUCT(squared_input);
164 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
165
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100166 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
167 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
168 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
169 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
170 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
171 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
172 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
173 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174
175 const int current_pos = get_global_id(0) << 2;
176
177 const int left_pos = max(current_pos - (int)radius, -3);
178 const int right_pos = min(current_pos + (int)radius, (int)((get_global_size(0) << 2) + 3 - 1));
179
180 for(int i = left_pos; i <= right_pos; i += 1)
181 {
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100182 acc = ADD_OP(acc, LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&squared_in, i - current_pos, 0, 0)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 }
184
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100185 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
186 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
187 normalized = POW_OP(acc, beta_v);
188 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
189 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100191 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192}