Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    compute.cpp
 
Abstract:
 
    This module implements miscellaneous computation routines.
 
    Our usage requires building platform specific versions of the algorithm to
    target different instruction sets. The implementation below targets the
    base instruction set (typically SSE2) while assembly implementations target
    newer instruction sets (such as FMA3).
 
--*/
 
#include "mlasi.h"
#include "elementwise_constants.h"
#include "softmax.h"
 
//
// Bundles the constants for use by kernels written in assembly.
//
 
MLAS_INTERNAL_DATA const MLAS_EXP_CONSTANTS MlasExpConstants = {
    -103.9720840454f,
    88.7762626647950f,
    -88.3762626647949f,
    88.3762626647949f,
    MLAS_ROUNDING_BIAS_MAGIC,
    1.44269504088896341f,
    -6.93145752e-1f,
    -1.42860677e-6f,
    0x1.694000p-10,
    0x1.125edcp-7,
    0x1.555b5ap-5,
    0x1.555450p-3,
    0x1.fffff6p-2,
    0x1.000000p+0,
    int32_t(0xC1000000),
    int32_t(0x3F800000),
};
 
MLAS_INTERNAL_DATA const float MlasMinimumF32Value = std::numeric_limits<float>::lowest();
 
//
// Define the parameters to execute segments of a softmax operation on worker
// threads.
//
 
template <typename T>
struct MLAS_SOFTMAX_WORK_BLOCK {
    ptrdiff_t ThreadCountN;
    bool LogSoftmax;
    bool SmoothSoftmax;
    float Sink;
    const T* Input;
    T* Output;
    size_t N;
    size_t D;
};
 
MLAS_FORCEINLINE
MLAS_FLOAT32X4
MlasComputeExpVector(
    MLAS_FLOAT32X4 Vector
)
/*++
 
Routine Description:
 
    This routine computes the exponential function for the supplied vector.
 
    This merges ideas from multiple vectorized expf() implementations:
 
        1.  The original polynomials of expf() are extracted from MlasComputeErf, which
            was based on an answer to the following Stack Overflow post:
 
            https://stackoverflow.com/questions/35148198/efficient-faithfully-rounded-implementation-of-error-function-erff
 
        2.  The author of the answer further refined the polynomials at:
 
            https://forums.developer.nvidia.com/t/a-more-accurate-performance-competitive-implementation-of-expf/47528/5
 
            Using these polynomials yields even closer results to the Microsoft
            UCRT version of std::expf() than the values from the above post.
 
        3.  XNNPACK has a further useful refinement to extend the effective
            range of results from [-88.376, 88.376] to [-103.972, 88.776] by
            splitting the step of exponent reconstruction into two pieces. This
            yields results similar to an AVX512 implementation using VSCALEFPS.
 
Arguments:
 
    Vector - Supplies the values to operate on.
 
Return Value:
 
    Returns the exponential function of the input.
 
--*/
{
    Vector = MlasClampFloat32x4(Vector, MlasExpConstants.LowerRange, MlasExpConstants.UpperRange);
 
    //
    // Range reduction of the input by computing "(2 ^ m) * exp(reduced)".
    //
 
    const auto RoundingBias = MlasBroadcastFloat32x4(MlasExpConstants.RoundingBias);
 
    auto biased = MlasMultiplyAddFloat32x4(Vector, MlasExpConstants.Log2Reciprocal, RoundingBias);
    auto m = MlasSubtractFloat32x4(biased, RoundingBias);
 
    Vector = MlasMultiplyAddFloat32x4(m, MlasExpConstants.Log2High, Vector);
    Vector = MlasMultiplyAddFloat32x4(m, MlasExpConstants.Log2Low, Vector);
 
    //
    // Compute the scaling factors used to reconstruct the "(2 ^ m)" value
    // from above. To cover the entire single precision floating point range,
    // two scaling factors are needed to handle exponents [-150, 128].
    //
 
    const auto MinimumExponent = MlasBroadcastInt32x4(MlasExpConstants.MinimumExponent);
    const auto MaximumExponent = MlasBroadcastInt32x4(MlasExpConstants.MaximumExponent);
 
    auto overflow = MlasShiftLeftInt32x4<23>(MlasReinterpretAsInt32x4(biased));
    auto normal = overflow;
#if defined(MLAS_SSE2_INTRINSICS)
    // N.B. PMINSD/PMAXSD were not added until SSE 4.1, but the lower 16 bits
    // are zero, so they can be ignored for this computation, so use PMINSW/PMAXSW
    // instead.
    normal = _mm_min_epi16(normal, MaximumExponent);
    normal = _mm_max_epi16(normal, MinimumExponent);
#elif defined(MLAS_LSX_INTRINSICS)
    normal = __lsx_vmin_h(normal, MaximumExponent);
    normal = __lsx_vmax_h(normal, MinimumExponent);
#else
    normal = MlasMinimumInt32x4(normal, MaximumExponent);
    normal = MlasMaximumInt32x4(normal, MinimumExponent);
#endif
    overflow = MlasSubtractInt32x4(overflow, normal);
    overflow = MlasAddInt32x4(overflow, MaximumExponent);
    normal = MlasAddInt32x4(normal, MaximumExponent);
 
    //
    // Compute the polynomial approximation of exp(reduced) and reconstruct
    // the final result using the above scaling factors. The final term of
    // the polynomial (poly_6=1.0f) is merged as the multiply/add of the
    // overflow exponent (reference XNNPACK).
    //
 
    auto p = MlasBroadcastFloat32x4(MlasExpConstants.poly_0);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_1);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_2);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_3);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_4);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_56);
 
    Vector = MlasMultiplyFloat32x4(Vector, MlasReinterpretAsFloat32x4(overflow));
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasReinterpretAsFloat32x4(overflow));
    p = MlasMultiplyFloat32x4(p, MlasReinterpretAsFloat32x4(normal));
 
    return p;
}
 
