Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    dequantize.cpp
 
Abstract:
 
    This module implements routines to dequantize buffers.
 
    The dequantization formula as specified in the ONNX operator documentation is:
 
        Output = (Input - ZeroPoint) * Scale
 
--*/
 
#include "mlasi.h"
 
//
// DequantizeLinear reference implementation using the C++ runtime.
//
 
template<typename InputType>
static
MLAS_FORCEINLINE
void
MlasDequantizeLinearRefImpl(
    const InputType* Input,
    float* Output,
    size_t N,
    float Scale,
    InputType ZeroPoint
    )
/*++
 
Routine Description:
 
    This routine quantizes the input buffer using the supplied quantization
    parameters.
 
Arguments:
 
    Input - Supplies the input buffer with quantized data.
 
    Output - Supplies the output buffer.
 
    N - Supplies the number of elements to process.
 
    Scale - Supplies the quantization scale.
 
    ZeroPoint - Supplies the quantization zero point value.
 
Return Value:
 
    None.
 
--*/
{
    int32_t ZeroPointS32 = static_cast<int32_t>(ZeroPoint);
 
    for (size_t n = 0; n < N; n++) {
        Output[n] = static_cast<float>(static_cast<int32_t>(Input[n]) - ZeroPointS32) * Scale;
    }
}
 
#if defined(MLAS_SSE2_INTRINSICS)
// Implementation for Intel SSE 2. Refer to the Intel Intrisics Guide:
// https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html
 
void
MLASCALL
MlasDequantizeLinearS8Kernel(
    const int8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    int8_t ZeroPoint
    )
{
    const __m128 ScaleVector = MlasBroadcastFloat32x4(Scale);
    const __m128i ZeroPointS16Vector = _mm_set1_epi16(static_cast<int16_t>(ZeroPoint)); // Broadcast zp to 8 int16s
    const __m128i Zeros = _mm_setzero_si128();
 
    while (N >= 16) {
        // Load a vector of 16 int8s: [0 ... 15]
        __m128i VectorS8 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(Input));
 
        // Sign-extend into 2 vectors of 8 int16s
        __m128i SignMaskS8 = _mm_cmpgt_epi8(Zeros, VectorS8); // 0xFF for every negative byte in VectorS8
        __m128i VectorS16_0 = _mm_unpacklo_epi8(VectorS8, SignMaskS8); // [0 ... 7]
        __m128i VectorS16_1 = _mm_unpackhi_epi8(VectorS8, SignMaskS8); // [8 ... 15]
 
        // Subtract the zero-points in int16 domain.
        VectorS16_0 = _mm_sub_epi16(VectorS16_0, ZeroPointS16Vector);
        VectorS16_1 = _mm_sub_epi16(VectorS16_1, ZeroPointS16Vector);
 
        // Sign-extend into 4 vectors of 4 int32s
        __m128i SignMaskS16_0 = _mm_cmpgt_epi16(Zeros, VectorS16_0);
        __m128i VectorS32_0 = _mm_unpacklo_epi16(VectorS16_0, SignMaskS16_0); // [0 ... 3]
        __m128i VectorS32_1 = _mm_unpackhi_epi16(VectorS16_0, SignMaskS16_0); // [4 ... 7]
 
        __m128i SignMaskS16_1 = _mm_cmpgt_epi16(Zeros, VectorS16_1);
        __m128i VectorS32_2 = _mm_unpacklo_epi16(VectorS16_1, SignMaskS16_1); // [8 ... 11]
        __m128i VectorS32_3 = _mm_unpackhi_epi16(VectorS16_1, SignMaskS16_1); // [12 ... 15]
 
        // Cast each int32x4 to float and multiply by the scale vector.
        __m128 VectorF32_0 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_0), ScaleVector);
        __m128 VectorF32_1 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_1), ScaleVector);
        __m128 VectorF32_2 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_2), ScaleVector);
        __m128 VectorF32_3 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_3), ScaleVector);
 
        // Store each int32x4 into the output.
        _mm_storeu_ps(Output + 0, VectorF32_0);
        _mm_storeu_ps(Output + 4, VectorF32_1);
        _mm_storeu_ps(Output + 8, VectorF32_2);
        _mm_storeu_ps(Output + 12, VectorF32_3);
 
        Input += 16;
        Output += 16;
        N -= 16;
    }
 
    // Handle leftover elements (< 16) with the scalar reference implementation.
    MlasDequantizeLinearRefImpl(Input, Output, N, Scale, ZeroPoint);
}
 
