-
Notifications
You must be signed in to change notification settings - Fork 5
/
bindings.cu
373 lines (301 loc) · 9.83 KB
/
bindings.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
#include <torch/extension.h>
#include <vector>
#include "lif_kernels.h"
#include "leaky_kernels.h"
#include "experimental_kernels.h"
#define CHECK_CUDA(x) AT_ASSERTM(x.is_cuda(), #x " must be a CUDA tensor")
#define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous")
#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)
#define CHECK_DEVICE(x, y) AT_ASSERTM(x.device().index() == y.device().index(), #x " and " #y " must be in same CUDA device")
// LIF dynamics
void lifForward(
const torch::Tensor& outputSpikes,
const torch::Tensor& vmem,
const torch::Tensor& input,
const torch::Tensor& vmemPostInitial,
const torch::Tensor& alpha,
const torch::Tensor& membrSubtract,
const torch::Tensor& theta,
const torch::Tensor& thetaLow,
const bool applyThetaLow,
const int maxNumSpikes)
{
CHECK_INPUT(input);
CHECK_INPUT(outputSpikes);
CHECK_INPUT(vmem);
CHECK_INPUT(vmemPostInitial);
CHECK_INPUT(alpha);
CHECK_INPUT(membrSubtract);
CHECK_INPUT(theta);
CHECK_INPUT(thetaLow);
// check if tensors are on same device
CHECK_DEVICE(input, vmem);
CHECK_DEVICE(input, outputSpikes);
CHECK_DEVICE(input, vmemPostInitial);
CHECK_DEVICE(input, alpha);
CHECK_DEVICE(input, membrSubtract);
CHECK_DEVICE(input, theta);
CHECK_DEVICE(input, thetaLow);
// set the current cuda device to wherever the tensor input resides
cudaSetDevice(input.device().index());
unsigned nTimesteps = input.size(-1);
unsigned nNeurons = input.size(0);
// convert maxNumSpikes to usnigned (-1 will become max)
unsigned maxNumSpikesU = maxNumSpikes;
// // output spikes
// auto outputSpikes = torch::empty_like(input);
// // membrane potential
// auto vmem = torch::empty_like(input);
lifForwardCuda<float>(
outputSpikes.data_ptr<float>(),
vmem.data_ptr<float>(),
input.data_ptr<float>(),
vmemPostInitial.data_ptr<float>(),
alpha.data_ptr<float>(),
membrSubtract.data_ptr<float>(),
theta.data_ptr<float>(),
thetaLow.data_ptr<float>(),
applyThetaLow, maxNumSpikesU, nNeurons, nTimesteps);
return;
}
torch::Tensor lifBackward(
const torch::Tensor& surr,
const torch::Tensor& outputGrad,
const torch::Tensor& notClipped,
const torch::Tensor& alpha,
const torch::Tensor& membrSubtract)
{
CHECK_INPUT(surr);
CHECK_INPUT(outputGrad);
CHECK_INPUT(notClipped);
CHECK_INPUT(alpha);
CHECK_INPUT(membrSubtract);
// check if tensors are on same device
CHECK_DEVICE(surr, outputGrad);
CHECK_DEVICE(surr, notClipped);
CHECK_DEVICE(surr, alpha);
CHECK_DEVICE(surr, membrSubtract);
// set the current cuda device to wherever the tensor surr resides
cudaSetDevice(surr.device().index());
unsigned nTimesteps = surr.size(-1);
unsigned nNeurons = surr.size(0);
// input gradients
auto inputGrad = torch::empty_like(surr);
lifBackwardCuda<float>(
inputGrad.data_ptr<float>(),
outputGrad.data_ptr<float>(),
surr.data_ptr<float>(),
notClipped.data_ptr<float>(),
alpha.data_ptr<float>(),
membrSubtract.data_ptr<float>(),
nNeurons, nTimesteps);
return inputGrad;
}
torch::Tensor lifBackwardAlpha(
const torch::Tensor& surr,
const torch::Tensor& outputGrad,
const torch::Tensor& vmemPost,
const torch::Tensor& vmemPostInitial,
const torch::Tensor& notClipped,
const torch::Tensor& alpha,
const torch::Tensor& membrSubtract)
{
CHECK_INPUT(surr);
CHECK_INPUT(outputGrad);
CHECK_INPUT(vmemPost);
CHECK_INPUT(vmemPostInitial);
CHECK_INPUT(notClipped);
CHECK_INPUT(alpha);
CHECK_INPUT(membrSubtract);
// check if tensors are on same device
CHECK_DEVICE(surr, outputGrad);
CHECK_DEVICE(surr, vmemPost);
CHECK_DEVICE(surr, vmemPostInitial);
CHECK_DEVICE(surr, notClipped);
CHECK_DEVICE(surr, alpha);
CHECK_DEVICE(surr, membrSubtract);
// set the current cuda device to wherever the tensor surr resides
cudaSetDevice(surr.device().index());
unsigned nTimesteps = surr.size(-1);
unsigned nNeurons = surr.size(0);
// input gradients
auto alphaGrad = torch::empty_like(alpha);
lifBackwardAlphaCuda<float>(
alphaGrad.data_ptr<float>(),
outputGrad.data_ptr<float>(),
vmemPost.data_ptr<float>(),
vmemPostInitial.data_ptr<float>(),
surr.data_ptr<float>(),
notClipped.data_ptr<float>(),
alpha.data_ptr<float>(),
membrSubtract.data_ptr<float>(),
nNeurons, nTimesteps);
return alphaGrad;
}
// Leaky integrators
torch::Tensor leakyForward(
const torch::Tensor& input,
const torch::Tensor& vmemInitial,
const torch::Tensor& alpha)
{
CHECK_INPUT(input);
CHECK_INPUT(vmemInitial);
CHECK_INPUT(alpha);
// check if tensors are on same device
CHECK_DEVICE(input, vmemInitial);
CHECK_DEVICE(input, alpha);
// set the current cuda device to wherever the tensor vmemInitial resides
cudaSetDevice(vmemInitial.device().index());
unsigned nTimesteps = input.size(-1);
unsigned nNeurons = input.size(0);
// Tensor to store membrane potential
auto vmemFull = torch::empty_like(input);
leakyForwardCuda<float>(
vmemFull.data_ptr<float>(),
input.data_ptr<float>(),
vmemInitial.data_ptr<float>(),
alpha.data_ptr<float>(),
nNeurons, nTimesteps);
return vmemFull;
}
torch::Tensor leakyBackward(
const torch::Tensor& outputGrad,
const torch::Tensor& alpha)
{
CHECK_INPUT(outputGrad);
CHECK_INPUT(alpha);
// check if tensors are on same device
CHECK_DEVICE(outputGrad, alpha);
// set the current cuda device to wherever the tensor outputGrad resides
cudaSetDevice(outputGrad.device().index());
unsigned nTimesteps = outputGrad.size(-1);
unsigned nNeurons = outputGrad.size(0);
// Tensor to store input gradient
auto inputGrad = torch::empty_like(outputGrad);
leakyBackwardCuda<float>(
inputGrad.data_ptr<float>(),
outputGrad.data_ptr<float>(),
alpha.data_ptr<float>(),
nNeurons, nTimesteps);
return inputGrad;
}
torch::Tensor leakyBackwardAlpha(
const torch::Tensor& outputGrad,
const torch::Tensor& output,
const torch::Tensor& vmemInitial,
const torch::Tensor& alpha)
{
CHECK_INPUT(outputGrad);
CHECK_INPUT(output);
CHECK_INPUT(vmemInitial);
CHECK_INPUT(alpha);
// check if tensors are on same device
CHECK_DEVICE(outputGrad, output);
CHECK_DEVICE(outputGrad, vmemInitial);
CHECK_DEVICE(outputGrad, alpha);
// set the current cuda device to wherever the tensor outputGrad resides
cudaSetDevice(outputGrad.device().index());
unsigned nTimesteps = outputGrad.size(-1);
unsigned nNeurons = outputGrad.size(0);
// Tensor to store alpha gradient
auto alphaGrad = torch::zeros_like(alpha);
leakyBackwardAlphaCuda<float>(
alphaGrad.data_ptr<float>(),
outputGrad.data_ptr<float>(),
output.data_ptr<float>(),
vmemInitial.data_ptr<float>(),
alpha.data_ptr<float>(),
nNeurons, nTimesteps);
return alphaGrad;
}
// Experimental functions
torch::Tensor spikeForward(
torch::Tensor d_u,
const float alpha,
const float membrSubtract,
const float theta,
const float theta_low,
const bool applyThetaLow,
const int maxNumSpikes)
{
CHECK_INPUT(d_u);
// set the current cuda device to wherever the tensor d_u resides
cudaSetDevice(d_u.device().index());
// Tensor to collect output spikes
auto d_s = torch::zeros_like(d_u);
// convert maxNumSpikes to usnigned (-1 will become max)
unsigned maxNumSpikesU = maxNumSpikes;
unsigned nTimesteps = d_u.size(-1);
unsigned nNeurons = d_u.size(0);
spikeForwardCuda<float>(
d_s.data_ptr<float>(),
d_u.data_ptr<float>(),
alpha, membrSubtract, nNeurons, nTimesteps, theta, theta_low, applyThetaLow, maxNumSpikesU);
return d_s;
}
torch::Tensor spikeBackwardRefrCuda(
const torch::Tensor& surr, const torch::Tensor& outputGrad, const torch::Tensor& refr)
{
CHECK_INPUT(surr);
CHECK_INPUT(outputGrad);
CHECK_INPUT(refr);
// check if tensors are on same device
CHECK_DEVICE(surr, outputGrad);
CHECK_DEVICE(surr, refr);
// set the current cuda device to wherever the tensor d_u resides
cudaSetDevice(surr.device().index());
unsigned refrSize = refr.size(-1);
unsigned nTimesteps = surr.size(-1);
unsigned nNeurons = surr.size(0);
// jacobian
auto jaco = torch::zeros({nNeurons, nTimesteps, nTimesteps}, torch::dtype(torch::kFloat32).device(surr.device()));
// input gradients
auto inputGrad = torch::zeros_like(surr);
spikeBackwardRefr<float>(
inputGrad.data_ptr<float>(),
outputGrad.data_ptr<float>(),
jaco.data_ptr<float>(),
surr.data_ptr<float>(),
refr.data_ptr<float>(),
nNeurons, refrSize, nTimesteps);
return inputGrad;
}
torch::Tensor spikeBackward(
const torch::Tensor& surr,
const torch::Tensor& outputGrad,
const torch::Tensor& notClipped,
const float alpha,
const float membrSubtract)
{
CHECK_INPUT(surr);
CHECK_INPUT(outputGrad);
CHECK_INPUT(notClipped);
// check if tensors are on same device
CHECK_DEVICE(surr, outputGrad);
CHECK_DEVICE(surr, notClipped);
// set the current cuda device to wherever the tensor d_u resides
cudaSetDevice(surr.device().index());
unsigned nTimesteps = surr.size(-1);
unsigned nNeurons = surr.size(0);
// input gradients
auto inputGrad = torch::empty_like(surr);
spikeBackwardCuda<float>(
inputGrad.data_ptr<float>(),
outputGrad.data_ptr<float>(),
surr.data_ptr<float>(),
notClipped.data_ptr<float>(),
alpha, membrSubtract, nNeurons, nTimesteps);
return inputGrad;
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
{
m.def("spikeForward" , &spikeForward , "Spike generation forward pass");
m.def("spikeBackward" , &spikeBackward , "Spike generation backward pass");
m.def("spikeBackwardRefr", &spikeBackwardRefrCuda, "Spike generation backward pass for arbitrary refractory response");
m.def("lifBackward" , &lifBackward , "LIF backward pass");
m.def("lifBackwardAlpha" , &lifBackwardAlpha , "LIF backward pass for alphas");
m.def("lifForward" , &lifForward , "LIF forward dynamics");
m.def("leakyForward" , &leakyForward , "Forward pass of leaky integrator");
m.def("leakyBackward" , &leakyBackward , "Backward pass of leaky integrator");
m.def("leakyBackwardAlpha", &leakyBackwardAlpha,"Backward pass of leaky integrator for alphas");
}