void
MLASCALL
MlasComputeExpF32Kernel(
    const float* Input,
    float* Output,
    size_t N
)
/*++
 
Routine Description:
 
    This routine implements the generic kernel for the exponential function.
 
Arguments:
 
    Input - Supplies the input buffer.
 
    Output - Supplies the output buffer.
 
    N - Supplies the number of elements to process.
 
Return Value:
 
    None.
 
--*/
{
    while (N > 0) {
        MLAS_FLOAT32X4 Vector;
 
        if (N >= 4) {
            Vector = MlasLoadFloat32x4(Input);
        } else {
#if defined(MLAS_SSE2_INTRINSICS)
            // N.B. SSE2 lacks a broadcast load instruction, so avoid a shuffle
            // and use zeroes for the upper elements.
            Vector = _mm_load_ss(Input);
#elif defined(MLAS_LSX_INTRINSICS)
            Vector = (MLAS_FLOAT32X4)__lsx_vldrepl_w(Input, 0);
#else
            Vector = MlasBroadcastFloat32x4(Input);
#endif
        }
 
        Vector = MlasComputeExpVector(Vector);
 
        if (N >= 4) {
            MlasStoreFloat32x4(Output, Vector);
 
            Input += 4;
            Output += 4;
            N -= 4;
 
        } else {
            MlasStoreLaneFloat32x4<0>(Output, Vector);
 
            Input += 1;
            Output += 1;
            N -= 1;
        }
    }
}
 
template <>
void
MLASCALL
MlasComputeExp<float>(
    const float* Input,
    float* Output,
    size_t N
)
/*++
 
Routine Description:
 
    This routine computes the exponential function.
 
    N.B. This implementation supports in place updates of the output buffer.
 
Arguments:
 
    Input - Supplies the input buffer.
 
    Output - Supplies the output buffer.
 
    N - Supplies the number of elements to process.
 
Return Value:
 
    None.
 
--*/
{
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_RISCV64) || defined(MLAS_USE_SVE)
    GetMlasPlatform().ComputeExpF32Kernel(Input, Output, N);
#else
    MlasComputeExpF32Kernel(Input, Output, N);
#endif
}
 
template <>
void MLASCALL
MlasComputeExp<MLAS_FP16>(
    const MLAS_FP16* Input,
    MLAS_FP16* Output,
    size_t N
) {
    const auto* dispatch = GetMlasPlatform().SoftmaxDispatch;
    if (dispatch == nullptr || dispatch->Exp_Fp16 == nullptr) {
        MLAS_THROW_EX(std::runtime_error, "Exp_Fp16 is not supported.");
    }
    dispatch->Exp_Fp16(Input, Output, N);
}
 
