interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cpu / simd-gemm.h

266 lines · 8796 bytes Raw Blame History
1#pragma once
2
3// Computes C[M x N] += A[M x K] * B[K x N]
4
5#include "simd-mappings.h"
6
7// TODO: add support for sizeless vector types
8#if defined(GGML_SIMD) && !defined(__ARM_FEATURE_SVE) && !defined(__riscv_v)
9
10// TODO: untested on avx512
11// These are in units of GGML_F32_EPR
12#if defined(__AVX512F__) || defined (__ARM_NEON__)
13 static constexpr int GEMM_RM = 4;
14 static constexpr int GEMM_RN = 4; // 16+4+1 = 25/32
15#elif defined(__AVX2__) || defined(__AVX__)
16 static constexpr int GEMM_RM = 6;
17 static constexpr int GEMM_RN = 2; // 12+2+1 = 15/16
18#else
19 static constexpr int GEMM_RM = 2;
20 static constexpr int GEMM_RN = 2;
21#endif
22
23template <int RM, int RN>
24static inline void simd_gemm_ukernel(
25 float * GGML_RESTRICT C,
26 const float * GGML_RESTRICT A,
27 const float * GGML_RESTRICT B,
28 int K, int N)
29{
30 static constexpr int KN = GGML_F32_EPR;
31
32 GGML_F32_VEC acc[RM][RN];
33 for (int64_t i = 0; i < RM; i++) {
34 for (int r = 0; r < RN; r++) {
35 acc[i][r] = GGML_F32_VEC_LOAD(C + i * N + r * KN);
36 }
37 }
38
39 for (int64_t kk = 0; kk < K; kk++) {
40 GGML_F32_VEC Bv[RN];
41 for (int r = 0; r < RN; r++) {
42 Bv[r] = GGML_F32_VEC_LOAD(B + kk * N + r * KN);
43 }
44 for (int64_t i = 0; i < RM; i++) {
45 GGML_F32_VEC p = GGML_F32_VEC_SET1(A[i * K + kk]);
46 for (int r = 0; r < RN; r++) {
47 acc[i][r] = GGML_F32_VEC_FMA(acc[i][r], Bv[r], p);
48 }
49 }
50 }
51
52 for (int64_t i = 0; i < RM; i++) {
53 for (int r = 0; r < RN; r++) {
54 GGML_F32_VEC_STORE(C + i * N + r * KN, acc[i][r]);
55 }
56 }
57}
58
59template <int RM>
60static inline void simd_gemm_ukernel_tail(
61 float * GGML_RESTRICT C,
62 const float * GGML_RESTRICT A,
63 const float * GGML_RESTRICT B,
64 int K, int N, int cols)
65{
66#if defined(__AVX512F__)
67 const __mmask16 mask = (1u << cols) - 1;
68 __m512 acc[RM];
69 for (int64_t i = 0; i < RM; i++) {
70 acc[i] = _mm512_maskz_loadu_ps(mask, C + i * N);
71 }
72 for (int64_t kk = 0; kk < K; kk++) {
73 const __m512 b = _mm512_maskz_loadu_ps(mask, B + kk * N);
74 for (int64_t i = 0; i < RM; i++) {
75 acc[i] = _mm512_mask3_fmadd_ps(_mm512_set1_ps(A[i * K + kk]), b, acc[i], mask);
76 }
77 }
78 for (int64_t i = 0; i < RM; i++) {
79 _mm512_mask_storeu_ps(C + i * N, mask, acc[i]);
80 }
81#elif defined(__AVX2__)
82 const __m256i mask = _mm256_cmpgt_epi32(_mm256_set1_epi32(cols), _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7));
83 __m256 acc[RM];
84 for (int64_t i = 0; i < RM; i++) {
85 acc[i] = _mm256_maskload_ps(C + i * N, mask);
86 }
87 for (int64_t kk = 0; kk < K; kk++) {
88 const __m256 b = _mm256_maskload_ps(B + kk * N, mask);
89 for (int64_t i = 0; i < RM; i++) {
90 acc[i] = GGML_F32_VEC_FMA(acc[i], b, _mm256_set1_ps(A[i * K + kk]));
91 }
92 }
93 for (int64_t i = 0; i < RM; i++) {
94 _mm256_maskstore_ps(C + i * N, mask, acc[i]);
95 }
96#else
97 for (int64_t j = 0; j < cols; j++) {
98 for (int64_t i = 0; i < RM; i++) {
99 float a = C[i * N + j];
100 for (int64_t kk = 0; kk < K; kk++) {
101 a += A[i * K + kk] * B[kk * N + j];
102 }
103 C[i * N + j] = a;
104 }
105 }
106#endif
107}
108
109// C[M x N] += A[M x K] * B[K x N]
110static void simd_gemm(
111 float * GGML_RESTRICT C,
112 const float * GGML_RESTRICT A,
113 const float * GGML_RESTRICT B,
114 int M, int K, int N)
115{
116 static constexpr int KN = GGML_F32_EPR;
117
118 int64_t ii = 0;
119 for (; ii + GEMM_RM <= M; ii += GEMM_RM) {
120 int64_t jj = 0;
121 for (; jj + GEMM_RN * KN <= N; jj += GEMM_RN * KN) {
122 simd_gemm_ukernel<GEMM_RM, GEMM_RN>(C + jj, A, B + jj, K, N);
123 }
124 for (; jj + KN <= N; jj += KN) {
125 simd_gemm_ukernel<GEMM_RM, 1>(C + jj, A, B + jj, K, N);
126 }
127 if (jj < N) {
128 simd_gemm_ukernel_tail<GEMM_RM>(C + jj, A, B + jj, K, N, N - jj);
129 }
130
131 A += GEMM_RM * K;
132 C += GEMM_RM * N;
133 }
134
135 // Tail rows: one at a time
136 for (; ii < M; ii++) {
137 int64_t jj = 0;
138 for (; jj + GEMM_RN * KN <= N; jj += GEMM_RN * KN) {
139 simd_gemm_ukernel<1, GEMM_RN>(C + jj, A, B + jj, K, N);
140 }
141 for (; jj + KN <= N; jj += KN) {
142 simd_gemm_ukernel<1, 1>(C + jj, A, B + jj, K, N);
143 }
144 if (jj < N) {
145 simd_gemm_ukernel_tail<1>(C + jj, A, B + jj, K, N, N - jj);
146 }
147
148 A += K;
149 C += N;
150 }
151}
152#elif defined(GGML_SIMD) && defined(__riscv_v)
153// RM accumulators + 1 B vector = RM + 1 <= 8 => RM <= 7
154// Microkernel: C[RM x vl] += A[RM x K] * B[K x N]
155template <int RM>
156static inline void rvv_simd_gemm_ukernel(
157 float * GGML_RESTRICT C,
158 const float * GGML_RESTRICT A,
159 const float * GGML_RESTRICT B,
160 int K, int N, size_t vl)
161{
162 static_assert(RM >= 1 && RM <= 7, "RM must be 1..7 for LMUL=4");
163
164 vfloat32m4_t acc_0 = __riscv_vle32_v_f32m4(C + 0 * N, vl);
165 vfloat32m4_t acc_1, acc_2, acc_3, acc_4, acc_5, acc_6;
166 if constexpr (RM > 1) acc_1 = __riscv_vle32_v_f32m4(C + 1 * N, vl);
167 if constexpr (RM > 2) acc_2 = __riscv_vle32_v_f32m4(C + 2 * N, vl);
168 if constexpr (RM > 3) acc_3 = __riscv_vle32_v_f32m4(C + 3 * N, vl);
169 if constexpr (RM > 4) acc_4 = __riscv_vle32_v_f32m4(C + 4 * N, vl);
170 if constexpr (RM > 5) acc_5 = __riscv_vle32_v_f32m4(C + 5 * N, vl);
171 if constexpr (RM > 6) acc_6 = __riscv_vle32_v_f32m4(C + 6 * N, vl);
172
173 for (int kk = 0; kk < K; kk++) {
174 vfloat32m4_t b_0 = __riscv_vle32_v_f32m4(B + kk * N, vl);
175
176 acc_0 = __riscv_vfmacc_vf_f32m4(acc_0, A[0 * K + kk], b_0, vl);
177 if constexpr (RM > 1) acc_1 = __riscv_vfmacc_vf_f32m4(acc_1, A[1 * K + kk], b_0, vl);
178 if constexpr (RM > 2) acc_2 = __riscv_vfmacc_vf_f32m4(acc_2, A[2 * K + kk], b_0, vl);
179 if constexpr (RM > 3) acc_3 = __riscv_vfmacc_vf_f32m4(acc_3, A[3 * K + kk], b_0, vl);
180 if constexpr (RM > 4) acc_4 = __riscv_vfmacc_vf_f32m4(acc_4, A[4 * K + kk], b_0, vl);
181 if constexpr (RM > 5) acc_5 = __riscv_vfmacc_vf_f32m4(acc_5, A[5 * K + kk], b_0, vl);
182 if constexpr (RM > 6) acc_6 = __riscv_vfmacc_vf_f32m4(acc_6, A[6 * K + kk], b_0, vl);
183 }
184
185 __riscv_vse32_v_f32m4(C + 0 * N, acc_0, vl);
186 if constexpr (RM > 1) __riscv_vse32_v_f32m4(C + 1 * N, acc_1, vl);
187 if constexpr (RM > 2) __riscv_vse32_v_f32m4(C + 2 * N, acc_2, vl);
188 if constexpr (RM > 3) __riscv_vse32_v_f32m4(C + 3 * N, acc_3, vl);
189 if constexpr (RM > 4) __riscv_vse32_v_f32m4(C + 4 * N, acc_4, vl);
190 if constexpr (RM > 5) __riscv_vse32_v_f32m4(C + 5 * N, acc_5, vl);
191 if constexpr (RM > 6) __riscv_vse32_v_f32m4(C + 6 * N, acc_6, vl);
192}
193
194template <int RM>
195static inline void rvv_simd_gemm_dispatch_tail(
196 float * GGML_RESTRICT C,
197 const float * GGML_RESTRICT A,
198 const float * GGML_RESTRICT B,
199 int K, int N, int KN, int remaining_rows)
200{
201 if constexpr (RM > 0) {
202 if (remaining_rows == RM) {
203 int64_t jj = 0;
204 for (; jj + KN <= N; jj += KN) {
205 rvv_simd_gemm_ukernel<RM>(C + jj, A, B + jj, K, N, KN);
206 }
207 if (jj < N) {
208 rvv_simd_gemm_ukernel<RM>(C + jj, A, B + jj, K, N, N - jj);
209 }
210 } else {
211 rvv_simd_gemm_dispatch_tail<RM - 1>(C, A, B, K, N, KN, remaining_rows);
212 }
213 }
214}
215
216static constexpr int GEMM_RM = 7;
217
218// C[M x N] += A[M x K] * B[K x N]
219static void simd_gemm(
220 float * GGML_RESTRICT C,
221 const float * GGML_RESTRICT A,
222 const float * GGML_RESTRICT B,
223 int M, int K, int N)
224{
225 const int KN = (int)__riscv_vlenb();
226 int64_t ii = 0;
227 for (; ii + GEMM_RM <= M; ii += GEMM_RM) {
228 int64_t jj = 0;
229 for (; jj + KN <= N; jj += KN) {
230 rvv_simd_gemm_ukernel<GEMM_RM>(C + jj, A, B + jj, K, N, KN);
231 }
232 if (jj < N) {
233 rvv_simd_gemm_ukernel<GEMM_RM>(C + jj, A, B + jj, K, N, N - jj);
234 }
235 A += GEMM_RM * K;
236 C += GEMM_RM * N;
237 }
238
239 int remaining_rows = M - ii;
240 rvv_simd_gemm_dispatch_tail<GEMM_RM - 1>(C, A, B, K, N, KN, remaining_rows);
241}
242
243#if defined(__GNUC__) && !defined(__clang__)
244#pragma GCC diagnostic pop
245#endif
246
247#else // scalar path
248
249static void simd_gemm(
250 float * GGML_RESTRICT C,
251 const float * GGML_RESTRICT A,
252 const float * GGML_RESTRICT B,
253 int M, int K, int N)
254{
255 for (int64_t i = 0; i < M; i++) {
256 for (int64_t j = 0; j < N; j++) {
257 float sum = C[i * N + j];
258 for (int64_t kk = 0; kk < K; kk++) {
259 sum += A[i * K + kk] * B[kk * N + j];
260 }
261 C[i * N + j] = sum;
262 }
263 }
264}
265
266#endif // GGML_SIMD