Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    qkv_quant_kernel_neon.cpp
 
Abstract:
 
    ARM NEON optimized implementation of quantized KV-cache GEMM kernels
    for MlasQKGemm and MlasSVGemm. Dequantizes INT8/INT4 B on the fly and
    accumulates in FP32 using 128-bit NEON vectors.
 
--*/
 
#include "qkv_quant_kernel.h"
#include "qkv_quant_common.h"
#include "mlas_qkv_quant.h"
 
#include <arm_neon.h>
#include <cstring>
#include <memory>
 
using namespace MlasKVQuantInternal;
 
namespace {
 
//
// Dequantize 8 INT8 values starting at `col`.
// Per-channel rows are always scaled. Per-tensor rows may defer scaling.
// Produces two float32x4_t (8 floats total) stored into dst.
//
inline void
DequantInt8x8_Neon(const int8_t* src, size_t col, bool per_channel,
                   const float* scales, bool apply_per_tensor_scale, float* dst)
{
    // Load 8 int8 values
    int8x8_t raw = vld1_s8(src + col);
    // Widen to int16
    int16x8_t i16 = vmovl_s8(raw);
    // Widen to int32 (low and high halves)
    int32x4_t i32_lo = vmovl_s16(vget_low_s16(i16));
    int32x4_t i32_hi = vmovl_s16(vget_high_s16(i16));
    // Convert to float
    float32x4_t f_lo = vcvtq_f32_s32(i32_lo);
    float32x4_t f_hi = vcvtq_f32_s32(i32_hi);
 
    if (per_channel) {
        float32x4_t sc_lo = vld1q_f32(scales + col);
        float32x4_t sc_hi = vld1q_f32(scales + col + 4);
        f_lo = vmulq_f32(f_lo, sc_lo);
        f_hi = vmulq_f32(f_hi, sc_hi);
    } else if (apply_per_tensor_scale) {
        float32x4_t sc = vdupq_n_f32(scales[0]);
        f_lo = vmulq_f32(f_lo, sc);
        f_hi = vmulq_f32(f_hi, sc);
    }
 
    vst1q_f32(dst, f_lo);
    vst1q_f32(dst + 4, f_hi);
}
 
//
// Dequantize 8 INT4 values (4 packed bytes) starting at even column `col`.
// Per-channel rows are always scaled. Per-tensor rows may defer scaling.
//
inline void
DequantInt4x8_Neon(const uint8_t* src, size_t col, bool per_channel,
                   const float* scales, bool apply_per_tensor_scale, float* dst)
{
    const uint8_t* base = src + col / 2;
 
    // Extract 8 nibbles from 4 bytes
    alignas(8) int8_t nibbles[8];
    nibbles[0] = static_cast<int8_t>((base[0] & 0x0F) - kInt4Bias);
    nibbles[1] = static_cast<int8_t>(((base[0] >> 4) & 0x0F) - kInt4Bias);
    nibbles[2] = static_cast<int8_t>((base[1] & 0x0F) - kInt4Bias);
    nibbles[3] = static_cast<int8_t>(((base[1] >> 4) & 0x0F) - kInt4Bias);
    nibbles[4] = static_cast<int8_t>((base[2] & 0x0F) - kInt4Bias);
    nibbles[5] = static_cast<int8_t>(((base[2] >> 4) & 0x0F) - kInt4Bias);
    nibbles[6] = static_cast<int8_t>((base[3] & 0x0F) - kInt4Bias);
    nibbles[7] = static_cast<int8_t>(((base[3] >> 4) & 0x0F) - kInt4Bias);
 
    int8x8_t raw = vld1_s8(nibbles);
    int16x8_t i16 = vmovl_s8(raw);
    int32x4_t i32_lo = vmovl_s16(vget_low_s16(i16));
    int32x4_t i32_hi = vmovl_s16(vget_high_s16(i16));
    float32x4_t f_lo = vcvtq_f32_s32(i32_lo);
    float32x4_t f_hi = vcvtq_f32_s32(i32_hi);
 
    if (per_channel) {
        float32x4_t sc_lo = vld1q_f32(scales + col);
        float32x4_t sc_hi = vld1q_f32(scales + col + 4);
        f_lo = vmulq_f32(f_lo, sc_lo);
        f_hi = vmulq_f32(f_hi, sc_hi);
    } else if (apply_per_tensor_scale) {
        float32x4_t sc = vdupq_n_f32(scales[0]);
        f_lo = vmulq_f32(f_lo, sc);
        f_hi = vmulq_f32(f_hi, sc);
    }
 
    vst1q_f32(dst, f_lo);
    vst1q_f32(dst + 4, f_hi);
}
 
//
// Dequantize one row of length `cols` from packed quantized buffer into `dst`.
// `apply_per_tensor_scale=false` leaves per-tensor rows unscaled so callers can
// factor the single scale out of an outer accumulation loop.
//
void
DequantRow_Neon(
    const void* src_raw,
    float* dst,
    size_t cols,
    MLAS_KV_QUANT_TYPE qt,
    const float* scales,
    bool apply_per_tensor_scale)
{
    const bool int4 = IsInt4Mode(qt);
    const bool per_channel = IsPerChannelMode(qt);
 
    size_t c = 0;
    const size_t vec_end = (cols / 8) * 8;
 
    if (!int4) {
        const auto* src = static_cast<const int8_t*>(src_raw);
        for (; c < vec_end; c += 8) {
            DequantInt8x8_Neon(src, c, per_channel, scales, apply_per_tensor_scale, dst + c);
        }
        for (; c < cols; ++c) {
            if (per_channel) {
                dst[c] = static_cast<float>(src[c]) * scales[c];
            } else if (apply_per_tensor_scale) {
                dst[c] = static_cast<float>(src[c]) * scales[0];
            } else {
                dst[c] = static_cast<float>(src[c]);
            }
        }
    } else {
        const auto* src = static_cast<const uint8_t*>(src_raw);
        for (; c < vec_end; c += 8) {
            DequantInt4x8_Neon(src, c, per_channel, scales, apply_per_tensor_scale, dst + c);
        }
        for (; c < cols; ++c) {
            uint8_t packed = src[c / 2];
            int nibble = (c & 1) == 0 ? (packed & 0x0F) : ((packed >> 4) & 0x0F);
            if (per_channel) {
                dst[c] = static_cast<float>(nibble - kInt4Bias) * scales[c];
            } else if (apply_per_tensor_scale) {
                dst[c] = static_cast<float>(nibble - kInt4Bias) * scales[0];
            } else {
                dst[c] = static_cast<float>(nibble - kInt4Bias);
            }
        }
    }
}
 
//
// QKGemm:  C[M,N] = Alpha * A[M,K] * B^T[K,N]
//
void
QKGemm_Neon(
    size_t M,
    size_t N,
    size_t K,
    float Alpha,
    const float* A,
    size_t lda,
    const void* B,
    MLAS_KV_QUANT_TYPE QuantType,
    const float* Scales,
    float* C,
    size_t ldc)
{
    const size_t row_bytes = MlasKVQuantPackedRowBytes(QuantType, K);
    const auto* B_bytes = static_cast<const uint8_t*>(B);
 
    float b_stack[256];
    float* b_buf = b_stack;
    std::unique_ptr<float[]> heap_buf;
    if (K > 256) {
        heap_buf.reset(new float[K]);
        b_buf = heap_buf.get();
    }
 
    for (size_t n = 0; n < N; ++n) {
        const uint8_t* b_row = B_bytes + n * row_bytes;
        DequantRow_Neon(b_row, b_buf, K, QuantType, Scales, true);
 
        for (size_t m = 0; m < M; ++m) {
            const float* a_row = A + m * lda;
 
            float32x4_t acc0 = vdupq_n_f32(0.0f);
            float32x4_t acc1 = vdupq_n_f32(0.0f);
            float32x4_t acc2 = vdupq_n_f32(0.0f);
            float32x4_t acc3 = vdupq_n_f32(0.0f);
 
            size_t k = 0;
            const size_t vec_end = (K / 16) * 16;
 
            for (; k < vec_end; k += 16) {
                float32x4_t a0 = vld1q_f32(a_row + k);
                float32x4_t b0 = vld1q_f32(b_buf + k);
                acc0 = vfmaq_f32(acc0, a0, b0);
 
                float32x4_t a1 = vld1q_f32(a_row + k + 4);
                float32x4_t b1 = vld1q_f32(b_buf + k + 4);
                acc1 = vfmaq_f32(acc1, a1, b1);
 
                float32x4_t a2 = vld1q_f32(a_row + k + 8);
                float32x4_t b2 = vld1q_f32(b_buf + k + 8);
                acc2 = vfmaq_f32(acc2, a2, b2);
 
                float32x4_t a3 = vld1q_f32(a_row + k + 12);
                float32x4_t b3 = vld1q_f32(b_buf + k + 12);
                acc3 = vfmaq_f32(acc3, a3, b3);
            }
 
            // Process remaining 4-element chunks
            for (; k + 4 <= K; k += 4) {
                float32x4_t a0 = vld1q_f32(a_row + k);
                float32x4_t b0 = vld1q_f32(b_buf + k);
                acc0 = vfmaq_f32(acc0, a0, b0);
            }
 
            // Sum accumulators
            acc0 = vaddq_f32(acc0, acc1);
            acc2 = vaddq_f32(acc2, acc3);
            acc0 = vaddq_f32(acc0, acc2);
            float dot = vaddvq_f32(acc0);
 
            // Scalar tail
            for (; k < K; ++k) {
                dot += a_row[k] * b_buf[k];
            }
 
            C[m * ldc + n] = Alpha * dot;
        }
    }
}
 
void
QKGemmFp16_Neon(
    size_t M,
    size_t N,
    size_t K,
    float Alpha,
    const MLAS_FP16* A,
    size_t lda,
    const void* B,
    MLAS_KV_QUANT_TYPE QuantType,
    const float* Scales,
    float* C,
    size_t ldc)
{
    // NEON converts the FP16 query tile to FP32 once and reuses the FP32 kernel.
    // Decode (M == 1) fits the stack scratch; only wider multi-row prefill tiles
    // whose M * K exceeds it spill to the heap.
    const size_t a_count = M * K;
    float a_stack[256];
    float* a_buf = a_stack;
    std::unique_ptr<float[]> heap_buf;
    if (a_count > 256) {
        heap_buf = std::make_unique<float[]>(a_count);
        a_buf = heap_buf.get();
    }
 
    for (size_t m = 0; m < M; ++m) {
        MlasConvertHalfToFloatBuffer(A + m * lda, a_buf + m * K, K);
    }
    QKGemm_Neon(M, N, K, Alpha, a_buf, K, B, QuantType, Scales, C, ldc);
}
 
//
// SVGemm:  C[M,N] = Beta * C[M,N] + A[M,K] * B[K,N]
//
void
SVGemm_Neon(
    size_t M,
    size_t N,
    size_t K,
    const float* A,
    size_t lda,
    const void* B,
    MLAS_KV_QUANT_TYPE QuantType,
    const float* Scales,
    float* C,
    size_t ldc,
    float Beta)
{
    const size_t row_bytes = MlasKVQuantPackedRowBytes(QuantType, N);
    const auto* B_bytes = static_cast<const uint8_t*>(B);
    const bool per_channel = IsPerChannelMode(QuantType);
 
    float b_stack[256];
    float* b_buf = b_stack;
    std::unique_ptr<float[]> heap_buf;
    if (N > 256) {
        heap_buf.reset(new float[N]);
        b_buf = heap_buf.get();
    }
 
    const size_t vec_end_n = (N / 4) * 4;
 
    for (size_t m = 0; m < M; ++m) {
        float* c_row = C + m * ldc;
        const float* a_row = A + m * lda;
 
        // Initialize output
        if (Beta == 0.0f) {
            size_t n = 0;
            for (; n < vec_end_n; n += 4) {
                vst1q_f32(c_row + n, vdupq_n_f32(0.0f));
            }
            for (; n < N; ++n) {
                c_row[n] = 0.0f;
            }
        } else if (Beta != 1.0f) {
            float32x4_t beta_vec = vdupq_n_f32(Beta);
            size_t n = 0;
            for (; n < vec_end_n; n += 4) {
                float32x4_t c_vec = vld1q_f32(c_row + n);
                c_vec = vmulq_f32(c_vec, beta_vec);
                vst1q_f32(c_row + n, c_vec);
            }
            for (; n < N; ++n) {
                c_row[n] *= Beta;
            }
        }
 
        // When Beta != 0 and per-tensor, we must apply scale inline during
        // dequantization (can't defer scaling since C already has scaled values).
        const bool apply_scale_inline = per_channel || (Beta != 0.0f);
 
        for (size_t k = 0; k < K; ++k) {
            const uint8_t* b_row_packed = B_bytes + k * row_bytes;
            DequantRow_Neon(b_row_packed, b_buf, N, QuantType, Scales, apply_scale_inline);
 
            const float a_val = a_row[k];
            float32x4_t a_broadcast = vdupq_n_f32(a_val);
 
            size_t n = 0;
            for (; n < vec_end_n; n += 4) {
                float32x4_t c_vec = vld1q_f32(c_row + n);
                float32x4_t b_vec = vld1q_f32(b_buf + n);
                c_vec = vfmaq_f32(c_vec, a_broadcast, b_vec);
                vst1q_f32(c_row + n, c_vec);
            }
            for (; n < N; ++n) {
                c_row[n] += a_val * b_buf[n];
            }
        }
 
        if (!apply_scale_inline) {
            const float32x4_t scale_vec = vdupq_n_f32(Scales[0]);
            size_t n = 0;
            for (; n < vec_end_n; n += 4) {
                float32x4_t c_vec = vld1q_f32(c_row + n);
                c_vec = vmulq_f32(c_vec, scale_vec);
                vst1q_f32(c_row + n, c_vec);
            }
            for (; n < N; ++n) {
                c_row[n] *= Scales[0];
            }
        }
    }
}
 
void
SVGemmFp16_Neon(
    size_t M,
    size_t N,
    size_t K,
    const float* A,
    size_t lda,
    const void* B,
    MLAS_KV_QUANT_TYPE QuantType,
    const float* Scales,
    MLAS_FP16* C,
    size_t ldc,
    float Beta)
{
    // FP32 accumulation scratch for one output row (N == head_size). Stays on the
    // stack for typical head sizes and only spills to the heap when N > 256.
    float c_stack[256];
    float* c_buf = c_stack;
    std::unique_ptr<float[]> heap_buf;
    if (N > 256) {
        heap_buf = std::make_unique<float[]>(N);
        c_buf = heap_buf.get();
    }
 
    for (size_t m = 0; m < M; ++m) {
        MLAS_FP16* c_row = C + m * ldc;
        if (Beta != 0.0f) {
            MlasConvertHalfToFloatBuffer(c_row, c_buf, N);
        }
        SVGemm_Neon(1, N, K, A + m * lda, lda, B, QuantType, Scales, c_buf, N, Beta);
        MlasConvertFloatToHalfBuffer(c_buf, c_row, N);
    }
}
 
}  // namespace
 
const MLAS_KV_QUANT_GEMM_DISPATCH MlasKVQuantGemmDispatchNeon = []() {
    MLAS_KV_QUANT_GEMM_DISPATCH d;
    d.QKGemm = QKGemm_Neon;
    d.QKGemmFp16 = QKGemmFp16_Neon;
    d.SVGemm = SVGemm_Neon;
    d.SVGemmFp16 = SVGemmFp16_Neon;
    return d;
}();