MLAS_FORCEINLINE
MLAS_FLOAT32X4
MlasComputeSumExpVector(
    MLAS_FLOAT32X4 Vector,
    MLAS_FLOAT32X4 NegativeMaximumVector
)
/*++
 
Routine Description:
 
    This routine computes the exponential function for the supplied vector.
 
    This function handles a narrower range of inputs compared to
    MlasComputeExpVector in order to improve efficiency.
 
Arguments:
 
    Vector - Supplies the values to operate on.
 
    NegativeMaximumVector - Supplies the broadcasted negative maximum
        value that is added to each element before computing the exponential
        function.
 
Return Value:
 
    Returns the exponential function of the input.
 
--*/
{
    //
    // Subtract the maximum value from every element.
    //
    // N.B. For each of use by the assembly kernels, this value has been negated
    // so add the value instead.
    //
 
    Vector = MlasAddFloat32x4(Vector, NegativeMaximumVector);
 
    //
    // Clamp to the lower range of this function.
    //
    // The value should already be negative or equal to zero as every value has
    // been reduced by the maximum value.
    //
 
#if defined(MLAS_SSE2_INTRINSICS)
    // N.B. MINPS and MAXPS propagates the value from the second vector if the
    // value is a NaN.
#endif
    Vector = MlasMaximumFloat32x4(MlasBroadcastFloat32x4(MlasExpConstants.LowerRangeSumExp), Vector);
 
    //
    // Range reduction of the input by computing "(2 ^ m) * exp(reduced)".
    //
 
    const auto RoundingBias = MlasBroadcastFloat32x4(MlasExpConstants.RoundingBias);
 
    auto biased = MlasMultiplyAddFloat32x4(Vector, MlasExpConstants.Log2Reciprocal, RoundingBias);
    auto m = MlasSubtractFloat32x4(biased, RoundingBias);
 
    Vector = MlasMultiplyAddFloat32x4(m, MlasExpConstants.Log2High, Vector);
    Vector = MlasMultiplyAddFloat32x4(m, MlasExpConstants.Log2Low, Vector);
 
    //
    // Compute the scaling factor used to reconstruct the "(2 ^ m)" value
    // from above. The effective range of this function is smaller than
    // MlasComputeExp to reduce the number of operations.
    //
 
    auto normal = MlasShiftLeftInt32x4<23>(MlasReinterpretAsInt32x4(biased));
    normal = MlasAddInt32x4(normal, MlasBroadcastInt32x4(MlasExpConstants.MaximumExponent));
 
    //
    // Compute the polynomial approximation of exp(reduced) and reconstruct
    // the final result using the above scale factor.
    //
 
    auto p = MlasBroadcastFloat32x4(MlasExpConstants.poly_0);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_1);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_2);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_3);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_4);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_56);
    p = MlasMultiplyAddFloat32x4(p, Vector, MlasExpConstants.poly_56);
 
    p = MlasMultiplyFloat32x4(p, MlasReinterpretAsFloat32x4(normal));
 
    return p;
}
 
float
MLASCALL
MlasComputeSumExpF32Kernel(
    const float* Input,
    float* Output,
    size_t N,
    const float* NegativeMaximum
)
/*++
 
Routine Description:
 
    This routine implements the generic kernel for the sum of exponential
    functions.
 
Arguments:
 
    Input - Supplies the input buffer.
 
    Output - Optionally supplies the output buffer. When used for Softmax,
        the output buffer is used to store the intermediate exp() results. When
        used for LogSoftmax, the intermediate exp() results are not required.
 
    N - Supplies the number of elements to process.
 
    NegativeMaximum - Supplies the address of the negative maximum
        value that is added to each element before computing the exponential
        function.
 
Return Value:
 
    Returns the sum of the exponential functions.
 
--*/
{
    MLAS_FLOAT32X4 NegativeMaximumVector = MlasBroadcastFloat32x4(*NegativeMaximum);
    float Accumulator = 0.0f;
 
    if (N >= 4) {
        MLAS_FLOAT32X4 AccumulatorVector = MlasZeroFloat32x4();
 
#if !defined(MLAS_SSE2_INTRINSICS)
 
        //
        // Unroll the loop for architectures that can benefit from improved
        // instruction level parallelism.
        //
        // N.B. The extra code size is not worth the benefit for SSE2 as the
        // MLAS_TARGET_AMD64 build already has specialized AVX2/AVX512F kernels
        // that do this.
        //
 
        while (N >= 8) {
            MLAS_FLOAT32X4 Vector0 = MlasLoadFloat32x4(Input);
            MLAS_FLOAT32X4 Vector1 = MlasLoadFloat32x4(Input + 4);
 
            Vector0 = MlasComputeSumExpVector(Vector0, NegativeMaximumVector);
            Vector1 = MlasComputeSumExpVector(Vector1, NegativeMaximumVector);
            AccumulatorVector = MlasAddFloat32x4(AccumulatorVector, Vector0);
            AccumulatorVector = MlasAddFloat32x4(AccumulatorVector, Vector1);
 
            if (Output != nullptr) {
                MlasStoreFloat32x4(Output, Vector0);
                MlasStoreFloat32x4(Output + 4, Vector1);
                Output += 8;
            }
 
            Input += 8;
            N -= 8;
        }
 
#endif
 
        while (N >= 4) {
            MLAS_FLOAT32X4 Vector = MlasLoadFloat32x4(Input);
 
            Vector = MlasComputeSumExpVector(Vector, NegativeMaximumVector);
            AccumulatorVector = MlasAddFloat32x4(AccumulatorVector, Vector);
 
            if (Output != nullptr) {
                MlasStoreFloat32x4(Output, Vector);
                Output += 4;
            }
 
            Input += 4;
            N -= 4;
        }
 
        Accumulator = MlasReduceAddFloat32x4(AccumulatorVector);
    }
 
    while (N > 0) {
#if defined(MLAS_SSE2_INTRINSICS)
        // N.B. SSE2 lacks a broadcast load instruction, so avoid a shuffle and
        // use zeroes for the upper elements.
        MLAS_FLOAT32X4 Vector = _mm_load_ss(Input);
#elif defined(MLAS_LSX_INTRINSICS)
        MLAS_FLOAT32X4 Vector = (MLAS_FLOAT32X4)__lsx_vldrepl_w(Input, 0);
#else
        MLAS_FLOAT32X4 Vector = MlasBroadcastFloat32x4(Input);
#endif
 
        Vector = MlasComputeSumExpVector(Vector, NegativeMaximumVector);
        Accumulator += MlasExtractLaneFloat32x4<0>(Vector);
 
        if (Output != nullptr) {
            MlasStoreLaneFloat32x4<0>(Output, Vector);
            Output += 1;
        }
 
        Input += 1;
        N -= 1;
    }
    return Accumulator;
}
 
