aboutsummaryrefslogtreecommitdiff
path: root/liblangdet/gemm.c
blob: c750be1115dc8d4f7dd170a93a72e4c830523265 (plain) (blame)
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
#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);
}