#include "kernels.h" #ifdef USE_BLAS # ifdef __APPLE__ # include # else # include # 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); }