float
MLASCALL
MlasReduceMaximumF32Kernel(
    const float* Input,
    size_t N
)
/*++
 
Routine Description:
 
    This routine implements the generic kernel to find the maximum value of
    the supplied buffer.
 
Arguments:
 
    Input - Supplies the input buffer.
 
    N - Supplies the number of elements to process.
 
Return Value:
 
    Returns the maximum value of the supplied buffer.
 
--*/
{
    float Maximum = MlasMinimumF32Value;
 
    if (N >= 4) {
        MLAS_FLOAT32X4 MaximumVector0 = MlasBroadcastFloat32x4(Maximum);
 
        if (N >= 16) {
            MLAS_FLOAT32X4 MaximumVector1 = MaximumVector0;
            MLAS_FLOAT32X4 MaximumVector2 = MaximumVector0;
            MLAS_FLOAT32X4 MaximumVector3 = MaximumVector0;
 
            while (N >= 16) {
                MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, MlasLoadFloat32x4(Input));
                MaximumVector1 = MlasMaximumFloat32x4(MaximumVector1, MlasLoadFloat32x4(Input + 4));
                MaximumVector2 = MlasMaximumFloat32x4(MaximumVector2, MlasLoadFloat32x4(Input + 8));
                MaximumVector3 = MlasMaximumFloat32x4(MaximumVector3, MlasLoadFloat32x4(Input + 12));
 
                Input += 16;
                N -= 16;
            }
 
            MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, MaximumVector1);
            MaximumVector2 = MlasMaximumFloat32x4(MaximumVector2, MaximumVector3);
            MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, MaximumVector2);
        }
 
        while (N >= 4) {
            MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, MlasLoadFloat32x4(Input));
 
            Input += 4;
            N -= 4;
        }
 
        Maximum = MlasReduceMaximumFloat32x4(MaximumVector0);
    }
 
    while (N > 0) {
        Maximum = std::max(Maximum, *Input);
 
        Input += 1;
        N -= 1;
    }
    return Maximum;
}
 
