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
|
#include "crossbar_generator.h"
#include "read_crossbar.h"
#include "spires_interface.h"
#include <math.h>
#include <plplot/plplot.h>
#include <spires.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_NEURONS 400
#define NUM_INPUTS 4
#define NUM_OUTPUTS 2
#define NUM_CROSSBAR_COLUMNS (NUM_OUTPUTS * 2)
#define NUM_TRAINING_STEPS 500
#define NUM_STEPS 2000
#define SPIKE_THRESHOLD 0.9
#define SPIKE_AMPLITUDE 0.1
#define PI 3.14159265358979323846
static int plot_raster(const Reservoir_State_Matrix *matrix,
size_t neurons_to_plot, double spike_threshold);
static int plot_reservoir_predictions(const double *expected,
const double *predicted,
size_t num_samples, size_t num_outputs,
size_t output_to_plot);
int main(void)
{
// discrete LIF parameters for spires
double lif_config[] = {
0.0, // V_off
1.0, // V_th
0.2, // leak rate
0.5, // bias
};
const spires_reservoir_config config = {
.num_neurons = NUM_NEURONS,
.num_inputs = NUM_INPUTS,
.num_outputs = NUM_OUTPUTS,
.spectral_radius = 0.95,
.ei_ratio = 0.8,
.input_strength = 0.1,
.connectivity = 0.1,
.dt = 1.0,
.connectivity_type = SPIRES_CONN_RANDOM,
.neuron_type = SPIRES_NEURON_LIF_DISCRETE,
.neuron_params = lif_config};
spires_reservoir *reservoir = NULL;
spires_status status = spires_reservoir_create(&config, &reservoir);
if (status != SPIRES_OK) {
fprintf(stderr, "Failed to create reservoir");
return -1;
}
// create training inputs
double training_inputs[NUM_TRAINING_STEPS * NUM_INPUTS];
for (size_t timestep = 0; timestep < NUM_TRAINING_STEPS; timestep++) {
for (size_t input = 0; input < NUM_INPUTS; input++) {
training_inputs[timestep * NUM_INPUTS + input] =
sin(2.0 * PI * (double)timestep / 50.0);
}
}
// create target outputs
double target_outputs[NUM_TRAINING_STEPS * NUM_OUTPUTS];
for (size_t timestep = 0; timestep < NUM_TRAINING_STEPS; timestep++) {
size_t next_timestep = (timestep + 1) % NUM_TRAINING_STEPS;
double target = sin(2.0 * PI * (double)next_timestep / 50.0);
for (size_t output = 0; output < NUM_OUTPUTS; output++) {
target_outputs[timestep * NUM_OUTPUTS + output] =
target;
}
}
Reservoir_State_Matrix state_matrix = {0};
if (collect_reservoir_states(reservoir, training_inputs,
NUM_TRAINING_STEPS, &state_matrix) != 0) {
fprintf(stderr, "Failed to collect reservoir states");
spires_reservoir_destroy(reservoir);
return -1;
}
printf("collected state matrix: %zu x %zu\n", state_matrix.num_samples,
state_matrix.num_features);
// training the readout layer
const double lambda = 1.0e-4;
int training_status =
train_reservoir(reservoir, training_inputs, target_outputs,
NUM_TRAINING_STEPS, lambda);
if (training_status < 0) {
fprintf(stderr, "Failed to train the reservoir");
free_reservoir_state_matrix(&state_matrix);
spires_reservoir_destroy(reservoir);
return -1;
}
// generate raster plot for verification
if (plot_raster(&state_matrix, NUM_NEURONS, SPIKE_THRESHOLD) != 0) {
fprintf(stderr, "Failed to plot raster\n");
}
// copy readout weights and convert to conductances
double *initial_resistances = NULL;
conductance_mapping mapping;
if (convert_weights_to_resistances(
reservoir, NUM_NEURONS, NUM_OUTPUTS, 1000.0, 100000.0,
&initial_resistances, &mapping) != 0) {
return -1;
}
double *row_voltages =
malloc(state_matrix.num_features * state_matrix.num_samples *
sizeof(double));
if (row_voltages == NULL) {
fprintf(stderr, "Failed to allocate spikes voltages");
free(initial_resistances);
free_reservoir_state_matrix(&state_matrix);
spires_reservoir_destroy(reservoir);
return -1;
}
for (size_t sample = 0; sample < state_matrix.num_samples; sample++) {
for (size_t neuron = 0; neuron < state_matrix.num_features;
neuron++) {
size_t index =
sample * state_matrix.num_features + neuron;
row_voltages[index] =
SPIKE_AMPLITUDE * state_matrix.states[index];
}
}
const Crossbar_Config crossbar_config = {
.rows = state_matrix.num_features,
.columns = NUM_CROSSBAR_COLUMNS,
.input_series = row_voltages,
.num_samples = state_matrix.num_samples,
.initial_resistance = initial_resistances,
.model_path = "models/hp_memristor.cir",
.subcircuit_name = "memristor",
.load_resistance = 50.0,
.time_step = 1e-6,
.stop_time = state_matrix.num_samples * 1e-6,
.print_state_nodes = 0};
if (generate_crossbar("output/crossbar.cir", &crossbar_config) < 0) {
fprintf(stderr, "failed to create crossbar config");
free(initial_resistances);
free_reservoir_state_matrix(&state_matrix);
spires_reservoir_destroy(reservoir);
return -1;
}
printf("Generated crossbar!!");
// call ngspice for crossbar
if (run_ngspice("output/crossbar.cir") < 0) {
fprintf(stderr, "Failed to run_ngspice");
free(initial_resistances);
free_reservoir_state_matrix(&state_matrix);
spires_reservoir_destroy(reservoir);
return -1;
}
printf("ran ngspice!!");
// crossbar parameters needed for reading
Crossbar_Output_Matrix crossbar_output = {
.num_samples = NUM_TRAINING_STEPS,
.num_outputs = NUM_CROSSBAR_COLUMNS,
.time = NULL,
.voltages = NULL};
if (read_crossbar("output/crossbar_output.dat", NUM_CROSSBAR_COLUMNS,
&crossbar_output) < 0) {
fprintf(stderr, "Failed to read crossbar output file");
free(initial_resistances);
free_reservoir_state_matrix(&state_matrix);
spires_reservoir_destroy(reservoir);
free_crossbar_output_matrix(&crossbar_output);
return -1;
}
printf("read the crossbar outputs!!\n");
double *decoded_outputs =
malloc(NUM_OUTPUTS * NUM_TRAINING_STEPS * sizeof(double));
if (decoded_outputs == NULL) {
fprintf(stderr,
"Failed to allocate memory for decoded outputs");
}
if (convert_output_to_software(
NUM_NEURONS, NUM_OUTPUTS, NUM_TRAINING_STEPS,
crossbar_output.voltages, initial_resistances,
crossbar_config.load_resistance, &mapping, row_voltages,
SPIKE_AMPLITUDE, decoded_outputs) < 0) {
fprintf(stderr,
"Failed to convert crossbar outputs back to software");
return -1;
}
// comparing prediction
for (int i = 0; i < NUM_TRAINING_STEPS; i++) {
printf("expected: %g , predicted: %g\n", target_outputs[i],
decoded_outputs[i]);
}
// plotting expected vs. prediction
plot_reservoir_predictions(target_outputs, decoded_outputs,
NUM_TRAINING_STEPS, NUM_OUTPUTS, 0);
printf("YAY IT WORKED!!! Cleaning up :)");
free(initial_resistances);
// free(spikes_voltages);
free(row_voltages);
free(decoded_outputs);
free_reservoir_state_matrix(&state_matrix);
spires_reservoir_destroy(reservoir);
free_crossbar_output_matrix(&crossbar_output);
return 0;
}
static int plot_raster(const Reservoir_State_Matrix *matrix,
size_t neurons_to_plot, double spike_threshold)
{
if (!matrix || !matrix->states || matrix->num_samples == 0) {
return -1;
}
if (neurons_to_plot > matrix->num_features) {
neurons_to_plot = matrix->num_features;
}
// count spikes
size_t spike_count = 0;
for (size_t t = 0; t < matrix->num_samples; t++) {
for (size_t n = 0; n < neurons_to_plot; n++) {
double value =
matrix->states[t * matrix->num_features + n];
if (value > spike_threshold) {
spike_count++;
}
}
}
if (spike_count == 0) {
fprintf(stderr, "No spikes found above threshold %.3f\n",
spike_threshold);
return -1;
}
PLFLT *x = malloc(spike_count * sizeof(*x));
PLFLT *y = malloc(spike_count * sizeof(*y));
if (!x || !y) {
free(x);
free(y);
return -1;
}
// fill spike coordinates
size_t k = 0;
for (size_t t = 0; t < matrix->num_samples; t++) {
for (size_t n = 0; n < neurons_to_plot; n++) {
double value =
matrix->states[t * matrix->num_features + n];
if (value > spike_threshold) {
x[k] = (PLFLT)t;
y[k] = (PLFLT)n;
k++;
}
}
}
// output to png
plsdev("svg");
plsfnam("output/reservoir_raster.svg");
plsetopt("geometry", "1600x1200");
plscolbg(255, 255, 255);
plinit();
plscol0(1, 40, 40, 40); // gray axis
plscol0(2, 0, 0, 0); // blue points
plcol0(1);
plwidth(1.0);
plenv(0.0, (PLFLT)(matrix->num_samples - 1), 0.0,
(PLFLT)(neurons_to_plot - 1), 0, 0);
pllab("Timestep", "Neuron index", "SPIRES Reservoir Raster Plot");
plcol0(2);
plwidth(1.0);
for (size_t i = 0; i < spike_count; i++) {
PLFLT xline[2] = {x[i], x[i]};
PLFLT yline[2] = {y[i] - 0.35, y[i] + 0.35};
plline(2, xline, yline);
}
plend();
free(x);
free(y);
return 0;
}
static int plot_reservoir_predictions(const double *expected,
const double *predicted,
size_t num_samples, size_t num_outputs,
size_t output_to_plot)
{
PLFLT *x;
PLFLT *y_expected;
PLFLT *y_predicted;
PLFLT y_min;
PLFLT y_max;
if (!expected || !predicted || num_samples == 0 ||
output_to_plot >= num_outputs)
return -1;
x = malloc(num_samples * sizeof(*x));
y_expected = malloc(num_samples * sizeof(*y_expected));
y_predicted = malloc(num_samples * sizeof(*y_predicted));
if (!x || !y_expected || !y_predicted) {
free(x);
free(y_expected);
free(y_predicted);
return -1;
}
y_min = (PLFLT)expected[output_to_plot];
y_max = y_min;
for (size_t sample = 0; sample < num_samples; sample++) {
size_t index;
index = sample * num_outputs + output_to_plot;
x[sample] = (PLFLT)sample;
y_expected[sample] = (PLFLT)expected[index];
y_predicted[sample] = (PLFLT)predicted[index];
if (y_expected[sample] < y_min)
y_min = y_expected[sample];
if (y_expected[sample] > y_max)
y_max = y_expected[sample];
if (y_predicted[sample] < y_min)
y_min = y_predicted[sample];
if (y_predicted[sample] > y_max)
y_max = y_predicted[sample];
}
{
PLFLT margin;
margin = (y_max - y_min) * 0.1;
if (margin == 0.0)
margin = 1.0;
y_min -= margin;
y_max += margin;
}
plsdev("svg");
plsfnam("output/reservoir_prediction.svg");
plsetopt("geometry", "1600x1000");
plscolbg(255, 255, 255);
plinit();
plscol0(1, 0, 0, 0);
plscol0(2, 30, 90, 200);
plscol0(3, 200, 50, 50);
plcol0(1);
plwidth(1.0);
plenv(0.0, (PLFLT)(num_samples - 1), y_min, y_max, 0, 0);
pllab("Timestep", "Output", "Expected vs SPICE Crossbar Prediction");
plcol0(2);
plwidth(2.0);
plline((PLINT)num_samples, x, y_expected);
plcol0(3);
plwidth(2.0);
plline((PLINT)num_samples, x, y_predicted);
plcol0(1);
plcol0(2);
plptex((PLFLT)(num_samples * 0.75), y_max - 0.08 * (y_max - y_min), 1.0,
0.0, 0.0, "Expected");
plcol0(3);
plptex((PLFLT)(num_samples * 0.75), y_max - 0.16 * (y_max - y_min), 1.0,
0.0, 0.0, "Predicted");
plend();
free(x);
free(y_expected);
free(y_predicted);
return 0;
}
|