diff options
Diffstat (limited to 'liblangdet/act.c')
| -rw-r--r-- | liblangdet/act.c | 29 |
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; + } + } +} |
