summaryrefslogtreecommitdiff
path: root/compute/ARMComputeEx/src/core/CL/cl_kernels/instance_normalization_ex.cl
blob: 1d96150f8b19404c029e0a23b6edc8e0bc9fff56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright (c) 2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "helpers.h"

#if defined(VEC_SIZE) && defined(DATA_TYPE) && defined(EPSILON) && defined(DIM_X) && \
    defined(DIM_Y) && defined(DIM_Z)
/** This function normalizes the input 2D tensor across the first dimension with respect to mean and
 * standard deviation of the same dimension.
 *
 * @attention Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g.
 * -DVEC_SIZE=16
 * @attention Data type should be passed using the -DDATA_TYPE=data_type compile flag, e.g.
 * -DDATA_TYPE=float
 * @attention Normalization epsilon parameter should be given as a preprocessor argument with
 * -DEPSILON=value. e.g. -DEPSILON=0.001f
 * @attention Dimensions X, Y, and Z should be given as a preprocessor argument with -DDIM_X=value,
 * -DDIM_Y=value, -DDIM_Z=value. e.g. -DDIM_X=6, -DDIM_Y=2, -DDIM_Z=7
 *
 * @param[in]  input_ptr                            Pointer to the first source tensor. Supported
 * data types: F16/F32
 * @param[in]  input_stride_x                       Stride of the first source tensor in X dimension
 * (in bytes)
 * @param[in]  input_step_x                         input_stride_x * number of elements along X
 * processed per workitem(in bytes)
 * @param[in]  input_stride_y                       Stride of the first source tensor in Y dimension
 * (in bytes)
 * @param[in]  input_step_y                         input_stride_y * number of elements along Y
 * processed per workitem(in bytes)
 * @param[in]  input_stride_z                       Stride of the first source tensor in Z dimension
 * (in bytes)
 * @param[in]  input_step_z                         input_stride_z * number of elements along Z
 * processed per workitem(in bytes)
 * @param[in]  input_offset_first_element_in_bytes  The offset of the first element in the first
 * source tensor
 * @param[out] output_ptr                           (Optional) Pointer to the destination tensor.
 * Supported data types: same as @p input_ptr
 * @param[in]  output_stride_x                      (Optional) Stride of the destination tensor in X
 * dimension (in bytes)
 * @param[in]  output_step_x                        (Optional) output_stride_x * number of elements
 * along X processed per workitem(in bytes)
 * @param[in]  output_stride_y                      (Optional) Stride of the destination tensor in Y
 * dimension (in bytes)
 * @param[in]  output_step_y                        (Optional) output_stride_y * number of elements
 * along Y processed per workitem(in bytes)
 * @param[in]  output_stride_z                      (Optional) Stride of the destination tensor in Z
 * dimension (in bytes)
 * @param[in]  output_step_z                        (Optional) output_stride_z * number of elements
 * along Z processed per workitem(in bytes)
 * @param[in]  output_offset_first_element_in_bytes (Optional) The offset of the first element in
 * the destination tensor
 * @param[in]  gamma_ptr                            (Optional) Pointer to the gamma tensor.
 * Supported data types: same as @p input_ptr
 * @param[in]  gamma_stride_x                       (Optional) Stride of the gamma tensor in X
 * dimension (in bytes)
 * @param[in]  gamma_step_x                         (Optional) output_stride_x * number of elements
 * along X processed per workitem(in bytes)
 * @param[in]  gamma_offset_first_element_in_bytes  (Optional) The offset of the first element in
 * the gamma tensor
 * @param[in]  beta_ptr                             (Optional) Pointer to the beta tensor. Supported
 * data types: same as @p input_ptr
 * @param[in]  beta_stride_x                        (Optional) Stride of the beta tensor in X
 * dimension (in bytes)
 * @param[in]  beta_step_x                          (Optional) output_stride_x * number of elements
 * along X processed per workitem(in bytes)
 * @param[in]  beta_offset_first_element_in_bytes   (Optional) The offset of the first element in
 * the beta tensor
 */
__kernel void instance_normalization_ex(TENSOR4D_DECLARATION(input),
#ifndef IN_PLACE
                                        TENSOR4D_DECLARATION(output)
#endif /* IN_PLACE */
#ifdef GAMMA
                                            ,
                                        VECTOR_DECLARATION(gamma)
#endif // GAMMA
#ifdef BETA
                                            ,
                                        VECTOR_DECLARATION(beta)
#endif // BETA
                                            )
{
  Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, 0);
#ifndef IN_PLACE
  Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(output, 0);
#endif /* IN_PLACE */

  float sum = 0.f;
  float sum_sq = 0.f;

#if defined(NHWC)

  const int ch = get_global_id(0);    // Current channel
  const int batch = get_global_id(2); // Current batch
  const int elements_plane = DIM_Y * DIM_Z;

  for (int i_w = 0; i_w < DIM_Y; ++i_w)
  {
    for (int i_h = 0; i_h < DIM_Z; ++i_h)
    {
      float data = (float)*((__global DATA_TYPE *)tensor4D_offset(&in, ch, i_w, i_h, batch));
      sum += data;
      sum_sq += data * data;
    }
  }

