/* Hand-rolled SIMD kernels (NEON, AVX2+FMA, scalar). */ #ifndef GA_KERNELS_H #define GA_KERNELS_H #include /* C[m,n] = alpha * A[m,k] * B[n,k]^T + beta * C */ void ga_kernel_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); /* Same, with B stored as IEEE half (widened in registers, bit-identical result). */ void ga_kernel_gemm_nt_f16(int m, int n, int k, float alpha, const float *a, int lda, const uint16_t *b, int ldb, float beta, float *c, int ldc); /* Longest int8 row (k) the kernels accept. */ #define GA_I8_MAX_K 16384 /* Per row and per run of `group` values: scale = max|x| / 127 (1 if all zero), * q = round_even(x / scale). scale is [rows, k / group]; q is [rows, k], or the pair layout * when ga_kernel_i8_pairs(group) (rows 2p, 2p+1 at q + 2p * k; the last of total_rows is * paired with itself when odd). x/q/scale must start at an even row. */ void ga_kernel_quantize_rows(const float *x, int rows, int k, int ldx, int group, int8_t *q, float *scale, int total_rows); /* 1 when the int8 kernel uses i8mm (smmla) for this group size: weights must then be packed * with ga_kernel_i8_pack_weights and activations come in the pair layout. */ int ga_kernel_i8_pairs(int group); /* w[n, k] -> pair layout [ceil(n / 2)][k / 8][2][8] (odd last row repeated); out holds * (n + 1) / 2 * 2 * k bytes. */ void ga_kernel_i8_pack_weights(const int8_t *w, int n, int k, int8_t *out); /* Weight scales [n, groups] -> the kernel's layout [ceil(n / 4)][groups][4] (short last * block repeats its last column). */ void ga_kernel_i8_interleave_scales(const float *scale, int n, int groups, float *out); /* C[m,n] = sum_g (A_g . B_g) * a_scale[i,g] * b_scale[j,g] over groups of `group` along k * (k % group == 0); exact int32 group sums, fma-accumulated in group order. b_scale is in * the interleaved layout, starting at column 0 (column offsets must be multiples of 4). A and * B are in the pair layout when ga_kernel_i8_pairs(group); row offsets must then be even. */ void ga_kernel_gemm_i8(int m, int n, int k, int group, const int8_t *a, int lda, const float *a_scale, const int8_t *b, int ldb, const float *b_scale, float *c, int ldc); /* "sdot", "neon", "avx2" or "scalar": the int8 kernel picked at run time. */ const char *ga_kernel_i8_name(void); /* e[i] = exp(-x[i]) */ void ga_kernel_exp_neg(const float *x, float *e, int n); /* Name of the instruction set compiled in. */ const char *ga_kernel_name(void); #endif