forked from tugrul512bit/FastSimpleNeuralNetworkTrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastSimpleNeuralNetworkTrainer.h
More file actions
608 lines (526 loc) · 26.5 KB
/
Copy pathFastSimpleNeuralNetworkTrainer.h
File metadata and controls
608 lines (526 loc) · 26.5 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#pragma once
#include<vector>
#include<iostream>
#include<memory>
#include<functional>
#include<exception>
#include"UfSaCL.h"
namespace GPGPU
{
namespace Util
{
template<int ... NEURAL_NETWORK_ARCHITECTURE>
constexpr int ComputeNumberOfNeuralNetworkParameters()
{
int result = 0;
constexpr int vec[sizeof...(NEURAL_NETWORK_ARCHITECTURE)] = { NEURAL_NETWORK_ARCHITECTURE... };
int lastWidth = 1;
for (int i = 0; i < sizeof...(NEURAL_NETWORK_ARCHITECTURE); i++)
{
result += lastWidth * vec[i] + vec[i];
lastWidth = vec[i];
}
return result;
}
template<int ... NEURAL_NETWORK_ARCHITECTURE>
constexpr int ComputeSizeOfFirstLayer()
{
constexpr int vec[sizeof...(NEURAL_NETWORK_ARCHITECTURE)] = { NEURAL_NETWORK_ARCHITECTURE... };
return vec[0];
}
template<int ... NEURAL_NETWORK_ARCHITECTURE>
constexpr int ComputeSizeOfLastLayer()
{
constexpr int vec[sizeof...(NEURAL_NETWORK_ARCHITECTURE)] = { NEURAL_NETWORK_ARCHITECTURE... };
return vec[(sizeof...(NEURAL_NETWORK_ARCHITECTURE)) - 1];
}
template<int ... NEURAL_NETWORK_ARCHITECTURE>
constexpr int ComputeLargestLayerSize()
{
int result = 0;
constexpr int vec[sizeof...(NEURAL_NETWORK_ARCHITECTURE)] = { NEURAL_NETWORK_ARCHITECTURE... };
for (int i = 0; i < sizeof...(NEURAL_NETWORK_ARCHITECTURE); i++)
{
if (result < vec[i])
result = vec[i];
}
return result;
}
}
template<int INPUT_SIZE, int OUTPUT_SIZE>
class TrainingData
{
public:
TrainingData():_sz(0)
{
}
void AddInputOutputPair(std::vector<float> inputElements, std::vector<float> outputElements)
{
_sz++;
for (int i = 0; i < INPUT_SIZE; i++)
{
_inputs.push_back(inputElements[i]);
}
for (int i = 0; i < OUTPUT_SIZE; i++)
{
_outputs.push_back(outputElements[i]);
}
}
int Size()
{
return _sz;
}
std::vector<float> GetInputs()
{
return _inputs;
}
std::vector<float> GetOutputs()
{
return _outputs;
}
private:
int _sz;
std::vector<float> _inputs;
std::vector<float> _outputs;
};
class TrainedModel
{
private:
int _numInputs;
int _numOutputs;
std::vector<float> _parameters;
std::vector<int> _architecture;
std::function<std::vector<float>(std::vector<float> inputs)> _run;
std::string _functionCode;
public:
TrainedModel() { _numInputs = 0; _numOutputs = 0; }
TrainedModel(std::vector<float> parameters, std::vector<int> architecture,
std::function<std::vector<float>(std::vector<float> inputs)> run,
std::string functionCode)
{
_numInputs = architecture[0];
_numOutputs = architecture[architecture.size() - 1];
_parameters = parameters;
_architecture = architecture;
_run = run;
_functionCode = functionCode;
}
// inference
std::vector<float> Run(std::vector<float> inputs)
{
if (inputs.size() != _numInputs)
{
std::cout << "Error: input size is not same as neural architecture" << std::endl;
exit(1);
}
return _run(inputs);
}
// infer on GPU for many different inputs (higher throughput, higher latency)
// designed for millions of NPCs running their own brains
std::vector<std::vector<float>> RunMultiple(std::vector<std::vector<float>> inputSets)
{
throw std::logic_error("not implemented");
return std::vector<std::vector<float>>();
}
// save parameters to use elsewhere
std::vector<float> GetParameters()
{
return _parameters;
}
// to use code directly in GPU kernels with C-style function
std::string GetFunctionCodeString()
{
return _functionCode;
}
};
/* GPU - based Trainer Tool For Simple Neural Networks
Every GPU has thousands of pipelines
Every pipeline runs the same neural architecture but with different data
Each inpt-output training data pair flows through a different GPU pipeline
Possible to take millions of training data pairs
Parameters are stored in in-chip fast shared-memory(local memory)
*/
template<int NUM_PARALLEL_SIMULATIONS,int ... NEURAL_NETWORK_ARCHITECTURE>
class FastSimpleNeuralNetworkTrainer
{
private:
std::vector<int> _architecture;
std::string constantsDefines;
std::shared_ptr<UFSACL::UltraFastSimulatedAnnealing<Util::ComputeNumberOfNeuralNetworkParameters<NEURAL_NETWORK_ARCHITECTURE...>(), NUM_PARALLEL_SIMULATIONS>> _sim;
bool _built;
public:
FastSimpleNeuralNetworkTrainer(const int numThreadsPerBlock = 256)
{
_architecture = { NEURAL_NETWORK_ARCHITECTURE... };
_built = false;
try
{
constantsDefines = std::string("#define NUM_NETWORK_INPUTS ")+std::to_string(Util::ComputeSizeOfFirstLayer<NEURAL_NETWORK_ARCHITECTURE...>())+std::string(R"(
)");
constantsDefines += std::string("#define NUM_NETWORK_OUTPUTS ") + std::to_string(Util::ComputeSizeOfLastLayer<NEURAL_NETWORK_ARCHITECTURE...>()) + std::string(R"(
)");
constantsDefines += std::string("#define NUM_NETWORK_LARGEST_LAYER ") + std::to_string(Util::ComputeLargestLayerSize<NEURAL_NETWORK_ARCHITECTURE...>()) + std::string(R"(
)");
// gpu-accelerated simulated-annealing that launches 1 block per simulation
_sim = std::make_shared<UFSACL::UltraFastSimulatedAnnealing<Util::ComputeNumberOfNeuralNetworkParameters<NEURAL_NETWORK_ARCHITECTURE...>(), NUM_PARALLEL_SIMULATIONS>>(
R"(
const int nData = settings[0];
const int nLayers = settings[1];
float energyLocal = 0.0f;
// do same work for each pair of input-output & compute error (as energy for simulated annealing)
// this parallel for loop is not per workitem but works on all workitems at once, so any local variables (energyLocal) only visible by themselves
parallelFor(nData,
{
int i=loopId;
float trainingDataInputTmp[NUM_NETWORK_INPUTS];
float trainingDataOutputTmp[NUM_NETWORK_OUTPUTS];
for(int itInp = 0; itInp<NUM_NETWORK_INPUTS; itInp++)
trainingDataInputTmp[itInp] = trainingDataInput[i*NUM_NETWORK_INPUTS + itInp];
trainingDataOutputTmp[0] = 0.0f;
Compute(architecture, trainingDataInputTmp, trainingDataOutputTmp, nLayers, parameters);
for(int itOutp = 0; itOutp<NUM_NETWORK_OUTPUTS; itOutp++)
{
float diff = (trainingDataOutput[i*NUM_NETWORK_OUTPUTS + itOutp] - trainingDataOutputTmp[itOutp]);
energy += pow(fabs(diff),0.5f);
}
});
energy += energyLocal;
)", numThreadsPerBlock);
_sim->addFunctionDefinition(constantsDefines+R"(
void Compute(global int * architecture, float * input, float * output, int numLayers, local float * parameters)
{
int parameterCtr = 0;
float layerVal[NUM_NETWORK_LARGEST_LAYER];
float layerValTmp[NUM_NETWORK_LARGEST_LAYER];
for(int i=0;i<numLayers;i++)
{
if(i==0)
{
// input layer
int n = architecture[i];
for(int j=0;j<n;j++)
{
const float bias = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron input multiplier
const float mult = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron output
layerVal[j] = tanh(mult * input[j] + bias);
}
}
else if(i==numLayers-1)
{
// output layer
int n = architecture[i];
int n0 = architecture[i-1];
for(int j=0;j<n;j++)
{
const float bias = parameters[parameterCtr++]*2.0f - 1.0f;
float acc = 0.0f;
for(int k=0;k<n0;k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
output[j] = tanh(acc + bias);
}
}
else
{
// hidden layer
int n = architecture[i];
int n0 = architecture[i-1];
for(int j=0;j<n;j++)
{
const float bias = parameters[parameterCtr++]*2.0f - 1.0f;
float acc = 0.0f;
for(int k=0;k<n0;k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
layerValTmp[j] = tanh(acc + bias);
}
for(int j=0;j<n;j++)
layerVal[j]=layerValTmp[j];
}
}
}
)");
}
catch (std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
}
/*
---- neural network parameters ----
trainingData: input-output data pairs for training
testInput: sample input from user to be used in callback that is called whenever a better energy is found by simulated annealing
callbackBetterEnergyFound: this is called whenever solver(simulated annealing) finds a better set of parameters
returns outputs of neural network with input given by testInput
---- simulated annealing parameters ----
startTemperature: usually between 1 and 0.1, decides maximum randomness added to parameters
stopTemperature: usually close to 0, decides minimum randomness added to parameters
coolingRate: how fast temperature goes down. generally between 1.00001f and 2.0f.
numReheating: once cooling is completed, result may not be the global one, so it retries while keeping the best parameter set, for this amount of times
*/
TrainedModel Train
(
TrainingData<Util::ComputeSizeOfFirstLayer<NEURAL_NETWORK_ARCHITECTURE...>(), Util::ComputeSizeOfLastLayer<NEURAL_NETWORK_ARCHITECTURE...>()> trainingData,
std::vector<float> testInput,
std::function<void(std::vector<float>)> callbackBetterEnergyFound,
float startTemperature = 1.0f,
float stopTemperature = 0.0001f,
float coolingRate = 1.1f,
int numReHeating = 5,
bool debugPerformance = false,
bool debugDevice = false,
bool debugEnergy = true
)
{
const int nTrainingData = trainingData.Size();
std::vector<float> trainingDataInput = trainingData.GetInputs();
std::vector<float> trainingDataOutput = trainingData.GetOutputs();
std::vector<int> settings = { nTrainingData,(int)_architecture.size() };
_sim->addUserInput("architecture", _architecture);
_sim->addUserInput("trainingDataInput", trainingDataInput);
_sim->addUserInput("trainingDataOutput", trainingDataOutput);
_sim->addUserInput("settings", settings);
if (!_built)
{
_built = true;
_sim->build();
}
int numInputs = Util::ComputeSizeOfFirstLayer<NEURAL_NETWORK_ARCHITECTURE...>();
int numOutputs = Util::ComputeSizeOfLastLayer<NEURAL_NETWORK_ARCHITECTURE...>();
int numLargestLayerSize = Util::ComputeLargestLayerSize<NEURAL_NETWORK_ARCHITECTURE...>();
std::vector<int> architecture = _architecture;
std::vector<float> prm = _sim->run(
startTemperature, stopTemperature, coolingRate, numReHeating,
debugPerformance, debugDevice, debugEnergy,
[numInputs,numOutputs,numLargestLayerSize,
callbackBetterEnergyFound, testInput,architecture]
(float* optimizedParameters)
{
std::vector<float> output(numOutputs,0.0f);
float* parameters = optimizedParameters;
{
int parameterCtr = 0;
std::vector<float> layerVal(numLargestLayerSize);
std::vector<float> layerValTmp(numLargestLayerSize);
for (int i = 0; i < architecture.size(); i++)
{
if (i == 0)
{
// input layer
int n = architecture[i];
for (int j = 0; j < n; j++)
{
const float bias = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron input multiplier
const float mult = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron output
layerVal[j] = tanh(mult * testInput[j] + bias);
}
}
else if (i == architecture.size() - 1)
{
// output layer
int n = architecture[i];
int n0 = architecture[i - 1];
for (int j = 0; j < n; j++)
{
const float bias = parameters[parameterCtr++] * 2.0f - 1.0f;
float acc = 0.0f;
for (int k = 0; k < n0; k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
output[j] = tanh(acc + bias);
}
}
else
{
// hidden layer
int n = architecture[i];
int n0 = architecture[i - 1];
for (int j = 0; j < n; j++)
{
const float bias = parameters[parameterCtr++] * 2.0f - 1.0f;
float acc = 0.0f;
for (int k = 0; k < n0; k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
layerValTmp[j] = tanh(acc + bias);
}
for (int j = 0; j < n; j++)
layerVal[j] = layerValTmp[j];
}
}
callbackBetterEnergyFound(output);
}
}
);
TrainedModel model(
prm,
architecture,
[numInputs, numOutputs, numLargestLayerSize,
callbackBetterEnergyFound, architecture, prm]
(std::vector<float> input)
{
std::vector<float> output(numOutputs, 0.0f);
const float* parameters = prm.data();
{
int parameterCtr = 0;
std::vector<float> layerVal(numLargestLayerSize);
std::vector<float> layerValTmp(numLargestLayerSize);
for (int i = 0; i < architecture.size(); i++)
{
if (i == 0)
{
// input layer
int n = architecture[i];
for (int j = 0; j < n; j++)
{
const float bias = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron input multiplier
const float mult = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron output
layerVal[j] = tanh(mult * input[j] + bias);
}
}
else if (i == architecture.size() - 1)
{
// output layer
int n = architecture[i];
int n0 = architecture[i - 1];
for (int j = 0; j < n; j++)
{
const float bias = parameters[parameterCtr++] * 2.0f - 1.0f;
float acc = 0.0f;
for (int k = 0; k < n0; k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
output[j] = tanh(acc + bias);
}
}
else
{
// hidden layer
int n = architecture[i];
int n0 = architecture[i - 1];
for (int j = 0; j < n; j++)
{
const float bias = parameters[parameterCtr++] * 2.0f - 1.0f;
float acc = 0.0f;
for (int k = 0; k < n0; k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++] * 2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
layerValTmp[j] = tanh(acc + bias);
}
for (int j = 0; j < n; j++)
layerVal[j] = layerValTmp[j];
}
}
return output;
}
},
constantsDefines + std::string(R"(
void Compute(global int * architecture, float * input, float * output, int numLayers, local float * parameters)
{
int parameterCtr = 0;
float layerVal[NUM_NETWORK_LARGEST_LAYER];
float layerValTmp[NUM_NETWORK_LARGEST_LAYER];
for(int i=0;i<numLayers;i++)
{
if(i==0)
{
// input layer
int n = architecture[i];
for(int j=0;j<n;j++)
{
const float bias = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron input multiplier
const float mult = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron output
layerVal[j] = tanh(mult * input[j] + bias);
}
}
else if(i==numLayers-1)
{
// output layer
int n = architecture[i];
int n0 = architecture[i-1];
for(int j=0;j<n;j++)
{
const float bias = parameters[parameterCtr++]*2.0f - 1.0f;
float acc = 0.0f;
for(int k=0;k<n0;k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
output[j] = tanh(acc + bias);
}
}
else
{
// hidden layer
int n = architecture[i];
int n0 = architecture[i-1];
for(int j=0;j<n;j++)
{
const float bias = parameters[parameterCtr++]*2.0f - 1.0f;
float acc = 0.0f;
for(int k=0;k<n0;k++)
{
// neuron input multiplier
const float mult = parameters[parameterCtr++]*2.0f - 1.0f;
// neuron output
acc += mult * layerVal[k];
}
layerValTmp[j] = tanh(acc + bias);
}
for(int j=0;j<n;j++)
layerVal[j]=layerValTmp[j];
}
}
}
)")+std::string(R"(
void ComputeNetwork(int inputOutputPairId,int * architecture, int nLayers, float * parameters)
{
int i=inputOutputPairId;
float trainingDataInputTmp[NUM_NETWORK_INPUTS];
float trainingDataOutputTmp[NUM_NETWORK_OUTPUTS];
for(int itInp = 0; itInp<NUM_NETWORK_INPUTS; itInp++)
trainingDataInputTmp[itInp] = trainingDataInput[i*NUM_NETWORK_INPUTS + itInp];
trainingDataOutputTmp[0] = 0.0f;
Compute(architecture, trainingDataInputTmp, trainingDataOutputTmp, nLayers, parameters);
}
)")
);
return model;
}
};
}