void
MLASCALL
MlasReduceMinimumMaximumF32Kernel(
    const float* Input,
    float* Min,
    float* Max,
    size_t N
)
{
    float tmp_min = std::numeric_limits<float>::max();
    float tmp_max = std::numeric_limits<float>::lowest();
 
    if (N >= 4) {
        MLAS_FLOAT32X4 MaximumVector0 = MlasBroadcastFloat32x4(tmp_max);
        MLAS_FLOAT32X4 MinimumVector0 = MlasBroadcastFloat32x4(tmp_min);
 
        if (N >= 16) {
            MLAS_FLOAT32X4 MaximumVector1 = MaximumVector0;
            MLAS_FLOAT32X4 MaximumVector2 = MaximumVector0;
            MLAS_FLOAT32X4 MaximumVector3 = MaximumVector0;
 
            MLAS_FLOAT32X4 MinimumVector1 = MinimumVector0;
            MLAS_FLOAT32X4 MinimumVector2 = MinimumVector0;
            MLAS_FLOAT32X4 MinimumVector3 = MinimumVector0;
 
            while (N >= 16) {
                MLAS_FLOAT32X4 InputVector0 = MlasLoadFloat32x4(Input);
                MLAS_FLOAT32X4 InputVector1 = MlasLoadFloat32x4(Input + 4);
                MLAS_FLOAT32X4 InputVector2 = MlasLoadFloat32x4(Input + 8);
                MLAS_FLOAT32X4 InputVector3 = MlasLoadFloat32x4(Input + 12);
 
                MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, InputVector0);
                MaximumVector1 = MlasMaximumFloat32x4(MaximumVector1, InputVector1);
                MaximumVector2 = MlasMaximumFloat32x4(MaximumVector2, InputVector2);
                MaximumVector3 = MlasMaximumFloat32x4(MaximumVector3, InputVector3);
 
                MinimumVector0 = MlasMinimumFloat32x4(MinimumVector0, InputVector0);
                MinimumVector1 = MlasMinimumFloat32x4(MinimumVector1, InputVector1);
                MinimumVector2 = MlasMinimumFloat32x4(MinimumVector2, InputVector2);
                MinimumVector3 = MlasMinimumFloat32x4(MinimumVector3, InputVector3);
 
                Input += 16;
                N -= 16;
            }
 
            MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, MaximumVector1);
            MaximumVector2 = MlasMaximumFloat32x4(MaximumVector2, MaximumVector3);
            MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, MaximumVector2);
 
            MinimumVector0 = MlasMinimumFloat32x4(MinimumVector0, MinimumVector1);
            MinimumVector2 = MlasMinimumFloat32x4(MinimumVector2, MinimumVector3);
            MinimumVector0 = MlasMinimumFloat32x4(MinimumVector0, MinimumVector2);
        }
 
        while (N >= 4) {
            MLAS_FLOAT32X4 InputVector0 = MlasLoadFloat32x4(Input);
            MaximumVector0 = MlasMaximumFloat32x4(MaximumVector0, InputVector0);
 
            MinimumVector0 = MlasMinimumFloat32x4(MinimumVector0, InputVector0);
 
            Input += 4;
            N -= 4;
        }
 
        tmp_min = MlasReduceMinimumFloat32x4(MinimumVector0);
        tmp_max = MlasReduceMaximumFloat32x4(MaximumVector0);
    }
 
    while (N > 0) {
        tmp_max = std::max(tmp_max, *Input);
        tmp_min = std::min(tmp_min, *Input);
 
        Input += 1;
        N -= 1;
    }
 
    *Min = tmp_min;
    *Max = tmp_max;
}
 
void
MLASCALL
MlasComputeSoftmaxOutputF32Kernel(
    float* Output,
    size_t N,
    const float* Parameters
)
/*++
 
Routine Description:
 
    This routine implements the generic kernel to produce the final output for
    the softmax operation.
 
Arguments:
 
    Output - Supplies the output buffer.
 
    N - Supplies the number of elements to process.
 
    Parameters - Supplies an array containing the scale value.
 
Return Value:
 
    None.
 
--*/
{
    const float Scale = Parameters[0];
 
    const MLAS_FLOAT32X4 ScaleVector = MlasBroadcastFloat32x4(Scale);
 
    while (N >= 16) {
        MLAS_FLOAT32X4 Vector0 = MlasMultiplyFloat32x4(ScaleVector, MlasLoadFloat32x4(Output));
        MLAS_FLOAT32X4 Vector1 = MlasMultiplyFloat32x4(ScaleVector, MlasLoadFloat32x4(Output + 4));
        MLAS_FLOAT32X4 Vector2 = MlasMultiplyFloat32x4(ScaleVector, MlasLoadFloat32x4(Output + 8));
        MLAS_FLOAT32X4 Vector3 = MlasMultiplyFloat32x4(ScaleVector, MlasLoadFloat32x4(Output + 12));
 
        MlasStoreFloat32x4(Output, Vector0);
        MlasStoreFloat32x4(Output + 4, Vector1);
        MlasStoreFloat32x4(Output + 8, Vector2);
        MlasStoreFloat32x4(Output + 12, Vector3);
 
        Output += 16;
        N -= 16;
    }
 
    while (N >= 4) {
        MlasStoreFloat32x4(Output, MlasMultiplyFloat32x4(ScaleVector, MlasLoadFloat32x4(Output)));
 
        Output += 4;
        N -= 4;
    }
 
    while (N > 0) {
        *Output *= Scale;
 
        Output += 1;
        N -= 1;
    }
}
 
