Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    activate.cpp
 
Abstract:
 
    This module implements the fused activation and bias addition routines.
 
--*/
 
#include "mlasi.h"
 
//
// Templates for bias addition functions.
//
 
template<bool AddBias>
struct MLAS_BIAS_ADDITION;
 
template<>
struct MLAS_BIAS_ADDITION<true>
{
    MLAS_FLOAT32X4 BiasBroadcast;
 
    void LoadNext(const float*& Bias)
    {
        BiasBroadcast = MlasBroadcastFloat32x4(Bias++);
    }
 
    MLAS_FLOAT32X4 Add(MLAS_FLOAT32X4 Value)
    {
        return MlasAddFloat32x4(Value, BiasBroadcast);
    }
 
    float Add(float Value)
    {
        return Value + MlasExtractLaneFloat32x4<0>(BiasBroadcast);
    }
};
 
template<>
struct MLAS_BIAS_ADDITION<false>
{
    void LoadNext(const float*& Bias)
    {
        MLAS_UNREFERENCED_PARAMETER(Bias);
    }
 
    MLAS_FLOAT32X4 Add(MLAS_FLOAT32X4 Value)
    {
        return Value;
    }
 
    float Add(float Value)
    {
        return Value;
    }
};
 
//
// Templates for activation functions.
//
 
template<MLAS_ACTIVATION_KIND ActivationKind>
struct MLAS_ACTIVATION_FUNCTION;
 
template<>
struct MLAS_ACTIVATION_FUNCTION<MlasIdentityActivation>
{
    MLAS_ACTIVATION_FUNCTION(const MLAS_ACTIVATION* Activation)
    {
        MLAS_UNREFERENCED_PARAMETER(Activation);
    }
 
    MLAS_FLOAT32X4 Activate(MLAS_FLOAT32X4 Value)
    {
        return Value;
    }
 
    float Activate(float Value)
    {
        return Value;
    }
};
 
template<>
struct MLAS_ACTIVATION_FUNCTION<MlasReluActivation>
{
    const MLAS_FLOAT32X4 ZeroFloat32x4 = MlasZeroFloat32x4();
 
    MLAS_ACTIVATION_FUNCTION(const MLAS_ACTIVATION* Activation)
    {
        MLAS_UNREFERENCED_PARAMETER(Activation);
    }
 
    MLAS_FLOAT32X4 Activate(MLAS_FLOAT32X4 Value)
    {
        return MlasMaximumFloat32x4(ZeroFloat32x4, Value);
    }
 
    float Activate(float Value)
    {
#if defined(MLAS_SSE2_INTRINSICS)
        return _mm_cvtss_f32(Activate(_mm_set_ss(Value)));
#else
        return std::max(Value, 0.0f);
#endif
    }
};
 
template<>
struct MLAS_ACTIVATION_FUNCTION<MlasLeakyReluActivation>
{
    const MLAS_FLOAT32X4 ZeroFloat32x4 = MlasZeroFloat32x4();
 
    MLAS_FLOAT32X4 AlphaBroadcast;
 
    MLAS_ACTIVATION_FUNCTION(const MLAS_ACTIVATION* Activation)
    {
        AlphaBroadcast = MlasBroadcastFloat32x4(&Activation->Parameters.LeakyRelu.alpha);
    }
 
    MLAS_FLOAT32X4 Activate(MLAS_FLOAT32X4 Value)
    {
        MLAS_FLOAT32X4 ValueTimesAlpha = MlasMultiplyFloat32x4(Value, AlphaBroadcast);
 
#if defined(MLAS_NEON_INTRINSICS)
#if defined(_WIN32) && !defined(__clang__)
        return vbslq_f32(vcleq_z_f32_ex(Value), ValueTimesAlpha, Value);
#else
        // N.B. Standard NEON headers lack an intrinsic for the "vcle #0" form.
        return vbslq_f32(vcleq_f32(Value, ZeroFloat32x4), ValueTimesAlpha, Value);
#endif
#elif defined(MLAS_AVX_INTRINSICS)
        return _mm_blendv_ps(ValueTimesAlpha, Value, _mm_cmple_ps(ZeroFloat32x4, Value));
#elif defined(MLAS_SSE2_INTRINSICS)
        return MlasBlendFloat32x4(ValueTimesAlpha, Value, _mm_cmple_ps(ZeroFloat32x4, Value));
#elif defined(MLAS_VSX_INTRINSICS) || defined(MLAS_ZVECTOR_INTRINSICS)
        return vec_sel(ValueTimesAlpha, Value, vec_cmple(ZeroFloat32x4, Value));
#elif defined(MLAS_LSX_INTRINSICS)
        return MlasBlendFloat32x4(ValueTimesAlpha, Value, (__m128)__lsx_vfcmp_cle_s(ZeroFloat32x4, Value));
#else
        return MlasBlendFloat32x4(ValueTimesAlpha, Value, ZeroFloat32x4 < Value);
#endif
    }
 
