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
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* General matrix multiplication.
*/
#include "kernels.h"
#include "impl.h"
#include "threadpool.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#if (defined(__AVX512F__) || defined(__AVX2__)) && \
(defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86))
# include <immintrin.h>
#endif
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
# include <arm_neon.h>
#endif
#ifdef USE_BLAS
# ifdef __APPLE__
# include <Accelerate/Accelerate.h>
# else
# include <cblas.h>
# endif
#endif
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
/* Matrix and vector ops:
* GEMM, dtype-dispatch linear kernels, BF16-fused matvec/QKV/pair workers,
* and element-wise residual add. */
/* Basic element-wise operations */
void add_inplace(float *a, const float *b, int n) {
for (int i = 0; i < n; i++)
a[i] += b[i];
}
/* Matrix operations */
void matmul_t(float *C, const float *A, const float *B, int M, int K, int N) {
#ifdef USE_BLAS
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, M, N, K, 1.0f, A, K, B, K, 0.0f, C, N);
#else
for (int m = 0; m < M; m++) {
for (int n = 0; n < N; n++) {
float sum = 0.0f;
for (int k = 0; k < K; k++) {
sum += A[m * K + k] * B[n * K + k];
}
C[m * N + n] = sum;
}
}
#endif
}
void linear(
float *y, const float *x, const float *W, const float *b, int seq_len, int in_dim, int out_dim) {
#ifdef USE_BLAS
if (seq_len == 1) {
cblas_sgemv(CblasRowMajor, CblasNoTrans, out_dim, in_dim, 1.0f, W, in_dim, x, 1, 0.0f, y, 1);
} else {
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, seq_len, out_dim, in_dim, 1.0f, x,
in_dim, W, in_dim, 0.0f, y, out_dim);
}
if (b != NULL) {
for (int s = 0; s < seq_len; s++) {
for (int o = 0; o < out_dim; o++) {
y[s * out_dim + o] += b[o];
}
}
}
#else
for (int s = 0; s < seq_len; s++) {
const float *x_row = x + s * in_dim;
float *y_row = y + s * out_dim;
for (int o = 0; o < out_dim; o++) {
const float *w_row = W + o * in_dim;
float sum = (b != NULL) ? b[o] : 0.0f;
for (int i = 0; i < in_dim; i++) {
sum += x_row[i] * w_row[i];
}
y_row[o] = sum;
}
}
#endif
}
void linear_nobias(float *y, const float *x, const float *W, int seq_len, int in_dim, int out_dim) {
linear(y, x, W, NULL, seq_len, in_dim, out_dim);
}
/* Convert bf16 buffer to f32 buffer */
void bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) {
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
size_t i = 0;
uint32_t *d = (uint32_t *)(void *)dst;
for (; i + 8 <= n; i += 8) {
uint16x8_t v = vld1q_u16(src + i);
uint32x4_t lo = vshlq_n_u32(vmovl_u16(vget_low_u16(v)), 16);
uint32x4_t hi = vshlq_n_u32(vmovl_u16(vget_high_u16(v)), 16);
vst1q_u32(d + i, lo);
vst1q_u32(d + i + 4, hi);
}
for (; i < n; i++)
d[i] = ((uint32_t)src[i]) << 16;
#else
uint32_t *d = (uint32_t *)(void *)dst;
for (size_t i = 0; i < n; i++)
d[i] = ((uint32_t)src[i]) << 16;
#endif
}
/*
* Fused BF16 matvec: y[out_dim] = W_bf16[out_dim, in_dim] @ x[in_dim] + bias
* Processes 2 output rows at a time to amortize x vector loads.
*/
static void bf16_matvec_fused(
float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) {
bf16_matvec_fused_impl(y, x, W_bf16, bias, in_dim, out_dim);
}
/* Threaded matvec: split output rows across threads */
typedef struct {
float *y;
const float *x;
const uint16_t *W_bf16;
const float *bias;
int in_dim;
int out_dim;
} matvec_task_t;
static void matvec_worker(int tid, int n_threads, void *arg) {
matvec_task_t *t = (matvec_task_t *)arg;
int chunk = (t->out_dim + n_threads - 1) / n_threads;
int start = tid * chunk;
int end = start + chunk;
if (end > t->out_dim)
end = t->out_dim;
if (start >= end)
return;
bf16_matvec_fused(t->y + start, t->x, t->W_bf16 + (size_t)start * t->in_dim,
t->bias ? t->bias + start : NULL, t->in_dim, end - start);
}
static void bf16_matvec_threaded(
float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) {
if (tp_num_threads() <= 1) {
bf16_matvec_fused(y, x, W_bf16, bias, in_dim, out_dim);
return;
}
matvec_task_t task = {y, x, W_bf16, bias, in_dim, out_dim};
parallel_for(matvec_worker, &task);
}
typedef struct {
float *y;
const float *x;
const uint16_t *W_bf16;
const float *bias;
int seq_len;
int in_dim;
int out_dim;
} bf16_linear_rows_task_t;
static void bf16_linear_rows_worker(int tid, int n_threads, void *arg) {
bf16_linear_rows_task_t *t = (bf16_linear_rows_task_t *)arg;
int chunk = (t->seq_len + n_threads - 1) / n_threads;
int start = tid * chunk;
int end = start + chunk;
if (end > t->seq_len)
end = t->seq_len;
if (start >= end)
return;
for (int s = start; s < end; s++) {
bf16_matvec_fused(t->y + (size_t)s * t->out_dim, t->x + (size_t)s * t->in_dim, t->W_bf16,
t->bias, t->in_dim, t->out_dim);
}
}
static void bf16_linear_rows(float *y,
const float *x,
const uint16_t *W_bf16,
const float *bias,
int seq_len,
int in_dim,
int out_dim) {
bf16_linear_rows_task_t task = {
.y = y,
.x = x,
.W_bf16 = W_bf16,
.bias = bias,
.seq_len = seq_len,
.in_dim = in_dim,
.out_dim = out_dim,
};
if (tp_num_threads() > 1 && seq_len >= 2)
parallel_for(bf16_linear_rows_worker, &task);
else
bf16_linear_rows_worker(0, 1, &task);
}
typedef struct {
float *a;
float *b;
const float *x;
const uint16_t *Wa_bf16;
const uint16_t *Wb_bf16;
int seq_len;
int in_dim;
int a_dim;
int b_dim;
int total_dim;
} pair_matvec_task_t;
static void pair_matvec_range(const pair_matvec_task_t *t, int row, int start, int end) {
const float *x_row = t->x + (size_t)row * t->in_dim;
int a_end = t->a_dim;
int b_end = a_end + t->b_dim;
if (start < a_end) {
int s = start;
int e = end < a_end ? end : a_end;
if (s < e) {
bf16_matvec_fused(t->a + (size_t)row * t->a_dim + s, x_row,
t->Wa_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s);
}
}
if (end > a_end && start < b_end) {
int s = start > a_end ? start - a_end : 0;
int e_abs = end < b_end ? end : b_end;
int e = e_abs - a_end;
if (s < e) {
bf16_matvec_fused(t->b + (size_t)row * t->b_dim + s, x_row,
t->Wb_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s);
}
}
}
static void pair_matvec_worker(int tid, int n_threads, void *arg) {
pair_matvec_task_t *t = (pair_matvec_task_t *)arg;
int total_work = t->seq_len * t->total_dim;
int chunk = (total_work + n_threads - 1) / n_threads;
int start = tid * chunk;
int end = start + chunk;
if (end > total_work)
end = total_work;
if (start >= end)
return;
while (start < end) {
int row = start / t->total_dim;
int pos = start - row * t->total_dim;
int row_end = (row + 1) * t->total_dim;
int stop = end < row_end ? end : row_end;
pair_matvec_range(t, row, pos, stop - row * t->total_dim);
start = stop;
}
}
void linear_nobias_bf16_pair(float *a,
float *b,
const float *x,
const uint16_t *Wa_bf16,
const uint16_t *Wb_bf16,
int seq_len,
int in_dim,
int a_dim,
int b_dim) {
if (seq_len <= 0)
return;
if (tp_num_threads() <= 1) {
for (int s = 0; s < seq_len; s++) {
const float *x_row = x + (size_t)s * in_dim;
bf16_matvec_fused(a + (size_t)s * a_dim, x_row, Wa_bf16, NULL, in_dim, a_dim);
bf16_matvec_fused(b + (size_t)s * b_dim, x_row, Wb_bf16, NULL, in_dim, b_dim);
}
return;
}
pair_matvec_task_t task = {
.a = a,
.b = b,
.x = x,
.Wa_bf16 = Wa_bf16,
.Wb_bf16 = Wb_bf16,
.seq_len = seq_len,
.in_dim = in_dim,
.a_dim = a_dim,
.b_dim = b_dim,
.total_dim = a_dim + b_dim,
};
parallel_for(pair_matvec_worker, &task);
}
typedef struct {
float *q;
float *k;
float *v;
const float *x;
const uint16_t *Wq_bf16;
const uint16_t *Wk_bf16;
const uint16_t *Wv_bf16;
int in_dim;
int q_dim;
int kv_dim;
int seq_len;
int total_dim;
} qkv_matvec_task_t;
static void qkv_matvec_range(const qkv_matvec_task_t *t, int row, int start, int end) {
const float *x_row = t->x + (size_t)row * t->in_dim;
int q_end = t->q_dim;
int k_end = q_end + t->kv_dim;
int v_end = k_end + t->kv_dim;
if (start < q_end) {
int s = start;
int e = end < q_end ? end : q_end;
if (s < e) {
bf16_matvec_fused(t->q + (size_t)row * t->q_dim + s, x_row,
t->Wq_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s);
}
}
if (end > q_end && start < k_end) {
int s = start > q_end ? start - q_end : 0;
int e_abs = end < k_end ? end : k_end;
int e = e_abs - q_end;
if (s < e) {
bf16_matvec_fused(t->k + (size_t)row * t->kv_dim + s, x_row,
t->Wk_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s);
}
}
if (end > k_end && start < v_end) {
int s = start > k_end ? start - k_end : 0;
int e_abs = end < v_end ? end : v_end;
int e = e_abs - k_end;
if (s < e) {
bf16_matvec_fused(t->v + (size_t)row * t->kv_dim + s, x_row,
t->Wv_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s);
}
}
}
static void qkv_matvec_worker(int tid, int n_threads, void *arg) {
qkv_matvec_task_t *t = (qkv_matvec_task_t *)arg;
int total_work = t->seq_len * t->total_dim;
int chunk = (total_work + n_threads - 1) / n_threads;
int start = tid * chunk;
int end = start + chunk;
if (end > total_work)
end = total_work;
if (start >= end)
return;
while (start < end) {
int row = start / t->total_dim;
int pos = start - row * t->total_dim;
int row_end = (row + 1) * t->total_dim;
int stop = end < row_end ? end : row_end;
qkv_matvec_range(t, row, pos, stop - row * t->total_dim);
start = stop;
}
}
void linear_nobias_bf16_qkv(float *q,
float *k,
float *v,
const float *x,
const uint16_t *Wq_bf16,
const uint16_t *Wk_bf16,
const uint16_t *Wv_bf16,
int seq_len,
int in_dim,
int q_dim,
int kv_dim) {
if (seq_len <= 0)
return;
if (tp_num_threads() <= 1) {
for (int s = 0; s < seq_len; s++) {
const float *x_row = x + (size_t)s * in_dim;
bf16_matvec_fused(q + (size_t)s * q_dim, x_row, Wq_bf16, NULL, in_dim, q_dim);
bf16_matvec_fused(k + (size_t)s * kv_dim, x_row, Wk_bf16, NULL, in_dim, kv_dim);
bf16_matvec_fused(v + (size_t)s * kv_dim, x_row, Wv_bf16, NULL, in_dim, kv_dim);
}
return;
}
qkv_matvec_task_t task = {
.q = q,
.k = k,
.v = v,
.x = x,
.Wq_bf16 = Wq_bf16,
.Wk_bf16 = Wk_bf16,
.Wv_bf16 = Wv_bf16,
.in_dim = in_dim,
.q_dim = q_dim,
.kv_dim = kv_dim,
.seq_len = seq_len,
.total_dim = q_dim + 2 * kv_dim,
};
parallel_for(qkv_matvec_worker, &task);
}
void linear_nobias_bf16(
float *y, const float *x, const uint16_t *W_bf16, int seq_len, int in_dim, int out_dim) {
if (seq_len == 1) {
bf16_matvec_threaded(y, x, W_bf16, NULL, in_dim, out_dim);
return;
}
/* Callers send longer sequences through an F32-widened weight matrix in
* caller-owned scratch.
* This per-row path remains correct for any length. */
bf16_linear_rows(y, x, W_bf16, NULL, seq_len, in_dim, out_dim);
}
|