Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    qpostprocessor.cpp
 
Abstract:
 
    This module implements the post processor for QGEMM.
 
--*/
 
#include "mlasi.h"
 
void MLAS_QGEMM_SCALE_BIAS_OUTPUT_PROCESSOR::Process(
    const int32_t* C,
    size_t StartM,
    size_t StartN,
    size_t CountM,
    size_t CountN,
    size_t ldc
    ) const
{
    if (Bias_) {
        if (QuantGran_ == MLAS_QUANTIZATION_GRANULARITY::PerColumn) {
            if (OutputMode_ == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
                ProcessImpl<true, MLAS_QGEMM_OUTPUT_MODE::AccumulateMode, MLAS_QUANTIZATION_GRANULARITY::PerColumn>(
                    C,
                    StartM,
                    StartN,
                    CountM,
                    CountN,
                    ldc);
            } else {
                ProcessImpl<true, MLAS_QGEMM_OUTPUT_MODE::ZeroMode, MLAS_QUANTIZATION_GRANULARITY::PerColumn>(
                    C,
                    StartM,
                    StartN,
                    CountM,
                    CountN,
                    ldc);
            }
        } else if (OutputMode_ == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
            ProcessImpl<true, MLAS_QGEMM_OUTPUT_MODE::AccumulateMode, MLAS_QUANTIZATION_GRANULARITY::PerMatrix>(
                C,
                StartM,
                StartN,
                CountM,
                CountN,
                ldc);
        } else {
            ProcessImpl<true, MLAS_QGEMM_OUTPUT_MODE::ZeroMode, MLAS_QUANTIZATION_GRANULARITY::PerMatrix>(
                C,
                StartM,
                StartN,
                CountM,
                CountN,
                ldc);
        }
    } else {
        if (QuantGran_ == MLAS_QUANTIZATION_GRANULARITY::PerColumn) {
            if (OutputMode_ == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
                ProcessImpl<false, MLAS_QGEMM_OUTPUT_MODE::AccumulateMode, MLAS_QUANTIZATION_GRANULARITY::PerColumn>(
                    C,
                    StartM,
                    StartN,
                    CountM,
                    CountN,
                    ldc);
            } else {
                ProcessImpl<false, MLAS_QGEMM_OUTPUT_MODE::ZeroMode, MLAS_QUANTIZATION_GRANULARITY::PerColumn>(
                    C,
                    StartM,
                    StartN,
                    CountM,
                    CountN,
                    ldc);
            }
        } else if (OutputMode_ == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
            ProcessImpl<false, MLAS_QGEMM_OUTPUT_MODE::AccumulateMode, MLAS_QUANTIZATION_GRANULARITY::PerMatrix>(
                C,
                StartM,
                StartN,
                CountM,
                CountN,
                ldc);
        } else {
            ProcessImpl<false, MLAS_QGEMM_OUTPUT_MODE::ZeroMode, MLAS_QUANTIZATION_GRANULARITY::PerMatrix>(
                C,
                StartM,
                StartN,
                CountM,
                CountN,
                ldc);
        }
    }
}
 
template<bool HasBias, MLAS_QGEMM_OUTPUT_MODE Mode, MLAS_QUANTIZATION_GRANULARITY QuantGran>
inline
void
MLAS_QGEMM_SCALE_BIAS_OUTPUT_PROCESSOR::ProcessImpl(
    const int32_t* C,
    size_t StartM,
    size_t StartN,
    size_t CountM,
    size_t CountN,
    size_t ldc) const
/*++
 
Routine Description:
 
    This routine converts the output matrix C to a floating point format using
    the stored scale and bias parameters.
 
Arguments:
 
    C - Supplies the address of matrix C.
 
    StartM - Supplies the starting row offset relative to the matrix.
 
    StartN - Supplies the starting column offset relative to the matrix.
 
    CountM - Supplies the number of rows of the output matrix to process.
 
    CountN - Supplies the number of columns of the output matrix to process.
 
    ldc - Supplies the leading dimension of C.
 
Return Value:
 
    None.
 
--*/
{
    float* Output = Output_;
    const float* Bias = Bias_;
    const float* Scale = Scale_;
 
    if (HasBias) {
        Bias += StartN;
    }
 
    if(QuantGran == MLAS_QUANTIZATION_GRANULARITY::PerColumn){
        Scale += StartN;
    }
 
    MLAS_FLOAT32X4 ScaleVector = MlasBroadcastFloat32x4(Scale_);
#if !defined(MLAS_SSE2_INTRINSICS)
    float ScaleValue = MlasExtractLaneFloat32x4<0>(ScaleVector);
#endif
 
    C += StartM * ldc + StartN;
    Output += StartM * LeadingDimensionOutput_ + StartN;
 
 
    while (CountM-- > 0) {
 
        float* c_out = Output;
        const int32_t* c = C;
        const float* bias = Bias;
        const float* scale = Scale;
 
        size_t n = CountN;
 
        while (n >= 4) {
 
            MLAS_FLOAT32X4 FloatVector = MlasCastToFloat32x4(MlasLoadInt32x4(c));
 
            if (QuantGran == MLAS_QUANTIZATION_GRANULARITY::PerColumn) {
                ScaleVector = MlasLoadFloat32x4(scale);
                scale += 4;
            }
 
            if (Mode == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
                FloatVector = MlasMultiplyAddFloat32x4(FloatVector, ScaleVector, MlasLoadFloat32x4(c_out));
            } else {
                FloatVector = MlasMultiplyFloat32x4(FloatVector, ScaleVector);
            }
 
            if (HasBias) {
                FloatVector = MlasAddFloat32x4(FloatVector, MlasLoadFloat32x4(bias));
                bias += 4;
            }
 
            MlasStoreFloat32x4(c_out, FloatVector);
 
            c_out += 4;
            c += 4;
            n -= 4;
        }
 
        for (size_t offset = 0; offset < n; offset++) {
 
#if defined(MLAS_SSE2_INTRINSICS)
            __m128 FloatVector = _mm_set_ss(float(c[offset]));
 
            if (QuantGran == MLAS_QUANTIZATION_GRANULARITY::PerColumn) {
                ScaleVector = _mm_load_ss(&scale[offset]);
            }
 
            if (Mode == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
                FloatVector = _mm_add_ps(_mm_mul_ss(FloatVector, ScaleVector), _mm_load_ss(&c_out[offset]));
            } else {
                FloatVector = _mm_mul_ss(FloatVector, ScaleVector);
            }
 
            if (HasBias) {
                FloatVector = _mm_add_ss(FloatVector, _mm_load_ss(&bias[offset]));
            }
 
            _mm_store_ss(&c_out[offset], FloatVector);
#else
            if (QuantGran == MLAS_QUANTIZATION_GRANULARITY::PerColumn) {
                ScaleValue = scale[offset];
            }
 
            float result = float(c[offset]) * ScaleValue;
            if (HasBias) {
                result += bias[offset];
            }
 
            if (Mode == MLAS_QGEMM_OUTPUT_MODE::AccumulateMode) {
                c_out[offset] += result;
            } else {
                c_out[offset] = result;
            }
#endif
        }
 
        C += ldc;
        Output += LeadingDimensionOutput_;
    }
}