    float Activate(float Value)
    {
        float ValueTimesAlpha = Value * MlasExtractLaneFloat32x4<0>(AlphaBroadcast);
 
#if defined(MLAS_SSE2_INTRINSICS)
        return (Value >= MlasExtractLaneFloat32x4<0>(ZeroFloat32x4)) ? Value : ValueTimesAlpha;
#else
        return (Value >= 0.0f) ? Value : ValueTimesAlpha;
#endif
    }
};
 
template<>
struct MLAS_ACTIVATION_FUNCTION<MlasClipActivation>
{
    MLAS_FLOAT32X4 MinimumBroadcast;
    MLAS_FLOAT32X4 MaximumBroadcast;
 
    MLAS_ACTIVATION_FUNCTION(const MLAS_ACTIVATION* Activation)
    {
        MinimumBroadcast = MlasBroadcastFloat32x4(&Activation->Parameters.Clip.minimum);
        MaximumBroadcast = MlasBroadcastFloat32x4(&Activation->Parameters.Clip.maximum);
    }
 
    MLAS_FLOAT32X4 Activate(MLAS_FLOAT32X4 Value)
    {
        Value = MlasMaximumFloat32x4(MinimumBroadcast, Value);
        Value = MlasMinimumFloat32x4(MaximumBroadcast, Value);
 
        return Value;
    }
 
    float Activate(float Value)
    {
#if defined(MLAS_SSE2_INTRINSICS)
        return _mm_cvtss_f32(Activate(_mm_set_ss(Value)));
#else
        Value = std::max(Value, MlasExtractLaneFloat32x4<0>(MinimumBroadcast));
        Value = std::min(Value, MlasExtractLaneFloat32x4<0>(MaximumBroadcast));
 
        return Value;
#endif
    }
};
 
template<>
struct MLAS_ACTIVATION_FUNCTION<MlasHardSigmoidActivation>
{
    MLAS_FLOAT32X4 AlphaBroadcast;
    MLAS_FLOAT32X4 BetaBroadcast;
    MLAS_FLOAT32X4 MinimumBroadcast;
    MLAS_FLOAT32X4 MaximumBroadcast;
 
    MLAS_ACTIVATION_FUNCTION(const MLAS_ACTIVATION* Activation)
    {
        AlphaBroadcast = MlasBroadcastFloat32x4(&Activation->Parameters.HardSigmoid.alpha);
        BetaBroadcast = MlasBroadcastFloat32x4(&Activation->Parameters.HardSigmoid.beta);
        MinimumBroadcast = MlasZeroFloat32x4();
        MaximumBroadcast = MlasBroadcastFloat32x4(1.0f);
    }
 
    MLAS_FLOAT32X4 Activate(MLAS_FLOAT32X4 Value)
    {
        Value = MlasMultiplyAddFloat32x4(Value, AlphaBroadcast, BetaBroadcast);
        Value = MlasMinimumFloat32x4(MaximumBroadcast, Value);
        Value = MlasMaximumFloat32x4(MinimumBroadcast, Value);
 
        return Value;
    }
 
    float Activate(float Value)
    {
#if defined(MLAS_SSE2_INTRINSICS)
        return _mm_cvtss_f32(Activate(_mm_set_ss(Value)));
#else
        Value = MlasExtractLaneFloat32x4<0>(AlphaBroadcast) * Value + MlasExtractLaneFloat32x4<0>(BetaBroadcast);
        Value = std::min(Value, MlasExtractLaneFloat32x4<0>(MaximumBroadcast));
        Value = std::max(Value, MlasExtractLaneFloat32x4<0>(MinimumBroadcast));
 
        return Value;
#endif
    }
};
 
template<>
struct MLAS_ACTIVATION_FUNCTION<MlasHardSwishActivation>
{
    MLAS_FLOAT32X4 AlphaBroadcast;
    MLAS_FLOAT32X4 BetaBroadcast;
    MLAS_FLOAT32X4 MinimumBroadcast;
    MLAS_FLOAT32X4 MaximumBroadcast;
 
