aboutsummaryrefslogtreecommitdiff
path: root/src/forward.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /src/forward.c
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to '')
-rw-r--r--libffwd/forward.c (renamed from src/forward.c)309
1 files changed, 159 insertions, 150 deletions
diff --git a/src/forward.c b/libffwd/forward.c
index 28243af..e8a5cdf 100644
--- a/src/forward.c
+++ b/libffwd/forward.c
@@ -1,4 +1,4 @@
-#include "embed.h"
+#include "ffwd.h"
#include "model_internal.h"
#include "dtype.h"
#include "kernels.h"
@@ -11,8 +11,8 @@
/* Below these packed-sequence lengths the fused BF16 row kernels beat widening
* the weight to F32 and dispatching through BLAS; above them, widen once. */
-#define EMBED_BF16_QKV_FUSE_MAX_SEQ 16
-#define EMBED_BF16_PAIR_FUSE_MAX_SEQ 4
+#define FFWD_BF16_QKV_FUSE_MAX_SEQ 16
+#define FFWD_BF16_PAIR_FUSE_MAX_SEQ 4
/* ========================================================================
* Linear projections (weight-dtype dispatch) and workspace scratch growth
@@ -20,10 +20,10 @@
static void bf16_row_to_f32(float *dst, const uint16_t *src, int n) {
for (int i = 0; i < n; i++)
- dst[i] = embed_bf16_to_f32(src[i]);
+ dst[i] = ffwd_bf16_to_f32(src[i]);
}
-static void copy_weight_row(float *dst, const embed_weight_ref_t *w, size_t row, int row_width) {
+static void copy_weight_row(float *dst, const ffwd_weight_ref_t *w, size_t row, int row_width) {
if (w->dtype == DTYPE_F32) {
const float *src = (const float *)w->data + row * (size_t)row_width;
memcpy(dst, src, (size_t)row_width * sizeof(float));
@@ -33,7 +33,7 @@ static void copy_weight_row(float *dst, const embed_weight_ref_t *w, size_t row,
}
}
-static int ensure_bf16_widen(embed_workspace_t *ws, size_t count) {
+static int ensure_bf16_widen(ffwd_workspace_t *ws, size_t count) {
if (ws->bf16_widen_count >= count)
return 0;
float *p = (float *)realloc(ws->bf16_widen, count * sizeof(float));
@@ -44,15 +44,15 @@ static int ensure_bf16_widen(embed_workspace_t *ws, size_t count) {
return 0;
}
-void linear_nobias_weight(embed_workspace_t *ws,
+void linear_nobias_weight(ffwd_workspace_t *ws,
float *y,
const float *x,
- const embed_weight_ref_t *w,
+ const ffwd_weight_ref_t *w,
int seq_len,
int in_dim,
int out_dim) {
if (w->dtype == DTYPE_F32) {
- embed_linear_nobias(y, x, (const float *)w->data, seq_len, in_dim, out_dim);
+ linear_nobias(y, x, (const float *)w->data, seq_len, in_dim, out_dim);
return;
}
if (seq_len > 16) {
@@ -60,31 +60,30 @@ void linear_nobias_weight(embed_workspace_t *ws,
* BLAS-backed F32 path; below that the fused BF16 row kernels win. */
size_t count = (size_t)out_dim * (size_t)in_dim;
if (ensure_bf16_widen(ws, count) == 0) {
- embed_bf16_to_f32_buf(ws->bf16_widen, (const uint16_t *)w->data, count);
- embed_linear_nobias(y, x, ws->bf16_widen, seq_len, in_dim, out_dim);
+ bf16_to_f32_buf(ws->bf16_widen, (const uint16_t *)w->data, count);
+ linear_nobias(y, x, ws->bf16_widen, seq_len, in_dim, out_dim);
return;
}
}
- embed_linear_nobias_bf16(y, x, (const uint16_t *)w->data, seq_len, in_dim, out_dim);
+ linear_nobias_bf16(y, x, (const uint16_t *)w->data, seq_len, in_dim, out_dim);
}
-static void linear_qkv_weight(embed_workspace_t *ws,
+static void linear_qkv_weight(ffwd_workspace_t *ws,
float *q,
float *k,
float *v,
const float *x,
- const embed_weight_ref_t *wq,
- const embed_weight_ref_t *wk,
- const embed_weight_ref_t *wv,
+ const ffwd_weight_ref_t *wq,
+ const ffwd_weight_ref_t *wk,
+ const ffwd_weight_ref_t *wv,
int seq_len,
int in_dim,
int q_dim,
int kv_dim) {
if (wq->dtype == DTYPE_BF16 && wk->dtype == DTYPE_BF16 && wv->dtype == DTYPE_BF16 &&
- seq_len <= EMBED_BF16_QKV_FUSE_MAX_SEQ) {
- embed_linear_nobias_bf16_qkv(q, k, v, x, (const uint16_t *)wq->data,
- (const uint16_t *)wk->data, (const uint16_t *)wv->data,
- seq_len, in_dim, q_dim, kv_dim);
+ seq_len <= FFWD_BF16_QKV_FUSE_MAX_SEQ) {
+ linear_nobias_bf16_qkv(q, k, v, x, (const uint16_t *)wq->data, (const uint16_t *)wk->data,
+ (const uint16_t *)wv->data, seq_len, in_dim, q_dim, kv_dim);
return;
}
@@ -103,20 +102,20 @@ static void add_bias_rows(float *x, const float *bias, int rows, int dim) {
}
}
-static void linear_pair_weight(embed_workspace_t *ws,
+static void linear_pair_weight(ffwd_workspace_t *ws,
float *a,
float *b,
const float *x,
- const embed_weight_ref_t *wa,
- const embed_weight_ref_t *wb,
+ const ffwd_weight_ref_t *wa,
+ const ffwd_weight_ref_t *wb,
int seq_len,
int in_dim,
int a_dim,
int b_dim) {
if (wa->dtype == DTYPE_BF16 && wb->dtype == DTYPE_BF16 &&
- seq_len <= EMBED_BF16_PAIR_FUSE_MAX_SEQ) {
- embed_linear_nobias_bf16_pair(a, b, x, (const uint16_t *)wa->data,
- (const uint16_t *)wb->data, seq_len, in_dim, a_dim, b_dim);
+ seq_len <= FFWD_BF16_PAIR_FUSE_MAX_SEQ) {
+ linear_nobias_bf16_pair(a, b, x, (const uint16_t *)wa->data, (const uint16_t *)wb->data,
+ seq_len, in_dim, a_dim, b_dim);
return;
}
@@ -128,11 +127,18 @@ static void linear_pair_weight(embed_workspace_t *ws,
* Packed forward pass
*
* The workspace-growth helpers these call (ensure_buffers, ensure_rope_cache,
- * ensure_attention_scores, ensure_offsets) live in model.c beside the workspace
- * lifecycle and embed_workspace_nbytes, whose size accounting mirrors them.
+ * ensure_attention_scores, ensure_offsets) live in model_blas.c beside the
+ * workspace lifecycle and ffwd_workspace_nbytes, whose size accounting mirrors
+ * them.
* ======================================================================== */
-int build_offsets(const embed_input_t *inputs,
+static ffwd_attention_fn attention_for_config(const ffwd_config_t *cfg) {
+ return cfg->attention_mode == FFWD_ATTENTION_CAUSAL
+ ? causal_gqa_attention_packed_with_scratch
+ : bidirectional_gqa_attention_packed_with_scratch;
+}
+
+int build_offsets(const ffwd_input_t *inputs,
int batch,
int require_ids,
int *offsets,
@@ -159,9 +165,9 @@ int build_offsets(const embed_input_t *inputs,
return 0;
}
-static int forward_packed_slice_inplace(const embed_model_t *model,
- embed_workspace_t *ws,
- const embed_input_t *inputs,
+static int forward_packed_slice_inplace(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const ffwd_input_t *inputs,
int batch,
const int *offsets,
int total_seq,
@@ -170,12 +176,12 @@ static int forward_packed_slice_inplace(const embed_model_t *model,
int layer_start,
int layer_end,
int apply_final_norm) {
- if (!model || !ws || ws->model != model || !inputs || batch <= 0 || !offsets ||
- total_seq <= 0 || max_seq <= 0)
+ if (!model || !ws || ws->model != model || !inputs || batch <= 0 || !offsets || total_seq <= 0 ||
+ max_seq <= 0)
return -1;
- const embed_config_t *cfg = &model->config;
- const embed_weights_t *w = &model->weights;
+ const ffwd_config_t *cfg = &model->config;
+ const ffwd_weights_t *w = &model->weights;
if (layer_start < 0 || layer_start > layer_end || layer_end > cfg->n_layers ||
layer_start < model->layer_start || layer_end > model->layer_end ||
(layer_start == 0 && input_states) || (layer_start != 0 && !input_states) ||
@@ -219,7 +225,7 @@ static int forward_packed_slice_inplace(const embed_model_t *model,
for (int i = 0; i < inputs[b].n_tokens; i++) {
int id = inputs[b].ids[i];
if (id < 0 || id >= cfg->vocab_size) {
- fprintf(stderr, "embed: invalid token id %d at input %d position %d\n", id, b,
+ fprintf(stderr, "ffwd: invalid token id %d at input %d position %d\n", id, b,
i);
return -1;
}
@@ -236,53 +242,54 @@ static int forward_packed_slice_inplace(const embed_model_t *model,
/* 2. Transformer layers. Linear/MLP ops run over all packed tokens;
* attention is explicitly block-diagonal by sequence offsets. */
for (int layer = layer_start; layer < layer_end; layer++) {
- const embed_layer_t *l = &w->layers[layer];
+ const ffwd_layer_t *l = &w->layers[layer];
- embed_rms_norm(x_norm, x, l->input_norm, total_seq, hidden, eps);
+ rms_norm(x_norm, x, l->input_norm, total_seq, hidden, eps);
- linear_qkv_weight(ws, q_buf, k_buf, v_buf, x_norm, &l->wq, &l->wk, &l->wv, total_seq,
- hidden, q_dim, kv_dim);
+ linear_qkv_weight(ws, q_buf, k_buf, v_buf, x_norm, &l->wq, &l->wk, &l->wv, total_seq, hidden,
+ q_dim, kv_dim);
add_bias_rows(q_buf, l->q_bias, total_seq, q_dim);
add_bias_rows(k_buf, l->k_bias, total_seq, kv_dim);
add_bias_rows(v_buf, l->v_bias, total_seq, kv_dim);
if (cfg->qk_norm) {
- embed_rms_norm_per_head(q_buf, l->q_norm, total_seq, n_heads, head_dim, eps);
- embed_rms_norm_per_head(k_buf, l->k_norm, total_seq, n_kv_heads, head_dim, eps);
+ rms_norm_per_head(q_buf, l->q_norm, total_seq, n_heads, head_dim, eps);
+ rms_norm_per_head(k_buf, l->k_norm, total_seq, n_kv_heads, head_dim, eps);
}
for (int b = 0; b < batch; b++) {
int start = offsets[b];
int len = offsets[b + 1] - start;
- embed_apply_rope_neox(q_buf + (size_t)start * q_dim, rope_cos, rope_sin, len, n_heads,
- head_dim);
- embed_apply_rope_neox(k_buf + (size_t)start * kv_dim, rope_cos, rope_sin, len,
- n_kv_heads, head_dim);
+ apply_rope_neox(q_buf + (size_t)start * q_dim, rope_cos, rope_sin, len, n_heads,
+ head_dim);
+ apply_rope_neox(k_buf + (size_t)start * kv_dim, rope_cos, rope_sin, len, n_kv_heads,
+ head_dim);
}
- model->attention(attn_out, q_buf, k_buf, v_buf, offsets, batch, n_heads, n_kv_heads,
- head_dim, scale, ws->attn_scores, ws->attn_scores_bytes);
+ ffwd_attention_fn attention = attention_for_config(cfg);
+ attention(attn_out, q_buf, k_buf, v_buf, offsets, batch, n_heads, n_kv_heads, head_dim, scale,
+ ws->attn_scores, ws->attn_scores_bytes);
linear_nobias_weight(ws, proj_out, attn_out, &l->wo, total_seq, q_dim, hidden);
- embed_add_inplace(x, proj_out, total_seq * hidden);
+ add_inplace(x, proj_out, total_seq * hidden);
- embed_rms_norm(x_norm, x, l->post_attn_norm, total_seq, hidden, eps);
+ rms_norm(x_norm, x, l->post_attn_norm, total_seq, hidden, eps);
linear_pair_weight(ws, ffn_gate, ffn_up, x_norm, &l->gate_proj, &l->up_proj, total_seq,
hidden, inter, inter);
- embed_silu_mul_inplace(ffn_gate, ffn_up, total_seq * inter);
+ silu_mul_inplace(ffn_gate, ffn_up, total_seq * inter);
linear_nobias_weight(ws, proj_out, ffn_gate, &l->down_proj, total_seq, inter, hidden);
- embed_add_inplace(x, proj_out, total_seq * hidden);
+ add_inplace(x, proj_out, total_seq * hidden);
}
if (apply_final_norm)
- embed_rms_norm(x, x, w->norm, total_seq, hidden, eps);
+ rms_norm(x, x, w->norm, total_seq, hidden, eps);
return 0;
}
-int forward_packed_inplace(const embed_model_t *model,
- embed_workspace_t *ws,
- const embed_input_t *inputs,
+int forward_packed_inplace(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const ffwd_input_t *inputs,
int batch,
const int *offsets,
int total_seq,
@@ -299,19 +306,19 @@ int forward_packed_inplace(const embed_model_t *model,
* cfg->position_id_offset (0 for BERT, pad_id + 1 for RoBERTa/XLM-R). Leaves the
* final hidden states in ws->x; pooling reads them as-is because the last
* layer's LayerNorm is already the model's final normalization. */
-static int forward_packed_bert(const embed_model_t *model,
- embed_workspace_t *ws,
- const embed_input_t *inputs,
+static int forward_packed_bert(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const ffwd_input_t *inputs,
int batch,
const int *offsets,
int total_seq,
int max_seq) {
- if (!model || !ws || ws->model != model || !inputs || batch <= 0 || !offsets ||
- total_seq <= 0 || max_seq <= 0)
+ if (!model || !ws || ws->model != model || !inputs || batch <= 0 || !offsets || total_seq <= 0 ||
+ max_seq <= 0)
return -1;
- const embed_config_t *cfg = &model->config;
- const embed_weights_t *w = &model->weights;
+ const ffwd_config_t *cfg = &model->config;
+ const ffwd_weights_t *w = &model->weights;
int hidden = cfg->hidden_size;
int n_heads = cfg->n_heads;
int head_dim = cfg->head_dim;
@@ -319,11 +326,12 @@ static int forward_packed_bert(const embed_model_t *model,
float eps = cfg->layer_norm_eps;
if (!w->embed_tokens.data || !w->position_embeddings || !w->token_type_embeddings ||
- !w->embed_ln_w || !w->embed_ln_b)
+ !w->ffwd_ln_w || !w->ffwd_ln_b)
return -1;
if (max_seq > cfg->max_position_embeddings - cfg->position_id_offset) {
fprintf(stderr,
- "embed: sequence length %d plus position offset %d exceeds max_position_embeddings %d\n",
+ "ffwd: sequence length %d plus position offset %d exceeds max_position_embeddings "
+ "%d\n",
max_seq, cfg->position_id_offset, cfg->max_position_embeddings);
return -1;
}
@@ -342,22 +350,22 @@ static int forward_packed_bert(const embed_model_t *model,
for (int i = 0; i < inputs[b].n_tokens; i++) {
int id = inputs[b].ids[i];
if (id < 0 || id >= cfg->vocab_size) {
- fprintf(stderr, "embed: invalid token id %d at input %d position %d\n", id, b, i);
+ fprintf(stderr, "ffwd: invalid token id %d at input %d position %d\n", id, b, i);
return -1;
}
float *row = x + (size_t)(start + i) * hidden;
copy_weight_row(row, &w->embed_tokens, (size_t)id, hidden);
int pos = cfg->position_id_offset + i;
- embed_add_inplace(row, w->position_embeddings + (size_t)pos * hidden, hidden);
- embed_add_inplace(row, w->token_type_embeddings, hidden);
+ add_inplace(row, w->position_embeddings + (size_t)pos * hidden, hidden);
+ add_inplace(row, w->token_type_embeddings, hidden);
}
}
- embed_layer_norm(x, x, w->embed_ln_w, w->embed_ln_b, total_seq, hidden, eps);
+ layer_norm(x, x, w->ffwd_ln_w, w->ffwd_ln_b, total_seq, hidden, eps);
float scale = 1.0f / sqrtf((float)head_dim);
for (int layer = 0; layer < cfg->n_layers; layer++) {
- const embed_layer_t *l = &w->layers[layer];
+ const ffwd_layer_t *l = &w->layers[layer];
/* Self-attention (post-norm): projections read x directly. */
linear_qkv_weight(ws, q_buf, k_buf, v_buf, x, &l->wq, &l->wk, &l->wv, total_seq, hidden,
@@ -366,35 +374,36 @@ static int forward_packed_bert(const embed_model_t *model,
add_bias_rows(k_buf, l->k_bias, total_seq, hidden);
add_bias_rows(v_buf, l->v_bias, total_seq, hidden);
- model->attention(attn_out, q_buf, k_buf, v_buf, offsets, batch, n_heads, n_heads, head_dim,
- scale, ws->attn_scores, ws->attn_scores_bytes);
+ ffwd_attention_fn attention = attention_for_config(cfg);
+ attention(attn_out, q_buf, k_buf, v_buf, offsets, batch, n_heads, n_heads, head_dim, scale,
+ ws->attn_scores, ws->attn_scores_bytes);
linear_nobias_weight(ws, proj_out, attn_out, &l->wo, total_seq, hidden, hidden);
add_bias_rows(proj_out, l->o_bias, total_seq, hidden);
- embed_add_inplace(x, proj_out, total_seq * hidden);
- embed_layer_norm(x, x, l->input_norm, l->attn_ln_bias, total_seq, hidden, eps);
+ add_inplace(x, proj_out, total_seq * hidden);
+ layer_norm(x, x, l->input_norm, l->attn_ln_bias, total_seq, hidden, eps);
/* Feed-forward (post-norm): dense -> GeLU -> dense. */
linear_nobias_weight(ws, ffn_up, x, &l->up_proj, total_seq, hidden, inter);
add_bias_rows(ffn_up, l->ffn_inter_bias, total_seq, inter);
- if (cfg->ffn_act == EMBED_ACT_GELU_TANH)
- embed_gelu_tanh_inplace(ffn_up, total_seq * inter);
+ if (cfg->ffn_act == FFWD_ACT_GELU_TANH)
+ gelu_tanh_inplace(ffn_up, total_seq * inter);
else
- embed_gelu_inplace(ffn_up, total_seq * inter);
+ gelu_inplace(ffn_up, total_seq * inter);
linear_nobias_weight(ws, proj_out, ffn_up, &l->down_proj, total_seq, inter, hidden);
add_bias_rows(proj_out, l->ffn_out_bias, total_seq, hidden);
- embed_add_inplace(x, proj_out, total_seq * hidden);
- embed_layer_norm(x, x, l->post_attn_norm, l->ffn_ln_bias, total_seq, hidden, eps);
+ add_inplace(x, proj_out, total_seq * hidden);
+ layer_norm(x, x, l->post_attn_norm, l->ffn_ln_bias, total_seq, hidden, eps);
}
return 0;
}
-static int pool_embeddings(const embed_model_t *model,
- const embed_workspace_t *ws,
+static int pool_embeddings(const ffwd_model_t *model,
+ const ffwd_workspace_t *ws,
const int *offsets,
int batch,
float *out_embeddings) {
- const embed_config_t *cfg = &model->config;
+ const ffwd_config_t *cfg = &model->config;
int hidden = cfg->hidden_size;
float eps = cfg->rms_norm_eps;
/* Qwen pools pre-final-norm states and applies the final RMSNorm here; BERT
@@ -410,10 +419,10 @@ static int pool_embeddings(const embed_model_t *model,
int len = end - start;
float *emb = out_embeddings + (size_t)b * hidden;
- if (cfg->pooling_mode == EMBED_POOL_LAST_TOKEN || cfg->pooling_mode == EMBED_POOL_CLS) {
+ if (cfg->pooling_mode == FFWD_POOL_LAST_TOKEN || cfg->pooling_mode == FFWD_POOL_CLS) {
/* Single-token pooling: CLS takes the first token, last-token the
* final one. */
- int idx = cfg->pooling_mode == EMBED_POOL_CLS ? start : end - 1;
+ int idx = cfg->pooling_mode == FFWD_POOL_CLS ? start : end - 1;
const float *row = x + (size_t)idx * hidden;
if (norm_weight) {
float sum_sq = 0.0f;
@@ -446,7 +455,7 @@ static int pool_embeddings(const embed_model_t *model,
emb[d] *= inv_len;
}
- if (cfg->normalize_embeddings && embed_l2_normalize(emb, hidden) != 0)
+ if (cfg->normalize_embeddings && ffwd_l2_normalize(emb, hidden) != 0)
return -1;
}
return 0;
@@ -456,11 +465,11 @@ static int pool_embeddings(const embed_model_t *model,
* Public forward/embed APIs
* ======================================================================== */
-int embed_model_forward_into(const embed_model_t *model,
- embed_workspace_t *ws,
- const int *token_ids,
- int n_tokens,
- float *out_states) {
+int ffwd_model_forward_into(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const int *token_ids,
+ int n_tokens,
+ float *out_states) {
if (!model || !ws || !token_ids || n_tokens <= 0 || !out_states)
return -1;
@@ -469,7 +478,7 @@ int embed_model_forward_into(const embed_model_t *model,
mul_size(count, sizeof(float), &bytes) != 0)
return -1;
- embed_input_t input = {token_ids, n_tokens};
+ ffwd_input_t input = {token_ids, n_tokens};
int offsets[2] = {0, n_tokens};
if (forward_packed_inplace(model, ws, &input, 1, offsets, n_tokens, n_tokens, 1) != 0)
return -1;
@@ -478,12 +487,12 @@ int embed_model_forward_into(const embed_model_t *model,
return 0;
}
-int embed_pool_spans(const embed_config_t *cfg,
- const float *states,
- int n_tokens,
- const embed_span_t *spans,
- int n_spans,
- float *out_embeddings) {
+int ffwd_pool_spans(const ffwd_config_t *cfg,
+ const float *states,
+ int n_tokens,
+ const ffwd_span_t *spans,
+ int n_spans,
+ float *out_embeddings) {
if (!cfg || !states || n_tokens <= 0 || !spans || n_spans <= 0 || !out_embeddings)
return -1;
@@ -514,19 +523,19 @@ int embed_pool_spans(const embed_config_t *cfg,
return 0;
}
-int embed_model_encode_spans_batch(const embed_model_t *model,
- embed_workspace_t *ws,
- const embed_context_input_t *inputs,
- int batch,
- float *out_embeddings) {
+int ffwd_model_encode_spans_batch(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const ffwd_context_input_t *inputs,
+ int batch,
+ float *out_embeddings) {
if (!model || !ws || !inputs || batch <= 0 || !out_embeddings)
return -1;
if (ensure_offsets(ws, batch) != 0)
return -1;
- if ((size_t)batch > SIZE_MAX / sizeof(embed_input_t))
+ if ((size_t)batch > SIZE_MAX / sizeof(ffwd_input_t))
return -1;
- embed_input_t *packed_inputs = malloc((size_t)batch * sizeof(*packed_inputs));
+ ffwd_input_t *packed_inputs = malloc((size_t)batch * sizeof(*packed_inputs));
if (!packed_inputs)
return -1;
int *offsets = ws->offsets;
@@ -534,7 +543,7 @@ int embed_model_encode_spans_batch(const embed_model_t *model,
int max_seq = 0;
int total_spans = 0;
for (int b = 0; b < batch; b++) {
- const embed_context_input_t *input = &inputs[b];
+ const ffwd_context_input_t *input = &inputs[b];
int n_tokens = input->input.n_tokens;
if (!input->input.ids || n_tokens <= 0 || !input->spans || input->n_spans <= 0 ||
total_seq > INT_MAX - n_tokens || total_spans > INT_MAX - input->n_spans) {
@@ -568,35 +577,35 @@ int embed_model_encode_spans_batch(const embed_model_t *model,
int span_offset = 0;
int hidden = model->config.hidden_size;
for (int b = 0; b < batch; b++) {
- const embed_context_input_t *input = &inputs[b];
- if (embed_pool_spans(&model->config, ws->x + (size_t)offsets[b] * hidden,
- input->input.n_tokens, input->spans, input->n_spans,
- out_embeddings + (size_t)span_offset * hidden) != 0)
+ const ffwd_context_input_t *input = &inputs[b];
+ if (ffwd_pool_spans(&model->config, ws->x + (size_t)offsets[b] * hidden,
+ input->input.n_tokens, input->spans, input->n_spans,
+ out_embeddings + (size_t)span_offset * hidden) != 0)
return -1;
span_offset += input->n_spans;
}
return 0;
}
-int embed_model_encode_spans(const embed_model_t *model,
- embed_workspace_t *ws,
- const int *token_ids,
- int n_tokens,
- const embed_span_t *spans,
- int n_spans,
- float *out_embeddings) {
- embed_context_input_t input = {
+int ffwd_model_encode_spans(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const int *token_ids,
+ int n_tokens,
+ const ffwd_span_t *spans,
+ int n_spans,
+ float *out_embeddings) {
+ ffwd_context_input_t input = {
.input = {token_ids, n_tokens},
.spans = spans,
.n_spans = n_spans,
};
- return embed_model_encode_spans_batch(model, ws, &input, 1, out_embeddings);
+ return ffwd_model_encode_spans_batch(model, ws, &input, 1, out_embeddings);
}
-float *embed_model_forward(const embed_model_t *model,
- embed_workspace_t *ws,
- const int *token_ids,
- int n_tokens) {
+float *ffwd_model_forward(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const int *token_ids,
+ int n_tokens) {
if (!model || !ws || !token_ids || n_tokens <= 0)
return NULL;
@@ -607,18 +616,18 @@ float *embed_model_forward(const embed_model_t *model,
if (!out)
return NULL;
- if (embed_model_forward_into(model, ws, token_ids, n_tokens, out) != 0) {
+ if (ffwd_model_forward_into(model, ws, token_ids, n_tokens, out) != 0) {
free(out);
return NULL;
}
return out;
}
-int embed_model_encode_batch(const embed_model_t *model,
- embed_workspace_t *ws,
- const embed_input_t *inputs,
- int batch,
- float *out_embeddings) {
+int ffwd_model_encode_batch(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const ffwd_input_t *inputs,
+ int batch,
+ float *out_embeddings) {
if (!model || !ws || !inputs || batch <= 0 || !out_embeddings)
return -1;
@@ -631,7 +640,7 @@ int embed_model_encode_batch(const embed_model_t *model,
if (build_offsets(inputs, batch, 1, offsets, &total_seq, &max_seq) != 0)
return -1;
- int rc = model->config.family == EMBED_FAMILY_BERT
+ int rc = model->config.family == FFWD_FAMILY_BERT
? forward_packed_bert(model, ws, inputs, batch, offsets, total_seq, max_seq)
: forward_packed_inplace(model, ws, inputs, batch, offsets, total_seq, max_seq, 0);
if (rc == 0)
@@ -640,11 +649,11 @@ int embed_model_encode_batch(const embed_model_t *model,
return rc;
}
-int embed_pool_batch(const embed_config_t *cfg,
- const float *states,
- const int *seq_lengths,
- int batch,
- float *out_embeddings) {
+int ffwd_pool_batch(const ffwd_config_t *cfg,
+ const float *states,
+ const int *seq_lengths,
+ int batch,
+ float *out_embeddings) {
if (!cfg || !states || !seq_lengths || batch <= 0 || !out_embeddings || cfg->hidden_size <= 0)
return -1;
@@ -655,10 +664,10 @@ int embed_pool_batch(const embed_config_t *cfg,
return -1;
float *emb = out_embeddings + (size_t)b * cfg->hidden_size;
- if (cfg->pooling_mode == EMBED_POOL_LAST_TOKEN || cfg->pooling_mode == EMBED_POOL_CLS) {
+ if (cfg->pooling_mode == FFWD_POOL_LAST_TOKEN || cfg->pooling_mode == FFWD_POOL_CLS) {
/* CLS takes the first token, last-token the final one; these states
* already carry the model's final RMSNorm. */
- int idx = cfg->pooling_mode == EMBED_POOL_CLS ? offset : offset + len - 1;
+ int idx = cfg->pooling_mode == FFWD_POOL_CLS ? offset : offset + len - 1;
const float *row = states + (size_t)idx * cfg->hidden_size;
memcpy(emb, row, (size_t)cfg->hidden_size * sizeof(float));
} else {
@@ -672,26 +681,26 @@ int embed_pool_batch(const embed_config_t *cfg,
for (int d = 0; d < cfg->hidden_size; d++)
emb[d] *= inv_len;
}
- if (cfg->normalize_embeddings && embed_l2_normalize(emb, cfg->hidden_size) != 0)
+ if (cfg->normalize_embeddings && ffwd_l2_normalize(emb, cfg->hidden_size) != 0)
return -1;
offset += len;
}
return 0;
}
-int embed_model_encode_into(const embed_model_t *model,
- embed_workspace_t *ws,
- const int *token_ids,
- int n_tokens,
- float *out_embedding) {
- embed_input_t input = {token_ids, n_tokens};
- return embed_model_encode_batch(model, ws, &input, 1, out_embedding);
+int ffwd_model_encode_into(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const int *token_ids,
+ int n_tokens,
+ float *out_embedding) {
+ ffwd_input_t input = {token_ids, n_tokens};
+ return ffwd_model_encode_batch(model, ws, &input, 1, out_embedding);
}
-float *embed_model_encode(const embed_model_t *model,
- embed_workspace_t *ws,
- const int *token_ids,
- int n_tokens) {
+float *ffwd_model_encode(const ffwd_model_t *model,
+ ffwd_workspace_t *ws,
+ const int *token_ids,
+ int n_tokens) {
if (!model || !ws || !token_ids || n_tokens <= 0)
return NULL;
@@ -702,7 +711,7 @@ float *embed_model_encode(const embed_model_t *model,
if (!emb)
return NULL;
- if (embed_model_encode_into(model, ws, token_ids, n_tokens, emb) != 0) {
+ if (ffwd_model_encode_into(model, ws, token_ids, n_tokens, emb) != 0) {
free(emb);
return NULL;
}