void
MLASCALL
MlasComputeLogSoftmaxOutputF32Kernel(
    const float* Input,
    float* Output,
    size_t N,
    const float* Parameters
)
/*++
 
Routine Description:
 
    This routine implements the generic kernel to produce the final output for
    the log softmax operation.
 
Arguments:
 
    Input - Supplies the input buffer.
 
    Output - Supplies the output buffer.
 
    N - Supplies the number of elements to process.
 
    Parameters - Supplies an array containing the negative maximum and
        logarithm values.
 
Return Value:
 
    None.
 
--*/
{
    const float NegativeMaximum = Parameters[0];
    const float Logarithm = Parameters[1];
 
    const MLAS_FLOAT32X4 NegativeMaximumVector = MlasBroadcastFloat32x4(NegativeMaximum);
    const MLAS_FLOAT32X4 LogarithmVector = MlasBroadcastFloat32x4(Logarithm);
 
    while (N >= 16) {
        MLAS_FLOAT32X4 Vector0 = MlasLoadFloat32x4(Input);
        MLAS_FLOAT32X4 Vector1 = MlasLoadFloat32x4(Input + 4);
        MLAS_FLOAT32X4 Vector2 = MlasLoadFloat32x4(Input + 8);
        MLAS_FLOAT32X4 Vector3 = MlasLoadFloat32x4(Input + 12);
 
        Vector0 = MlasAddFloat32x4(Vector0, NegativeMaximumVector);
        Vector1 = MlasAddFloat32x4(Vector1, NegativeMaximumVector);
        Vector2 = MlasAddFloat32x4(Vector2, NegativeMaximumVector);
        Vector3 = MlasAddFloat32x4(Vector3, NegativeMaximumVector);
 
        Vector0 = MlasSubtractFloat32x4(Vector0, LogarithmVector);
        Vector1 = MlasSubtractFloat32x4(Vector1, LogarithmVector);
        Vector2 = MlasSubtractFloat32x4(Vector2, LogarithmVector);
        Vector3 = MlasSubtractFloat32x4(Vector3, LogarithmVector);
 
        MlasStoreFloat32x4(Output, Vector0);
        MlasStoreFloat32x4(Output + 4, Vector1);
        MlasStoreFloat32x4(Output + 8, Vector2);
        MlasStoreFloat32x4(Output + 12, Vector3);
 
        Input += 16;
        Output += 16;
        N -= 16;
    }
 
    while (N >= 4) {
        MLAS_FLOAT32X4 Vector = MlasLoadFloat32x4(Input);
        Vector = MlasAddFloat32x4(Vector, NegativeMaximumVector);
        Vector = MlasSubtractFloat32x4(Vector, LogarithmVector);
        MlasStoreFloat32x4(Output, Vector);
 
        Input += 4;
        Output += 4;
        N -= 4;
    }
 
    while (N > 0) {
        *Output = *Input + NegativeMaximum - Logarithm;
 
        Input += 1;
        Output += 1;
        N -= 1;
    }
}
 
template <typename T>
void
MlasComputeSoftmaxThreaded(
    void* Context,
    ptrdiff_t Index
);
 
template <>
void
MlasComputeSoftmaxThreaded<float>(
    void* Context,
    ptrdiff_t Index
)
/*++
 
Routine Description:
 
    This routine is invoked from a worker thread to execute a segment of a
    softmax or log softmax operation.
 
Arguments:
 
    Context - Supplies the pointer to the context for the threaded operation.
 
    ThreadId - Supplies the current index of the threaded operation.
 
Return Value:
 
    None.
 
--*/
{
    const auto* WorkBlock = (MLAS_SOFTMAX_WORK_BLOCK<float>*)Context;
    
    //
    // Partition the operation along the N dimension.
    //
 
    size_t n;
    size_t CountN;
 
    MlasPartitionWork(Index, WorkBlock->ThreadCountN, WorkBlock->N, &n, &CountN);
 
    //
    // Compute the softmax or log softmax function.
    //
 
    const size_t D = WorkBlock->D;
    const bool LogSoftmax = WorkBlock->LogSoftmax;
    const bool SmoothSoftmax = WorkBlock->SmoothSoftmax;
    const float Sink = WorkBlock->Sink;
 
    const float* Input = WorkBlock->Input + n * D;
    float* Output = WorkBlock->Output + n * D;
 
#if defined(MLAS_SSE2_INTRINSICS)
    // TODO: Use std::hardware_constructive_interference_size
    constexpr size_t CacheLineSize = 64;
    constexpr size_t ElementsPerCacheLine = CacheLineSize / sizeof(float);
#endif
 
    while (CountN > 0) {
#if defined(MLAS_SSE2_INTRINSICS)
        //
        // Prefetch the next row of the input buffer.
        //
 
        for (size_t i = 0; i * ElementsPerCacheLine < D; i++) {
            _mm_prefetch((char*)(Input + D) + i * CacheLineSize, _MM_HINT_T0);
        }
#endif
 
        //
        // Find the maximum value for the row.
        //
        float Maximum;
 
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_LARCH64) || defined(MLAS_USE_SVE) || defined(MLAS_TARGET_RISCV64)
        Maximum = GetMlasPlatform().ReduceMaximumF32Kernel(Input, D);
#else 
        Maximum = MlasReduceMaximumF32Kernel(Input, D);
#endif
        if (SmoothSoftmax && Sink > Maximum) {
            Maximum = Sink;
        }
 
        float NegativeMaximum = -Maximum;
 
        //
        // Compute the exponential function for each element of the row (save to Temp if provided) and
        // compute the sum of these exponential functions.
        //
        float* Temp = LogSoftmax ? nullptr : Output;
        float Accumulation;
 
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_USE_SVE) || defined(MLAS_TARGET_RISCV64)
        Accumulation = GetMlasPlatform().ComputeSumExpF32Kernel(Input, Temp, D, &NegativeMaximum);
