aboutsummaryrefslogtreecommitdiff
path: root/liblangdet/norm.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/norm.c
parentcf7d8169ec12cb3adf90d4ff78b3257e74985302 (diff)
downloadlangdet-37cbaef9ceb828c667da8f398027ba5415ee7475.tar.gz
langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.zip
Rename CPU library API and sources to langdet
Diffstat (limited to 'liblangdet/norm.c')
-rw-r--r--liblangdet/norm.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/liblangdet/norm.c b/liblangdet/norm.c
new file mode 100644
index 0000000..59e8bdb
--- /dev/null
+++ b/liblangdet/norm.c
@@ -0,0 +1,33 @@
+#include "kernels.h"
+
+#include <math.h>
+
+/* Safe in place: each row's mean and variance are reduced before it is rewritten. */
+void layer_norm(float *out,
+ const float *x,
+ const float *gamma,
+ const float *beta,
+ int seq_len,
+ int hidden,
+ float eps) {
+ if (seq_len <= 0 || hidden <= 0)
+ return;
+ float inv_h = 1.0f / (float)hidden;
+ for (int s = 0; s < seq_len; s++) {
+ const float *row = x + (size_t)s * hidden;
+ float *dst = out + (size_t)s * hidden;
+ float mean = 0.0f;
+ for (int d = 0; d < hidden; d++)
+ mean += row[d];
+ mean *= inv_h;
+ float var = 0.0f;
+ for (int d = 0; d < hidden; d++) {
+ float diff = row[d] - mean;
+ var += diff * diff;
+ }
+ var *= inv_h;
+ float inv_std = 1.0f / sqrtf(var + eps);
+ for (int d = 0; d < hidden; d++)
+ dst[d] = gamma[d] * (row[d] - mean) * inv_std + beta[d];
+ }
+}