Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/ocl4dnn_pooling.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
#define CONCAT(A,B) A##_##B
44
#define TEMPLATE(name,type) CONCAT(name,type)
45
46
#if defined(cl_khr_fp16)
47
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
48
#endif
49
50
#if defined KERNEL_MAX_POOL
51
52
__kernel void
53
#ifdef HAVE_MASK
54
TEMPLATE(max_pool_forward_mask, Dtype)
55
#else
56
TEMPLATE(max_pool_forward, Dtype)
57
#endif
58
(
59
const int nthreads, __global const Dtype* bottom_data,
60
const int channels, const int height, const int width,
61
const int pooled_height, const int pooled_width,
62
__global Dtype* top_data
63
#ifdef HAVE_MASK
64
, __global Dtype* mask
65
#endif
66
)
67
{
68
int index = get_global_id(0);
69
if (index >= nthreads)
70
return;
71
72
const int pw = index % pooled_width;
73
const int xx = index / pooled_width;
74
const int ph = xx % pooled_height;
75
const int ch = xx / pooled_height;
76
int hstart = ph * STRIDE_H - PAD_T;
77
int wstart = pw * STRIDE_W - PAD_L;
78
Dtype maxval = -FLT_MAX;
79
int maxidx = -1;
80
int in_offset = ch * height * width;
81
for (int h = 0; h < KERNEL_H; ++h)
82
{
83
int off_y = hstart + h;
84
if (off_y >= 0 && off_y < height)
85
{
86
for (int w = 0; w < KERNEL_W; ++w)
87
{
88
int off_x = wstart + w;
89
if (off_x >= 0 && off_x < width)
90
{
91
Dtype val = bottom_data[in_offset + off_y * width + off_x];
92
maxidx = (val > maxval) ? (off_y * width + off_x) : maxidx;
93
maxval = fmax(val, maxval);
94
}
95
}
96
}
97
}
98
top_data[index] = maxval;
99
#ifdef HAVE_MASK
100
mask[index] = maxidx;
101
#endif
102
}
103
104
#elif defined KERNEL_AVE_POOL
105
106
__kernel void TEMPLATE(ave_pool_forward, Dtype)(
107
const int nthreads, __global const Dtype* bottom_data,
108
const int channels, const int height, const int width,
109
const int pooled_height, const int pooled_width,
110
__global Dtype* top_data)
111
{
112
int index = get_global_id(0);
113
if (index >= nthreads)
114
return;
115
116
const int pw = index % pooled_width;
117
const int xx = index / pooled_width;
118
const int ph = xx % pooled_height;
119
const int ch = xx / pooled_height;
120
int hstart = ph * STRIDE_H - PAD_T;
121
int wstart = pw * STRIDE_W - PAD_L;
122
int hend = min(hstart + KERNEL_H, height + PAD_B);
123
int wend = min(wstart + KERNEL_W, width + PAD_R);
124
int pool_size;
125
#ifdef AVE_POOL_PADDING_AREA
126
pool_size = (hend - hstart) * (wend - wstart);
127
hstart = max(hstart, (int)0);
128
wstart = max(wstart, (int)0);
129
hend = min(hend, height);
130
wend = min(wend, width);
131
#else
132
hstart = max(hstart, (int)0);
133
wstart = max(wstart, (int)0);
134
hend = min(hend, height);
135
wend = min(wend, width);
136
pool_size = (hend - hstart) * (wend - wstart);
137
#endif
138
Dtype aveval = 0;
139
int in_offset = ch * height * width;
140
for (int h = hstart; h < hend; ++h)
141
{
142
for (int w = wstart; w < wend; ++w)
143
{
144
aveval += bottom_data[in_offset + h * width + w];
145
}
146
}
147
top_data[index] = aveval / pool_size;
148
}
149
150
#elif defined KERNEL_STO_POOL
151
152
__kernel void TEMPLATE(sto_pool_forward_test,Dtype)(
153
const int nthreads, __global const Dtype* bottom_data,
154
const int channels, const int height, const int width,
155
const int pooled_height, const int pooled_width,
156
__global Dtype* top_data)
157
{
158
for (int index = get_global_id(0); index < nthreads;
159
index += get_global_size(0))
160
{
161
const int pw = index % pooled_width;
162
const int ph = (index / pooled_width) % pooled_height;
163
const int c = (index / pooled_width / pooled_height) % channels;
164
const int n = index / pooled_width / pooled_height / channels;
165
const int hstart = ph * STRIDE_H;
166
const int hend = min(hstart + KERNEL_H, height);
167
const int wstart = pw * STRIDE_W;
168
const int wend = min(wstart + KERNEL_W, width);
169
// We set cumsum to be 0 to avoid divide-by-zero problems
170
Dtype cumsum = FLT_MIN;
171
Dtype cumvalues = 0.;
172
__global const Dtype* bottom_slice = bottom_data
173
+ (n * channels + c) * height * width;
174
// First pass: get sum
175
for (int h = hstart; h < hend; ++h) {
176
for (int w = wstart; w < wend; ++w) {
177
Dtype v = bottom_slice[h * width + w];
178
cumsum += v;
179
cumvalues += v * v;
180
}
181
}
182
top_data[index] = cumvalues / cumsum;
183
}
184
}
185
186
#endif // KERNEL_*
187
188