    static constexpr float Alpha = 1.0f / 6.0f;
    static constexpr float Beta = 0.5f;
    static constexpr float Minimum = 0.0f;
    static constexpr float Maximum = 1.0f;
 
    MLAS_ACTIVATION_FUNCTION(const MLAS_ACTIVATION* Activation)
    {
        MLAS_UNREFERENCED_PARAMETER(Activation);
        AlphaBroadcast = MlasBroadcastFloat32x4(Alpha);
        BetaBroadcast = MlasBroadcastFloat32x4(Beta);
        MinimumBroadcast = MlasZeroFloat32x4();
        MaximumBroadcast = MlasBroadcastFloat32x4(Maximum);
    }
 
    MLAS_FLOAT32X4 Activate(MLAS_FLOAT32X4 Value)
    {
        MLAS_FLOAT32X4 Gate = MlasMultiplyAddFloat32x4(Value, AlphaBroadcast, BetaBroadcast);
        Gate = MlasMinimumFloat32x4(MaximumBroadcast, Gate);
        Gate = MlasMaximumFloat32x4(MinimumBroadcast, Gate);
 
        return MlasMultiplyFloat32x4(Value, Gate);
    }
 
    float Activate(float Value)
    {
#if defined(MLAS_SSE2_INTRINSICS)
        return _mm_cvtss_f32(Activate(_mm_set_ss(Value)));
#else
        float Gate = Alpha * Value + Beta;
        Gate = std::min(Gate, Maximum);
        Gate = std::max(Gate, Minimum);
 
        return Value * Gate;
#endif
    }
};
 
template<MLAS_ACTIVATION_KIND ActivationKind, bool AddBias>
void
MlasActivationKernel(
    const MLAS_ACTIVATION* Activation,
    float* Buffer,
    const float* Bias,
    size_t M,
    size_t N,
    size_t ldc
    )
/*++
 
Routine Description:
 
    This routine steps over the output matrix and invokes the templated bias
    addition and activation functions.
 
Arguments:
 
    Activation - Supplies the parameters for the activation.
 
    Buffer - Supplies the output matrix.
 
    Bias - Supplies the optional bias vector.
 
    M - Supplies the number of elements of the bias vector and the number of
        rows in the output matrix.
 
    N - Supplies the number of columns of the output matrix.
 
    ldc - Supplies the number of elements per row of the output matrix.
 
Return Value:
 
    None.
 
--*/
{
    MLAS_ACTIVATION_FUNCTION<ActivationKind> ActivationFunction(Activation);
    MLAS_BIAS_ADDITION<AddBias> BiasAddition;
 
    //
    // Step through each row of the output matrix.
    //
 
    while (M-- > 0) {
 
        float* buffer = Buffer;
        size_t n = N;
 
        BiasAddition.LoadNext(Bias);
 
        if (n >= 4) {
 
            do {
 
                MLAS_FLOAT32X4 Vector = BiasAddition.Add(MlasLoadFloat32x4(buffer));
                MlasStoreFloat32x4(buffer, ActivationFunction.Activate(Vector));
                buffer += 4;
                n -= 4;
 
            } while (n >= 4);
        }
 
        while (n > 0) {
 
            float Scalar = BiasAddition.Add(*buffer);
            *buffer++ = ActivationFunction.Activate(Scalar);
            n -= 1;
        }
 
        Buffer += ldc;
    }
}
 
template<>
inline
void
MlasActivationKernel<MlasIdentityActivation, false>(
    const MLAS_ACTIVATION* Activation,
    float* Buffer,
    const float* Bias,
    size_t M,
    size_t N,
    size_t ldc
    )
/*++
 
Routine Description:
 
    This routine is invoked for the special case of an identity operation with
    no bias addition, which translates to a no-op.
 
Arguments:
 
    Activation - Supplies the parameters for the activation.
 
    Buffer - Supplies the output matrix.
 
    Bias - Supplies the optional bias vector.
 
    M - Supplies the number of elements of the bias vector and the number of
        rows in the output matrix.
 
    N - Supplies the number of columns of the output matrix.
 
    ldc - Supplies the number of elements per row of the output matrix.
 
Return Value:
 
    None.
 
--*/
{
    //
    // No operation.
    //
 
    MLAS_UNREFERENCED_PARAMETER(Activation);
    MLAS_UNREFERENCED_PARAMETER(Buffer);
    MLAS_UNREFERENCED_PARAMETER(Bias);
    MLAS_UNREFERENCED_PARAMETER(M);
    MLAS_UNREFERENCED_PARAMETER(N);
    MLAS_UNREFERENCED_PARAMETER(ldc);
}
 
