-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkras.asm
524 lines (427 loc) · 13.3 KB
/
kras.asm
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
bits 64
%include "tensorFlopCaput.asm"
section .data
;===- Verbosity Texts -===;
;=========================;
showWeightsText db "Weights of the CNN :",10
SHOW_WEIGHTS_TEXT_LENGTH equ $-showWeightsText
showNeuronsText db "Activated neurons :",10
SHOW_NEURONS_TEXT_LENGTH equ $-showNeuronsText
showUnactivatedNeuronsText db "Unactivated neurons :",30
SHOW_UNACTIVATED_NEURONS_TEXT_LENGTH equ $-showUnactivatedNeuronsText
showLossHistoryText db "Losses :",10
SHOW_LOSS_HISTORY_TEXT_LENGTH equ $-showLossHistoryText
showNeuronsCostsText db "Backpropagation neurons' costs :",10
SHOW_NEURONS_COSTS_TEXT_LENGTH equ $-showNeuronsCostsText
showOutputText db "Last output :",10
SHOW_OUTPUT_TEXT_LENGTH equ $-showOutputText
showInputText db "Last input :",10
SHOW_INPUT_TEXT_LENGTH equ $-showInputText
showWeightsGradientText db "Weights gradient :",10
SHOW_WEIGHTS_GRADIENT_TEXT_LENGTH equ $-showWeightsGradientText
section .bss
;===- API Userish Variables -===;
;===============================;
;# Add layer #;
krasLayersCount: resq 1
krasLayersOffset: resq 1
krasLayersSizes: resq MAX_LAYERS_COUNT
krasActivationFunctions: resq MAX_LAYERS_COUNT
krasBiases: resq MAX_LAYERS_COUNT
krasBiasesInitialiser: resq MAX_LAYERS_COUNT
;# Fit #;
krasLearningRate: resq 1
krasEpochs: resq 1
krasBatchSize: resq 1
;# Export #;
krasExportFileName: resq 1
;# Generated sample #;
krasSampleSize : resq 1
krasSampleInputDim : resq 1
krasSampleOutputDim : resq 1
;# Verification sample #;
verificationInputs: resq 2
krasVerificationSquareData: resq 1
section .text
;===========================;
;/\- Kras-TensorFlop API -/\;
;===========================;
krasInitialise:
call krasPrepare
call initialiseTensorFlop
ret
krasPrepare:
mov qword [krasLayersCount], 0
mov qword [krasLayersOffset], 0
ret
krasAddLayer:
mov qword rcx, [krasLayersOffset]
; Store the layer size, and go back to offset
add qword rcx, krasLayersSizes
mov [rcx], rax
sub qword rcx, krasLayersSizes
; Store the layer activation function, and go back to offset
add qword rcx, krasActivationFunctions
mov [rcx], rbx
sub qword rcx, krasActivationFunctions
; Store if the layer has biases or not, and go back to offset
add qword rcx, krasBiases
mov [rcx], rdx
sub qword rcx, krasBiases
; Store biases initialiser , and go back to offset
add qword rcx, krasBiasesInitialiser
mov [rcx], rdi
sub qword rcx, krasBiasesInitialiser
inc qword [krasLayersCount]
add qword [krasLayersOffset], DOUBLE_SIZE
ret
krasCompile:
call krasInitialiseLayers
call createCnn
ret
krasInitialiseLayers:
mov qword rax, [krasLayersCount]
mov qword [CnnLayersCount], rax
mov qword rax, [CnnLayersCount]
mov qword rbx, krasLayersSizes
mov qword rcx, 0
mov qword r8, CnnLayersSizes
mov qword r9, DOUBLE_SIZE
call insertListAtIndex
mov qword rax, [CnnLayersCount]
mov qword rbx, krasActivationFunctions
mov qword rcx, 0
mov qword r8, CnnActivationFunctions
mov qword r9, DOUBLE_SIZE
call insertListAtIndex
ret
krasPredict:
call predictCnn
ret
krasFit:
mov [krasEpochs], rax
mov [CnnEpochs], rax
mov qword [krasLearningRate], rbx
mov qword [CnnLearningRate], rbx
mov [krasBatchSize], rcx
mov [CnnBatchSize], rcx
call fitCnn
ret
;===========================;
;/\- Kras- Dev Verbosity -/\;
;===========================;
krasShowWeights:
mov qword rbx, [CnnWeightsMatrixPointer]
mov qword rcx,[CnnWeightsCount]
call showPseudoMatrix
ret
krasShowNeurons:
mov rcx, showNeuronsText
mov rdx, SHOW_NEURONS_TEXT_LENGTH
call print
mov qword rbx, [CnnActivatedMatrixPointer]
mov qword rcx, [CnnNeuronsCount]
call showPseudoMatrix
call displayHugeSeparator
ret
krasShowUnactivatedNeurons:
mov rcx, showUnactivatedNeuronsText
mov rdx, SHOW_UNACTIVATED_NEURONS_TEXT_LENGTH
call print
mov qword rbx, [CnnUnactivatedMatrixPointer]
mov qword rcx, [CnnNeuronsCount]
call showPseudoMatrix
call displayHugeSeparator
ret
krasShowNeuronsCosts:
mov rcx, showNeuronsCostsText
mov rdx, SHOW_NEURONS_COSTS_TEXT_LENGTH
call print
mov qword rbx, [CnnCostMatrixPointer]
mov qword rcx, [CnnNeuronsCount]
call showPseudoMatrix
call displaySeperator
ret
krasShowWeightsGradient:
mov rcx, showWeightsGradientText
mov rdx, SHOW_WEIGHTS_GRADIENT_TEXT_LENGTH
call print
mov qword rbx, [CnnBackpropagationWeightsMatrixPointer]
mov qword rcx,[CnnWeightsCount]
call showPseudoMatrix
call displayHugeSeparator
ret
krasShowLossHistory:
mov rcx, showLossHistoryText
mov rdx, SHOW_LOSS_HISTORY_TEXT_LENGTH
call print
mov qword rbx, lossHistory
mov qword rcx, [krasEpochs]
mov qword rax, [krasBatchSize]
mul rcx
mov rcx, rax
call showPseudoMatrix
call displayHugeSeparator
ret
krasShowOutput:
mov rcx, showOutputText
mov rdx, SHOW_OUTPUT_TEXT_LENGTH
call print
mov qword rbx, [CnnActivatedMatrixPointer]
add qword rbx, [CnnLastLayerOffset]
mov qword rcx, [CnnDataOutputDim]
call showPseudoMatrix
call displayHugeSeparator
ret
krasShowInput:
mov rcx, showInputText
mov rdx, SHOW_INPUT_TEXT_LENGTH
call print
mov qword rbx, [CnnActivatedMatrixPointer]
add qword rbx, 0
mov qword rcx, [CnnDataInputDim]
call showPseudoMatrix
call displayHugeSeparator
ret
krasShowUnactivatedOutput:
mov rcx, showOutputText
mov rdx, SHOW_OUTPUT_TEXT_LENGTH
call print
mov qword rbx, [CnnUnactivatedMatrixPointer]
add qword rbx, [CnnLastLayerOffset]
mov qword rcx, [CnnDataOutputDim]
call showPseudoMatrix
call displaySeperator
ret
;===============================;
;/\- Kras- Sample management -/\;
;===============================;
krasPrepareSample:
;# Allocate memory for the input sample #;
mov rax, [krasSampleSize]
mov rdx, [krasSampleInputDim]
mul rdx
mov qword [cafElements], rax ; number of elements in my sample
mov qword [cafSize], DOUBLE_SIZE ; size of a float
call caf
mov qword [CnnDataInputSample], rax
;# Allocate memory for the output sample #;
mov rax, [krasSampleSize]
mov rdx, [krasSampleOutputDim]
mul rdx
mov qword [cafElements], rax ; number of elements in my sample
mov qword [cafSize], DOUBLE_SIZE ; size of a float
call caf
mov qword [CnnDataOutputSample], rax
;# Get input size #;
mov rdx, [krasSampleInputDim]
mov rax, DOUBLE_SIZE
mul rdx
mov qword [CnnDataInputSize], rax
;# Get output size #;
mov rdx, [krasSampleOutputDim]
mov rax, DOUBLE_SIZE
mul rdx
mov qword [CnnDataOutputSize], rax
ret
krasCreateSample:
mov qword [krasSampleSize], rax
mov qword [krasSampleInputDim], rbx
mov qword [krasSampleOutputDim], rcx
call krasPrepareSample
call krasGenerateRadiusSample
ret
krasGenerateRadiusSample:
mov qword rdi, [krasSampleInputDim]
mov rbx, [CnnDataInputSample]
mov rcx, [CnnDataOutputSample]
mov rdx, [krasSampleSize]
fcomp st0
krasGenerateRadiusSampleMainLOOP:
call generateRandomSample
call getDistance
fld qword [INITIAL_CLASSIFICATION_RADIUS_VALUE]
fld qword [computedFloatPointer]
fcomip st0, st1
jb krasGenerateRadiusSampleInside
krasGenerateRadiusSampleOutside:
fldz ; change
fstp qword [rcx]
add rcx, DOUBLE_SIZE
fld1
fstp qword [rcx]
jmp krasGenerateRadiusSampleDone
krasGenerateRadiusSampleInside:
fldz
fstp qword [rcx]
add rcx, DOUBLE_SIZE
fld1
fstp qword [rcx]
krasGenerateRadiusSampleDone:
add rbx, [CnnDataInputSize]
add rcx, DOUBLE_SIZE
fcomp st0
dec rdx
cmp qword rdx, 0
jnz krasGenerateRadiusSampleMainLOOP
ret
;=================================;
;/\- Kras- Verification sample -/\;
;=================================;
krasVerifySquare:
mov qword [cafElements], 800 ; number of elements in my square, outputdim*side**2
mov qword [cafSize], DOUBLE_SIZE ; size of a float
call caf
mov qword [krasVerificationSquareData], rax
fldz
fld1
fsubp
fst qword [verificationInputs]
fstp qword [verificationInputs+DOUBLE_SIZE]
xor rdx, rdx
xor rcx, rcx
mov rbx, [krasVerificationSquareData]
mov rax, [CnnActivatedMatrixPointer]
add rax, [CnnLastLayerOffset]
krasVerifySquareXYLOOP:
; Set my inputs as my square values
mov rdi, [CnnDataInputSample]
fld qword [verificationInputs]
fstp qword [rdi]
fld qword [verificationInputs+DOUBLE_SIZE]
fstp qword [rdi+DOUBLE_SIZE]
; Actually predict my square value
saveRegisters
call krasPredict
getBackRegisters
; Store my values
mov rdi, [rax]
mov [rbx], rdi
add rbx, DOUBLE_SIZE
mov rdi, [rax+DOUBLE_SIZE]
mov [rbx], rdi
add rbx,DOUBLE_SIZE
; Increment my square dx
fld qword [INITIAL_VERIFICATION_SQUARE_DXY]
fld qword [verificationInputs]
faddp
fstp qword [verificationInputs]
; Looping back
inc rdx
cmp rdx, 20
jne krasVerifySquareXYLOOP
; Set back my dx to zero
fldz
fld1
fsubp
fstp qword [verificationInputs]
xor rdx, rdx
; Increment my square dy
fld qword [INITIAL_VERIFICATION_SQUARE_DXY]
fld qword [verificationInputs+DOUBLE_SIZE]
faddp
fstp qword [verificationInputs+DOUBLE_SIZE]
; Looping back to dx=0
inc rcx
cmp rcx, 20
jne krasVerifySquareXYLOOP
ret
krasSaveVerificationSquare:
mov rax, 5
mov rbx, [krasExportFileName]
mov rcx, 65
mov rdx, 0o777
int 0x80
push rax
mov rbx, rax
mov rax, 4
mov rcx, [krasVerificationSquareData]
mov rdx, 6400
int 0x80
pop rbx
mov rax, 6
int 0x80
ret
;=============================;
;/\- Kras- File management -/\;
;=============================;
krasLoadWeights:
mov rax, 5
mov rbx, [krasExportFileName]
mov rcx, 0
mov rdx, 0o777
int 0x80
push rax
mov rbx, rax
mov rax, 3
mov rcx, [CnnWeightsMatrixPointer]
mov rdx, 616
int 0x80
pop rbx
mov rax, 6
int 0x80
ret
krasSaveLoss:
mov rax, 5
mov rbx, [krasExportFileName]
mov rcx, 65
mov rdx, 0o777
int 0x80
push rax
mov rbx, rax
mov rax, 4
mov rcx, lossHistory
mov rdx, 32000
int 0x80
pop rbx
mov rax, 6
int 0x80
ret
krasSaveWeights:
mov rax, 5
mov rbx, [krasExportFileName]
mov rcx, 65
mov rdx, 0o777
int 0x80
push rax
mov rbx, rax
mov rcx, [CnnWeightsMatrixPointer]
mov rdx, [CnnWeightsCount]
mov rax, DOUBLE_SIZE
mul rdx
mov rdx, rax
mov rax, 4
int 0x80
pop rbx
mov rax, 6
int 0x80
ret
;===========================;
;/\- Kras- Calling Macro -/\;
;===========================;
%macro addLayerParameters 4
mov rax, %1
mov rbx, %2
mov rdx, %3
mov rdi, %4
%endmacro
%macro fitParameters 3
mov rax, %1
mov rbx, %3
mov rcx, %2
mov qword [CnnStaticInput], TF_TRUE
%endmacro
%macro fitExtraParameters 1
mov qword [CnnStaticInput], %1
%endmacro
%macro saveParameters 1
mov qword [krasExportFileName], %1
%endmacro
%macro loadParameters 1
mov qword [krasExportFileName], %1
%endmacro
%macro sampleParameters 3
mov rax, %1
mov rbx, %2
mov rcx, %3
%endmacro