aboutsummaryrefslogtreecommitdiff
path: root/liblangdet/forward.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/forward.c
parentcf7d8169ec12cb3adf90d4ff78b3257e74985302 (diff)
downloadlangdet-37cbaef9ceb828c667da8f398027ba5415ee7475.tar.gz
langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.zip
Rename CPU library API and sources to langdet
Diffstat (limited to 'liblangdet/forward.c')
-rw-r--r--liblangdet/forward.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/liblangdet/forward.c b/liblangdet/forward.c
new file mode 100644
index 0000000..14b1327
--- /dev/null
+++ b/liblangdet/forward.c
@@ -0,0 +1,96 @@
+#include "model_internal.h"
+#include "kernels.h"
+
+#include <limits.h>
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+static void add_bias_rows(float *x, const float *bias, int rows, int dim) {
+ for (int row = 0; row < rows; row++) {
+ float *dst = x + (size_t)row * dim;
+ for (int d = 0; d < dim; d++)
+ dst[d] += bias[d];
+ }
+}
+
+int langdet_model_encode_into(const langdet_model_t *model,
+ langdet_workspace_t *ws,
+ const int *token_ids,
+ int n_tokens,
+ float *out_embedding) {
+ if (!model || !ws || ws->model != model || !token_ids || n_tokens <= 0 || !out_embedding)
+ return -1;
+
+ const langdet_config_t *cfg = &model->config;
+ const langdet_weights_t *w = &model->weights;
+ int hidden = cfg->hidden_size;
+ int n_heads = cfg->n_heads;
+ int head_dim = cfg->head_dim;
+ int inter = cfg->intermediate_size;
+ float eps = cfg->layer_norm_eps;
+
+ if (n_tokens > cfg->max_position_embeddings - cfg->position_id_offset) {
+ fprintf(stderr,
+ "langdet: sequence length %d plus position offset %d exceeds max_position_embeddings "
+ "%d\n",
+ n_tokens, cfg->position_id_offset, cfg->max_position_embeddings);
+ return -1;
+ }
+ if (n_tokens > INT_MAX / hidden || n_tokens > INT_MAX / inter ||
+ ensure_buffers(ws, cfg, n_tokens) != 0)
+ return -1;
+ int offsets[2] = {0, n_tokens};
+ ensure_attention_scores(ws, offsets, 1);
+
+ float *x = ws->x;
+ float *q_buf = ws->q, *k_buf = ws->k, *v_buf = ws->v;
+ float *attn_out = ws->attn_out, *proj_out = ws->proj_out, *ffn_up = ws->ffn_up;
+
+ for (int i = 0; i < n_tokens; i++) {
+ int id = token_ids[i];
+ if (id < 0 || id >= cfg->vocab_size) {
+ fprintf(stderr, "langdet: invalid token id %d at position %d\n", id, i);
+ return -1;
+ }
+ float *row = x + (size_t)i * hidden;
+ memcpy(row, w->embed_tokens + (size_t)id * hidden, (size_t)hidden * sizeof(float));
+ int pos = cfg->position_id_offset + i;
+ add_inplace(row, w->position_embeddings + (size_t)pos * hidden, hidden);
+ add_inplace(row, w->token_type_embeddings, hidden);
+ }
+ layer_norm(x, x, w->langdet_ln_w, w->langdet_ln_b, n_tokens, hidden, eps);
+
+ float scale = 1.0f / sqrtf((float)head_dim);
+
+ for (int layer = 0; layer < cfg->n_layers; layer++) {
+ const langdet_layer_t *l = &w->layers[layer];
+
+ linear_nobias(q_buf, x, l->wq, n_tokens, hidden, hidden);
+ linear_nobias(k_buf, x, l->wk, n_tokens, hidden, hidden);
+ linear_nobias(v_buf, x, l->wv, n_tokens, hidden, hidden);
+ add_bias_rows(q_buf, l->q_bias, n_tokens, hidden);
+ add_bias_rows(k_buf, l->k_bias, n_tokens, hidden);
+ add_bias_rows(v_buf, l->v_bias, n_tokens, hidden);
+
+ bidirectional_gqa_attention_packed_with_scratch(
+ attn_out, q_buf, k_buf, v_buf, offsets, 1, n_heads, n_heads, head_dim, scale,
+ ws->attn_scores, ws->attn_scores_bytes);
+
+ linear_nobias(proj_out, attn_out, l->wo, n_tokens, hidden, hidden);
+ add_bias_rows(proj_out, l->o_bias, n_tokens, hidden);
+ add_inplace(x, proj_out, n_tokens * hidden);
+ layer_norm(x, x, l->input_norm, l->attn_ln_bias, n_tokens, hidden, eps);
+
+ linear_nobias(ffn_up, x, l->up_proj, n_tokens, hidden, inter);
+ add_bias_rows(ffn_up, l->ffn_inter_bias, n_tokens, inter);
+ gelu_inplace(ffn_up, n_tokens * inter);
+ linear_nobias(proj_out, ffn_up, l->down_proj, n_tokens, inter, hidden);
+ add_bias_rows(proj_out, l->ffn_out_bias, n_tokens, hidden);
+ add_inplace(x, proj_out, n_tokens * hidden);
+ layer_norm(x, x, l->post_attn_norm, l->ffn_ln_bias, n_tokens, hidden, eps);
+ }
+ /* The classifier consumes <s> after the encoder's final LayerNorm, without L2 normalization. */
+ memcpy(out_embedding, x, (size_t)hidden * sizeof(float));
+ return 0;
+}