template<MLAS_ACTIVATION_KIND ActivationKind>
inline
void
MlasActivationKernel(
    const MLAS_ACTIVATION* Activation,
    float* Buffer,
    const float* Bias,
    size_t M,
    size_t N,
    size_t ldc
    )
/*++
 
Routine Description:
 
    This routine invokes the appropriate activation kernel based on the
    optional bias vector.
 
Arguments:
 
    Activation - Supplies the parameters for the activation.
 
    Buffer - Supplies the output matrix.
 
    Bias - Supplies the optional bias vector.
 
    M - Supplies the number of elements of the bias vector and the number of
        rows in the output matrix.
 
    N - Supplies the number of columns of the output matrix.
 
    ldc - Supplies the number of elements per row of the output matrix.
 
Return Value:
 
    None.
 
--*/
{
    if (Bias != nullptr) {
        MlasActivationKernel<ActivationKind, true>(Activation, Buffer, Bias, M, N, ldc);
    } else {
        MlasActivationKernel<ActivationKind, false>(Activation, Buffer, Bias, M, N, ldc);
    }
}
 
void
MLASCALL
MlasActivation(
    const MLAS_ACTIVATION* Activation,
    float* Buffer,
    const float* Bias,
    size_t M,
    size_t N,
    size_t ldc
    )
/*++
 
Routine Description:
 
    This routine applies an activation function to the output matrix after
    optionally adding a bias vector.
 
Arguments:
 
    Activation - Supplies the parameters for the activation.
 
    Buffer - Supplies the output matrix.
 
    Bias - Supplies the optional bias vector.
 
    M - Supplies the number of elements of the bias vector and the number of
        rows in the output matrix.
 
    N - Supplies the number of columns of the output matrix.
 
    ldc - Supplies the number of elements per row of the output matrix.
 
Return Value:
 
    None.
 
--*/
{
#if defined(MLAS_TARGET_RISCV64) && defined(MLAS_USE_RVV)
    // The platform routine covers the element-wise kinds and returns false for the
    // rest, which fall through to the switch below.
 
    if (GetMlasPlatform().ActivationRoutine != nullptr &&
        GetMlasPlatform().ActivationRoutine(Activation, Buffer, Bias, M, N, ldc)) {
        return;
    }
#endif
 
    switch (Activation->ActivationKind) {
 
        case MlasIdentityActivation:
        {
            MlasActivationKernel<MlasIdentityActivation>(Activation, Buffer, Bias, M, N, ldc);
            break;
        }
 
        case MlasReluActivation:
        {
            MlasActivationKernel<MlasReluActivation>(Activation, Buffer, Bias, M, N, ldc);
            break;
        }
 
        case MlasLeakyReluActivation:
        {
            MlasActivationKernel<MlasLeakyReluActivation>(Activation, Buffer, Bias, M, N, ldc);
            break;
        }
 
        case MlasTanhActivation:
        {
            if (Bias != nullptr) {
                MlasActivationKernel<MlasIdentityActivation, true>(Activation, Buffer, Bias, M, N, ldc);
            }
 
            if (N == ldc) {
                MlasComputeTanh(Buffer, Buffer, M * N);
            } else {
                while (M-- > 0) {
                    MlasComputeTanh(Buffer, Buffer, N);
                    Buffer += ldc;
                }
            }
 
            break;
        }
 
        case MlasLogisticActivation:
        {
            if (Bias != nullptr) {
                MlasActivationKernel<MlasIdentityActivation, true>(Activation, Buffer, Bias, M, N, ldc);
            }
 
            if (N == ldc) {
                MlasComputeLogistic(Buffer, Buffer, M * N);
            } else {
                while (M-- > 0) {
                    MlasComputeLogistic(Buffer, Buffer, N);
                    Buffer += ldc;
                }
            }
 
            break;
        }
 
        case MlasClipActivation:
        {
            MlasActivationKernel<MlasClipActivation>(Activation, Buffer, Bias, M, N, ldc);
            break;
        }
 
        case MlasHardSigmoidActivation:
        {
            MlasActivationKernel<MlasHardSigmoidActivation>(Activation, Buffer, Bias, M, N, ldc);
            break;
        }
 
        case MlasHardSwishActivation:
        {
            MlasActivationKernel<MlasHardSwishActivation>(Activation, Buffer, Bias, M, N, ldc);
            break;
        }
 
        case MlasActivationKindCount:
        {
            MLAS_THROW_EX(std::runtime_error, "bad mlas activation kind");
            break;
        }
    }
}