Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    tanh.cpp
 
Abstract:
 
    This module implements routines to compute the hyperbolic tangent function.
 
    This implementation uses the same polynomial coefficients and algorithm as
    found in Eigen. 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 floating point constants for use by kernels written in assembly.
//
 
MLAS_INTERNAL_DATA const MLAS_TANH_CONSTANTS MlasTanhConstants = {
    -9.0f,
    9.0f,
    -2.76076847742355e-16f,
    2.00018790482477e-13f,
    -8.60467152213735e-11f,
    5.12229709037114e-08f,
    1.48572235717979e-05f,
    6.37261928875436e-04f,
    4.89352455891786e-03f,
    1.19825839466702e-06f,
    1.18534705686654e-04f,
    2.26843463243900e-03f,
    4.89352518554385e-03f,
};
 
void
MLASCALL
MlasTanhKernel(
    const float* Input,
    float* Output,
    size_t N
    )
/*++
 
Routine Description:
 
    This routine implements the generic kernel for the hyperbolic tangent 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 >= 4) {
 
        MLAS_FLOAT32X4 Value = MlasLoadFloat32x4(Input);
 
        Value = MlasMaximumFloat32x4(MlasBroadcastFloat32x4(MlasTanhConstants.LowerRange), Value);
        Value = MlasMinimumFloat32x4(MlasBroadcastFloat32x4(MlasTanhConstants.UpperRange), Value);
 
        MLAS_FLOAT32X4 ValueSquared = MlasMultiplyFloat32x4(Value, Value);
 
        MLAS_FLOAT32X4 p;
        p = MlasMultiplyAddFloat32x4(ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.alpha_13),
            MlasBroadcastFloat32x4(MlasTanhConstants.alpha_11));
        p = MlasMultiplyAddFloat32x4(p, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.alpha_9));
        p = MlasMultiplyAddFloat32x4(p, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.alpha_7));
        p = MlasMultiplyAddFloat32x4(p, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.alpha_5));
        p = MlasMultiplyAddFloat32x4(p, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.alpha_3));
        p = MlasMultiplyAddFloat32x4(p, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.alpha_1));
        p = MlasMultiplyFloat32x4(p, Value);
 
        MLAS_FLOAT32X4 q;
        q = MlasMultiplyAddFloat32x4(ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.beta_6),
            MlasBroadcastFloat32x4(MlasTanhConstants.beta_4));
        q = MlasMultiplyAddFloat32x4(q, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.beta_2));
        q = MlasMultiplyAddFloat32x4(q, ValueSquared, MlasBroadcastFloat32x4(MlasTanhConstants.beta_0));
 
        MlasStoreFloat32x4(Output, MlasDivideFloat32x4(p, q));
 
        Input += 4;
        Output += 4;
        N -= 4;
    }
 
    while (N > 0) {
 
        float Value = *Input++;
 
        // This odd two-step process exists to ensure an input value of NaN carries through
        // without modification because "std::min" and "std::max" return unreliable results
        // when NaNs are involved, and it's clear from the test's reference outputs that
        // they want a NaN on output whenever the input is a NaN.
        float v_tmp;
        v_tmp = (Value < MlasTanhConstants.LowerRange) ? MlasTanhConstants.LowerRange : Value;
        Value = (v_tmp > MlasTanhConstants.UpperRange) ? MlasTanhConstants.UpperRange : v_tmp;
 
        float ValueSquared = Value * Value;
 
        float p;
        p = ValueSquared * MlasTanhConstants.alpha_13 + MlasTanhConstants.alpha_11;
        p = p * ValueSquared + MlasTanhConstants.alpha_9;
        p = p * ValueSquared + MlasTanhConstants.alpha_7;
        p = p * ValueSquared + MlasTanhConstants.alpha_5;
        p = p * ValueSquared + MlasTanhConstants.alpha_3;
        p = p * ValueSquared + MlasTanhConstants.alpha_1;
        p = p * Value;
 
        float q;
        q = ValueSquared * MlasTanhConstants.beta_6 + MlasTanhConstants.beta_4;
        q = q * ValueSquared + MlasTanhConstants.beta_2;
        q = q * ValueSquared + MlasTanhConstants.beta_0;
 
        *Output++ = (p / q);
 
        N -= 1;
    }
}
 
template <>
void
MLASCALL
MlasComputeTanh<float>(
    const float* Input,
    float* Output,
    size_t N
    )
/*++
 
Routine Description:
 
    This routine computes the hyperbolic tangent function.
 
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().TanhKernelRoutine(Input, Output, N);
#else
    MlasTanhKernel(Input, Output, N);
#endif
}
 
template <>
void
MLASCALL
MlasComputeTanh<MLAS_FP16>(
    const MLAS_FP16* Input,
    MLAS_FP16* Output,
    size_t N
) {
    if(GetMlasPlatform().TanhFP16KernelRoutine){
        GetMlasPlatform().TanhFP16KernelRoutine(Input, Output, N);
        return;
    }
    const auto* dispatch = GetMlasPlatform().SoftmaxDispatch;
    if (dispatch == nullptr || dispatch->Tanh_Fp16 == nullptr) {
        MLAS_THROW_EX(std::runtime_error, "Tanh_Fp16 is not supported.");
    }
    dispatch->Tanh_Fp16(Input, Output, N);
}
 
template <>
void
MLASCALL
MlasComputeSoftcap<float>(
    const float* Input,
    float* Output,
    size_t N,
    float cap
) {
    for (size_t i = 0; i < N; i++) {
        Output[i] = Input[i] / cap;
        Output[i] = std::tanh(Output[i]);
        Output[i] = Output[i] * cap;
    }
}
 
template <>
void
MLASCALL
MlasComputeSoftcap<MLAS_FP16>(
    const MLAS_FP16* Input,
    MLAS_FP16* Output,
    size_t N,
    MLAS_FP16 cap
) {
    const auto* dispatch = GetMlasPlatform().SoftmaxDispatch;
    if (dispatch == nullptr || dispatch->Softcap_Fp16 == nullptr) {
        MLAS_THROW_EX(std::runtime_error, "Softcap_Fp16 is not supported.");
    }
    dispatch->Softcap_Fp16(Input, Output, N, cap);
}