void
MLASCALL
MlasDequantizeLinearU8Kernel(
    const uint8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    uint8_t ZeroPoint
    )
{
    const __m128 ScaleVector = MlasBroadcastFloat32x4(Scale);
    const __m128i ZeroPointS16Vector = _mm_set1_epi16(static_cast<int16_t>(ZeroPoint)); // Broadcast zp to 8 int16s
    const __m128i Zeros = _mm_setzero_si128();
 
    while (N >= 16) {
        // Load a vector of 16 uint8s: [0 ... 15]
        __m128i VectorU8 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(Input));
 
        // Zero-extend into 2 vectors of 8 uint16s
        __m128i VectorU16_0 = _mm_unpacklo_epi8(VectorU8, Zeros); // [0 ... 7]
        __m128i VectorU16_1 = _mm_unpackhi_epi8(VectorU8, Zeros); // [8 ... 15]
 
        // Subtract the zero-points as uint16s. Due to two's compliment, negative results can be reinterpreted as int16
        __m128i VectorS16_0 = _mm_sub_epi16(VectorU16_0, ZeroPointS16Vector);
        __m128i VectorS16_1 = _mm_sub_epi16(VectorU16_1, ZeroPointS16Vector);
 
        // Sign-extend into 4 vectors of 4 int32s
        __m128i SignMaskS16_0 = _mm_cmpgt_epi16(Zeros, VectorS16_0);
        __m128i VectorS32_0 = _mm_unpacklo_epi16(VectorS16_0, SignMaskS16_0); // [0 ... 3]
        __m128i VectorS32_1 = _mm_unpackhi_epi16(VectorS16_0, SignMaskS16_0); // [4 ... 7]
 
        __m128i SignMaskS16_1 = _mm_cmpgt_epi16(Zeros, VectorS16_1);
        __m128i VectorS32_2 = _mm_unpacklo_epi16(VectorS16_1, SignMaskS16_1); // [8 ... 11]
        __m128i VectorS32_3 = _mm_unpackhi_epi16(VectorS16_1, SignMaskS16_1); // [12 ... 15]
 
        // Cast each int32x4 to float and multiply by the scale vector.
        __m128 VectorF32_0 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_0), ScaleVector);
        __m128 VectorF32_1 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_1), ScaleVector);
        __m128 VectorF32_2 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_2), ScaleVector);
        __m128 VectorF32_3 = _mm_mul_ps(_mm_cvtepi32_ps(VectorS32_3), ScaleVector);
 
        // Store each int32x4 into the output.
        _mm_storeu_ps(Output + 0, VectorF32_0);
        _mm_storeu_ps(Output + 4, VectorF32_1);
        _mm_storeu_ps(Output + 8, VectorF32_2);
        _mm_storeu_ps(Output + 12, VectorF32_3);
 
        Input += 16;
        Output += 16;
        N -= 16;
    }
 
    // Handle leftover elements (< 16) with the scalar reference implementation.
    MlasDequantizeLinearRefImpl(Input, Output, N, Scale, ZeroPoint);
}
 
