-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmiscellaneous.jl
1731 lines (1653 loc) · 46.6 KB
/
miscellaneous.jl
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using LoopVectorization
using LinearAlgebra
using OffsetArrays
using Test
if !isdefined(@__MODULE__, Symbol("@_avx"))
include("testsetup.jl")
end
@testset "Miscellaneous" begin
# T = Float32
# Unum, Tnum = LoopVectorization.register_count() == 16 ? (1, 6) : (1, 8)
dot3q = :(
for m ∈ 1:M, n ∈ 1:N
s += x[m] * A[m, n] * y[n]
end
)
lsdot3 = LoopVectorization.loopset(dot3q)
if LoopVectorization.register_count() ≠ 32
# @test LoopVectorization.choose_order(lsdot3) == ([:n, :m], :n, :m, :m, 1, 6)
@test LoopVectorization.choose_order(lsdot3) == ([:n, :m], :m, :n, :m, 1, 6)
elseif Bool(LoopVectorization.has_opmask_registers())
# @test LoopVectorization.choose_order(lsdot3) == ([:n, :m], :n, Symbol("##undefined##"), :m, 4, -1)
@test LoopVectorization.choose_order(lsdot3) == ([:n, :m], :m, :n, :m, 1, 8)
else
@test LoopVectorization.choose_order(lsdot3) == ([:n, :m], :m, :n, :m, 1, 8)
# @test LoopVectorization.choose_order(lsdot3) == ([:n, :m], :n, :m, :m, 1, 8)
end
@static if VERSION < v"1.4"
dot3(x, A, y) = dot(x, A * y)
else
dot3(x, A, y) = dot(x, A, y)
end
function dot3avx(x, A, y)
s = zero(promote_type(eltype(x), eltype(A), eltype(y)))
@turbo for m ∈ axes(A, 1), n ∈ axes(A, 2)
s += x[m] * A[m, n] * y[n]
end
s
end
function dot3v2avx(x, A, y)
s = zero(promote_type(eltype(x), eltype(A), eltype(y)))
@turbo for n ∈ axes(A, 2)
t = zero(s)
for m ∈ axes(A, 1)
t += x[m] * A[m, n]
end
s += t * y[n]
end
s
end
q = :(
for n ∈ 1:N
t = zero(s)
for m ∈ 1:M
t += x[m] * A[m, n]
end
s += t * y[n]
end
)
ls = LoopVectorization.loopset(q)
function dot3avx24(x, A, y)
M, N = size(A)
s = zero(promote_type(eltype(x), eltype(A), eltype(y)))
@turbo unroll = (2, 4) for m ∈ 1:M, n ∈ 1:N
s += x[m] * A[m, n] * y[n]
end
s
end
function dot3_avx(x, A, y)
M, N = size(A)
s = zero(promote_type(eltype(x), eltype(A), eltype(y)))
@_avx for m ∈ 1:M, n ∈ 1:N
s += x[m] * A[m, n] * y[n]
end
s
end
subcolq = :(
for i ∈ 1:size(A, 2), j ∈ eachindex(x)
B[j, i] = A[j, i] - x[j]
end
)
lssubcol = LoopVectorization.loopset(subcolq)
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:i,:j], :i, Symbol("##undefined##"), :j, 1, -1)
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:i,:j], :j, :i, :j, 1, 8)
@test LoopVectorization.choose_order(lssubcol) == (
Symbol[:i, :j],
:j,
:i,
:j,
1,
ifelse((LoopVectorization.register_count() == 32), 8, 6),
)
# if LoopVectorization.register_count() != 8
# # @test LoopVectorization.choose_order(lssubcol) == (Symbol[:j,:i], :j, :i, :j, Unum, Tnum)
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:j,:i], :j, :i, :j, 1, 1)
# end
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:i,:j], :i, Symbol("##undefined##"), :j, 1, -1)
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:j,:i], :i, Symbol("##undefined##"), :j, 4, -1)
# if LoopVectorization.register_count() == 32
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:i,:j], :j, :i, :j, 2, 10)
# elseif LoopVectorization.register_count() == 16
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:i,:j], :j, :i, :j, 2, 6)
# end
# @test LoopVectorization.choose_order(lssubcol) == (Symbol[:j,:i], :j, Symbol("##undefined##"), :j, 4, -1)
## @turbo is SLOWER!!!!
## need to fix!
function mysubcol!(B, A, x)
@inbounds for i ∈ 1:size(A, 2)
@simd for j ∈ eachindex(x)
B[j, i] = A[j, i] - x[j]
end
end
end
function mysubcolavx!(B, A, x)
@turbo for i ∈ 1:size(A, 2), j ∈ eachindex(x)
B[j, i] = A[j, i] - x[j]
end
end
function mysubcol_avx!(B, A, x)
@_avx for i ∈ 1:size(A, 2), j ∈ eachindex(x)
B[j, i] = A[j, i] - x[j]
end
end
colsumq = :(
for i ∈ 1:size(A, 2), j ∈ eachindex(x)
x[j] += A[j, i] - 1 / 4
end
)
lscolsum = LoopVectorization.loopset(colsumq)
# if LoopVectorization.register_count() != 8
# # @test LoopVectorization.choose_order(lscolsum) == (Symbol[:j,:i], :j, :i, :j, Unum, Tnum)
# @test LoopVectorization.choose_order(lscolsum) == (Symbol[:j,:i], :j, :i, :j, 1, 1)
# end
if Sys.ARCH === :aarch64
@test LoopVectorization.choose_order(lscolsum) ==
(Symbol[:j, :i], :j, Symbol("##undefined##"), :j, 8, -1)
else
@test LoopVectorization.choose_order(lscolsum) ==
(Symbol[:j, :i], :j, Symbol("##undefined##"), :j, 4, -1)
end
# my colsum is wrong (by 0.25), but slightly more interesting
function mycolsum!(x, A)
@. x = 0
@inbounds for i ∈ 1:size(A, 2)
@simd for j ∈ eachindex(x)
x[j] += A[j, i] - 0.25
end
end
end
function mycolsumavx!(x, A)
@turbo for j ∈ eachindex(x)
xⱼ = zero(eltype(x))
for i ∈ 1:size(A, 2)
xⱼ += A[j, i] - 0.25
end
x[j] = xⱼ
end
end
function mycolsum_avx!(x, A)
@_avx for j ∈ eachindex(x)
xⱼ = zero(eltype(x))
for i ∈ 1:size(A, 2)
xⱼ += A[j, i] - 1 / 4
end
x[j] = xⱼ
end
end
varq = :(
for j ∈ eachindex(s²), i ∈ 1:size(A, 2)
δ = A[j, i] - x̄[j]
s²[j] += δ * δ
end
)
lsvar = LoopVectorization.loopset(varq)
# LoopVectorization.choose_order(lsvar)
# @test LoopVectorization.choose_order(lsvar) == (Symbol[:j,:i], :j, :i, :j, Unum, Tnum)
# if LoopVectorization.register_count() == 32
if Sys.ARCH === :aarch64
@test LoopVectorization.choose_order(lsvar) ==
(Symbol[:j, :i], :j, Symbol("##undefined##"), :j, 8, -1)
else
@test LoopVectorization.choose_order(lsvar) ==
(Symbol[:j, :i], :j, Symbol("##undefined##"), :j, 4, -1)
end
# @test LoopVectorization.choose_order(lsvar) == (Symbol[:j,:i], :j, :i, :j, 2, 10)
# else#if LoopVectorization.register_count() == 16
# @test LoopVectorization.choose_order(lsvar) == (Symbol[:j,:i], :j, Symbol("##undefined##"), :j, 8, -1)
# @test LoopVectorization.choose_order(lsvar) == (Symbol[:j,:i], :j, :i, :j, 2, 6)
# end
function piestsimd(rounds)
pi = 1.0
@simd for i = 2:(rounds+2)
x = (-1)^iseven(i)
pi += x / (2 * i - 1)
end
return pi * 4
end
function piestturbo(rounds)
pi = 1.0
@turbo for i = 2:(rounds+2)
x = (-1)^iseven(i)
pi += x / (2 * i - 1)
end
return pi * 4
end
@test piestturbo(4096) ≈ π rtol = 1e-4
@test piestturbo(2000) ≈ piestsimd(2000)
function myvar!(s², A, x̄)
@. s² = 0
@inbounds for i ∈ 1:size(A, 2)
@simd for j ∈ eachindex(s²)
δ = A[j, i] - x̄[j]
s²[j] += δ * δ
end
end
end
function myvaravx!(s², A, x̄)
@turbo for j ∈ eachindex(s²)
s²ⱼ = zero(eltype(s²))
x̄ⱼ = x̄[j]
for i ∈ 1:size(A, 2)
δ = A[j, i] - x̄ⱼ
s²ⱼ += δ * δ
end
s²[j] = s²ⱼ
end
end
function myvar_avx!(s², A, x̄)
@_avx for j ∈ eachindex(s²)
s²ⱼ = zero(eltype(s²))
x̄ⱼ = x̄[j]
for i ∈ 1:size(A, 2)
δ = A[j, i] - x̄ⱼ
s²ⱼ += δ * δ
end
s²[j] = s²ⱼ
end
end
function setcolumstovectorplus100!(Z::AbstractArray{T}, A) where {T}
for i in axes(A, 1), j in axes(Z, 2)
acc = zero(T)
acc = acc + A[i] + 100
Z[i, j] = acc
end
end
function setcolumstovectorplus100avx!(Z::AbstractArray{T}, A) where {T}
@turbo for i in axes(A, 1), j in axes(Z, 2)
acc = zero(T)
acc = acc + A[i] + (26 + 74)
Z[i, j] = acc
end
end
function mvp(P, basis, coeffs::Vector{T}) where {T}
len_c = length(coeffs)
len_P = size(P, 1)
p = zero(T)
for n = 1:len_c
pn = coeffs[n]
for a = 1:len_P
pn *= P[a, basis[a, n]]
end
p += pn
end
return p
end
function mvpavx(P, basis, coeffs::Vector{T}) where {T}
C = length(coeffs)
A = size(P, 1)
p = zero(T)
@turbo for c ∈ 1:C
pc = coeffs[c]
for a = 1:A
pc *= P[a, basis[a, c]]
end
p += pc
end
return p
end
function mvp_avx(P, basis, coeffs::Vector{T}) where {T}
len_c = length(coeffs)
len_P = size(P, 1)
p = zero(T)
@_avx for n = 1:len_c
pn = coeffs[n]
for a = 1:len_P
pn *= P[a, basis[a, n]]
end
p += pn
end
return p
end
bq = :(
for n = 1:len_c
pn = coeffs[n]
for a = 1:len_P
pn *= P[a, basis[a, n]]
end
p += pn
end
)
lsb = LoopVectorization.loopset(bq)
function threemulaccum_lv(A, B, C)
D = zero(promote_type(eltype(A), eltype(B), eltype(C)))
@turbo for i in axes(C, 1), j in axes(C, 2), k in axes(C, 3)
D += A[i, j] * B[i, k] * C[i, j, k]
end
D
end
function threemulaccum_base(A, B, C)
D = zero(promote_type(eltype(A), eltype(B), eltype(C)))
@inbounds @fastmath for i in axes(C, 1), j in axes(C, 2), k in axes(C, 3)
D += A[i, j] * B[i, k] * C[i, j, k]
end
D
end
function clenshaw!(ret, x, coeff)
@inbounds for j = 1:length(ret)
ret[j] = clenshaw(x[j], coeff)
end
end
function clenshaw_avx!(ret, x, coeff)
@_avx for j = 1:length(ret)
ret[j] = clenshaw(x[j], coeff)
end
end
function clenshawavx!(ret, x, coeff)
@turbo for j = 1:length(ret)
ret[j] = clenshaw(x[j], coeff)
end
end
# ret = y2; coeff = c;
# LoopVectorization.@turbo_debug for j in 1:length(ret)
# ret[j] = clenshaw(x[j], coeff)
# end
# t = β₁ = β₂ = ρ = s = 0.0; weights = rand(1); nodes = rand(1); lomnibus(args...) = +(args...)
# LoopVectorization.@turbo_debug for i ∈ eachindex(weights, nodes)
# s += weights[i] * lomnibus(nodes[i], t, β₁, β₂, ρ)
# end
# @macroexpand @turbo for i ∈ eachindex(weights, nodes)
# s += weights[i] * lomnibus(nodes[i], t, β₁, β₂, ρ)
# end
function softmax3_core!(lse, qq, xx, tmpmax, maxk, nk)
for k in Base.OneTo(maxk)
@inbounds for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
qq[i, k] = tmp
end
end
for k = maxk+1:nk
@inbounds for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_coreavx1!(lse, qq, xx, tmpmax, maxk, nk)
for k in Base.OneTo(maxk)
@turbo for i in eachindex(lse)
tmp = (tmpm -> exp(xx[i, k] - tmpm))(tmpmax[i])
lse[i] += tmp
qq[i, k] = tmp
end
end
for k = maxk+1:nk
@turbo for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_core_avx1!(lse, qq, xx, tmpmax, maxk, nk)
for k in Base.OneTo(maxk)
@_avx for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
qq[i, k] = tmp
end
end
for k = maxk+1:nk
@_avx for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_coreavx2!(lse, qq, xx, tmpmax, maxk, nk)
@turbo for k in Base.OneTo(maxk)
for i in eachindex(lse)
tmp = (yy -> exp(yy[i, k] - tmpmax[i]))(xx)
lse[i] += tmp
qq[i, k] = tmp
end
end
if maxk < nk
@turbo for k = maxk+1:nk
for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
end
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_core_avx2!(lse, qq, xx, tmpmax, maxk, nk)
@_avx for k in Base.OneTo(maxk)
for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
qq[i, k] = tmp
end
end
if maxk < nk
@_avx for k = maxk+1:nk
for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
end
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_coreavx3!(lse, qq, xx, tmpmax, maxk, nk)
for k in Base.OneTo(nk)
@turbo for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
k <= maxk && (qq[i, k] = tmp)
end
end
@turbo qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_core_avx3!(lse, qq, xx, tmpmax, maxk, nk)
for k in Base.OneTo(nk)
@_avx for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
k <= maxk && (qq[i, k] = tmp)
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
# qif = :(for i in eachindex(lse)
# tmp = exp(xx[i,k] - tmpmax[i])
# lse[i] += tmp
# k <= maxk && (qq[i,k] = tmp)
# end)
# lsif = LoopVectorization.loopset(qif)
function softmax3_coreavx4!(lse, qq, xx, tmpmax, maxk, nk)
@turbo for k in Base.OneTo(nk)
for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
k <= maxk && (qq[i, k] = tmp)
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
function softmax3_core_avx4!(lse, qq, xx, tmpmax, maxk, nk)
@_avx for k in Base.OneTo(nk)
for i in eachindex(lse)
tmp = exp(xx[i, k] - tmpmax[i])
lse[i] += tmp
k <= maxk && (qq[i, k] = tmp)
end
end
qq[:, Base.OneTo(maxk)] ./= vec(lse)
end
add_1_dim(x::AbstractArray) = reshape(x, size(x)..., 1)
check_finite(x::AbstractArray) = all(isfinite.(x)) || throw(error("x not finite!"))
function softmax3_setup!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
ndims(q) == 1 + ndims(lse) || throw(DimensionMismatch())
xsizes = size(x)
xsizes == size(q) ||
throw(DimensionMismatch("size(x) = $(size(x)) but size(q) = $(size(q))"))
nk = last(xsizes)
for i in Base.OneTo(ndims(lse))
size(q, i) == size(lse, i) == size(tmpmax, i) || throw(
DimensionMismatch(
"size(x) = $(size(x)), size(lse) = $(size(lse)), and size(tmpmax) = $(size(tmpmax))",
),
)
end
0 < maxk <= nk || throw(DomainError(maxk))
1 == LinearAlgebra.stride1(q) == LinearAlgebra.stride1(x) ||
throw(error("Arrays not strided"))
isempty(x) && throw(error("x empty"))
check_finite(x)
maximum!(add_1_dim(tmpmax), x)
fill!(lse, zero(T))
xx = reshape(x, :, nk)
qq = reshape(q, :, nk)
lse, qq, xx, tmpmax, maxk, nk
end
function softmax3_base!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_core!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3avx1!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_coreavx1!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3_avx1!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_core_avx1!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3avx2!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_coreavx2!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3_avx2!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_core_avx2!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3avx3!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_coreavx3!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3_avx3!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_core_avx3!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3avx4!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_coreavx4!(lse, qq, xx, tmpmax, maxk, nk)
end
function softmax3_avx4!(
q::AA,
lse::A,
tmpmax::A,
x::AA,
maxk = size(q, ndims(q)),
) where {T<:Real,A<:Array{T},AA<:AbstractArray{T}}
lse, qq, xx, tmpmax, maxk, nk = softmax3_setup!(q, lse, tmpmax, x, maxk)
softmax3_core_avx4!(lse, qq, xx, tmpmax, maxk, nk)
end
function sumprodavx(x)
s = zero(eltype(x))
p = one(eltype(x))
@turbo for i ∈ eachindex(x)
s += x[i]
p *= x[i]
end
s, p
end
function sumprod_avx(x)
s = zero(eltype(x))
p = one(eltype(x))
@_avx for i ∈ eachindex(x)
s += x[i]
p *= x[i]
end
s, p
end
function test_bit_shift(counter)
accu = zero(first(counter))
@inbounds for i ∈ eachindex(counter)
accu += counter[i] << 1
end
accu
end
function test_bit_shiftavx(counter)
accu = zero(first(counter))
@turbo for i ∈ eachindex(counter)
accu += counter[i] << (-3 * 4 + 13)
end
accu
end
function test_bit_shift_avx(counter)
accu = zero(first(counter))
@_avx for i ∈ eachindex(counter)
accu += counter[i] << 1
end
accu
end
function test_for_with_different_index!(c, a, b, start_sample, num_samples)
@inbounds for i = start_sample:num_samples+start_sample-1
c[i] = b[i] * a[i]
end
end
function test_for_with_different_indexavx!(c, a, b, start_sample, num_samples)
@turbo for i = start_sample:num_samples+start_sample-1
aᵢ = a[i]
c[i] = ((x, y) -> x * y)(b[i], aᵢ)
end
end
function test_for_with_different_index_avx!(c, a, b, start_sample, num_samples)
@_avx for i = start_sample:num_samples+start_sample-1
c[i] = b[i] * a[i]
end
end
function rshift_i!(out)
n = length(out)
@inbounds for i = 1:n
out[i] = out[i] << i
end
end
function rshift_i_avx!(out)
n = length(out)
@turbo for i = 1:n
out[i] = out[i] << i
end
end
function one_plus_i!(out)
n = length(out)
@inbounds for i = 1:n
out[i] = 1 + i
end
end
function one_plus_i_avx!(out)
n = length(out)
@turbo for i = 1:n
out[i] = 1 + i
end
end
function addsumtoeach!(y, z)
@inbounds @fastmath for i in axes(z, 1)
@simd ivdep for j in axes(y, 1)
y[j] = y[j] + z[i]
end
end
end
function addsumtoeachavx!(y, z)
@turbo for i in axes(z, 1)
for j in axes(y, 1)
y[j] = y[j] + z[i]
end
end
end
function crossedsumavx!(x, y, z)
@turbo for i in axes(x, 1)
for j in axes(x, 2)
x[i, j] = x[i, j] + z[i]
y[j, i] = y[j, i] + z[i]
end
end
end
function crossedsum!(x, y, z)
@inbounds @fastmath for i in axes(x, 1)
for j in axes(x, 2)
x[i, j] = x[i, j] + z[i]
y[j, i] = y[j, i] + z[i]
end
end
end
# should be:
# 3.0 4.0 1.0 2.0 7.0 8.0 5.0 6.0 11.0 12.0 9.0 10.0 15.0 16.0 13.0 … 191.0 192.0 189.0 190.0 195.0 196.0 193.0 194.0 197.0 198.0 199.0
# 1.0 4.0 5.0 2.0 3.0 8.0 9.0 6.0 7.0 12.0 13.0 10.0 11.0 16.0 17.0 … 187.0 192.0 193.0 190.0 191.0 196.0 197.0 194.0 195.0 198.0 199.0
function instruct_x_avx!(r::AbstractVector, loc::Int)
@turbo for lhs = 0:(length(r)>>1)-(1<<(loc-1))
# mask locations before
p = lhs + lhs & ~(1 << (loc - 1) - 1)
q = lhs + lhs & ~(1 << (loc - 1) - 1) + 1 << (loc - 1)
# swap rows
tmp = r[p+1]
r[p+1] = r[q+1]
r[q+1] = tmp
end
return r
end
function instruct_x!(r::AbstractVector, loc::Int)
for lhs = 0:(length(r)>>1)-(1<<(loc-1))
# mask locations before
p = lhs + lhs & ~(1 << (loc - 1) - 1)
q = lhs + lhs & ~(1 << (loc - 1) - 1) + 1 << (loc - 1)
# swap rows
tmp = r[p+1]
r[p+1] = r[q+1]
r[q+1] = tmp
end
return r
end
function multiple_unrolls_split_depchains!(
c_re::AbstractArray{T},
a_re,
b_re,
a_im,
b_im,
keep = nothing,
) where {T}
for k = 1:2
for n = 1:2
# acc = ifelse(keep === nothing, zero(T), c_re[k, n]) # same problem
acc = keep === nothing ? zero(T) : c_re[k, n]
# acc = zero(T) # this works fine
for c = 1:2
acc = acc + (a_re[k, n, c] * b_re[c, k] + a_im[k, n, c] * b_im[c, k])
end
c_re[k, n] = acc
end
end
c_re
end
function multiple_unrolls_split_depchains_avx!(
c_re::AbstractArray{T},
a_re,
b_re,
a_im,
b_im,
keep = nothing,
) where {T}
@turbo for k = 1:2
for n = 1:2
# acc = ifelse(keep === nothing, zero(T), c_re[k, n]) # same problem
acc = keep === nothing ? zero(T) : c_re[k, n]
# acc = zero(T) # this works fine
for c = 1:2
acc = acc + (a_re[k, n, c] * b_re[c, k] + a_im[k, n, c] * b_im[c, k])
end
c_re[k, n] = acc
end
end
c_re
end
function MatCalcWtDW!(m)
l, n = size(m.Wt)
fill!(m.Wt_D_W, 0)
@turbo for k = 1:n
for j = 1:l
for i = 1:l
m.Wt_D_W[i, j] += m.Wt[i, k] * m.Wt[j, k] * m.d[k]
end
end
end
end
function loopinductvardivision(τ)
M, N = size(τ)
for t = 1:N, j = 1:M
τ[j, t] = ((j - 1) / (M - 1))
end
τ
end
function loopinductvardivisionavx(τ)
M, N = size(τ)
@turbo for t = 1:N, j = 1:M
τ[j, t] = ((j - 1) / (M - 1))
end
τ
end
function maxavx!(R::AbstractArray{T}, Q, keep = nothing) where {T}
@turbo for i in axes(Q, 1)
# acc = -999 # works fine
acc = ifelse(isnothing(keep), typemin(T), R[i])
for j in axes(Q, 2), k in axes(Q, 3)
acc = max(acc, Q[i, j, k])
end
R[i] = acc
end
R
end
function reductionorder(E1, n)
t = 0.5
a = 1.0
_s = 0.0
k = length(E1)
@turbo for j = 1:k
for i = 1:n
v = a * (1 - t * t)
_s += v
end
E1[j] = _s / n
end
E1
end
function splitintonoloop(U, E1)
t = 0.5
a = 1.0
_s = 0.0
n, k = size(U)
@turbo for j = 1:k
for i = 1:n
u = tanh(a * U[i, j])
v = a * (1 - t * t)
U[i, j] = u
_s += v
end
E1[j] = _s / n
end
U, E1
end
function splitintonoloop_reference(U, E1)
t = 0.5
a = 1.0
_s = 0.0
n, k = size(U)
for j = 1:k
for i = 1:n
u = tanh(a * U[i, j])
v = a * (1 - t * t)
U[i, j] = u
_s += v
end
E1[j] = _s / n
end
U, E1
end
function findreducedparentfornonvecstoreavx!(
U::AbstractMatrix{T},
E1::AbstractVector{T},
) where {T}
n, k = size(U)
_s = zero(T)
a = 1.0
@turbo for j = 1:k
for i = 1:n
t = tanh(a * U[i, j])
U[i, j] = t
_s += a * (1 - t^2)
end
E1[j] = (x -> x / n)(_s)
end
U, E1
end
function findreducedparentfornonvecstore!(
U::AbstractMatrix{T},
E1::AbstractVector{T},
) where {T}
n, k = size(U)
_s = zero(T)
a = 1.0
for j = 1:k
for i = 1:n
t = tanh(a * U[i, j])
U[i, j] = t
_s += a * (1 - t^2)
end
E1[j] = _s / n
end
U, E1
end
function powcseliteral!(x)
@turbo for i ∈ eachindex(x)
x[i] = 3^4
end
x
end
function powcsesymbol!(x, a = 3)
@turbo for i ∈ eachindex(x)
x[i] = a^4
end
x
end
function powfastmath!(x::Vector{T}) where {T}
@turbo for i = 1:length(x)
xv = x[i]
@fastmath x[i] = 1 / (xv^2)
end
x
end
@inline ninereturns(x) = (0.25x, 0.5x, 0.75, 1.0x, 1.25x, 1.5x, 1.75x, 2.0x, 2.25x)
function manyreturntest(x)
s = zero(eltype(x))
@fastmath for j ∈ eachindex(x)
a, b, c, d, e, f, g, h, i = ninereturns(x[j])
s += a * i + b * h + c * g - d
end
s
end
function manyreturntestavx(x)
s = zero(eltype(x))
@turbo for j ∈ eachindex(x)
a, b, c, d, e, f, g, h, i = ninereturns(x[j])
s += a * i + b * h + c * g - d
end
s
end
function maybe_const_issue144!(𝛥mat, 𝛥ℛ, mat, ℛ)
𝛥ℛ_value = 𝛥ℛ.value
for j in axes(mat, 2)
for i in axes(mat, 1)
ℰ𝓍1 = conj(𝛥ℛ_value) # could be outside both loops
ℰ𝓍2 = -(ℛ[j]) # could be outside i loop
ℰ𝓍3 = exp(ℰ𝓍2) # could be outside i loop
ℰ𝓍4 = exp(mat[i, j])
ℰ𝓍5 = ℰ𝓍3 * ℰ𝓍4
ℰ𝓍6 = ℰ𝓍1 * ℰ𝓍5
ℰ𝓍7 = conj(ℰ𝓍6)
𝛥mat[i, j] = 𝛥mat[i, j] + ℰ𝓍7
end
end
𝛥mat
end
function maybe_const_issue144_avx!(𝛥mat, 𝛥ℛ, mat, ℛ)
𝛥ℛ_value = 𝛥ℛ.value
@turbo for j in axes(mat, 2)
for i in axes(mat, 1)
ℰ𝓍1 = conj(𝛥ℛ_value)
ℰ𝓍2 = -(ℛ[j])
ℰ𝓍3 = exp(ℰ𝓍2)
ℰ𝓍4 = exp(mat[i, j])
ℰ𝓍5 = ℰ𝓍3 * ℰ𝓍4
ℰ𝓍6 = ℰ𝓍1 * ℰ𝓍5
ℰ𝓍7 = conj(ℰ𝓍6)
𝛥mat[i, j] = 𝛥mat[i, j] + ℰ𝓍7
end
end
𝛥mat
end
function grad!(𝛥x, 𝛥ℛ, x, 𝒶𝓍i = eachindex(x))
for i in 𝒶𝓍i
(i >= first(axes(𝛥x, 1))) & (i <= last(axes(𝛥x, 1))) && (𝛥x[i] = 𝛥x[i] + 𝛥ℛ[i])
end
𝛥x
end
function grad_avx!(𝛥x, 𝛥ℛ, x, 𝒶𝓍i = eachindex(x))
@turbo for i in 𝒶𝓍i
(i >= first(axes(𝛥x, 1))) & (i <= last(axes(𝛥x, 1))) && (𝛥x[i] = 𝛥x[i] + 𝛥ℛ[i])
end
𝛥x
end
function grad_avx_base!(𝛥x, 𝛥ℛ, x, 𝒶𝓍i = eachindex(x))
@turbo for i in 𝒶𝓍i
(i >= first(axes(𝛥x, 1))) & (i <= Base.last(axes(𝛥x, 1))) && (𝛥x[i] = 𝛥x[i] + 𝛥ℛ[i])
end
𝛥x
end
@eval function grad_avx_eval!(𝛥x, 𝛥ℛ, x, 𝒶𝓍i = eachindex(x))
@turbo for i in 𝒶𝓍i
(i >= $first($axes(𝛥x, 1))) & (i <= $last($axes(𝛥x, 1))) && (𝛥x[i] = 𝛥x[i] + 𝛥ℛ[i])
end
𝛥x