aboutsummaryrefslogtreecommitdiff
path: root/src/core/GLES_COMPUTE/cs_shaders/batchnormalization_layer.cs
blob: 81be9679b2573908f32921b2dad7256a2baed15f (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (c) 2017-2018 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.
 */

layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;

#include "helpers_cs.h"

#if defined(DATA_TYPE_FP16)
precision mediump float;
#endif /*DATA_TYPE_FP32*/

#define ADD_OP(a, b) ((a) + (b))
#define SUB_OP(a, b) ((a) - (b))
#define MUL_OP(a, b) ((a) * (b))
#define INVSQRT_OP(a) inversesqrt((a))
#define SQCVT_SAT(a) (a)

#if defined(LU_BRELU)
#define ACTIVATION_FUNC(x) min(max(x, float(B_VAL)), float(A_VAL))
#elif defined(BRELU)
#define ACTIVATION_FUNC(x) min(max(x, float(0)), float(A_VAL))
#elif defined(RELU)
#define ACTIVATION_FUNC(x) max(x, float(0))
#else /* defined(FUSED_ACT) */
#define ACTIVATION_FUNC(x) (x)
#endif /* defined(FUSED_ACT) */

/** Apply batch normalization.
 *
 * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32"
 * @note Epsilon parameter in the batch normalization equation should be given as a preprocessor argument using "#define EPSILON". e.g. "#define EPSILON 0.1"
 * @note Beta is optional with default value of 0. If not provided, the preprocessor argument "USE_DEFAULT_BETA" should be given
 * @note Gamma is optional with default value of 1. If not provided, the preprocessor argument "USE_DEFAULT_GAMMA" should be given
 *
 * @param[in]  src_ptr     Pointer to the first source tensor. Supported data types: F16/F32
 * @param[in]  src_attrs   The attributes of the source tensor
 * @param[out] dst_ptr     Pointer to the destination tensor. Supported data types: same as @p src_ptr
 * @param[in]  dst_attrs   The attributes of the destination tensor
 * @param[in]  mean_ptr    Pointer to the mean source tensor. Supported data types: same as @p src_ptr
 * @param[in]  mean_attrs  The attributes of the mean tensor
 * @param[in]  var_ptr     Pointer to the var tensor. Supported data types: same as @p src_ptr
 * @param[in]  var_attrs   The attributes of the var tensor
 * @param[in]  beta_ptr    (Optional) Pointer to the beta source tensor. If not provided, default value of beta is 0. Supported data types: same as @p src_ptr
 * @param[in]  beta_attrs  (Optional) The attributes of the beta tensor
 * @param[in]  gamma_ptr   (Optional) Pointer to the gamma source tensor. If not provided, default value of gamma is 1. Supported data types: same as @p src_ptr
 * @param[in]  gamma_attrs (Optional) The attributes of the gamma tensor
 */
SHADER_PARAMS_DECLARATION
{
    Tensor3DAttributes src_attrs;
    Tensor3DAttributes dst_attrs;
    VectorAttributes   mean_attrs;
    VectorAttributes   var_attrs;
#ifndef USE_DEFAULT_BETA
    VectorAttributes beta_attrs;
#endif /* USE_DEFAULT_BETA */
#ifndef USE_DEFAULT_GAMMA
    VectorAttributes gamma_attrs;
#endif /* USE_DEFAULT_GAMMA */
};

#ifdef DATA_TYPE_FP32
TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly);
TENSOR_DECLARATION(3, meanBuffer, float, mean_ptr, mean_shift, 2, readonly);
TENSOR_DECLARATION(4, varBuffer, float, var_ptr, var_shift, 2, readonly);
#ifndef USE_DEFAULT_BETA
TENSOR_DECLARATION(5, betaBuffer, float, beta_ptr, beta_shift, 2, readonly);
#endif /* USE_DEFAULT_BETA */
#ifndef USE_DEFAULT_GAMMA
#ifdef USE_DEFAULT_BETA
TENSOR_DECLARATION(5, gammaBuffer, float, gamma_ptr, gamma_shift, 2, readonly);
#else  /* USE_DEFAULT_BETA */
TENSOR_DECLARATION(6, gammaBuffer, float, gamma_ptr, gamma_shift, 2, readonly);
#endif /* USE_DEFAULT_BETA */
#endif /* USE_DEFAULT_GAMMA */

