-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSpatialPooler.cu
522 lines (441 loc) · 14.1 KB
/
SpatialPooler.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <stdio.h>
#include <cuda.h>
#include <curand_kernel.h>
using namespace std;
typedef unsigned int UInt;
typedef float Real;
struct args
{
// Parameters
UInt inputPitch;
UInt stimulusThreshold;
Real potentialPct;
Real connectedPct;
Real localAreaDensity;
Real synPermTrimThreshold;
Real synPermMax;
Real synPermConnected;
Real synPermActiveInc;
Real synPermInactiveDec;
Real synPermBelowStimulusInc;
UInt dutyCyclePeriod;
Real boostStrength;
Real minPctOdc;
bool learn;
// Data
bool* in_dev;
bool* cols_dev;
UInt* olaps_dev;
UInt* pot_dev;
Real* per_dev;
Real* boosts_dev;
Real* odc_dev; // odc serve to maintain same act. freq. for each col. (per block)
Real* adc_dev; // adc serve to compute boost factors
UInt* numPot_dev;
Real* minOdc_dev;
// Constants
UInt SP_SIZE;
UInt IN_SIZE;
UInt BLOCK_SIZE;
UInt NUM_BLOCKS;
UInt IN_BLOCK_SIZE; // Size of chunk of input processed by a single cuda block
UInt MAX_CONNECTED;
Real IN_DENSITY; // Density of input connections
UInt num_connected;
// Array pitches
size_t pot_dev_pitch;
size_t per_dev_pitch;
// Bookkeeping vars
UInt iteration_num;
UInt update_period;
curandState* dev_states;
};
__global__ void setup_kernel(curandState *state)
{
int id = threadIdx.x + blockIdx.x*blockDim.x;
curand_init(727612, id, 0, &state[id]);
}
__device__
inline void random_swap(volatile UInt& a, volatile UInt& b, curandState& state)
{
// if(curand(state) & 1)
if(curand_uniform(&state) < 0.5)
{
UInt temp;
temp = a;
a = b;
b = temp;
}
}
__global__
void generatePotentialPools(UInt* pot_dev, size_t pot_dev_pitch, UInt num_connected, UInt* input_indeces, curandState* states, UInt IN_BLOCK_SIZE)
{
UInt tx = threadIdx.x;
UInt BLOCK_SIZE = blockDim.x;
curandState localState = states[threadIdx.x + blockIdx.x*blockDim.x];
extern __shared__ volatile UInt shared[];
shared[tx] = input_indeces[tx];
int id = BLOCK_SIZE;
float x = 0;
while(id < IN_BLOCK_SIZE - tx)
{
// x = (float) (curand(&localState) % 100) / 100;
x = curand_uniform(&localState);
// if(x > (float) BLOCK_SIZE / IN_BLOCK_SIZE)
if(x < 0.5)
{
shared[tx] = input_indeces[tx+id];
}
id += BLOCK_SIZE;
}
__syncthreads();
// Do reduction on shared
if(BLOCK_SIZE >= 1024)
{
if(tx < 512)
{
random_swap(shared[tx], shared[tx+512], localState);
}
__syncthreads();
}
if(BLOCK_SIZE >= 512)
{
if(tx < 256)
{
random_swap(shared[tx], shared[tx+256], localState);
}
__syncthreads();
}
if(BLOCK_SIZE >= 256)
{
if(tx < 128)
{
random_swap(shared[tx], shared[tx+128], localState);
}
__syncthreads();
}
if(BLOCK_SIZE >= 128)
{
if(tx < 64)
{
random_swap(shared[tx], shared[tx+64], localState);
}
__syncthreads();
}
if(tx < 32)
{
if(BLOCK_SIZE >= 64)
random_swap(shared[tx], shared[tx+32], localState);
if(BLOCK_SIZE >= 32)
random_swap(shared[tx], shared[tx+16], localState);
if(BLOCK_SIZE >= 16)
random_swap(shared[tx], shared[tx+8], localState);
if(BLOCK_SIZE >= 8)
random_swap(shared[tx], shared[tx+4], localState);
if(BLOCK_SIZE >= 4)
random_swap(shared[tx], shared[tx+2], localState);
if(BLOCK_SIZE >= 2)
random_swap(shared[tx], shared[tx+1], localState);
}
__syncthreads();
if(tx < num_connected)
pot_dev[blockIdx.x*pot_dev_pitch + tx] = shared[tx];
}
__global__
void generatePermanences(Real* per_dev, size_t per_dev_pitch, Real connectedPct, Real synPermConnected, Real synPermMax, curandState* states)
{
UInt col = blockIdx.x;
UInt tx = threadIdx.x;
curandState localState = states[col*blockDim.x + tx];
bool connected = (Real) curand_uniform(&localState) <= connectedPct;
per_dev[col*per_dev_pitch + tx] = connected ? synPermConnected + (synPermMax - synPermConnected)*((Real) curand_uniform(&localState)) :
synPermConnected * (Real)curand_uniform(&localState);
}
__device__
void calculateOverlap(volatile UInt* olaps_sh, volatile bool* in_sh, bool* in_dev, UInt* pot_dev, size_t pot_dev_pitch, Real* per_dev, size_t per_dev_pitch, Real* boosts_dev, Real threshold, UInt numConnected, const UInt IN_BLOCK_SIZE)
{
UInt tx = threadIdx.x;
UInt sp_idx = blockDim.x*blockIdx.x + tx; // Global index in the SP
UInt in_block_start = IN_BLOCK_SIZE*blockIdx.x;
olaps_sh[tx] = 0;
for(int i = 0; i < IN_BLOCK_SIZE - tx; i += blockDim.x)
in_sh[tx + i] = in_dev[in_block_start + tx + i];
__syncthreads();
for(int i=0; i < numConnected; i++)
{
UInt bl_idx = pot_dev[sp_idx*pot_dev_pitch+i]; // Index of block-specific input
if(in_sh[bl_idx] && (per_dev[sp_idx*per_dev_pitch + i] > threshold))
olaps_sh[tx] += boosts_dev[sp_idx+i];
}
// __syncthreads();
//
// olaps_sh[tx] = olaps;
}
__device__
void inhibitColumns(volatile UInt* olaps_sh, bool* cols_dev, volatile Real* active_sh, bool &active, Real sparsity)
{
int tx = threadIdx.x;
int numLarger = 0;
active = false;
for(int i=0; i < blockDim.x; i++)
{
if(olaps_sh[i] > olaps_sh[tx]) numLarger++;
}
if(numLarger < sparsity * (Real) blockDim.x && numLarger > 0) active = true;
__syncthreads();
cols_dev[blockIdx.x*blockDim.x + tx] = active;
active_sh[tx] = active;
}
__device__
void adaptSynapses(bool* in_dev, UInt* pot_dev, Real* per_dev, Real synPermActiveInc, Real synPermInactiveDec, bool active, const UInt inBlockSize, UInt num_connected, const size_t per_dev_pitch, const size_t pot_dev_pitch)
{
int tx = threadIdx.x;
int sp_idx = blockDim.x*blockIdx.x + tx;
if(active)
{
for(int i=0; i < num_connected; i++)
{
int in_idx = pot_dev[sp_idx*pot_dev_pitch+i];
if(in_dev[inBlockSize*blockIdx.x + in_idx])
per_dev[sp_idx*per_dev_pitch+i] = min(1.0, per_dev[sp_idx*per_dev_pitch+i]+synPermActiveInc);
else
per_dev[sp_idx*per_dev_pitch+i] = max(per_dev[sp_idx*per_dev_pitch+i]-synPermInactiveDec, 0.0);
}
}
}
__device__
void updateDutyCycles(Real* odc_dev, Real* adc_dev, volatile UInt* olaps_sh, bool active, UInt iteration_num, UInt dutyCyclePeriod)
{
int tx = threadIdx.x;
// Let grow divisor only to a dutyCyclePeriod to not make the update increasingly negligible
Real period = dutyCyclePeriod > iteration_num ? iteration_num : dutyCyclePeriod;
odc_dev[blockDim.x*blockIdx.x+tx] = (odc_dev[blockDim.x*blockIdx.x+tx]*(period-1) + (Real)(olaps_sh[tx] > 0)) / period;
adc_dev[blockDim.x*blockIdx.x+tx] = (odc_dev[blockDim.x*blockIdx.x+tx]*(period-1) + (Real)active) / period;
}
__device__
void averageActivity(volatile Real* active_sh)
{
Real avg = 0;
for(int i=0; i < blockDim.x; i++)
{
avg += active_sh[i];
}
active_sh[threadIdx.x] = avg / (Real)blockDim.x;
}
__device__
void averageActivityReduction(volatile Real* active_sh)
{
int tx = threadIdx.x;
UInt BLOCK_SIZE = blockDim.x;
if(BLOCK_SIZE >= 1024)
{
if(tx < 512)
{
active_sh[tx] += active_sh[tx+512];
}
__syncthreads();
}
if(BLOCK_SIZE >= 512)
{
if(tx < 256)
{
active_sh[tx] += active_sh[tx+256];
}
__syncthreads();
}
if(BLOCK_SIZE >= 256)
{
if(tx < 128)
{
active_sh[tx] += active_sh[tx+128];
}
__syncthreads();
}
if(BLOCK_SIZE >= 128)
{
if(tx < 64)
{
active_sh[tx] += active_sh[tx+64];
}
__syncthreads();
}
if(tx < 32)
{
if(BLOCK_SIZE >= 64)
active_sh[tx] += active_sh[tx+32];
if(BLOCK_SIZE >= 32)
active_sh[tx] += active_sh[tx+16];
if(BLOCK_SIZE >= 16)
active_sh[tx] += active_sh[tx+8];
if(BLOCK_SIZE >= 8)
active_sh[tx] += active_sh[tx+4];
if(BLOCK_SIZE >= 4)
active_sh[tx] += active_sh[tx+2];
if(BLOCK_SIZE >= 2)
active_sh[tx] += active_sh[tx+1];
}
__syncthreads();
// According to https://devblogs.nvidia.com/using-shared-memory-cuda-cc/, this should result in a broadcast
active_sh[tx] = active_sh[0] / BLOCK_SIZE;
}
__device__
void updateBoosts(Real* adc_dev, Real* boosts_dev, Real targetDensity, Real boostStrength)
{
int sp_idx = blockIdx.x*blockDim.x+threadIdx.x;
boosts_dev[sp_idx] = exp((targetDensity - adc_dev[sp_idx])*boostStrength);
}
__device__
void bumpUpColumnsWithWeakOdc(Real* odc_dev, Real* per_dev, UInt* numPot, Real* minOdc_dev, Real synPermBelowStimulusInc, const UInt MAX_CONNECTED)
{
int tx = threadIdx.x;
int sp_idx = blockIdx.x*blockDim.x+tx;
if(odc_dev[sp_idx] < minOdc_dev[blockIdx.x]) {
for(int i=0; i<numPot[sp_idx]; i++)
per_dev[tx*MAX_CONNECTED+i] += synPermBelowStimulusInc;
}
}
__device__
void updateMinOdc(Real* odc_dev, volatile Real* odc_sh, Real* minOdc_dev, Real minPctOdc, const UInt SP_SIZE)
{
Real maxOdc = 0;
for(int i=0; i<SP_SIZE; i++)
maxOdc = odc_dev[i] > maxOdc ? odc_dev[i] : maxOdc;
if(threadIdx.x == 0)
minOdc_dev[blockIdx.x] = minPctOdc * maxOdc;
}
__device__
void updateMinOdcReduction(Real* odc_dev, volatile Real* odc_sh, Real* minOdc_dev, Real minPctOdc, const UInt SP_SIZE)
{
int tx = threadIdx.x;
int sp_idx = blockDim.x*blockIdx.x + threadIdx.x;
UInt BLOCK_SIZE = blockDim.x;
odc_sh[tx] = odc_dev[sp_idx];
if(BLOCK_SIZE >= 1024)
{
if(tx < 512)
{
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+512]);
}
__syncthreads();
}
if(BLOCK_SIZE >= 512)
{
if(tx < 256)
{
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+256]);
}
__syncthreads();
}
if(BLOCK_SIZE >= 256)
{
if(tx < 128)
{
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+128]);
}
__syncthreads();
}
if(BLOCK_SIZE >= 128)
{
if(tx < 64)
{
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+64]);
}
__syncthreads();
}
if(tx < 32)
{
if(BLOCK_SIZE >= 64)
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+32]);
if(BLOCK_SIZE >= 32)
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+16]);
if(BLOCK_SIZE >= 16)
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+8]);
if(BLOCK_SIZE >= 8)
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+4]);
if(BLOCK_SIZE >= 4)
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+2]);
if(BLOCK_SIZE >= 2)
odc_sh[tx] = max(odc_sh[tx], odc_sh[tx+1]);
}
if(threadIdx.x == 0)
minOdc_dev[blockIdx.x] = minPctOdc * odc_sh[0];
}
__global__
void compute(args* ar_ptr)
{
// Global memory pointers
// bool* cols_dev = (bool*) data;
// bool* in_dev = &cols_dev[SP_SIZE];
// UInt* pot_dev = (UInt*) &in_dev[IN_SIZE];
// UInt* numPot_dev = &pot_dev[SP_SIZE*MAX_CONNECTED];
// Real* per_dev = (Real*) &numPot_dev[SP_SIZE];
// Real* boosts_dev = &per_dev[SP_SIZE*MAX_CONNECTED];
// UInt* olaps_dev = (UInt*) &boosts_dev[SP_SIZE*MAX_CONNECTED];
// Real* odc_dev = (Real*) &olaps_dev[SP_SIZE]; // odc serve to maintain same act. freq. for each col. (per block)
// Real* adc_dev = &odc_dev[MAX_CONNECTED*SP_SIZE]; // adc serve to compute boost factors
// Real* minOdc_dev = &adc_dev[MAX_CONNECTED*SP_SIZE]; // Stores minumum overlap duty cycles per block
if (blockIdx.x == 0 && threadIdx.x == 0)
ar_ptr->iteration_num++;
args ar = *ar_ptr;
bool active = false;
Real avg_act = 0;
extern __shared__ volatile UInt shared[];
volatile UInt* olaps_sh = &shared[0];
volatile Real* active_sh = (Real*)&shared[blockDim.x];
volatile Real* odc_sh = &active_sh[blockDim.x];
volatile bool* in_sh = (bool*) &odc_sh[blockDim.x];
// calculateOverlap(ar.in_dev, in_sh, ar.pot_dev, ar.per_dev, ar.boosts_dev, ar.numPot_dev, olaps_sh, ar.synPermConnected, ar.IN_BLOCK_SIZE, ar.MAX_CONNECTED);
calculateOverlap(olaps_sh, in_sh, ar.in_dev, ar.pot_dev, ar.pot_dev_pitch, ar.per_dev, ar.per_dev_pitch, ar.boosts_dev, ar.synPermConnected, ar.num_connected, ar.IN_BLOCK_SIZE);
__syncthreads();
inhibitColumns(olaps_sh, ar.cols_dev, active_sh, active, ar.localAreaDensity);
__syncthreads();
adaptSynapses(ar.cols_dev, ar.pot_dev, ar.per_dev, ar.synPermActiveInc, ar.synPermInactiveDec, active, ar.IN_BLOCK_SIZE, ar.num_connected, ar.per_dev_pitch, ar.pot_dev_pitch);
updateDutyCycles(ar.odc_dev, ar.adc_dev, olaps_sh, active, ar.iteration_num, ar.dutyCyclePeriod);
averageActivityReduction(active_sh);
__syncthreads();
updateBoosts(ar.adc_dev, ar.boosts_dev, avg_act, ar.boostStrength);
bumpUpColumnsWithWeakOdc(ar.odc_dev, ar.per_dev, ar.numPot_dev, ar.minOdc_dev, ar.synPermBelowStimulusInc, ar.MAX_CONNECTED);
if(ar.iteration_num % ar.update_period == 0)
updateMinOdc(ar.odc_dev, ar.odc_dev, ar.minOdc_dev, ar.minPctOdc, ar.SP_SIZE);
}
__global__
void calculateOverlap_wrapper(bool* in_dev, UInt* pot_dev, Real* per_dev, Real* boosts_dev, UInt* numPot_dev, Real threshold, const UInt inBlockSize, const UInt MAX_CONNECTED, UInt* olaps_dev, const UInt SP_SIZE, size_t pot_dev_pitch, size_t per_dev_pitch)
{
extern __shared__ volatile UInt shared[];
volatile UInt* olaps_sh = &shared[0];
volatile bool* in_sh = (bool*) &olaps_sh[blockDim.x];
calculateOverlap(olaps_sh, in_sh, in_dev, pot_dev, pot_dev_pitch, per_dev, per_dev_pitch, boosts_dev, threshold, MAX_CONNECTED, inBlockSize);
if(blockDim.x*blockIdx.x+threadIdx.x < SP_SIZE)
olaps_dev[blockDim.x*blockIdx.x+threadIdx.x] = olaps_sh[threadIdx.x];
}
__global__
void inhibitColumns_wrapper(UInt* olaps_dev, bool* cols_dev, Real localAreaDensity, const UInt BLOCK_SIZE)
{
extern __shared__ volatile UInt shared[];
volatile UInt* olaps_sh = &shared[0];
volatile Real* active_sh = (Real*) &olaps_sh[BLOCK_SIZE];
olaps_sh[threadIdx.x] = olaps_dev[threadIdx.x];
bool active = false;
__syncthreads();
inhibitColumns(olaps_sh, cols_dev, active_sh, active, localAreaDensity);
}
__global__
void adaptSynapses_wrapper(bool* in_dev, UInt* pot_dev, Real* per_dev, Real synPermActiveInc, Real synPermInactiveDec, bool* active_arr, const UInt IN_BLOCK_SIZE, const size_t per_dev_pitch, const size_t pot_dev_pitch, UInt num_connected, const UInt SP_SIZE)
{
int sp_idx = blockIdx.x*blockDim.x + threadIdx.x;
if(sp_idx < SP_SIZE)
{
bool active = active_arr[sp_idx];
adaptSynapses(in_dev, pot_dev, per_dev, synPermActiveInc, synPermInactiveDec, active, IN_BLOCK_SIZE, num_connected, per_dev_pitch, pot_dev_pitch);
}
}
__global__
void averageActivity_wrapper(bool* cols_dev, Real* avg_dev)
{
int tx = threadIdx.x;
extern __shared__ volatile UInt shared[];
volatile Real* active_sh = (Real*) &shared[0];
active_sh[tx] = (Real) cols_dev[tx];
averageActivityReduction(active_sh);
avg_dev[tx] = active_sh[tx];
}