Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    gelu.cpp
 
Abstract:
 
    This module implements routines to compute the exact Gelu function.
 
--*/
 
#include <numbers>
 
#include "mlasi.h"
 
void
MLASCALL
MlasGeluErfKernel(
    const float* Input,
    float* Output,
    size_t N
    )
{
    // This kernel is not buffer alias safe because it is implemented in
    // multiple passes: first scale Input into Output, then apply erf in place,
    // and finally combine that intermediate with the original Input values.
    // Callers must guarantee that Input and Output do not overlap (see mlas.h for aliasing requirements).
    for (size_t i = 0; i < N; ++i) {
        Output[i] = Input[i] * (1.0f / std::numbers::sqrt2_v<float>);
    }
 
    MlasComputeErf(Output, Output, N);
 
    for (size_t i = 0; i < N; ++i) {
        Output[i] = 0.5f * Input[i] * (Output[i] + 1.0f);
    }
}
 
void
MLASCALL
MlasComputeGeluErf(
    const float* Input,
    float* Output,
    size_t N
    )
{
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_RISCV64)
    // TODO: Add an intermediate fused AVX2/FMA3 GELU(erf) path on AMD64.
    // Today the dispatch jumps from the generic multi-pass implementation to
    // AVX512F, so non-AVX512 x64 machines fall back to the generic kernel.
    GetMlasPlatform().GeluErfKernelRoutine(Input, Output, N);
#else
    MlasGeluErfKernel(Input, Output, N);
#endif
}
 
void
MLASCALL
MlasComputeFP16Gelu(const MLAS_FP16* input,
                    MLAS_FP16* output,
                    MLAS_FP16* temp,
                    size_t count,
                    MLAS_GELU_ALGORITHM algo)
{
    if(GetMlasPlatform().GeluFP16KernelRoutine){
        GetMlasPlatform().GeluFP16KernelRoutine(input, output, temp, count, algo);
        return;
    }
    MLAS_UNREFERENCED_PARAMETER(temp); // 'temp' is only used by vectorized kernel implementations and it is unused in the scalar fallback path.
    for (size_t i = 0; i < count; ++i) {
        float x = static_cast<float>(input[i]);
        float gelu_val;
 
        if (algo == MlasGeluTanh) {
            // GELU approximation (tanh)
            const float B = 0.7978845608f;
            const float C = 0.044715f * B;
            float tanh_arg = x * (B + C * x * x);
            float tanh_res = std::tanh(tanh_arg);
            gelu_val = 0.5f * x * (1.0f + tanh_res);
        } else {
            // GELU exact (erf)
            gelu_val = 0.5f * x *
                (1.0f + std::erf(x * (1.0f / std::numbers::sqrt2_v<float>)));
        }
 
        output[i] = MLAS_FP16(gelu_val);
    }
}