-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensorFlopCaput.asm
889 lines (701 loc) · 26.2 KB
/
tensorFlopCaput.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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
bits 64
%include "tensorFlopKernel.asm"
section .data
;===- TensorFlop text -===;
;============================;
presentationText db "==============================",10,"======--- TENSORFLOP ---======",10,"==============================",10,"==== First ever assembly ====",10,"=== made CNN engine (flop) ===",10,"==============================",10,10
PRESENTATION_TEXT_LENGTH equ $-presentationText
;===- Test Purpose -===;
;=========================;
userInputIdealArrays dq 1.0, 0.0, 0.0, 0.0, 0.0,0.0,0.0,0.0,0.0,0.0
floatTest dq 1.0,0.7, 0.5, 0.0, 0.0, 0.0
floatTest2 dq 2.0, -3.0, 2.5, 5.0, 4.0, 2.0
section .text
;============================;
;/\- General TensorFlop -/\;
;============================;
initialiseTensorFlop:
mov rcx, presentationText
mov rdx, PRESENTATION_TEXT_LENGTH
call print
ret
;=============================;
;/\- TensorFlop Primitives -/\;
;=============================;
createCnn:
; INIT THE WHOLE CNN ;
call initCNN
; CREATE THE CNN HANDLE ;
call createCnnHandle
mov [tensorflopHandles], qword rax ; store my handle
; ALLOCATE PARAMETERS MEMORY AND STORE THEM INTO THE HANDLE ;
call createCnnWeights
; FILL THE WEIGHTS WITH RANDOM VALUES [0,1] ;
call fillCnnParameters
; CREATE ACTIVED NEURON MATRIX ;
call createStandardMatrix
mov qword [CnnActivatedMatrixPointer], rax
; CREATE UNACTIVED NEURON MATRIX ;
call createStandardMatrix
mov qword [CnnUnactivatedMatrixPointer], rax
; CREATE COSTS NEURON MATRIX ;
call createStandardMatrix
mov qword [CnnCostMatrixPointer], rax
ret
predictCnn:
; STORE THE INPUT ;
call initInput
; COMPUTE A PROPAGATION
call computeCnn
ret
fitCnn:
mov qword r11, [CnnEpochs]
epochLoop:
push r11
call resetWeightGradients
call batchCnn
call applyGradient
pop r11
dec r11
cmp r11, 0
jnz epochLoop
ret
batchCnn:
; LOOP THE AMOUNT OF BATCHSIZE NEEDED ;
mov qword r11, [CnnBatchSize]
backpropagationLOOP:
push r11
call initInput
; Compute a single propagation
call computeCnn
call calculateGlobalLoss
; Compute the propagation
call backpropagateCNN
call shiftInputAndOutput
pop r11
dec r11
cmp r11, 0
jnz backpropagationLOOP
ret
;========================;
;/\- CNN Creation -/\;
;========================;
initCNN: ; Brainstorm
call getWeightsCount
call getNeuronsCount
call getDataInputAndOutputSizes
call getFirstLayerOffset
call getLastLayerOffset
call initIndices
call createBackpropagationMatrix
ret
initIndices: ; Set all the indices as 0
mov qword [idealsIndex], 0
mov qword [lossHistoryIndex], 0
ret
initInput: ; Load my input (experimentals for now)
mov rbx, [CnnDataInputSample]
mov qword rax, [CnnDataInputDim]
mov qword rcx, 0
mov qword r8, [CnnActivatedMatrixPointer]
mov qword r9, DOUBLE_SIZE
call insertListAtIndex
mov qword rax, [CnnDataInputDim]
mov qword rcx, 0
mov qword r8, [CnnUnactivatedMatrixPointer]
mov qword r9, DOUBLE_SIZE
call insertListAtIndex
ret
shiftInputAndOutput:
cmp qword [CnnStaticInput], TF_FALSE
jne shiftInputAndOutputStatic
;mov qword rbx, [CnnDataInputSize]
;add qword [CnnDataInputSample], rbx
;mov qword rbx, [CnnDataOutputSize]
;add qword [CnnDataOutputSample], rbx
shiftInputAndOutputStatic:
ret
createCnnHandle: ; Create a CnnHandle and fill it with the data available
mov rax, [CnnLayersCount]
mov qword [cafElements], rax ; number of layers
mov qword [cafSize], 19 ; one layer needs 19 bytes
call caf ; allocate it, so we have our CnnHandles
mov rbx, rax ; save the handle base location
xor rcx, rcx; set my for loop as 0
xor r8, r8
inc r8
fillHandleLOOP:
; Store layer index (1 byte)
mov byte [rax], cl
inc rax
; Store neurons count, (1 byte )
;mov dl, byte [CnnOBSOLETELayersOBSOLETENeurons+rcx]
;mov byte [rax], dl
inc rax
; Store activation function ID, (1 byte)
mov dl, byte [CnnActivationFunctions+rcx]
mov byte [rax], dl
inc rax
; Let weigths-pointer and bias (16 bytes) as 0
; But still increment to next_information
add rax, 16
inc rcx
inc r8
cmp rcx, [CnnLayersCount]
jne fillHandleLOOP
mov rax, rbx ; return location of handle
ret
createCnnWeights: ; Allocate the memory for the weights
;== Allocate weights ==;
mov rax, [CnnWeightsCount]
mov qword [cafElements], rax ; number of weights
mov qword [cafSize], DOUBLE_SIZE ; a weight is a db float -> 8 bytes
call caf
mov qword [CnnWeightsMatrixPointer], rax
ret
fillCnnParameters: ; Fill weights and biases with random values
mov qword r11, [CnnLayersCount]
dec r11
call getWorkingCnn
mov r10, rax
mov qword r12, [CnnWeightsMatrixPointer]
xor rbx, rbx
layerLOOP:
mov rax, rbx
call getLocalWeightsCount ; get the amount of weights in rax
mov r13, rax
weightLOOP:
mov qword [randomRange], 100
call generateRandomFloat
fld qword [randomFloatPointer]
fld qword [randomHalfPart]
fsubp
fld qword [randomOddPart]
fmulp
fstp qword [randomFloatPointer]
mov qword rax, [randomFloatPointer]
mov qword [r12], rax
add r12, DOUBLE_SIZE
dec r13
cmp r13, 0
jne weightLOOP
add rbx, DOUBLE_SIZE
dec r11
cmp r11, 0
jne layerLOOP
ret
createStandardMatrix: ; Create a memory empty space for my activated, unactivated and loss
mov qword rdx, [CnnNeuronsCount]
mov qword [cafElements], rdx ; number of neurons
mov qword [cafSize], DOUBLE_SIZE ; a neuron is a db float -> 8 bytes
call caf
ret
;===========================;
;/\- CNN Propagation -/\;
;===========================;
computeCnn: ; Compute a full propagation
; rax -> volatile
; rbx -> weight pointer (input)
; rcx -> unactivated neuron pointer (output)
; rdx -> activated neuron pointer (input)
; rdi -> activated neuron pointer (output)
; r8 -> layer offset { Let L, L+1 : L*L+1*Double_Size
; r10 -> handle index (used to get rbx)
; r11 -> the layer count and loop index
; Get the informations we need
; - first layer's neuron-count
; - a pointer to my weights
mov qword rbx, [CnnWeightsMatrixPointer]; store in rbx the weight pointer
; Convulational layer count
mov r11, [CnnLayersCount]
dec qword r11
; Get the activated input neuron
mov rdx, [CnnActivatedMatrixPointer]
; We'll need to increment the pointer without modifying my raw matricies
mov rcx, [CnnUnactivatedMatrixPointer]
mov rdi, [CnnActivatedMatrixPointer]
add rcx, [CnnFirstLayerOffset]
add rdi, [CnnFirstLayerOffset]
xor r10, r10
; For each layer, proceed to the dots products in each layer
computeCnnLOOP:
; Store the input layer size
mov rax, [CnnLayersSizes+r10]
mov qword [computeInputLayerSize], rax
; Store the output layer size
add r10, DOUBLE_SIZE
mov rax, [CnnLayersSizes+r10]
mov qword [computeOutputLayerSize], rax
mov qword [computeUnchangedOutputLayerSize], rax
; Store the output layer activation function
mov rax, [CnnActivationFunctions+r10]
mov qword [computeActivationFunction], rax
; Eventually compute the layer
call computeSingleLayer
; Looping stuff
dec r11
cmp r11, 0
jnz computeCnnLOOP
ret
computeSingleLayer: ; Compute the propagation in a single layer
push rax
; Calculate the offset of each set of weights
push rdx
mov rax, [computeInputLayerSize]
mov r9, DOUBLE_SIZE
mul r9
mov r8, rax
pop rdx
mov [computeTempUnactivatedPointer], rcx
computeSingleLayerLOOP:
; Compute the dot product
call computeDotProduct
mov rax, [computeOutputNeuronPointer]
mov [rcx], rax ; Save in unactivated (Zs) matrix
add rcx, DOUBLE_SIZE
add rbx, r8 ; add to my weights pointer, the input-layer's neuron-count times the size of an element
dec qword [computeOutputLayerSize]
cmp qword [computeOutputLayerSize],0
jnz computeSingleLayerLOOP
call computeBiases
call computeScales
call computeActivation
; Add to my neurons pointer, the input-layer's neuron-count times the size of an element
add rdx, r8
pop rax
ret
computeDotProduct: ; Dot product of two pointer, vector-size specified in rbx
push rbx
push rdx
mov qword rax, [computeInputLayerSize]
mov qword [computeOutputNeuronPointer],0 ; reset the output neuron
push rax
computeDotProductLOOP:
pxor xmm0, xmm0 ; reset upper part of the xmm0 in case it's a single dot product
pxor xmm1, xmm1 ; reset upper part of the xmm1 in case it's a single dot product
movq xmm0, [rbx] ; store first weight
movq xmm1, [rdx] ; store first input
cmp byte [computeInputLayerSize], 2
jl computeSingleDotProduct
movhpd xmm0, [rbx+8] ; store second weight
movhpd xmm1, [rdx+8] ; store second input
computeSingleDotProduct:
mulpd xmm1,xmm0 ; process the dot product : (W{L,I,K},W{L,I+1,K}) . (A{L,I},A(L,I+1))
; or : (W{L,I,K},0) . (A{L,I},0)
; and store (add) in A(L+1,K)
movlpd [computeTempOutputPointer], xmm1 ; get the lower part of the dot product
fld qword [computeTempOutputPointer] ; push it on the stack
movhpd [computeTempOutputPointer], xmm1 ; get the higher part of the dot product
fld qword [computeTempOutputPointer] ; push it on the stack
faddp ; add them together
fld qword [computeOutputNeuronPointer] ; Load the summation
faddp ; add result to neuron
fstp qword [computeOutputNeuronPointer] ; store result to neuron
add qword rbx, 16
add qword rdx, 16
sub qword [computeInputLayerSize], 2
cmp qword [computeInputLayerSize], 0
jg computeDotProductLOOP
pop rax
mov qword [computeInputLayerSize], rax
pop rdx
pop rbx
ret
computeActivation: ; Activate my neurons
push rbx
push rcx
push rdx
cmp qword [computeActivationFunction], TF_SOFTMAX
je wholeLayerActivation
;======================================================;
;=============== Per Neuron Activation ================:
computeActivationLoop:
; Call the activation function
mov rdx, [computeTempUnactivatedPointer]
mov rax, [rdx]
mov [computedFloatPointer], rax
call selectActivationFunction
mov qword rax, [computedFloatPointer]
mov [rdi], rax ; Save in activated (As) matrix
; Update my pointers
add rdi, DOUBLE_SIZE
add qword [computeTempUnactivatedPointer], DOUBLE_SIZE
cmp [computeTempUnactivatedPointer], rcx
jne computeActivationLoop
jmp computeActivationDone
wholeLayerActivation:
;======================================================;
;================ Per Layer Activation ================:
mov rcx, [computeTempUnactivatedPointer]
mov rdx, rdi
mov rbx, [computeUnchangedOutputLayerSize]
call softMax
mov rdx, [computeUnchangedOutputLayerSize]
mov rax, DOUBLE_SIZE
mul rdx
add rdi, rax
computeActivationDone:
pop rdx
pop rcx
pop rbx
ret
computeBiases:
ret
computeScales:
ret
;===========================;
;/\- CNN Backpropagation -/\;
;===========================;
createBackpropagationMatrix:
mov qword rdx, [CnnWeightsCount]
mov qword [cafElements], rdx ; number of weights
mov qword [cafSize], DOUBLE_SIZE ; a weight is a db float -> 8 bytes
call caf
mov qword [CnnBackpropagationWeightsMatrixPointer], rax
ret
backpropagateFirstLayer:
;Proceed to calculate all sigmas
; sigma(i) = Act^-1(Zi)*2(Ai-I)
; and so (0.1+Ai)*(1.1-Ai)*2*(Ai-Ii)
mov rax, [CnnLayersCount]
dec rax
mov rdx, DOUBLE_SIZE
mul rdx
mov rdx, [CnnActivationFunctions+rax]
mov qword [computeActivationFunction], rdx
xor rdx, rdx ; loop index
mov qword rdi, [CnnCostMatrixPointer]
mov rbx, [CnnDataOutputSample]
mov qword rax, [CnnLastLayerOffset]
add qword rax, [CnnActivatedMatrixPointer] ; go to last layer
mov qword rcx, [CnnLastLayerOffset]
xor rdx, rdx
backpropagateFirstLayerSigmasLOOP:
fcomp st0
; Ai-Ii
fld qword [rax]
fld qword [rbx]
fsubp
; 2(Ai-Ii)
fld st0
faddp
fstp qword [backpropTempFloatPointer]
call backpropagateActivation
; Ai*(1-Ai) * 2*(Ai-Ii)
fld qword [computedFloatPointer]
fld qword [backpropTempFloatPointer]
fmulp
fstp qword [rdi]
; LOOP STUFF
add rdi, DOUBLE_SIZE
add rbx, DOUBLE_SIZE
add rax, DOUBLE_SIZE
add rcx, DOUBLE_SIZE
inc rdx
cmp rdx, [CnnDataOutputDim]
jne backpropagateFirstLayerSigmasLOOP
mov rdi, [CnnCostMatrixPointer] ; get dC/dAi
mov rsi, [CnnDataOutputDim] ; get dC/dAi
mov rax, DOUBLE_SIZE
mul rsi
mov rsi, rax
add rsi, [CnnCostMatrixPointer]
mov rax, -2
call getNeuronsLayerByteOffset
mov r8, rax
add r8, [CnnActivatedMatrixPointer]
mov rax, -2
call getLayerSize
mov rdx, DOUBLE_SIZE
mul rdx
mov qword [backpropLayerBytesSize], rax
mov rax, -1
call getWeightsLayerByteOffset
mov rdx, rax
add rdx, [CnnWeightsMatrixPointer]
mov rax, [CnnBackpropagationWeightsMatrixPointer]
xor rbx, rbx ; neuron-local index
xor rcx, rcx ; layer-local index
backpropagateFirstLayerNeuronsLOOP:
fcomp st0
fld qword [r8+rbx]
fld qword [rdi]
fmulp
fld qword [rax] ;
faddp ;
fstp qword [rax]
fld qword [rdx]
fld qword [rdi]
fmulp
fld qword [rsi+rbx]
faddp
fstp qword [rsi+rbx]
add rax, DOUBLE_SIZE
add rdx, DOUBLE_SIZE
add rbx, DOUBLE_SIZE
cmp rbx, [backpropLayerBytesSize]
jne backpropagateFirstLayerNeuronsLOOP
; Layer-locality
xor rbx, rbx
add rdi, DOUBLE_SIZE
inc rcx
cmp rcx, [CnnDataOutputDim]
jne backpropagateFirstLayerNeuronsLOOP
ret
backpropagateSingleLayerPrepare:
; Get the layer activation functon
mov qword rax, [backpropLayerIndex]
mov rdx, DOUBLE_SIZE
mul rdx
add rax, CnnActivationFunctions
mov r8, [rax]
mov [computeActivationFunction], r8
; Get the layer size
mov rdx, qword [backpropLayerIndex]
mov rax, DOUBLE_SIZE
mul rdx
mov rbx, rax
mov rax, [CnnLayersSizes+rbx]
;Actual offset
mov qword [backpropLayerSize], rax
; Get the layer offset (unactivated)
mov rax, qword [backpropLayerIndex]
call getNeuronsLayerByteOffset
mov qword [backpropNeuronsLayerByteOffset], rax
; Get the reverse layer offset (cost)
mov rax, qword [backpropLayerIndex]
call getReverseNeuronsLayerByteOffset
mov qword [backpropReverseNeuronsLayerByteOffset], rax
; Get the reverse former layer offset (cost)
mov rax, qword [backpropFormerLayerIndex]
call getReverseNeuronsLayerByteOffset
mov qword [backpropReverseNeuronsFormerLayerByteOffset], rax
; Get the former layer size in bytes
mov rdx, qword [backpropFormerLayerIndex]
mov rax, DOUBLE_SIZE
mul rdx
mov rbx, rax
mov byte al, [CnnLayersSizes+rax]
;Actual offset
mov rdx, DOUBLE_SIZE
mul rdx
mov qword [backpropFormerLayerBytesSize], rax
ret
backpropagateSingleLayerInitialise:
; Get the former layer offset -> Ai-1,k'
mov qword rax, [backpropFormerLayerIndex]
call getNeuronsLayerByteOffset
add rax, [CnnActivatedMatrixPointer]
mov r8, rax
; Get the former weights offset -> Wi-1,k
mov rax, [backpropFormerLayerIndex]
call getWeightsLayerByteOffset
add rax, [CnnWeightsMatrixPointer]
mov rdx, rax
; Get the former layer costs offset -> store dC/Ai-1,k'
mov rdi, [CnnCostMatrixPointer]
add rdi, [backpropReverseNeuronsLayerByteOffset]
; Get the former layer costs offset -> store dC/Ai-1,k'
mov rsi, [CnnCostMatrixPointer]
add rsi, [backpropReverseNeuronsFormerLayerByteOffset]
; Get the weight gradient offset
mov rax, [backpropLayerIndex]
call getReverseWeightsLayerByteOffset
add rax, [CnnBackpropagationWeightsMatrixPointer]
xor rbx, rbx ; neuron-local index
xor rcx, rcx ; layer-local index
ret
backpropagateSingleLayerSigmas:
mov rbx, [CnnCostMatrixPointer] ; Get the {dC/dAi | H(Zi)*dC/dAi} matrix
add rbx, [backpropReverseNeuronsLayerByteOffset] ; Go to the right layer
mov rcx, [backpropNeuronsLayerByteOffset] ; Go to the right layer
xor rax, rax ; loop index -> stop at layer size
backpropagateSingleLayerSigmasLOOP:
fcomp st0 ; Make sure FPU stack is cleared
; Inverse activation function -> ReLU : Heaviside
; -> LeakyReLU : LeakyHeaviside
call backpropagateActivation
; Multiply by dC/dAi to get sigma H(Zi)*dC/dAi
fld qword [computedFloatPointer]
fld qword [rbx]
fmulp
; Store the sigma
fstp qword [rbx]
; looping stuff
add rbx, DOUBLE_SIZE
add rcx, DOUBLE_SIZE
inc rax
cmp rax, [backpropLayerSize]
jne backpropagateSingleLayerSigmasLOOP
ret
backpropagateSingleLayer:
; Get the layer and former layer sizes
call backpropagateSingleLayerPrepare
; Transform layer dC/dAi into H(Zi)*dC/dAi
call backpropagateSingleLayerSigmas
; Store all pointers in needed registers
call backpropagateSingleLayerInitialise
backpropagateSingleLayerNeuronsLOOP:
fcomp st0
fld qword [r8+rbx]
fld qword [rdi]
fmulp
fld qword [rax] ;
faddp ;
fstp qword [rax]
fld qword [rdx]
fld qword [rdi]
fmulp
fld qword [rsi+rbx]
faddp
fstp qword [rsi+rbx]
add rax, DOUBLE_SIZE
add rdx, DOUBLE_SIZE
add rbx, DOUBLE_SIZE
cmp rbx, [backpropFormerLayerBytesSize]
jne backpropagateSingleLayerNeuronsLOOP
; Layer-locality
xor rbx, rbx
add rdi, DOUBLE_SIZE
inc rcx
cmp rcx, [backpropLayerSize]
jne backpropagateSingleLayerNeuronsLOOP
ret
backpropagateCNN:
; Reset my neurons gradient
call resetNeuronsCosts
; Get the first layer to back propagate
mov qword rax, [CnnLayersCount]
mov qword [backpropLayerIndex], rax
sub qword [backpropLayerIndex], 2
mov qword [backpropFormerLayerIndex], rax
sub qword [backpropFormerLayerIndex],3
; Backpropagate first layer
call backpropagateFirstLayer
; Backpropagate other layers
backpropagateCNNLOOP:
call backpropagateSingleLayer
dec qword [backpropLayerIndex]
dec qword [backpropFormerLayerIndex]
cmp qword [backpropLayerIndex],0
jnz backpropagateCNNLOOP
ret
backpropagateActivation:
push rcx
push rdx
cmp qword [computeActivationFunction], TF_SOFTMAX
je backpropagateActivationUseActivated
;=======================================================================;
;=============== Derivative Activation With Unactivated ================:
add rcx, [CnnUnactivatedMatrixPointer] ; Get the Zi matrix
mov rdx, [rcx]
mov [computedFloatPointer], rdx
call selectDerivativeActivationFunction
jmp backpropagateActivationDone
backpropagateActivationUseActivated:
;=====================================================================;
;=============== Derivative Activation With Activated ================:
add rcx, [CnnActivatedMatrixPointer]
mov rdx, [rcx]
mov [computedFloatPointer], rdx
call DerivativeSoftmax
backpropagateActivationDone:
pop rdx
pop rcx
ret
resetNeuronsCosts:
mov rax, [CnnNeuronsCount]
mov rbx, [CnnCostMatrixPointer]
resetNeuronsCostsLOOP:
mov qword [rbx], 0
add rbx, DOUBLE_SIZE
dec rax
cmp rax, 0
jnz resetNeuronsCostsLOOP
ret
resetWeightGradients:
mov rax, [CnnWeightsCount]
mov rbx, [CnnBackpropagationWeightsMatrixPointer]
resetWeightGradientsLOOP:
mov qword [rbx], 0
add rbx, DOUBLE_SIZE
dec rax
cmp rax, 0
jnz resetWeightGradientsLOOP
ret
applyGradient:
mov rcx, [CnnBackpropagationWeightsMatrixPointer]; Weights gradient pointer
mov qword rsi, [CnnLayersCount]
dec rsi
xor rbx, rbx
applyGradientLayerLOOP:
mov rdi, [CnnWeightsMatrixPointer] ; Weights pointer
mov qword rax, -1
sub rax, rbx ; ask for layer -(rbx+1)
call getWeightsLayerByteOffset
add rdi, rax
; Get layer index and offset
mov qword r8, [CnnLayersCount]
sub r8, rbx
mov rax, DOUBLE_SIZE
mul r8
mov r8, rax
; Get layer size
sub r8, DOUBLE_SIZE
mov rdx, [CnnLayersSizes+r8]
; Get former layer size
sub r8, DOUBLE_SIZE
mov rax, [CnnLayersSizes+r8]
mul rdx
applyGradientWeightsLOOP:
fcomp st0
; dC/dW * learning rate
fld qword [CnnLearningRate]
fld qword [rcx]
fmulp
; W = W-dC/dW
fld qword [rdi]
faddp
fstp qword [rdi]
add rdi, DOUBLE_SIZE ; jump to next weight
add rcx, DOUBLE_SIZE ; jump to next weight gradient
dec rax
jnz applyGradientWeightsLOOP
inc rbx
cmp rbx, rsi
jne applyGradientLayerLOOP
ret
calculateGlobalLoss:
mov rcx, [CnnDataOutputDim] ; Last layer size
mov r8, 0
mov rax, [CnnNeuronsCount] ; Amount of Neurons
sub rax, rcx ; Get the index of last layer
call getOffsetFromIndex ; get the offset in bytes
add rax, [CnnActivatedMatrixPointer] ; go to last layer
xor rcx, rcx
cmp qword [lossHistoryIndex], MAX_SIZE_LOSS_HISTORY
jge lossHistoryOverflow
mov rcx, [lossHistoryIndex]
lossHistoryOverflow:
fldz
fstp qword [lossHistory+rcx] ; set my loss as 0
xor rdx, rdx
xor r8, r8
mov rbx, [CnnDataOutputSample]
calculateGlobalLossLOOP:
fcomp st0
fld qword [rax+rdx]
fld qword [rbx+rdx]
fsubp
fld st0
fmulp
fld qword [lossHistory+rcx]
faddp
fstp qword [lossHistory+rcx]
add rdx, DOUBLE_SIZE
inc r8
cmp r8, [CnnDataOutputDim]
jne calculateGlobalLossLOOP
add qword [lossHistoryIndex], DOUBLE_SIZE
mov rax, [lossHistory+rcx]
ret