#else // !defined(NHWC)
  const int ch = get_global_id(2) % DIM_Z;    // Current channel
  const int batch = get_global_id(2) / DIM_Z; // Current batch
  const int elements_plane = DIM_X * DIM_Y;

  VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
  part_sum = 0.f;
  VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
  part_sum_sq = 0.f;
  // Calculate partial sum
  for (int y = 0; y < DIM_Y; ++y)
  {
    int x = 0;
    for (; x <= (DIM_X - VEC_SIZE); x += VEC_SIZE)
    {
      // Load data
      VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
      data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor4D_offset(&in, x, y, ch, batch));
      part_sum += data;
      part_sum_sq += data * data;
    }
    // Left-overs loop
    for (; x < DIM_X; ++x)
    {
      DATA_TYPE data = *((__global DATA_TYPE *)tensor4D_offset(&in, x, y, ch, batch));
      part_sum.s0 += data;
      part_sum_sq.s0 += data * data;
    }
  }
// Perform reduction
#if VEC_SIZE > 8
  part_sum.s01234567 += part_sum.s89abcdef;
  part_sum_sq.s01234567 += part_sum_sq.s89abcdef;
#endif // VEC_SIZE > 8
#if VEC_SIZE > 4
  part_sum.s0123 += part_sum.s4567;
  part_sum_sq.s0123 += part_sum_sq.s4567;
#endif // VEC_SIZE > 4
#if VEC_SIZE > 2
  part_sum.s01 += part_sum.s23;
  part_sum_sq.s01 += part_sum_sq.s23;
#endif // VEC_SIZE > 2
  part_sum.s0 += part_sum.s1;
  part_sum_sq.s0 += part_sum_sq.s1;

  sum = (float)part_sum.s0;
  sum_sq = (float)part_sum_sq.s0;

#endif // defined(NHWC)

  const float mean_float = (sum / elements_plane);
  const DATA_TYPE mean = (DATA_TYPE)mean_float;
  const float var_float = (sum_sq / elements_plane) - (mean_float * mean_float);
#if defined(GAMMA)
  const float multip_float = *((__global DATA_TYPE *)gamma_ptr + ch) / sqrt(var_float + EPSILON);
  const DATA_TYPE multip = (DATA_TYPE)multip_float;
#else  // !defined(GAMMA)
  const DATA_TYPE multip = (DATA_TYPE)0;
#endif // defined(GAMMA)
#if defined(BETA)
  const DATA_TYPE beta = *((__global DATA_TYPE *)beta_ptr + ch);
#else  // !defined(BETA)
  const DATA_TYPE beta = 0;
#endif // defined(BETA)

#if defined(NHWC)

  for (int i_w = 0; i_w < DIM_Y; ++i_w)
  {
    for (int i_h = 0; i_h < DIM_Z; ++i_h)
    {
      __global DATA_TYPE *input_address =
          (__global DATA_TYPE *)tensor4D_offset(&in, ch, i_w, i_h, batch);
#ifdef IN_PLACE
      __global DATA_TYPE *output_address = input_address;
#else  /* !IN_PLACE */
      __global DATA_TYPE *output_address =
          (__global DATA_TYPE *)tensor4D_offset(&out, ch, i_w, i_h, batch);
#endif /* IN_PLACE */
      *(output_address) = (*(input_address)-mean) * multip + beta;
    }
  }

#else // !defined(NHWC)
  for (int y = 0; y < DIM_Y; ++y)
  {
    int x = 0;
    for (; x <= (DIM_X - VEC_SIZE); x += VEC_SIZE)
    {
      __global DATA_TYPE *input_address =
          (__global DATA_TYPE *)tensor4D_offset(&in, x, y, ch, batch);
#ifdef IN_PLACE
      __global DATA_TYPE *output_address = input_address;
#else  /* !IN_PLACE */
      __global DATA_TYPE *output_address =
          (__global DATA_TYPE *)tensor4D_offset(&out, x, y, ch, batch);
#endif /* IN_PLACE */

      VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
      data = VLOAD(VEC_SIZE)(0, input_address);

      VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
      res = (data - mean) * multip + beta;
      VSTORE(VEC_SIZE)
      (res, 0, output_address);
    }
    // Left-overs loop
    for (; x < DIM_X; ++x)
    {
      __global DATA_TYPE *input_address =
          (__global DATA_TYPE *)tensor4D_offset(&in, x, y, ch, batch);
#ifdef IN_PLACE
      __global DATA_TYPE *output_address = input_address;
#else  /* !IN_PLACE */
      __global DATA_TYPE *output_address =
          (__global DATA_TYPE *)tensor4D_offset(&out, x, y, ch, batch);
#endif /* IN_PLACE */
      *(output_address) = (*(input_address)-mean) * multip + beta;
    }
  }
#endif // defined(NHWC)
}
#endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(EPSILON) && defined(DIM_X) && \
          defined(DIM_Y) && defined(DIM_Z) */