forked from btgraham/SparseConvNet-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SigmoidLayer.cu
302 lines (298 loc) · 11.8 KB
/
SigmoidLayer.cu
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include "SigmoidLayer.h"
#include "utilities.h"
////////////////////////////////////////////////////////////////////////////////
__global__ void dSigmoidReLu(float *a, float *b, int nOut) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
b[j] = (a[j] > 0) ? a[j] : 0;
}
}
void sigmoidReLU(float *a, float *b, int count, int nOut,
cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768 / 4, count - processed);
dSigmoidReLu << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, nOut);
processed += batch;
}
cudaCheckError();
}
__global__ void dSigmoidBackpropReLu(float *a, float *b, float *da, float *db,
int nOut) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
da[j] = (a[j] > 0) ? db[j] : 0;
}
}
void sigmoidBackpropReLU(float *a, float *b, float *da, float *db, int count,
int nOut, cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768 / 4, count - processed);
dSigmoidBackpropReLu << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, da + processed * nOut,
db + processed * nOut, nOut);
processed += batch;
}
cudaCheckError();
}
////////////////////////////////////////////////////////////////////////////////
__global__ void dSigmoidLogistic(float *a, float *b, int nOut) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
b[j] = 1.0f / (1.0f + exp(-a[j]));
}
}
void sigmoidLogistic(float *a, float *b, int count, int nOut,
cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768 / 4, count - processed);
dSigmoidLogistic << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, nOut);
processed += batch;
}
cudaCheckError();
}
__global__ void dSigmoidBackpropLogistic(float *a, float *b, float *da,
float *db, int nOut) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
da[j] = db[j] * b[j] * (1 - b[j]);
}
}
void sigmoidBackpropLogistic(float *a, float *b, float *da, float *db,
int count, int nOut, cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768 / 4, count - processed);
dSigmoidBackpropLogistic << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, da + processed * nOut,
db + processed * nOut, nOut);
processed += batch;
}
cudaCheckError();
}
////////////////////////////////////////////////////////////////////////////////
__global__ void dSigmoidTanh(float *a, float *b, int nOut) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
b[j] = tanhf(a[j]);
}
}
void sigmoidTanh(float *a, float *b, int count, int nOut,
cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768 / 4, count - processed);
dSigmoidTanh << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, nOut);
processed += batch;
}
cudaCheckError();
}
__global__ void dSigmoidBackpropTanh(float *a, float *b, float *da, float *db,
int nOut) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
da[j] = db[j] * (1 + b[j]) * (1 - b[j]);
}
}
void sigmoidBackpropTanh(float *a, float *b, float *da, float *db, int count,
int nOut, cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768 / 4, count - processed);
dSigmoidBackpropTanh << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, da + processed * nOut,
db + processed * nOut, nOut);
processed += batch;
}
cudaCheckError();
}
////////////////////////////////////////////////////////////////////////////////
__global__ void dSigmoidLeakyReLu(float *a, float *b, int nOut, float alpha) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
b[j] = (a[j] > 0) ? a[j] : (a[j] * alpha);
}
}
void sigmoidLeakyReLU(float *a, float *b, int count, int nOut,
float alpha, // 0.01 or 0.3 say
cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768, count - processed);
dSigmoidLeakyReLu << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, nOut, alpha);
processed += batch;
}
cudaCheckError();
}
__global__ void dSigmoidBackpropLeakyReLu(float *a, float *b, float *da,
float *db, int nOut, float alpha) {
int i = blockIdx.x * nOut;
for (int j = i + threadIdx.x; j < i + nOut; j += KERNELBLOCKSIZE) {
da[j] = (a[j] > 0) ? db[j] : (db[j] * alpha);
__syncthreads();
}
}
void sigmoidBackpropLeakyReLU(float *a, float *b, float *da, float *db,
int count, int nOut, float alpha,
cudaMemStream &memStream) {
int processed = 0;
while (processed < count) {
int batch = min(32768, count - processed);
dSigmoidBackpropLeakyReLu << <batch, KERNELBLOCKSIZE, 0, memStream.stream>>>
(a + processed * nOut, b + processed * nOut, da + processed * nOut,
db + processed * nOut, nOut, alpha);
processed += batch;
}
cudaCheckError();
}
////////////////////////////////////////////////////////////////////////////////
// SOFTMAX should only be used in the top layer;
// derivative contained in calculation of initial d_delta.
__global__ void dSigmoidSoftmax(float *a, float *b, int count, int nOut) {
for (int i = threadIdx.x; i < count; i += NTHREADS) {
float acc = 0.0f;
float mx = -10000.0f;
for (int k = 0; k < nOut; k++)
if (a[i * nOut + k] > mx)
mx = a[i * nOut + k];
for (int k = 0; k < nOut; k++) {
b[i * nOut + k] =
expf((a[i * nOut + k] -
mx)); // Subtract row max value for numerical stability.
acc += b[i * nOut + k];
}
for (int k = 0; k < nOut; k++) {
b[i * nOut + k] /= acc;
}
}
}
__global__ void dSigmoidBackpropSoftmax(float *a, float *b, float *da,
float *db, int count, int nOut) {
for (int i = 0; i < count; i++) {
for (int k = threadIdx.x; k < nOut; k += NTHREADS) {
da[i * nOut + k] = db[i * nOut + k];
}
}
}
////////////////////////////////////////////////////////////////////////////////
void applySigmoid(SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output, ActivationFunction fn,
cudaMemStream &memStream) {
switch (fn) {
case SIGMOID:
sigmoidLogistic(input.sub->features.dPtr(), output.sub->features.dPtr(),
output.nSpatialSites, output.featuresPresent.size(),
memStream);
break;
case TANH:
sigmoidTanh(input.sub->features.dPtr(), output.sub->features.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), memStream);
break;
case RELU:
sigmoidReLU(input.sub->features.dPtr(), output.sub->features.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), memStream);
break;
case LEAKYRELU:
sigmoidLeakyReLU(input.sub->features.dPtr(), output.sub->features.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), 0.01,
memStream);
break;
case VLEAKYRELU:
sigmoidLeakyReLU(input.sub->features.dPtr(), output.sub->features.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), 0.333,
memStream);
break;
case SOFTMAX:
dSigmoidSoftmax << <1, NTHREADS, 0, memStream.stream>>>
(input.sub->features.dPtr(), output.sub->features.dPtr(),
output.nSpatialSites, output.featuresPresent.size());
break;
case NOSIGMOID:
break;
}
}
void applySigmoidBackProp(SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output,
ActivationFunction fn, cudaMemStream &memStream) {
switch (fn) {
case SIGMOID:
sigmoidBackpropLogistic(
input.sub->features.dPtr(), output.sub->features.dPtr(),
input.sub->dfeatures.dPtr(), output.sub->dfeatures.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), memStream);
break;
case TANH:
sigmoidBackpropTanh(input.sub->features.dPtr(), output.sub->features.dPtr(),
input.sub->dfeatures.dPtr(),
output.sub->dfeatures.dPtr(), output.nSpatialSites,
output.featuresPresent.size(), memStream);
break;
case RELU:
sigmoidBackpropReLU(input.sub->features.dPtr(), output.sub->features.dPtr(),
input.sub->dfeatures.dPtr(),
output.sub->dfeatures.dPtr(), output.nSpatialSites,
output.featuresPresent.size(), memStream);
break;
case LEAKYRELU:
sigmoidBackpropLeakyReLU(
input.sub->features.dPtr(), output.sub->features.dPtr(),
input.sub->dfeatures.dPtr(), output.sub->dfeatures.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), 0.01, memStream);
break;
case VLEAKYRELU:
sigmoidBackpropLeakyReLU(
input.sub->features.dPtr(), output.sub->features.dPtr(),
input.sub->dfeatures.dPtr(), output.sub->dfeatures.dPtr(),
output.nSpatialSites, output.featuresPresent.size(), 0.333, memStream);
break;
case SOFTMAX:
dSigmoidBackpropSoftmax << <1, NTHREADS, 0, memStream.stream>>>
(input.sub->features.dPtr(), output.sub->features.dPtr(),
input.sub->dfeatures.dPtr(), output.sub->dfeatures.dPtr(),
output.nSpatialSites, output.featuresPresent.size());
break;
case NOSIGMOID:
break;
}
}
SigmoidLayer::SigmoidLayer(cudaMemStream &memStream, ActivationFunction fn)
: SpatiallySparseLayer(memStream), fn(fn) {
std::cout << sigmoidNames[fn] << std::endl;
};
void SigmoidLayer::preprocess(SpatiallySparseBatch &batch,
SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output) {
output.nFeatures = input.nFeatures;
output.featuresPresent.hVector() = input.featuresPresent.hVector();
output.spatialSize = input.spatialSize;
output.nSpatialSites = input.nSpatialSites;
output.grids = input.grids;
output.backpropErrors = input.backpropErrors;
}
void SigmoidLayer::forwards(SpatiallySparseBatch &batch,
SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output) {
output.sub->features.resize(output.nSpatialSites *
output.featuresPresent.size());
applySigmoid(input, output, fn, memStream);
}
void SigmoidLayer::backwards(SpatiallySparseBatch &batch,
SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output,
float learningRate, float momentum) {
if (input.backpropErrors) {
input.sub->dfeatures.resize(input.nSpatialSites *
input.featuresPresent.size());
applySigmoidBackProp(input, output, fn, memStream);
}
}
int SigmoidLayer::calculateInputSpatialSize(int outputSpatialSize) {
return outputSpatialSize;
}