aboutsummaryrefslogtreecommitdiff
path: root/src/kernels_gemm.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /src/kernels_gemm.c
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to '')
-rw-r--r--libffwd/gemm.c (renamed from src/kernels_gemm.c)101
1 files changed, 49 insertions, 52 deletions
diff --git a/src/kernels_gemm.c b/libffwd/gemm.c
index aa8dadf..bb54fb9 100644
--- a/src/kernels_gemm.c
+++ b/libffwd/gemm.c
@@ -1,5 +1,10 @@
+/*
+ * gemm.c - general Matrix Multiplication
+ */
+
#include "kernels.h"
-#include "kernels_impl.h"
+#include "impl.h"
+#include "threadpool.h"
#include <math.h>
#include <stdlib.h>
@@ -7,22 +12,22 @@
#if (defined(__AVX512F__) || defined(__AVX2__)) && \
(defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86))
-#include <immintrin.h>
+# include <immintrin.h>
#endif
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
-#include <arm_neon.h>
+# include <arm_neon.h>
#endif
#ifdef USE_BLAS
-#ifdef __APPLE__
-#include <Accelerate/Accelerate.h>
-#else
-#include <cblas.h>
-#endif
+# ifdef __APPLE__
+# include <Accelerate/Accelerate.h>
+# else
+# include <cblas.h>
+# endif
#endif
#ifndef M_PI
-#define M_PI 3.14159265358979323846
+# define M_PI 3.14159265358979323846
#endif
/* Matrix and vector ops: GEMM, the dtype-dispatch linear kernels, and the
@@ -32,7 +37,7 @@
* Basic Element-wise Operations
* ======================================================================== */
-void embed_add_inplace(float *a, const float *b, int n) {
+void add_inplace(float *a, const float *b, int n) {
for (int i = 0; i < n; i++)
a[i] += b[i];
}
@@ -41,7 +46,7 @@ void embed_add_inplace(float *a, const float *b, int n) {
* Matrix Operations
* ======================================================================== */
-void embed_matmul_t(float *C, const float *A, const float *B, int M, int K, int N) {
+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
@@ -57,17 +62,11 @@ void embed_matmul_t(float *C, const float *A, const float *B, int M, int K, int
#endif
}
-void embed_linear(float *y,
- const float *x,
- const float *W,
- const float *b,
- int seq_len,
- int in_dim,
- int out_dim) {
+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);
+ 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);
@@ -95,13 +94,12 @@ void embed_linear(float *y,
#endif
}
-void embed_linear_nobias(
- float *y, const float *x, const float *W, int seq_len, int in_dim, int out_dim) {
- embed_linear(y, x, W, NULL, seq_len, in_dim, out_dim);
+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 embed_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) {
+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;
@@ -127,7 +125,7 @@ void embed_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) {
*/
static void bf16_matvec_fused(
float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) {
- embed_bf16_matvec_fused_impl(y, x, W_bf16, bias, in_dim, out_dim);
+ bf16_matvec_fused_impl(y, x, W_bf16, bias, in_dim, out_dim);
}
/* Threaded matvec: split output rows across threads */
@@ -156,7 +154,7 @@ static void matvec_worker(int tid, int n_threads, void *arg) {
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 (embed_num_threads() <= 1) {
+ if (tp_num_threads() <= 1) {
bf16_matvec_fused(y, x, W_bf16, bias, in_dim, out_dim);
return;
}
@@ -207,7 +205,7 @@ static void bf16_linear_rows(float *y,
.out_dim = out_dim,
};
- if (embed_num_threads() > 1 && seq_len >= 2)
+ if (tp_num_threads() > 1 && seq_len >= 2)
parallel_for(bf16_linear_rows_worker, &task);
else
bf16_linear_rows_worker(0, 1, &task);
@@ -272,19 +270,19 @@ static void pair_matvec_worker(int tid, int n_threads, void *arg) {
}
}
-void embed_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) {
+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 (embed_num_threads() <= 1) {
+ 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);
@@ -380,21 +378,21 @@ static void qkv_matvec_worker(int tid, int n_threads, void *arg) {
}
}
-void embed_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) {
+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 (embed_num_threads() <= 1) {
+ 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);
@@ -421,14 +419,13 @@ void embed_linear_nobias_bf16_qkv(float *q,
parallel_for(qkv_matvec_worker, &task);
}
-void embed_linear_nobias_bf16(
+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 route longer sequences through an F32-widened weight matrix in
- * caller-owned scratch (see embed.c); this per-row path stays correct for
- * any length. */
+ * caller-owned scratch; this per-row path stays correct for any length. */
bf16_linear_rows(y, x, W_bf16, NULL, seq_len, in_dim, out_dim);
}