blob: 7e35e161c8c592c88a360cf9e4578a0fc1aa64da [file] [log] [blame]
Adnan AlSinan7075fe22021-07-05 13:12:52 +01001/*
2 * Copyright (c) 2017-2021 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#include "tile_helpers.h"
26
27#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
33#if defined(WIDTH_SIZE)
34/** Apply cross-map normalization.
35 *
36 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
37 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
38 * @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
40 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
41 *
42 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
43 * @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
58 */
59__kernel void normalization_layer_cross_map_nhwc(TENSOR3D_DECLARATION(input),
60 TENSOR3D_DECLARATION(output))
61{
62 // Offset computation
63 const uint x_offs = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER);
64
65 // Address computation
66 __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;
67 __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;
68
69 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
70 acc = 0;
71 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
72 coeff_v = SQCVT_SAT(COEFF);
73 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
74 beta_v = SQCVT_SAT(BETA);
75 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
76 kappa_v = SQCVT_SAT(KAPPA);
77
78 const int left_slice = max((int)0, (int)x_offs - (int)RADIUS);
79 const int right_slice = min((int)WIDTH_SIZE - 1, (int)x_offs + (int)RADIUS);
80
81 for(int i = left_slice; i <= right_slice; ++i)
82 {
83 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
84 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + i * sizeof(DATA_TYPE)));
85 acc = ADD_OP(acc, MUL_OP(values, values));
86 }
87
88 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
89 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
90 normalized = POW_OP(acc, beta_v);
91 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
92 normalized_pixel0 = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + x_offs * sizeof(DATA_TYPE))), normalized);
93
94 STORE_VECTOR_SELECT(normalized_pixel, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
95}
96#endif // defined(WIDTH_SIZE)
97
98#if defined(NUM_SLICES) && defined(DIM1_SIZE)
99/** Apply in-map normalization when tensors are in the NHWC data layout format.
100 *
101 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
102 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size, e.g. -DVEC_SIZE=16
103 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
104 * @note The number of slices should be given as a preprocessor argument using -DNUM_SLICES=size. e.g. -DNUM_SLICES=192
105 * @note Scaling coefficient (= alpha/norm_size), beta and kappa need to be passed at compile time using -DCOEFF, -DALPHA and -DKAPPA
106 *
107 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
108 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
109 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
110 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
111 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
112 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
113 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
114 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
115 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
116 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
117 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
118 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
119 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
120 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
121 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
122 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
123 */
124__kernel void normalization_layer_in_map_nhwc(TENSOR3D_DECLARATION(input),
125 TENSOR3D_DECLARATION(output))
126{
127 // Offset computation
128 const uint x_offs = GET_SPATIAL_IDX(0, VEC_SIZE, VEC_SIZE_LEFTOVER);
129 const int current_cols = get_global_id(1);
130 const int current_rows = get_global_id(2);
131
132 // Address computation
133 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE);
134 __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;
135
136 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
137 acc = 0;
138 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
139 coeff_v = SQCVT_SAT(COEFF);
140 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
141 beta_v = SQCVT_SAT(BETA);
142 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
143 kappa_v = SQCVT_SAT(KAPPA);
144
145 const int first_col = max(0, current_cols - (int)RADIUS);
146 const int last_col = min((int)DIM1_SIZE - 1, current_cols + (int)RADIUS);
147
148#if defined(IN_MAP_2D)
149 const int first_row = max(0, current_rows - (int)RADIUS);
150 const int last_row = min((int)NUM_SLICES - 1, current_rows + (int)RADIUS);
151#endif /* defined(IN_MAP_2D) */
152
153#if defined(IN_MAP_2D)
154 for(int j = first_row; j <= last_row; ++j)
155 {
156#else // defined(IN_MAP_2D)
157 const int j = current_rows;
158#endif /* defined(IN_MAP_2D) */
159 for(int i = first_col; i <= last_col; ++i)
160 {
161 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
162 values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + i * input_stride_y + j * input_stride_z));
163 acc = ADD_OP(acc, MUL_OP(values, values));
164 }
165#if defined(IN_MAP_2D)
166 }
167#endif /* defined(IN_MAP_2D) */
168
169 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
170 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
171 normalized = POW_OP(acc, beta_v);
172 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
173 normalized_pixel0 = DIV_OP(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + current_cols * output_stride_y + current_rows *output_stride_z)), normalized);
174
175 STORE_VECTOR_SELECT(normalized_pixel, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
176}
177#endif // defined(NUM_SLICES) && defined(DIM1_SIZE)