void main(void)
{
    Tensor3DIterator src_iter  = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    Tensor3DIterator dst_iter  = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
    VectorIterator   mean_iter = CONVERT_TO_VECTOR_ITERATOR(mean_attrs, mean_shift);
    VectorIterator   var_iter  = CONVERT_TO_VECTOR_ITERATOR(var_attrs, var_shift);
#ifndef USE_DEFAULT_BETA
    VectorIterator beta_iter = CONVERT_TO_VECTOR_ITERATOR(beta_attrs, beta_shift);
#endif /* USE_DEFAULT_BETA */
#ifndef USE_DEFAULT_GAMMA
    VectorIterator gamma_iter = CONVERT_TO_VECTOR_ITERATOR(gamma_attrs, gamma_shift);
#endif /* USE_DEFAULT_GAMMA */

    float input_value = 0.f;
    float denominator = 0.f;
    float numerator   = 0.f;
    float x_bar       = 0.f;

    uint current_slice = gl_GlobalInvocationID.z;

    input_value = LOAD_CURRENT_ITEM(src_ptr, src_iter);
    denominator = LOAD(var_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(var_iter, current_slice * var_attrs.stride_x));
    denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON))));

    // Calculate x bar and store results
    numerator = LOAD(mean_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(mean_iter, current_slice * mean_attrs.stride_x));
    numerator = SUB_OP(input_value, numerator);
    x_bar     = MUL_OP(numerator, denominator);

#ifndef USE_DEFAULT_GAMMA
    float gamma_param = LOAD(gamma_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(gamma_iter, current_slice * gamma_attrs.stride_x));

    x_bar = MUL_OP(gamma_param, x_bar);
#endif /* USE_DEFAULT_GAMMA */
#ifndef USE_DEFAULT_BETA
    float beta_param = LOAD(beta_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(beta_iter, current_slice * beta_attrs.stride_x));

    x_bar = ADD_OP(x_bar, beta_param);
#endif /* USE_DEFAULT_BETA */

    STORE_CURRENT_ITEM(dst_ptr, dst_iter, ACTIVATION_FUNC(x_bar));
}

#elif defined(DATA_TYPE_FP16)
TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly);
TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly);
TENSOR_DECLARATION(3, meanBuffer, uvec2, mean_ptr, mean_shift, 3, readonly);
TENSOR_DECLARATION(4, varBuffer, uvec2, var_ptr, var_shift, 3, readonly);
#ifndef USE_DEFAULT_BETA
TENSOR_DECLARATION(5, betaBuffer, uvec2, beta_ptr, beta_shift, 3, readonly);
#endif /* USE_DEFAULT_BETA */
#ifndef USE_DEFAULT_GAMMA
#ifdef USE_DEFAULT_BETA
TENSOR_DECLARATION(5, gammaBuffer, uvec2, gamma_ptr, gamma_shift, 3, readonly);
#else  /* USE_DEFAULT_BETA */
TENSOR_DECLARATION(6, gammaBuffer, uvec2, gamma_ptr, gamma_shift, 3, readonly);
#endif /* USE_DEFAULT_BETA */
#endif /* USE_DEFAULT_GAMMA */

