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
| /*++
|
| Copyright (c) Microsoft Corporation. All rights reserved.
|
| Licensed under the MIT License.
|
| Module Name:
|
| qkv_quant_kernel.h
|
| Abstract:
|
| Dispatch structure and common helpers for SIMD-optimized quantized KV-cache
| GEMM kernels (MlasQKGemm / MlasSVGemm). The dispatch table carries function
| pointers for QKGemm and SVGemm inner loops; the platform constructor in
| platform.cpp assigns the best available implementation at startup.
|
| --*/
|
| #pragma once
|
| #include "mlasi.h"
| #include "mlas_qkv_quant.h"
|
| /**
| * @brief Dispatch table for quantized KV-cache GEMM kernels.
| *
| * Each field is a function pointer for one of the optimized inner loops.
| * The scalar reference implementation in qkv_quant.cpp is used when no
| * vectorized path is available (i.e. when the dispatch pointer is nullptr).
| */
| struct MLAS_KV_QUANT_GEMM_DISPATCH {
| /**
| * QK^T GEMM kernel: C[M,N] = Alpha * A[M,K] * B^T[K,N]
| *
| * B is quantized (INT8 or INT4), logically [N, K] in packed row-major.
| * The kernel dequantizes B on the fly and accumulates in FP32.
| */
| typedef void(QKGemm_Fn)(
| 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
| );
|
| QKGemm_Fn* QKGemm = nullptr;
|
| /**
| * FP16-query QK^T GEMM kernel. The query is consumed directly while the
| * quantized K cache is dequantized and accumulated in FP32.
| */
| typedef void(QKGemmFp16_Fn)(
| 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
| );
|
| QKGemmFp16_Fn* QKGemmFp16 = nullptr;
|
| /**
| * S*V GEMM kernel: C[M,N] = Beta * C[M,N] + A[M,K] * B[K,N]
| *
| * B is quantized (INT8 or INT4), logically [K, N] in packed row-major.
| */
| typedef void(SVGemm_Fn)(
| 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
| );
|
| SVGemm_Fn* SVGemm = nullptr;
|
| /**
| * FP16-output S*V GEMM kernel. Accumulation stays in FP32 registers and
| * the final result is converted directly to FP16.
| */
| typedef void(SVGemmFp16_Fn)(
| 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
| );
|
| SVGemmFp16_Fn* SVGemmFp16 = nullptr;
| };
|
| extern const MLAS_KV_QUANT_GEMM_DISPATCH MlasKVQuantGemmDispatchAvx2;
| extern const MLAS_KV_QUANT_GEMM_DISPATCH MlasKVQuantGemmDispatchAvx512Vnni;
| extern const MLAS_KV_QUANT_GEMM_DISPATCH MlasKVQuantGemmDispatchNeon;
|
|