Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/mvn.cl
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2017, Intel Corporation, all rights reserved.
14
// Copyright (c) 2016-2017 Fabian David Tschopp, all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#if defined(cl_khr_fp16)
44
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
45
#endif
46
47
#define Dtype float
48
#define Dtype4 float4
49
#define Dtype8 float8
50
51
#if NUM == 8
52
#define load(src, index) vload8(0, src + index)
53
#define store(vec, dst, index) vstore8(vec, 0, dst + index)
54
#define vec_type Dtype8
55
#define CALC_MEAN calc_mean8
56
#define MVN mvn8
57
#define MEAN_FUSE mean_fuse8
58
#define MVN_FUSE mvn_fuse8
59
#elif NUM == 4
60
#define load(src, index) vload4(0, src + index)
61
#define store(vec, dst, index) vstore4(vec, 0, dst + index)
62
#define vec_type Dtype4
63
#define CALC_MEAN calc_mean4
64
#define MVN mvn4
65
#define MEAN_FUSE mean_fuse4
66
#define MVN_FUSE mvn_fuse4
67
#elif NUM == 1
68
#define load(src, index) src[index]
69
#define store(vec, dst, index) dst[index] = vec
70
#define vec_type Dtype
71
#define CALC_MEAN calc_mean1
72
#define MVN mvn1
73
#define MEAN_FUSE mean_fuse1
74
#define MVN_FUSE mvn_fuse1
75
#endif
76
77
__kernel void CALC_MEAN(__global const Dtype* src,
78
const int rows,
79
const int cols,
80
__global Dtype* mean,
81
__global Dtype* dst)
82
{
83
int x = get_global_id(0);
84
int y = get_global_id(1) * NUM;
85
int index = x * cols + y;
86
87
if (x >= rows || y >= cols)
88
return;
89
90
Dtype mean_val = mean[x];
91
vec_type src_vec = load(src, index);
92
vec_type dst_vec = src_vec - (vec_type)mean_val;
93
dst_vec = dst_vec * dst_vec;
94
store(dst_vec, dst, index);
95
}
96
97
__kernel void MVN(__global const Dtype* src,
98
const int rows,
99
const int cols,
100
const Dtype eps,
101
__global const Dtype* mean,
102
__global const Dtype* dev,
103
__global const Dtype* bnorm_weight,
104
__global const Dtype* bnorm_bias,
105
const int channels,
106
const float relu_slope,
107
__global Dtype* dst)
108
{
109
int x = get_global_id(0);
110
int y = get_global_id(1) * NUM;
111
int index = x * cols + y;
112
113
if (x >= rows || y >= cols)
114
return;
115
116
Dtype mean_val = mean[x];
117
Dtype dev_val = sqrt(dev[x]);
118
Dtype alpha;
119
#ifdef NORM_VARIANCE
120
alpha = 1 / (eps + dev_val);
121
#else
122
alpha = 1;
123
#endif
124
125
Dtype w = 1.f, b = 0.f;
126
#ifdef FUSE_BATCH_NORM
127
w = bnorm_weight[x % channels];
128
b = bnorm_bias[x % channels];
129
#endif
130
131
vec_type src_vec = load(src, index) - (vec_type)mean_val;
132
vec_type dst_vec = src_vec * alpha;
133
dst_vec = dst_vec * w + (vec_type)b;
134
135
#ifdef FUSE_RELU
136
vec_type new_val = dst_vec * relu_slope;
137
dst_vec = select(new_val, dst_vec, dst_vec > (vec_type)0.f);
138
#endif
139
140
store(dst_vec, dst, index);
141
}
142
143
__kernel void MEAN_FUSE(__global const T * A,
144
unsigned int A_col_size,
145
float alpha,
146
__global T4 * mean,
147
__global Dtype * tmp,
148
__local Dtype4 * work)
149
{
150
unsigned int row_gid = get_group_id(0);
151
unsigned int lid = get_local_id(0);
152
const __global T *src0_read = A + row_gid * 4 * A_col_size;
153
__global Dtype *dst0_read = tmp + row_gid * 4 * A_col_size;
154
Dtype4 dot0, dot1, dot2, dot3;
155
dot0 = dot1 = dot2 = dot3 = (Dtype4)(0.f);
156
157
unsigned int i = lid;
158
const Dtype4 b0 = (Dtype4)1.f;
159
while( i < A_col_size / 4)
160
{
161
const T4 a0 = vload4(i, src0_read);
162
const T4 a1 = vload4(i, src0_read + A_col_size);
163
const T4 a2 = vload4(i, src0_read + 2 * A_col_size);
164
const T4 a3 = vload4(i, src0_read + 3 * A_col_size);
165
166
dot0 += convert_float4(a0);
167
dot1 += convert_float4(a1);
168
dot2 += convert_float4(a2);
169
dot3 += convert_float4(a3);
170
171
i += get_local_size(0);
172
}
173
174
work[lid].s0 = dot(dot0, b0);
175
work[lid].s1 = dot(dot1, b0);
176
work[lid].s2 = dot(dot2, b0);
177
work[lid].s3 = dot(dot3, b0);
178
179
for(unsigned int stride=get_local_size(0)/2 ; stride>0 ; stride>>=1)
180
{
181
barrier(CLK_LOCAL_MEM_FENCE);
182
if(lid < stride)
183
work[lid] += work[lid+stride];
184
}
185
barrier(CLK_LOCAL_MEM_FENCE);
186
187
if(lid == 0)
188
{
189
mean[row_gid] = convert_T(alpha * work[0]);
190
}
191
192
Dtype4 sum = work[0] * alpha;
193
i = lid;
194
while( i < A_col_size / 4)
195
{
196
const T4 a0 = vload4(i, src0_read);
197
const T4 a1 = vload4(i, src0_read + A_col_size);
198
const T4 a2 = vload4(i, src0_read + 2 * A_col_size);
199
const T4 a3 = vload4(i, src0_read + 3 * A_col_size);
200
201
dot0 = convert_float4(a0) - (Dtype4)sum.x;
202
dot1 = convert_float4(a1) - (Dtype4)sum.y;
203
dot2 = convert_float4(a2) - (Dtype4)sum.z;
204
dot3 = convert_float4(a3) - (Dtype4)sum.w;
205
dot0 = dot0 * dot0;
206
dot1 = dot1 * dot1;
207
dot2 = dot2 * dot2;
208
dot3 = dot3 * dot3;
209
210
vstore4(dot0, i, dst0_read);
211
vstore4(dot1, i, dst0_read + A_col_size);
212
vstore4(dot2, i, dst0_read + 2 * A_col_size);
213
vstore4(dot3, i, dst0_read + 3 * A_col_size);
214
215
i += get_local_size(0);
216
}
217
}
218
219
__kernel void MVN_FUSE(__global const Dtype * tmp,
220
__global const T * A,
221
__global const T4 * mean,
222
unsigned int A_col_size,
223
const float alpha_val,
224
const float eps,
225
const float relu_slope,
226
__global const Dtype4 * bnorm_weight,
227
__global const Dtype4 * bnorm_bias,
228
__global T * B,
229
__local Dtype4 * work)
230
{
231
unsigned int row_gid = get_group_id(0);
232
unsigned int lid = get_local_id(0);
233
const __global Dtype *src0_read = tmp + row_gid * 4 * A_col_size;
234
const __global T *src1_read = A + row_gid * 4 * A_col_size;
235
__global T *dst0_read = B + row_gid * 4 * A_col_size;
236
Dtype4 dot0, dot1, dot2, dot3;
237
dot0 = dot1 = dot2 = dot3 = (Dtype4)(0.f);
238
239
unsigned int i = lid;
240
const Dtype4 b0 = (Dtype4)1.f;
241
while( i < A_col_size / 4)
242
{
243
const Dtype4 a0 = vload4(i, src0_read);
244
const Dtype4 a1 = vload4(i, src0_read + A_col_size);
245
const Dtype4 a2 = vload4(i, src0_read + 2 * A_col_size);
246
const Dtype4 a3 = vload4(i, src0_read + 3 * A_col_size);
247
248
dot0 += a0;
249
dot1 += a1;
250
dot2 += a2;
251
dot3 += a3;
252
253
i += get_local_size(0);
254
}
255
256
work[lid].s0 = dot(dot0, b0);
257
work[lid].s1 = dot(dot1, b0);
258
work[lid].s2 = dot(dot2, b0);
259
work[lid].s3 = dot(dot3, b0);
260
261
for(unsigned int stride=get_local_size(0)/2 ; stride>0 ; stride>>=1)
262
{
263
barrier(CLK_LOCAL_MEM_FENCE);
264
if(lid < stride)
265
work[lid] += work[lid+stride];
266
}
267
barrier(CLK_LOCAL_MEM_FENCE);
268
269
Dtype4 mean_val = convert_float4(mean[row_gid]);
270
Dtype4 dev_val = sqrt(work[0] * alpha_val) + (Dtype4)eps;
271
Dtype4 alpha = (Dtype4)1.f / dev_val;
272
273
Dtype4 w = (Dtype4)1.f;
274
Dtype4 b = (Dtype4)0.f;
275
#ifdef FUSE_BATCH_NORM
276
w = bnorm_weight[row_gid];
277
b = bnorm_bias[row_gid];
278
#endif
279
280
i = lid;
281
while( i < A_col_size / 4)
282
{
283
const T4 a0 = vload4(i, src1_read);
284
const T4 a1 = vload4(i, src1_read + A_col_size);
285
const T4 a2 = vload4(i, src1_read + 2 * A_col_size);
286
const T4 a3 = vload4(i, src1_read + 3 * A_col_size);
287
288
dot0 = (convert_float4(a0) - (Dtype4)mean_val.x) * alpha.x;
289
dot1 = (convert_float4(a1) - (Dtype4)mean_val.y) * alpha.y;
290
dot2 = (convert_float4(a2) - (Dtype4)mean_val.z) * alpha.z;
291
dot3 = (convert_float4(a3) - (Dtype4)mean_val.w) * alpha.w;
292
293
dot0 = dot0 * w.x + (Dtype4)b.x;
294
dot1 = dot1 * w.y + (Dtype4)b.y;
295
dot2 = dot2 * w.z + (Dtype4)b.z;
296
dot3 = dot3 * w.w + (Dtype4)b.w;
297
298
#ifdef FUSE_RELU
299
Dtype4 new0 = dot0 * relu_slope;
300
dot0 = select(new0, dot0, dot0 > (Dtype4)0.f);
301
302
Dtype4 new1 = dot1 * relu_slope;
303
dot1 = select(new1, dot1, dot1 > (Dtype4)0.f);
304
305
Dtype4 new2 = dot2 * relu_slope;
306
dot2 = select(new2, dot2, dot2 > (Dtype4)0.f);
307
308
Dtype4 new3 = dot3 * relu_slope;
309
dot3 = select(new3, dot3, dot3 > (Dtype4)0.f);
310
#endif
311
312
vstore4(convert_T(dot0), i, dst0_read);
313
vstore4(convert_T(dot1), i, dst0_read + A_col_size);
314
vstore4(convert_T(dot2), i, dst0_read + 2 * A_col_size);
315
vstore4(convert_T(dot3), i, dst0_read + 3 * A_col_size);
316
317
i += get_local_size(0);
318
}
319
}
320
321