aboutsummaryrefslogtreecommitdiff
path: root/liblangdet/act.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-29 13:11:00 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-29 13:11:00 +0200
commit37cbaef9ceb828c667da8f398027ba5415ee7475 (patch)
tree6103e92a9030766e78a35df00f7a1155d0d0ecf5 /liblangdet/act.c
parentcf7d8169ec12cb3adf90d4ff78b3257e74985302 (diff)
downloadlangdet-37cbaef9ceb828c667da8f398027ba5415ee7475.tar.gz
langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.zip
Rename CPU library API and sources to langdet
Diffstat (limited to 'liblangdet/act.c')
-rw-r--r--liblangdet/act.c29
1 files changed, 29 insertions, 0 deletions
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 <math.h>
+
+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;
+ }
+ }
+}