void main(void)
{
    Tensor3DIterator src_iter   = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    Tensor3DIterator dst_iter   = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
    VectorIterator   mean_iter  = CONVERT_TO_VECTOR_ITERATOR(mean_attrs, mean_shift);
    VectorIterator   var_iter   = CONVERT_TO_VECTOR_ITERATOR(var_attrs, var_shift);
#ifndef USE_DEFAULT_BETA
    VectorIterator   beta_iter  = CONVERT_TO_VECTOR_ITERATOR(beta_attrs, beta_shift);
#endif /* USE_DEFAULT_BETA */
#ifndef USE_DEFAULT_GAMMA
    VectorIterator   gamma_iter = CONVERT_TO_VECTOR_ITERATOR(gamma_attrs, gamma_shift);
#endif /* USE_DEFAULT_GAMMA */

    vec4  unpacked_s[5];
    float denominator;
    float numerator;
    float gamma_param = 1.f;
    float beta_param  = 0.f;
    vec4  x_bar;
    vec4  result;

    uint current_slice = gl_GlobalInvocationID.z;
    unpacked_s[0]      = LOAD_UNPACK4_CURRENT_ITEM_HALF(src_ptr, src_iter);
    unpacked_s[1]      = LOAD_UNPACK4_HALF(var_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(var_iter, current_slice * var_attrs.stride_x));
    unpacked_s[2]      = LOAD_UNPACK4_HALF(mean_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(mean_iter, current_slice * mean_attrs.stride_x));
#ifndef USE_DEFAULT_GAMMA
    unpacked_s[3]      = LOAD_UNPACK4_HALF(gamma_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(gamma_iter, current_slice * gamma_attrs.stride_x));
#endif /* USE_DEFAULT_BETA */
#ifndef USE_DEFAULT_BETA
    unpacked_s[4]      = LOAD_UNPACK4_HALF(beta_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(beta_iter, current_slice * beta_attrs.stride_x));
#endif /* USE_DEFAULT_GAMMA */

    if((current_slice % uint(4)) == uint(0))
    {
        denominator = unpacked_s[1].x;
        denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON))));

        // Calculate x bar
        numerator   = unpacked_s[2].x;
        x_bar       = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator);

#ifndef USE_DEFAULT_GAMMA
        gamma_param = unpacked_s[3].x;
#endif /* USE_DEFAULT_GAMMA */
#ifndef USE_DEFAULT_BETA
        beta_param  = unpacked_s[4].x;
#endif /* USE_DEFAULT_BETA */
    }
    else if((current_slice % uint(4)) == uint(1))
    {
        denominator = unpacked_s[1].y;
        denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON))));

        // Calculate x bar
        numerator   = unpacked_s[2].y;
        x_bar       = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator);

#ifndef USE_DEFAULT_GAMMA
        gamma_param = unpacked_s[3].y;
#endif /* USE_DEFAULT_GAMMA */
#ifndef USE_DEFAULT_BETA
        beta_param  = unpacked_s[4].y;
#endif /* USE_DEFAULT_BETA */
    }
    else if((current_slice % uint(4)) == uint(2))
    {
        denominator = unpacked_s[1].z;
        denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON))));

        // Calculate x bar
        numerator   = unpacked_s[2].z;
        x_bar       = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator);

#ifndef USE_DEFAULT_GAMMA
        gamma_param = unpacked_s[3].z;
#endif /* USE_DEFAULT_GAMMA */
#ifndef USE_DEFAULT_BETA
        beta_param  = unpacked_s[4].z;
#endif /* USE_DEFAULT_BETA */
    }
    else
    {
        denominator = unpacked_s[1].w;
        denominator = INVSQRT_OP(ADD_OP(denominator, SQCVT_SAT(float(ESPILON))));

        // Calculate x bar
        numerator   = unpacked_s[2].w;
        x_bar       = MUL_OP(SUB_OP(unpacked_s[0], numerator), denominator);

#ifndef USE_DEFAULT_GAMMA
        gamma_param = unpacked_s[3].w;
#endif /* USE_DEFAULT_GAMMA */
#ifndef USE_DEFAULT_BETA
        beta_param  = unpacked_s[4].w;
#endif /* USE_DEFAULT_BETA */
    }

#ifndef USE_DEFAULT_GAMMA
    x_bar = MUL_OP(gamma_param, x_bar);
#endif /* USE_DEFAULT_GAMMA */
#ifndef USE_DEFAULT_BETA
    x_bar = ADD_OP(x_bar, beta_param);
#endif /* USE_DEFAULT_BETA */

    result = ACTIVATION_FUNC(x_bar);

    STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, result);
}
#endif /*DATA_TYPE_FP16*/