template<>
void
MLASCALL
MlasDequantizeLinear<int8_t>(
    const int8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    int8_t ZeroPoint
    )
{
#if defined(MLAS_TARGET_AMD64)
    GetMlasPlatform().DequantizeLinearS8Kernel(
#else
    MlasDequantizeLinearS8Kernel(
#endif
        Input, Output, N, Scale, ZeroPoint);
}
 
template<>
void
MLASCALL
MlasDequantizeLinear<uint8_t>(
    const uint8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    uint8_t ZeroPoint
    )
{
#if defined(MLAS_TARGET_AMD64)
    GetMlasPlatform().DequantizeLinearU8Kernel(
#else
    MlasDequantizeLinearU8Kernel(
#endif
        Input, Output, N, Scale, ZeroPoint);
}
#elif defined(MLAS_NEON64_INTRINSICS)
// Implementation for ARM64 NEON. Refer to the ARM instrinsics guide:
// https://developer.arm.com/architectures/instruction-sets/intrinsics/
 
void
MLASCALL
MlasDequantizeLinearS8Kernel(
    const int8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    int8_t ZeroPoint
    )
{
    const float32x4_t ScaleVector = MlasBroadcastFloat32x4(Scale);
    const int16x8_t ZeroPointVector = vdupq_n_s16(ZeroPoint); // Broadcast ZeroPoint (sign-extended to 16bits)
 
    while (N >= 16) {
        // Load a vector of 16 int8s: [0 ... 15]
        int8x16_t VectorS8 = vld1q_s8(Input);
 
        // Sign-extend into 2 vectors of 8 int16s
        int16x8_t VectorS16_0 = vmovl_s8(vget_low_s8(VectorS8));  // [0 ... 7]
        int16x8_t VectorS16_1 = vmovl_s8(vget_high_s8(VectorS8)); // [8 ... 15]
 
        // Subtract the zero-points in int16 domain.
        VectorS16_0 = vsubq_s16(VectorS16_0, ZeroPointVector);
        VectorS16_1 = vsubq_s16(VectorS16_1, ZeroPointVector);
 
        // Sign-extend into 4 vectors of 4 int32s
        int32x4_t VectorS32_0 = vmovl_s16(vget_low_s16(VectorS16_0));  // [0 ... 3]
        int32x4_t VectorS32_1 = vmovl_s16(vget_high_s16(VectorS16_0)); // [4 ... 7]
        int32x4_t VectorS32_2 = vmovl_s16(vget_low_s16(VectorS16_1));  // [8 ... 11]
        int32x4_t VectorS32_3 = vmovl_s16(vget_high_s16(VectorS16_1)); // [12 ... 15]
 
        // Cast each int32x4 to float and multiply by the scale vector.
        float32x4_t VectorF32_0 = vmulq_f32(vcvtq_f32_s32(VectorS32_0), ScaleVector);
        float32x4_t VectorF32_1 = vmulq_f32(vcvtq_f32_s32(VectorS32_1), ScaleVector);
        float32x4_t VectorF32_2 = vmulq_f32(vcvtq_f32_s32(VectorS32_2), ScaleVector);
        float32x4_t VectorF32_3 = vmulq_f32(vcvtq_f32_s32(VectorS32_3), ScaleVector);
 
        // Store each int32x4 into the output.
        vst1q_f32(Output + 0, VectorF32_0);
        vst1q_f32(Output + 4, VectorF32_1);
        vst1q_f32(Output + 8, VectorF32_2);
        vst1q_f32(Output + 12, VectorF32_3);
 
        N -= 16;
        Input += 16;
        Output += 16;
    }
 
    // Handle leftover elements (< 16) with the scalar reference implementation.
    MlasDequantizeLinearRefImpl(Input, Output, N, Scale, ZeroPoint);
}
 
void
MLASCALL
MlasDequantizeLinearU8Kernel(
    const uint8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    uint8_t ZeroPoint
    )
{
    const float32x4_t ScaleVector = MlasBroadcastFloat32x4(Scale);
    const uint8x8_t ZeroPointVector = vdup_n_u8(ZeroPoint); // Broadcast ZeroPoint to 8 uint8s
 
    while (N >= 16) {
        // Load a vector of 16 uint8s: [0 ... 15]
        uint8x16_t VectorU8 = vld1q_u8(Input);
 
        // Subtract zero-point. The vsubl_u8 instruction zero-extends its arguments to uint16 first.
        // The reinterpret from uint16x8 to int16x8 is actually a NOP.
        int16x8_t VectorS16_0 = vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(VectorU8), ZeroPointVector));  // [0 ... 7]
        int16x8_t VectorS16_1 = vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(VectorU8), ZeroPointVector)); // [8 ... 15]
 
        // Sign-extend into 4 vectors of 4 int32s
        int32x4_t VectorS32_0 = vmovl_s16(vget_low_s16(VectorS16_0));  // [0 ... 3]
        int32x4_t VectorS32_1 = vmovl_s16(vget_high_s16(VectorS16_0)); // [4 ... 7]
        int32x4_t VectorS32_2 = vmovl_s16(vget_low_s16(VectorS16_1));  // [8 ... 11]
        int32x4_t VectorS32_3 = vmovl_s16(vget_high_s16(VectorS16_1)); // [12 ... 15]
 
        // Cast each int32x4 to float and multiply by the scale vector.
        float32x4_t VectorF32_0 = vmulq_f32(vcvtq_f32_s32(VectorS32_0), ScaleVector);
        float32x4_t VectorF32_1 = vmulq_f32(vcvtq_f32_s32(VectorS32_1), ScaleVector);
        float32x4_t VectorF32_2 = vmulq_f32(vcvtq_f32_s32(VectorS32_2), ScaleVector);
        float32x4_t VectorF32_3 = vmulq_f32(vcvtq_f32_s32(VectorS32_3), ScaleVector);
 
        // Store each int32x4 into the output.
        vst1q_f32(Output + 0, VectorF32_0);
        vst1q_f32(Output + 4, VectorF32_1);
        vst1q_f32(Output + 8, VectorF32_2);
        vst1q_f32(Output + 12, VectorF32_3);
 
        N -= 16;
        Input += 16;
        Output += 16;
    }
 
    // Handle leftover elements (< 16) with the scalar reference implementation.
    MlasDequantizeLinearRefImpl(Input, Output, N, Scale, ZeroPoint);
}
 
template<>
void
MLASCALL
MlasDequantizeLinear<int8_t>(
    const int8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    int8_t ZeroPoint
    )
{
    MlasDequantizeLinearS8Kernel(Input, Output, N, Scale, ZeroPoint);
}
 
template<>
void
MLASCALL
MlasDequantizeLinear<uint8_t>(
    const uint8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    uint8_t ZeroPoint
    )
{
    MlasDequantizeLinearU8Kernel(Input, Output, N, Scale, ZeroPoint);
}
#else
// Implementation that uses the scalar reference implementation.
 
template<typename InputType>
void
MLASCALL
MlasDequantizeLinear(
    const InputType* Input,
    float* Output,
    size_t N,
    float Scale,
    InputType ZeroPoint
    )
{
    MlasDequantizeLinearRefImpl(Input, Output, N, Scale, ZeroPoint);
}
 
template
void
MLASCALL
MlasDequantizeLinear<int8_t>(
    const int8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    int8_t ZeroPoint
    );
 
template
void
MLASCALL
MlasDequantizeLinear<uint8_t>(
    const uint8_t* Input,
    float* Output,
    size_t N,
    float Scale,
    uint8_t ZeroPoint
    );
 
#endif