#else
        Accumulation = MlasComputeSumExpF32Kernel(Input, Temp, D, &NegativeMaximum);
#endif
 
        if (SmoothSoftmax) {
            Accumulation += expf(Sink + NegativeMaximum);
        }
 
        if (LogSoftmax) {
            //
            // Compute the log softmax output.
            //
            float Parameters[] = {NegativeMaximum, std::log(Accumulation)};
 
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_LARCH64) || defined(MLAS_USE_SVE) || defined(MLAS_TARGET_RISCV64)
            GetMlasPlatform().ComputeLogSoftmaxOutputF32Kernel(Input, Output, D, Parameters);
#else 
 
            MlasComputeLogSoftmaxOutputF32Kernel(Input, Output, D, Parameters);
#endif
        } else {
            //
            // Normalize the softmax output.
            //
            float Parameters[] = {1.0f / Accumulation};
 
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_LARCH64) || defined(MLAS_USE_SVE) || defined(MLAS_TARGET_RISCV64)
            GetMlasPlatform().ComputeSoftmaxOutputF32Kernel(Output, D, Parameters);
#else
            MlasComputeSoftmaxOutputF32Kernel(Output, D, Parameters);
#endif
        }
 
        Input += D;
        Output += D;
        CountN--;
    }
}
 
template <>
void
MlasComputeSoftmaxThreaded<MLAS_FP16>(
    void* Context,
    ptrdiff_t Index
)
/*++
 
Routine Description:
 
    This routine is invoked from a worker thread to execute a segment of a
    softmax or log softmax operation.
 
Arguments:
 
    Context - Supplies the pointer to the context for the threaded operation.
 
    ThreadId - Supplies the current index of the threaded operation.
 
Return Value:
 
    None.
 
--*/
{
    const auto* WorkBlock = (MLAS_SOFTMAX_WORK_BLOCK<MLAS_FP16>*)Context;
    size_t n;
    size_t CountN;
    MlasPartitionWork(Index, WorkBlock->ThreadCountN, WorkBlock->N, &n, &CountN);
 
    const size_t D = WorkBlock->D;
    const bool LogSoftmax = WorkBlock->LogSoftmax;
    const bool SmoothSoftmax = WorkBlock->SmoothSoftmax;
 
    const MLAS_FP16* Input = WorkBlock->Input + n * D;
    MLAS_FP16* Output = WorkBlock->Output + n * D;
 
    const auto* dispatch = GetMlasPlatform().SoftmaxDispatch;
    if (dispatch == nullptr ||
        dispatch->ReduceMax_Fp16 == nullptr ||
        dispatch->SumExp_Fp16 == nullptr ||
        (LogSoftmax && dispatch->LogSoftmax_Fp16 == nullptr) ||
        (!LogSoftmax && dispatch->Softmax_Fp16 == nullptr)) {
        MLAS_THROW_EX(std::runtime_error, "Lacks kernels for fp16 softmax.");
    }
 
    while (CountN > 0) {
        MLAS_FP16 Maximum = dispatch->ReduceMax_Fp16(Input, D);
        MLAS_FP16 NegativeMaximum = Maximum.Negate();
        if (SmoothSoftmax && !NegativeMaximum.IsNegative()) {
            NegativeMaximum = MLAS_FP16::FromBits(0);
        }
 
        MLAS_FP16* Temp = LogSoftmax ? nullptr : Output;
        MLAS_FP16 Accumulation = dispatch->SumExp_Fp16(Input, Temp, D, NegativeMaximum);
        float accumulation_fp32 = Accumulation.ToFloat();
 
        if (SmoothSoftmax) {
            accumulation_fp32 += expf(NegativeMaximum.ToFloat());
        }
 
        if (LogSoftmax) {
            dispatch->LogSoftmax_Fp16(Input, Output, D, NegativeMaximum, MLAS_FP16(std::log(accumulation_fp32)));
        } else {
            dispatch->Softmax_Fp16(Output, Output, D, MLAS_FP16(accumulation_fp32));
        }
 
        Input += D;
        Output += D;
        CountN--;
    }
}
 
