#include "ga_math.h" #include #include "ga_kernels.h" #if defined(GA_USE_ACCELERATE) #define ACCELERATE_NEW_LAPACK #include #define GA_HAVE_CBLAS 1 #elif defined(GA_USE_CBLAS) #include #define GA_HAVE_CBLAS 1 #endif #define GA_BLOCK 1024 #define GA_SPLIT_ALIGN 8 #define GA_SPLIT_MIN (1 << 18) #define GA_SPLIT_COLS 64 #define GA_SPLIT_ELEMS 8192 const char *ga_math_backend(void) { #if defined(GA_USE_ACCELERATE) return "accelerate"; #elif defined(GA_USE_CBLAS) return "cblas"; #else return ga_kernel_name(); #endif } static void gemm_nt(int m, int n, int k, float alpha, const float *a, int lda, const float *b, int ldb, float beta, float *c, int ldc) { #if defined(GA_HAVE_CBLAS) cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc); #else ga_kernel_gemm_nt(m, n, k, alpha, a, lda, b, ldb, beta, c, ldc); #endif } void ga_gemm_nn(int m, int n, int k, float alpha, const float *a, int lda, const float *b, int ldb, float beta, float *c, int ldc) { #if defined(GA_HAVE_CBLAS) cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc); #else for (int i = 0; i < m; i++) { float *row = c + (size_t)i * ldc; for (int j = 0; j < n; j++) row[j] = beta == 0.0f ? 0.0f : beta * row[j]; for (int p = 0; p < k; p++) { float scale = alpha * a[(size_t)i * lda + p]; const float *src = b + (size_t)p * ldb; for (int j = 0; j < n; j++) row[j] += scale * src[j]; } } #endif } int ga_math_reads_f16(void) { #if defined(GA_HAVE_CBLAS) return 0; #else return 1; #endif } /* GA_INT8_WEIGHTS_ONLY: dequantize int8 weights at load (accuracy debugging). */ int ga_math_reads_i8(void) { #if defined(GA_INT8_WEIGHTS_ONLY) return 0; #else return ga_math_reads_f16(); #endif } /* Adds b[lo:hi) to columns [lo, hi) of every row of y[rows, ldy]. */ static void add_bias(float *y, int rows, int ldy, const float *b, int lo, int hi) { if (!b) return; for (int r = 0; r < rows; r++) { float *row = y + (size_t)r * ldy; for (int j = lo; j < hi; j++) row[j] += b[j]; } } /* Share [lo, hi) of n for one part; inner edges on multiples of GA_SPLIT_ALIGN keep the * kernel's row and column blocks whole, so results do not depend on the split. */ static void split_range(int n, int part, int parts, int *lo, int *hi) { *lo = (int)((long)n * part / parts) / GA_SPLIT_ALIGN * GA_SPLIT_ALIGN; *hi = (int)((long)n * (part + 1) / parts) / GA_SPLIT_ALIGN * GA_SPLIT_ALIGN; if (part + 1 == parts) *hi = n; } typedef struct gemm_task { int m, n, k; float alpha, beta; const float *a; const float *b; /* f32 weights, or NULL */ const uint16_t *bh; /* f16 weights, or NULL */ const int8_t *bq; /* int8 weights (with bq_scale and aq/aq_scale), or NULL */ const float *bq_scale; const int8_t *aq; const float *aq_scale; int group; /* int8: values per scale along k */ const float *bias; /* added per column, or NULL */ float *c; int lda, ldb, ldc; int by_rows; } gemm_task; static void gemm_block(const gemm_task *t, int r0, int r1, int c0, int c1) { const float *a = t->a + (size_t)r0 * t->lda; float *c = t->c + (size_t)r0 * t->ldc + c0; if (t->bq) ga_kernel_gemm_i8(r1 - r0, c1 - c0, t->k, t->group, t->aq + (size_t)r0 * t->k, t->k, t->aq_scale + (size_t)r0 * (t->k / t->group), t->bq + (size_t)c0 * t->ldb, t->ldb, t->bq_scale + (size_t)c0 * (t->k / t->group), c, t->ldc); else if (t->bh) ga_kernel_gemm_nt_f16(r1 - r0, c1 - c0, t->k, t->alpha, a, t->lda, t->bh + (size_t)c0 * t->ldb, t->ldb, t->beta, c, t->ldc); else gemm_nt(r1 - r0, c1 - c0, t->k, t->alpha, a, t->lda, t->b + (size_t)c0 * t->ldb, t->ldb, t->beta, c, t->ldc); add_bias(t->c + (size_t)r0 * t->ldc, r1 - r0, t->ldc, t->bias, c0, c1); } static void gemm_part(void *arg, int part, int parts) { const gemm_task *t = arg; int lo, hi; split_range(t->by_rows ? t->m : t->n, part, parts, &lo, &hi); if (hi <= lo) return; if (t->by_rows) gemm_block(t, lo, hi, 0, t->n); else gemm_block(t, 0, t->m, lo, hi); } /* Splits columns (weights) when there are enough, else rows; BLAS runs unsplit. */ static void run_gemm(gemm_task *t, ga_pool *pool) { int parts = ga_pool_size(pool); #if defined(GA_HAVE_CBLAS) if (!t->bh && !t->bq) pool = NULL; #endif if ((size_t)t->m * t->n * t->k < GA_SPLIT_MIN) pool = NULL; t->by_rows = t->n < GA_SPLIT_COLS * parts; ga_pool_run(pool, gemm_part, t); } void ga_gemm_nt(int m, int n, int k, float alpha, const float *a, int lda, const float *b, int ldb, float beta, float *c, int ldc, ga_pool *pool) { gemm_task t = {.m = m, .n = n, .k = k, .alpha = alpha, .beta = beta, .a = a, .b = b, .c = c, .lda = lda, .ldb = ldb, .ldc = ldc}; run_gemm(&t, pool); } void ga_linear(const float *x, int rows, int in, const float *w, const float *b, int out, float *y, ga_pool *pool) { gemm_task t = {.m = rows, .n = out, .k = in, .alpha = 1.0f, .a = x, .b = w, .bias = b, .c = y, .lda = in, .ldb = in, .ldc = out}; run_gemm(&t, pool); } void ga_linear_f16(const float *x, int rows, int in, const uint16_t *w, const float *b, int out, float *y, ga_pool *pool) { gemm_task t = {.m = rows, .n = out, .k = in, .alpha = 1.0f, .a = x, .bh = w, .bias = b, .c = y, .lda = in, .ldb = in, .ldc = out}; run_gemm(&t, pool); } typedef struct quantize_task { const float *x; int rows, in, group; int8_t *q; float *scale; } quantize_task; static void quantize_part(void *arg, int part, int parts) { const quantize_task *t = arg; int lo = t->rows * part / parts / 2 * 2; int hi = part + 1 == parts ? t->rows : t->rows * (part + 1) / parts / 2 * 2; if (hi > lo) ga_kernel_quantize_rows(t->x + (size_t)lo * t->in, hi - lo, t->in, t->in, t->group, t->q + (size_t)lo * t->in, t->scale + (size_t)lo * (t->in / t->group), t->rows - lo); } void ga_linear_i8(const float *x, int rows, int in, const int8_t *w, const float *w_scale, int group, const float *b, int out, float *y, int8_t *xq, float *xq_scale, ga_pool *pool) { quantize_task q = {.x = x, .rows = rows, .in = in, .group = group, .q = xq, .scale = xq_scale}; gemm_task t = {.m = rows, .n = out, .k = in, .alpha = 1.0f, .a = x, .bq = w, .bq_scale = w_scale, .aq = xq, .aq_scale = xq_scale, .group = group, .bias = b, .c = y, .lda = in, .ldb = in, .ldc = out}; ga_pool_run((size_t)rows * in >= GA_SPLIT_ELEMS ? pool : NULL, quantize_part, &q); run_gemm(&t, pool); } /* e[i] = exp(-x[i]) for n <= GA_BLOCK */ static void exp_neg(const float *x, float *e, int n) { #if defined(GA_USE_ACCELERATE) for (int i = 0; i < n; i++) e[i] = -x[i]; vvexpf(e, e, &n); #elif defined(GA_LIBM_EXP) for (int i = 0; i < n; i++) e[i] = expf(-x[i]); #else ga_kernel_exp_neg(x, e, n); #endif } static void silu(float *x, size_t n) { float e[GA_BLOCK]; while (n) { int count = n < GA_BLOCK ? (int)n : GA_BLOCK; exp_neg(x, e, count); for (int i = 0; i < count; i++) x[i] = x[i] / (1.0f + e[i]); x += count; n -= (size_t)count; } } static void sigmoid(float *x, size_t n) { float e[GA_BLOCK]; while (n) { int count = n < GA_BLOCK ? (int)n : GA_BLOCK; exp_neg(x, e, count); for (int i = 0; i < count; i++) x[i] = 1.0f / (1.0f + e[i]); x += count; n -= (size_t)count; } } typedef struct unary_task { void (*fn)(float *x, size_t n); float *x; size_t n; } unary_task; static void unary_part(void *arg, int part, int parts) { const unary_task *t = arg; size_t lo = t->n * (size_t)part / (size_t)parts / GA_SPLIT_ALIGN * GA_SPLIT_ALIGN; size_t hi = part + 1 == parts ? t->n : t->n * (size_t)(part + 1) / (size_t)parts / GA_SPLIT_ALIGN * GA_SPLIT_ALIGN; if (hi > lo) t->fn(t->x + lo, hi - lo); } static void run_unary(void (*fn)(float *, size_t), float *x, size_t n, ga_pool *pool) { unary_task t = {fn, x, n}; ga_pool_run(n >= GA_SPLIT_ELEMS ? pool : NULL, unary_part, &t); } void ga_silu(float *x, size_t n, ga_pool *pool) { run_unary(silu, x, n, pool); } void ga_sigmoid(float *x, size_t n, ga_pool *pool) { run_unary(sigmoid, x, n, pool); } void ga_rms_norm(const float *x, int rows, int dim, const float *weight, float eps, float *y) { for (int r = 0; r < rows; r++) { const float *in = x + (size_t)r * dim; float *out = y + (size_t)r * dim; float sum = 0.0f; float scale; for (int i = 0; i < dim; i++) sum += in[i] * in[i]; scale = 1.0f / sqrtf(sum / (float)dim + eps); for (int i = 0; i < dim; i++) out[i] = in[i] * scale * weight[i]; } }