blob: dbdad278654253557431df72ae77cf4436c22a14 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +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
Georgios Pinitas01624362017-11-30 10:53:31 +000035/** Apply cross-map normalization.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036 *
37 * @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 +010038 * @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 +010039 * @note The radius should be given as a preprocessor argument using -DRADIUS=size. e.g. -DRADIUS=5
40 * @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 +010041 * @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 +010042 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010043 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010044 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
45 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
46 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
47 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
48 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
49 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
50 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
51 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
52 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
53 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
54 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
55 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
56 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
57 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
58 * @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 +010059 */
60__kernel void normalization_layer_cross_map(TENSOR3D_DECLARATION(input),
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010061 TENSOR3D_DECLARATION(output))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062{
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010063 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
64 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
Michele Di Giorgio6c928342017-06-22 16:55:57 +010066 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
67 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
68 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
69 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
70 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
71 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
72 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
73 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 const int current_slice = get_global_id(2);
Georgios Pinitas01624362017-11-30 10:53:31 +000076 const int left_slice = max(-(int)RADIUS, -current_slice);
77 const int right_slice = min((int)RADIUS, (int)NUM_SLICES - 1 - current_slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
79 for(int i = left_slice; i <= right_slice; i++)
80 {
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010081 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Georgios Pinitas624b7782017-11-15 16:17:22 +000082 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, 0, 0, i));
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010083 acc = ADD_OP(acc, MUL_OP(values, values));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 }
85
Michele Di Giorgio6c928342017-06-22 16:55:57 +010086 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
87 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
88 normalized = POW_OP(acc, beta_v);
89 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
90 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
Michele Di Giorgio6c928342017-06-22 16:55:57 +010092 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093}
94
Georgios Pinitas01624362017-11-30 10:53:31 +000095/** Apply in-map normalization.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 *
97 * @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 +010098 * @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 +010099 * @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 +0100100 * @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 +0100101 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100102 * @param[in] input_ptr Pointer to the first source tensor. Supported data types: F16/F32
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100103 * @param[in] input_stride_x Stride of the first source tensor in X dimension (in bytes)
104 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
105 * @param[in] input_stride_y Stride of the first source tensor in Y dimension (in bytes)
106 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
107 * @param[in] input_stride_z Stride of the first source tensor in Z dimension (in bytes)
108 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
109 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source tensor
110 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
111 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
112 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
113 * @param[in] output_stride_y Stride of the first destination tensor in Y dimension (in bytes)
114 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
115 * @param[in] output_stride_z Stride of the first source tensor in Z dimension (in bytes)
116 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
117 * @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 +0100118 */
Georgios Pinitas01624362017-11-30 10:53:31 +0000119__kernel void normalization_layer_in_map(TENSOR3D_DECLARATION(input),
120 TENSOR3D_DECLARATION(output))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121{
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100122 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(input);
123 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100125 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
126 acc = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))0;
127 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
128 coeff_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(COEFF);
129 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
130 beta_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(BETA);
131 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
132 kappa_v = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))SQCVT_SAT(KAPPA);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133
Georgios Pinitas01624362017-11-30 10:53:31 +0000134 const int current_col = get_global_id(0) << 2;
135 const int left_pos = max(-(int)RADIUS, -3 - current_col);
136 const int right_pos = min((int)RADIUS, (int)((get_global_size(0) << 2) + 3 - 1 - current_col));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Georgios Pinitas01624362017-11-30 10:53:31 +0000138#if defined(IN_MAP_2D)
139 const int current_row = get_global_id(1);
140 const int first_row = max(-(int)RADIUS, -current_row);
141 const int last_row = min((int)RADIUS, (int)get_global_size(1) - 1 - current_row);
142#endif /* defined(IN_MAP_2D) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143
Georgios Pinitas01624362017-11-30 10:53:31 +0000144#if defined(IN_MAP_2D)
145 for(int j = first_row; j <= last_row; ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 {
Georgios Pinitas01624362017-11-30 10:53:31 +0000147#endif /* defined(IN_MAP_2D) */
148 for(int i = left_pos; i <= right_pos; ++i)
149 {
150#if defined(IN_MAP_2D)
151 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
152 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, j, 0));
153#else /* defined(IN_MAP_2D) */
154 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
155 values = LOAD_OP(0, (__global DATA_TYPE *)tensor3D_offset(&in, i, 0, 0));
156#endif /* defined(IN_MAP_2D) */
157 acc = ADD_OP(acc, MUL_OP(values, values));
158 }
159#if defined(IN_MAP_2D)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 }
Georgios Pinitas01624362017-11-30 10:53:31 +0000161#endif /* defined(IN_MAP_2D) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100163 acc = ADD_OP(MUL_OP(acc, coeff_v), kappa_v);
164 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
165 normalized = POW_OP(acc, beta_v);
166 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
167 normalized_pixel = DIV_OP(LOAD_OP(0, (__global DATA_TYPE *)in.ptr), normalized);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100169 STORE_OP(normalized_pixel, 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170}