template <typename T>
void
MLASCALL
MlasComputeSoftmax(
    const T* Input,
    T* Output,
    size_t N,
    size_t D,
    bool LogSoftmax,
    bool SmoothSoftmax,
    float Sink,
    MLAS_THREADPOOL* ThreadPool
)
/*++
 
Routine Description:
 
    This routine computes the softmax or log softmax function.
 
    N.B. This implementation supports in place updates of the output buffer.
 
Arguments:
 
    Input - Supplies the input buffer.
 
    Output - Supplies the output buffer.
 
    N - Supplies the number of rows to process.
 
    D - Supplies the number of columns per row to process.
 
    LogSoftmax - Supplies true if this is a log softmax operation, else false
        if this is a softmax operation.
 
    SmoothSoftmax - Supplies true if a smooth factor is used in softmax operation.
 
    Sink - Supplies the smooth factor to use in the softmax operation.
 
    ThreadPool - Supplies the thread pool object to use, else nullptr if the
        base library threading support should be used.
 
Return Value:
 
    None.
 
--*/
{
    MLAS_SOFTMAX_WORK_BLOCK<T> WorkBlock;
 
    //
    // Capture the softmax parameters to the work block.
    //
 
    WorkBlock.LogSoftmax = LogSoftmax;
    WorkBlock.SmoothSoftmax = SmoothSoftmax;
    WorkBlock.Input = Input;
    WorkBlock.Output = Output;
    WorkBlock.N = N;
    WorkBlock.D = D;
    WorkBlock.Sink = Sink;
 
    //
    // Compute the number of target threads given the complexity of the softmax
    // operation. Limit the number of threads to the number of rows and try to
    // keep each thread processing a minimum number of elements before using
    // another thread.
    //
 
    ptrdiff_t ThreadCountN = MlasGetMaximumThreadCount(ThreadPool);
 
    if (size_t(ThreadCountN) > N) {
        ThreadCountN = ptrdiff_t(N);
    }
 
    constexpr size_t MinimumElementsPerThread = 16384;
 
    size_t BlockCount = ((N * D) / MinimumElementsPerThread) + 1;
 
    if (size_t(ThreadCountN) > BlockCount) {
        ThreadCountN = ptrdiff_t(BlockCount);
    }
 
    WorkBlock.ThreadCountN = ThreadCountN;
 
    MlasExecuteThreaded(MlasComputeSoftmaxThreaded<T>, &WorkBlock, ThreadCountN, ThreadPool);
}
 
template
void
MLASCALL
MlasComputeSoftmax<float>(
    const float* Input,
    float* Output,
    size_t N,
    size_t D,
    bool LogSoftmax,
    bool SmoothSoftmax,
    float Sink,
    MLAS_THREADPOOL* ThreadPool
);
 
template
void
MLASCALL
MlasComputeSoftmax<MLAS_FP16>(
    const MLAS_FP16* Input,
    MLAS_FP16* Output,
    size_t N,
    size_t D,
    bool LogSoftmax,
    bool SmoothSoftmax,
    float Sink,
    MLAS_THREADPOOL* ThreadPool
);
 
template <>
bool
MLASCALL
MlasGQASupported<MLAS_FP16>(
    CBLAS_TRANSPOSE TransA,
    CBLAS_TRANSPOSE TransB
) {
    if (!MlasHGemmSupported(TransA, TransB)) {
        return false;
    }
 
    const auto* softmax_dispatch = GetMlasPlatform().SoftmaxDispatch;
    if (softmax_dispatch == nullptr ||
        softmax_dispatch->Tanh_Fp16 == nullptr ||
        softmax_dispatch->Softcap_Fp16 == nullptr ||
        softmax_dispatch->SumExp_Fp16 == nullptr ||
        softmax_dispatch->Softmax_Fp16 == nullptr ||
        softmax_dispatch->ReduceMax_Fp16 == nullptr) {
        return false;
    }
 
    return true;
}
 
template <>
bool
MLASCALL
MlasGQASupported<float>(
    CBLAS_TRANSPOSE TransA,
    CBLAS_TRANSPOSE TransB
) {
    MLAS_UNREFERENCED_PARAMETER(TransA);
    MLAS_UNREFERENCED_PARAMETER(TransB);
    return true;
}