From 37cbaef9ceb828c667da8f398027ba5415ee7475 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 29 Sep 2026 13:11:00 +0200 Subject: Rename CPU library API and sources to langdet --- liblangdet/act.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 liblangdet/act.c (limited to 'liblangdet/act.c') diff --git a/liblangdet/act.c b/liblangdet/act.c new file mode 100644 index 0000000..3912f2f --- /dev/null +++ b/liblangdet/act.c @@ -0,0 +1,29 @@ +#include "kernels.h" + +#include + +void gelu_inplace(float *x, int n) { + /* Exact erf GeLU; 0.70710678... = 1/sqrt(2). */ + for (int i = 0; i < n; i++) + x[i] = 0.5f * x[i] * (1.0f + erff(x[i] * 0.70710678118654752f)); +} + +void softmax(float *x, int rows, int cols) { + for (int r = 0; r < rows; r++) { + float *row = x + r * cols; + float max_val = row[0]; + for (int c = 1; c < cols; c++) { + if (row[c] > max_val) + max_val = row[c]; + } + float sum = 0.0f; + for (int c = 0; c < cols; c++) { + row[c] = expf(row[c] - max_val); + sum += row[c]; + } + float inv_sum = 1.0f / sum; + for (int c = 0; c < cols; c++) { + row[c] *= inv_sum; + } + } +} -- cgit v1.3.1