diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 13:11:00 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 13:11:00 +0200 |
| commit | 37cbaef9ceb828c667da8f398027ba5415ee7475 (patch) | |
| tree | 6103e92a9030766e78a35df00f7a1155d0d0ecf5 /liblangdet/gemm.c | |
| parent | cf7d8169ec12cb3adf90d4ff78b3257e74985302 (diff) | |
| download | langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.tar.gz langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.zip | |
Rename CPU library API and sources to langdet
Diffstat (limited to 'liblangdet/gemm.c')
| -rw-r--r-- | liblangdet/gemm.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/liblangdet/gemm.c b/liblangdet/gemm.c new file mode 100644 index 0000000..c750be1 --- /dev/null +++ b/liblangdet/gemm.c @@ -0,0 +1,49 @@ +#include "kernels.h" + +#ifdef USE_BLAS +# ifdef __APPLE__ +# include <Accelerate/Accelerate.h> +# else +# include <cblas.h> +# endif +#endif + +void add_inplace(float *a, const float *b, int n) { + for (int i = 0; i < n; i++) + a[i] += b[i]; +} + +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); +} |
